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