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_mgr_errors.h"
28 #include "form_mgr_adapter.h"
29 #include "form_render_mgr.h"
30 #include "form_serial_queue.h"
31 #include "form_timer_mgr.h"
32 #include "form_util.h"
33 #include "in_process_call_wrapper.h"
34 #include "want.h"
35
36 namespace OHOS {
37 namespace AppExecFwk {
38 namespace {
39 const int32_t MAIN_USER_ID = 100;
40 constexpr int32_t TASK_DELAY_TIME = 30; // ms
41 } // namespace
42 /**
43 * @brief Receiver Constructor.
44 * @param subscriberInfo Subscriber info.
45 */
FormSysEventReceiver(const EventFwk::CommonEventSubscribeInfo & subscriberInfo)46 FormSysEventReceiver::FormSysEventReceiver(const EventFwk::CommonEventSubscribeInfo &subscriberInfo)
47 : EventFwk::CommonEventSubscriber(subscriberInfo)
48 {}
49
HandleAbilityUpdate(const AAFwk::Want & want,std::string & bundleName)50 void FormSysEventReceiver::HandleAbilityUpdate(const AAFwk::Want& want, std::string &bundleName)
51 {
52 if (!serialQueue_) {
53 HILOG_ERROR("null serialQueue");
54 return;
55 }
56
57 auto task = [want, bundleName]() {
58 HILOG_INFO("bundle updated, bundleName:%{public}s", bundleName.c_str());
59 int userId = want.GetIntParam(KEY_USER_ID, 0);
60 FormEventUtil::HandleProviderUpdated(bundleName, userId);
61 };
62 serialQueue_->ScheduleTask(0, task);
63 }
64
HandlePackageDataCleared(std::string & bundleName,int32_t userId)65 void FormSysEventReceiver::HandlePackageDataCleared(std::string &bundleName, int32_t userId)
66 {
67 if (!serialQueue_) {
68 HILOG_ERROR("null serialQueue");
69 return;
70 }
71
72 auto task = [bundleName, userId]() {
73 FormEventUtil::HandleBundleDataCleared(bundleName, userId);
74 };
75 serialQueue_->ScheduleTask(0, task);
76 }
77
HandleScreenUnlocked()78 void FormSysEventReceiver::HandleScreenUnlocked()
79 {
80 if (!serialQueue_) {
81 HILOG_ERROR("null serialQueue");
82 return;
83 }
84
85 auto task = []() {
86 FormRenderMgr::GetInstance().OnScreenUnlock();
87 };
88 serialQueue_->ScheduleTask(0, task);
89 }
90
HandleUserUnlocked(int32_t userId)91 void FormSysEventReceiver::HandleUserUnlocked(int32_t userId)
92 {
93 if (userId == -1) {
94 HILOG_ERROR("invalid userId: -1");
95 return;
96 }
97
98 if (!serialQueue_) {
99 HILOG_ERROR("null serialQueue");
100 return;
101 }
102
103 auto task = [userId]() {
104 FormEventUtil::HandleOnUnlock(userId);
105 };
106 serialQueue_->ScheduleTask(0, task);
107 }
108
109 /**
110 * @brief Receive common event.
111 * @param eventData Common event data.
112 */
OnReceiveEvent(const EventFwk::CommonEventData & eventData)113 void FormSysEventReceiver::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
114 {
115 const AAFwk::Want& want = eventData.GetWant();
116 std::string action = want.GetAction();
117 std::string bundleName = want.GetElement().GetBundleName();
118 if (action.empty()) {
119 HILOG_ERROR("empty action");
120 return;
121 }
122 if (bundleName.empty() && action != EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED &&
123 action != EventFwk::CommonEventSupport::COMMON_EVENT_BUNDLE_SCAN_FINISHED &&
124 action != EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED &&
125 action != EventFwk::CommonEventSupport::COMMON_EVENT_SECOND_MOUNTED &&
126 action != EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON &&
127 action != EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) {
128 HILOG_ERROR("invalid param, action:%{public}s, bundleName:%{public}s",
129 action.c_str(), bundleName.c_str());
130 return;
131 }
132 if (serialQueue_ == nullptr) {
133 HILOG_ERROR("null serialQueue_");
134 return;
135 }
136 HILOG_INFO("action:%{public}s", action.c_str());
137 std::weak_ptr<FormSysEventReceiver> weakThis = shared_from_this();
138 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_ABILITY_UPDATED) {
139 HandleAbilityUpdate(want, bundleName);
140 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
141 int32_t userId = eventData.GetCode();
142 HandleUserIdRemoved(userId);
143 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_BUNDLE_SCAN_FINISHED) {
144 HandleBundleScanFinished();
145 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
146 HandleUserSwitched(eventData);
147 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) {
148 int userId = want.GetIntParam(KEY_USER_ID, Constants::DEFAULT_USERID);
149 HandlePackageDataCleared(bundleName, userId);
150 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) {
151 // el2 path maybe not unlocked
152 HandleScreenUnlocked();
153 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SECOND_MOUNTED) {
154 // el2 path is unlocked when receive SECOND_MOUNTED
155 int userId = want.GetIntParam(KEY_USER_ID, Constants::DEFAULT_USERID);
156 HandleUserUnlocked(userId);
157 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
158 HandleScreenOn();
159 } else {
160 HILOG_WARN("invalid action");
161 }
162 }
163
164 // multiuser
HandleUserIdRemoved(const int32_t userId)165 void FormSysEventReceiver::HandleUserIdRemoved(const int32_t userId)
166 {
167 if (userId == -1) {
168 HILOG_ERROR("invalid userId: -1");
169 return;
170 }
171
172 if (!serialQueue_) {
173 HILOG_ERROR("null serialQueue");
174 return;
175 }
176
177 serialQueue_->ScheduleTask(0, [userId]() {
178 std::vector<int64_t> removedFormIds;
179 FormDataMgr::GetInstance().DeleteFormsByUserId(userId, removedFormIds);
180 FormDbCache::GetInstance().DeleteDBFormsByUserId(userId);
181
182 // delete form timer
183 std::vector<int64_t>::iterator itRemoved;
184 for (itRemoved = removedFormIds.begin(); itRemoved != removedFormIds.end(); ++itRemoved) {
185 FormTimerMgr::GetInstance().RemoveFormTimer(*itRemoved);
186 }
187 });
188 }
189
HandleBundleScanFinished()190 void FormSysEventReceiver::HandleBundleScanFinished()
191 {
192 InitFormInfosAndRegister();
193 }
194
InitFormInfosAndRegister()195 void FormSysEventReceiver::InitFormInfosAndRegister()
196 {
197 if (!serialQueue_) {
198 HILOG_ERROR("null serialQueue");
199 return;
200 }
201
202 serialQueue_->ScheduleTask(TASK_DELAY_TIME, []() {
203 int32_t currUserId = FormUtil::GetCurrentAccountId();
204 if (currUserId == Constants::ANY_USERID) {
205 HILOG_INFO("use MAIN_USER_ID(%{public}d) instead of current userId: ANY_USERID(%{public}d)",
206 MAIN_USER_ID, currUserId);
207 currUserId = MAIN_USER_ID;
208 }
209 FormBmsHelper::GetInstance().RegisterBundleEventCallback();
210 if (!FormInfoMgr::GetInstance().HasReloadedFormInfos()) {
211 FormInfoMgr::GetInstance().ReloadFormInfos(currUserId);
212 }
213 });
214 }
215
HandleUserSwitched(const EventFwk::CommonEventData & eventData)216 void FormSysEventReceiver::HandleUserSwitched(const EventFwk::CommonEventData &eventData)
217 {
218 int32_t userId = eventData.GetCode();
219 if (userId < 0) {
220 HILOG_ERROR("invalid switched userId:%{public}d", userId);
221 return;
222 }
223
224 if (lastUserId_ == 0) {
225 HILOG_INFO("reboot init lastUserId");
226 lastUserId_ = userId;
227 return;
228 }
229
230 if (lastUserId_ == userId) {
231 HILOG_WARN("same userId");
232 return;
233 }
234
235 if (!serialQueue_) {
236 HILOG_ERROR("null serialQueue");
237 return;
238 }
239
240 int32_t lastUserId = lastUserId_;
241 lastUserId_ = userId;
242 HILOG_INFO("switch to userId: (%{public}d)", userId);
243
244 serialQueue_->ScheduleTask(0, [userId, lastUserId, this]() {
245 if (userId != MAIN_USER_ID) {
246 FormInfoMgr::GetInstance().ReloadFormInfos(userId);
247 }
248 });
249
250 FormTimerMgr::GetInstance().UpdateLimiterAlarm();
251 FormTimerMgr::GetInstance().UpdateAtTimerAlarm();
252 FormTimerMgr::GetInstance().UpdateDynamicAlarm();
253 }
254
HandleScreenOn()255 void FormSysEventReceiver::HandleScreenOn()
256 {
257 if (!serialQueue_) {
258 HILOG_ERROR("null serialQueue");
259 return;
260 }
261
262 serialQueue_->ScheduleTask(0, []() {
263 FormRenderMgr::GetInstance().NotifyScreenOn();
264 });
265 }
266
RecycleForms(int32_t userId)267 void FormSysEventReceiver::RecycleForms(int32_t userId)
268 {
269 std::vector<int64_t> formIds;
270 FormDataMgr::GetInstance().GetFormIdsByUserId(userId, formIds);
271 Want want;
272 want.SetParam(Constants::RECYCLE_FORMS_USER_ID, userId);
273 FormMgrAdapter::GetInstance().RecycleForms(formIds, want, false);
274 }
275 } // namespace AppExecFwk
276 } // namespace OHOS
277