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