1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "form_sys_event_receiver.h"
17
18 #include <cinttypes>
19
20 #include "common_event_support.h"
21 #include "fms_log_wrapper.h"
22 #include "form_bms_helper.h"
23 #include "form_constants.h"
24 #include "form_data_mgr.h"
25 #include "form_db_cache.h"
26 #include "form_info_mgr.h"
27 #include "form_render_mgr.h"
28 #include "form_serial_queue.h"
29 #include "form_timer_mgr.h"
30 #include "form_util.h"
31 #include "in_process_call_wrapper.h"
32 #include "want.h"
33
34 namespace OHOS {
35 namespace AppExecFwk {
36 namespace {
37 const int32_t MAIN_USER_ID = 100;
38 constexpr int32_t FORM_TASK_DELAY_TIME = 30; // ms
39 } // namespace
40 /**
41 * @brief Receiver Constructor.
42 * @param subscriberInfo Subscriber info.
43 */
FormSysEventReceiver(const EventFwk::CommonEventSubscribeInfo & subscriberInfo)44 FormSysEventReceiver::FormSysEventReceiver(const EventFwk::CommonEventSubscribeInfo &subscriberInfo)
45 : EventFwk::CommonEventSubscriber(subscriberInfo)
46 {}
47
OnReceiveEventForAbilityUpdate(const AAFwk::Want & want,std::string & bundleName)48 void FormSysEventReceiver::OnReceiveEventForAbilityUpdate(const AAFwk::Want& want, std::string &bundleName)
49 {
50 std::weak_ptr<FormSysEventReceiver> weakThis = shared_from_this();
51 auto task = [weakThis, want, bundleName]() {
52 HILOG_INFO("bundle updated, bundleName is %{public}s", bundleName.c_str());
53 std::shared_ptr<FormSysEventReceiver> sharedThis = weakThis.lock();
54 if (sharedThis) {
55 int userId = want.GetIntParam(KEY_USER_ID, 0);
56 sharedThis->formEventHelper_.HandleProviderUpdated(bundleName, userId);
57 }
58 };
59 serialQueue_->ScheduleTask(0, task);
60 }
61
OnReceiveEventForUserRemoved(int32_t userId)62 void FormSysEventReceiver::OnReceiveEventForUserRemoved(int32_t userId)
63 {
64 std::weak_ptr<FormSysEventReceiver> weakThis = shared_from_this();
65 auto task = [weakThis, userId]() {
66 std::shared_ptr<FormSysEventReceiver> sharedThis = weakThis.lock();
67 if (sharedThis) {
68 sharedThis->HandleUserIdRemoved(userId);
69 }
70 };
71 if (userId != -1) {
72 serialQueue_->ScheduleTask(0, task);
73 }
74 }
75
OnReceiveEventForPackageDataCleared(std::string & bundleName,int32_t userId)76 void FormSysEventReceiver::OnReceiveEventForPackageDataCleared(std::string &bundleName, int32_t userId)
77 {
78 std::weak_ptr<FormSysEventReceiver> weakThis = shared_from_this();
79 auto task = [weakThis, bundleName, userId]() {
80 std::shared_ptr<FormSysEventReceiver> sharedThis = weakThis.lock();
81 if (sharedThis) {
82 sharedThis->formEventHelper_.HandleBundleDataCleared(bundleName, userId);
83 }
84 };
85 serialQueue_->ScheduleTask(0, task);
86 }
87
OnReceiveEventForUserUnlocked()88 void FormSysEventReceiver::OnReceiveEventForUserUnlocked()
89 {
90 std::weak_ptr<FormSysEventReceiver> weakThis = shared_from_this();
91 auto task = [weakThis]() {
92 std::shared_ptr<FormSysEventReceiver> sharedThis = weakThis.lock();
93 if (sharedThis) {
94 sharedThis->formEventHelper_.HandleOnUnlock();
95 }
96 };
97 serialQueue_->ScheduleTask(0, task);
98 }
99
100 /**
101 * @brief Receive common event.
102 * @param eventData Common event data.
103 */
OnReceiveEvent(const EventFwk::CommonEventData & eventData)104 void FormSysEventReceiver::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
105 {
106 const AAFwk::Want& want = eventData.GetWant();
107 std::string action = want.GetAction();
108 std::string bundleName = want.GetElement().GetBundleName();
109 if (action.empty()) {
110 HILOG_ERROR("%{public}s failed, empty action", __func__);
111 return;
112 }
113 if (bundleName.empty() && action != EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED &&
114 action != EventFwk::CommonEventSupport::COMMON_EVENT_BUNDLE_SCAN_FINISHED &&
115 action != EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED &&
116 action != EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
117 HILOG_ERROR("%{public}s failed, invalid param, action: %{public}s, bundleName: %{public}s",
118 __func__, action.c_str(), bundleName.c_str());
119 return;
120 }
121 if (serialQueue_ == nullptr) {
122 HILOG_ERROR("%{public}s fail, serialQueue_ invalidate.", __func__);
123 return;
124 }
125 HILOG_INFO("%{public}s, action:%{public}s.", __func__, action.c_str());
126 std::weak_ptr<FormSysEventReceiver> weakThis = shared_from_this();
127 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_ABILITY_UPDATED) {
128 OnReceiveEventForAbilityUpdate(want, bundleName);
129 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
130 int32_t userId = eventData.GetCode();
131 OnReceiveEventForUserRemoved(userId);
132 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_BUNDLE_SCAN_FINISHED) {
133 HandleBundleScanFinished();
134 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
135 HandleUserSwitched(eventData);
136 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) {
137 int userId = want.GetIntParam(KEY_USER_ID, Constants::DEFAULT_USERID);
138 OnReceiveEventForPackageDataCleared(bundleName, userId);
139 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
140 OnReceiveEventForUserUnlocked();
141 } else {
142 HILOG_WARN("%{public}s warnning, invalid action.", __func__);
143 }
144 }
145
146 // multiuser
HandleUserIdRemoved(const int32_t userId)147 void FormSysEventReceiver::HandleUserIdRemoved(const int32_t userId)
148 {
149 std::vector<int64_t> removedFormIds;
150 FormDataMgr::GetInstance().DeleteFormsByUserId(userId, removedFormIds);
151 FormDbCache::GetInstance().DeleteDBFormsByUserId(userId);
152
153 // delete form timer
154 std::vector<int64_t>::iterator itRemoved;
155 for (itRemoved = removedFormIds.begin();itRemoved != removedFormIds.end(); ++itRemoved) {
156 FormTimerMgr::GetInstance().RemoveFormTimer(*itRemoved);
157 }
158 }
159
HandleBundleScanFinished()160 void FormSysEventReceiver::HandleBundleScanFinished()
161 {
162 if (!serialQueue_) {
163 HILOG_ERROR("serialQueue is nullptr!");
164 return;
165 }
166
167 serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, []() {
168 int32_t currUserId = FormUtil::GetCurrentAccountId();
169 if (currUserId == Constants::ANY_USERID) {
170 HILOG_INFO("use MAIN_USER_ID(%{public}d) instead of current userId: ANY_USERID(%{public}d)",
171 MAIN_USER_ID, currUserId);
172 currUserId = MAIN_USER_ID;
173 }
174 FormBmsHelper::GetInstance().RegisterBundleEventCallback();
175 FormInfoMgr::GetInstance().ReloadFormInfos(currUserId);
176 });
177 }
178
HandleUserSwitched(const EventFwk::CommonEventData & eventData)179 void FormSysEventReceiver::HandleUserSwitched(const EventFwk::CommonEventData &eventData)
180 {
181 int32_t userId = eventData.GetCode();
182 if (userId < 0) {
183 HILOG_ERROR("invalid switched userId: %{public}d", userId);
184 return;
185 }
186
187 if (userId == MAIN_USER_ID) {
188 HILOG_INFO("main user id has reload");
189 return;
190 }
191 HILOG_INFO("switch to userId: (%{public}d)", userId);
192
193 if (!serialQueue_) {
194 HILOG_ERROR("serialQueue is nullptr");
195 return;
196 }
197
198 serialQueue_->ScheduleTask(0, [userId]() {
199 FormInfoMgr::GetInstance().ReloadFormInfos(userId);
200 });
201 }
202 } // namespace AppExecFwk
203 } // namespace OHOS
204