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_constants.h"
23 #include "form_data_mgr.h"
24 #include "form_db_cache.h"
25 #include "form_info_mgr.h"
26 #include "form_render_mgr.h"
27 #include "form_serial_queue.h"
28 #include "form_timer_mgr.h"
29 #include "in_process_call_wrapper.h"
30 #include "want.h"
31
32 namespace OHOS {
33 namespace AppExecFwk {
34 namespace {
35 const std::string COMMON_EVENT_BUNDLE_SCAN_FINISHED = "usual.event.BUNDLE_SCAN_FINISHED";
36 } // namespace
37 /**
38 * @brief Receiver Constructor.
39 * @param subscriberInfo Subscriber info.
40 */
FormSysEventReceiver(const EventFwk::CommonEventSubscribeInfo & subscriberInfo)41 FormSysEventReceiver::FormSysEventReceiver(const EventFwk::CommonEventSubscribeInfo &subscriberInfo)
42 : EventFwk::CommonEventSubscriber(subscriberInfo)
43 {}
44 /**
45 * @brief Receive common event.
46 * @param eventData Common event data.
47 */
OnReceiveEvent(const EventFwk::CommonEventData & eventData)48 void FormSysEventReceiver::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
49 {
50 const AAFwk::Want& want = eventData.GetWant();
51 std::string action = want.GetAction();
52 std::string bundleName = want.GetElement().GetBundleName();
53 if (action.empty()) {
54 HILOG_ERROR("%{public}s failed, empty action", __func__);
55 return;
56 }
57 if (bundleName.empty() && action != EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED &&
58 action != COMMON_EVENT_BUNDLE_SCAN_FINISHED &&
59 action != EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
60 HILOG_ERROR("%{public}s failed, invalid param, action: %{public}s, bundleName: %{public}s",
61 __func__, action.c_str(), bundleName.c_str());
62 return;
63 }
64 if (serialQueue_ == nullptr) {
65 HILOG_ERROR("%{public}s fail, serialQueue_ invalidate.", __func__);
66 return;
67 }
68 HILOG_INFO("%{public}s, action:%{public}s.", __func__, action.c_str());
69 std::weak_ptr<FormSysEventReceiver> weakThis = shared_from_this();
70 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_ABILITY_UPDATED) {
71 auto task = [weakThis, want, bundleName]() {
72 HILOG_INFO("%{public}s, bundle updated, bundleName: %{public}s", __func__, bundleName.c_str());
73 std::shared_ptr<FormSysEventReceiver> sharedThis = weakThis.lock();
74 if (sharedThis) {
75 int userId = want.GetIntParam(KEY_USER_ID, 0);
76 sharedThis->formEventHelper_.HandleProviderUpdated(bundleName, userId);
77 }
78 };
79 serialQueue_->ScheduleTask(0, task);
80 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
81 int32_t userId = eventData.GetCode();
82 auto task = [weakThis, userId]() {
83 std::shared_ptr<FormSysEventReceiver> sharedThis = weakThis.lock();
84 if (sharedThis) {
85 sharedThis->HandleUserIdRemoved(userId);
86 }
87 };
88 if (userId != -1) {
89 serialQueue_->ScheduleTask(0, task);
90 }
91 } else if (action == COMMON_EVENT_BUNDLE_SCAN_FINISHED) {
92 auto task = [weakThis, want]() {
93 std::shared_ptr<FormSysEventReceiver> sharedThis = weakThis.lock();
94 if (sharedThis) {
95 int32_t userId = want.GetIntParam(KEY_USER_ID, Constants::DEFAULT_USERID);
96 sharedThis->HandleBundleScanFinished(userId);
97 }
98 };
99 serialQueue_->ScheduleTask(0, task);
100 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) {
101 int userId = want.GetIntParam(KEY_USER_ID, Constants::DEFAULT_USERID);
102 auto task = [weakThis, bundleName, userId]() {
103 std::shared_ptr<FormSysEventReceiver> sharedThis = weakThis.lock();
104 if (sharedThis) {
105 sharedThis->formEventHelper_.HandleBundleDataCleared(bundleName, userId);
106 }
107 };
108 serialQueue_->ScheduleTask(0, task);
109 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
110 auto task = [weakThis]() {
111 std::shared_ptr<FormSysEventReceiver> sharedThis = weakThis.lock();
112 if (sharedThis) {
113 sharedThis->formEventHelper_.HandleOnUnlock();
114 }
115 };
116 serialQueue_->ScheduleTask(0, task);
117 } else {
118 HILOG_WARN("%{public}s warnning, invalid action.", __func__);
119 }
120 }
121
122 // multiuser
HandleUserIdRemoved(const int32_t userId)123 void FormSysEventReceiver::HandleUserIdRemoved(const int32_t userId)
124 {
125 std::vector<int64_t> removedFormIds;
126 FormDataMgr::GetInstance().DeleteFormsByUserId(userId, removedFormIds);
127 FormDbCache::GetInstance().DeleteDBFormsByUserId(userId);
128
129 // delete form timer
130 std::vector<int64_t>::iterator itRemoved;
131 for (itRemoved = removedFormIds.begin();itRemoved != removedFormIds.end(); ++itRemoved) {
132 FormTimerMgr::GetInstance().RemoveFormTimer(*itRemoved);
133 }
134 }
135
HandleBundleScanFinished(const int32_t userId)136 void FormSysEventReceiver::HandleBundleScanFinished(const int32_t userId)
137 {
138 HILOG_INFO("%{public}s start", __func__);
139 FormInfoMgr::GetInstance().ReloadFormInfos(userId);
140 }
141 } // namespace AppExecFwk
142 } // namespace OHOS
143