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