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