• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "system_event_observer.h"
17 
18 #include "advanced_notification_service.h"
19 #include "ans_const_define.h"
20 #include "bundle_constants.h"
21 #include "common_event_manager.h"
22 #include "common_event_support.h"
23 #include "notification_preferences.h"
24 #include "notification_clone_manager.h"
25 #include "distributed_device_manager.h"
26 
27 namespace OHOS {
28 namespace Notification {
SystemEventObserver(const ISystemEvent & callbacks)29 SystemEventObserver::SystemEventObserver(const ISystemEvent &callbacks) : callbacks_(callbacks)
30 {
31     EventFwk::MatchingSkills matchingSkills;
32     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
33 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
34     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
35     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
36 #endif
37 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
38     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED);
39     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
40 #endif
41     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
42     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
43     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED);
44     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
45     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
46     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED);
47     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_RESTORE_START);
48     EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills);
49     commonEventSubscribeInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
50 
51     subscriber_ = std::make_shared<SystemEventSubscriber>(
52         commonEventSubscribeInfo, std::bind(&SystemEventObserver::OnReceiveEvent, this, std::placeholders::_1));
53 
54     EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
55 }
56 
~SystemEventObserver()57 SystemEventObserver::~SystemEventObserver()
58 {
59     EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
60 }
61 
GetBundleOption(AAFwk::Want want)62 sptr<NotificationBundleOption> SystemEventObserver::GetBundleOption(AAFwk::Want want)
63 {
64     auto element = want.GetElement();
65     std::string bundleName = element.GetBundleName();
66     int32_t appIndex = want.GetIntParam("appIndex", -1);
67     int32_t uid = want.GetIntParam(AppExecFwk::Constants::UID, -1);
68     sptr<NotificationBundleOption> bundleOption = new (std::nothrow) NotificationBundleOption(bundleName, uid);
69     if (bundleOption == nullptr) {
70         ANS_LOGE("Failed to create bundleOption.");
71         return nullptr;
72     }
73     bundleOption->SetAppIndex(appIndex);
74     return bundleOption;
75 }
76 
GetBundleOptionDataCleared(AAFwk::Want want)77 sptr<NotificationBundleOption> SystemEventObserver::GetBundleOptionDataCleared(AAFwk::Want want)
78 {
79     auto element = want.GetElement();
80     std::string bundleName = element.GetBundleName();
81     int32_t appIndex = want.GetIntParam("appIndex", -1);
82     // 元能力提供的UID,该UID获取的是want信息中bundleName对应的UID。
83     int32_t uid = want.GetIntParam("ohos.aafwk.param.targetUid", -1);
84     sptr<NotificationBundleOption> bundleOption = new (std::nothrow) NotificationBundleOption(bundleName, uid);
85     if (bundleOption == nullptr) {
86         ANS_LOGE("Failed to create bundleOption.");
87         return nullptr;
88     }
89     bundleOption->SetAppIndex(appIndex);
90     return bundleOption;
91 }
92 
OnReceiveEvent(const EventFwk::CommonEventData & data)93 void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data)
94 {
95     auto want = data.GetWant();
96     std::string action = want.GetAction();
97     ANS_LOGD("OnReceiveEvent action is %{public}s.", action.c_str());
98     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
99         if (callbacks_.onBundleRemoved != nullptr) {
100             sptr<NotificationBundleOption> bundleOption = GetBundleOption(want);
101             if (bundleOption != nullptr) {
102                 callbacks_.onBundleRemoved(bundleOption);
103             }
104         }
105 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
106     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
107         if (callbacks_.onScreenOn != nullptr) {
108             callbacks_.onScreenOn();
109         }
110     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
111         if (callbacks_.onScreenOff != nullptr) {
112             callbacks_.onScreenOff();
113         }
114 #endif
115     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
116         int32_t userId = data.GetCode();
117         if (userId <= SUBSCRIBE_USER_INIT) {
118             ANS_LOGE("Illegal userId, userId[%{public}d].", userId);
119             return;
120         }
121 
122         NotificationPreferences::GetInstance()->InitSettingFromDisturbDB(userId);
123         AdvancedNotificationService::GetInstance()->RecoverLiveViewFromDb(userId);
124         NotificationCloneManager::GetInstance().OnUserSwitch(userId);
125         DistributedDeviceManager::GetInstance().InitTrustList();
126         return;
127     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
128         int32_t userId = data.GetCode();
129         if (userId <= SUBSCRIBE_USER_INIT) {
130             ANS_LOGE("Illegal userId, userId[%{public}d].", userId);
131             return;
132         }
133         if (callbacks_.onResourceRemove != nullptr) {
134             callbacks_.onResourceRemove(userId);
135         }
136     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) {
137         if (callbacks_.onBundleDataCleared != nullptr) {
138             sptr<NotificationBundleOption> bundleOption = GetBundleOptionDataCleared(want);
139             if (bundleOption != nullptr) {
140                 callbacks_.onBundleDataCleared(bundleOption);
141             }
142         }
143     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_RESTORE_START) {
144         NotificationCloneManager::GetInstance().OnRestoreStart(want);
145     } else {
146         OnReceiveEventInner(data);
147     }
148 }
149 
OnReceiveEventInner(const EventFwk::CommonEventData & data)150 void SystemEventObserver::OnReceiveEventInner(const EventFwk::CommonEventData &data)
151 {
152     std::string action = data.GetWant().GetAction();
153     if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) == 0) {
154         return OnBundleAddEventInner(data);
155     }
156     if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) == 0) {
157         return OnBundleUpdateEventInner(data);
158     }
159     if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED) == 0) {
160         return OnBootSystemCompletedEventInner(data);
161     }
162 }
163 
OnBundleAddEventInner(const EventFwk::CommonEventData & data)164 void SystemEventObserver::OnBundleAddEventInner(const EventFwk::CommonEventData &data)
165 {
166     if (callbacks_.onBundleAdd != nullptr) {
167         sptr<NotificationBundleOption> bundleOption = GetBundleOption(data.GetWant());
168         if (bundleOption != nullptr) {
169             callbacks_.onBundleAdd(bundleOption);
170         }
171     }
172 }
173 
OnBundleUpdateEventInner(const EventFwk::CommonEventData & data)174 void SystemEventObserver::OnBundleUpdateEventInner(const EventFwk::CommonEventData &data)
175 {
176     if (callbacks_.onBundleUpdate != nullptr) {
177         sptr<NotificationBundleOption> bundleOption = GetBundleOption(data.GetWant());
178         if (bundleOption != nullptr) {
179             callbacks_.onBundleUpdate(bundleOption);
180         }
181     }
182 }
183 
OnBootSystemCompletedEventInner(const EventFwk::CommonEventData & data)184 void SystemEventObserver::OnBootSystemCompletedEventInner(const EventFwk::CommonEventData &data)
185 {
186     if (callbacks_.onBootSystemCompleted != nullptr) {
187         callbacks_.onBootSystemCompleted();
188     }
189 }
190 }  // namespace Notification
191 }  // namespace OHOS
192