• 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     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         NotificationPreferences::GetInstance()->InitSettingFromDisturbDB(userId);
104         AdvancedNotificationService::GetInstance()->RecoverLiveViewFromDb(userId);
105         NotificationCloneManager::GetInstance().OnUserSwitch(userId);
106     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
107         int32_t userId = data.GetCode();
108         if (userId <= SUBSCRIBE_USER_INIT) {
109             ANS_LOGE("Illegal userId, userId[%{public}d].", userId);
110             return;
111         }
112         if (callbacks_.onResourceRemove != nullptr) {
113             callbacks_.onResourceRemove(userId);
114         }
115     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) {
116         if (callbacks_.onBundleDataCleared != nullptr) {
117             sptr<NotificationBundleOption> bundleOption = GetBundleOption(want);
118             if (bundleOption != nullptr) {
119                 callbacks_.onBundleDataCleared(bundleOption);
120             }
121         }
122     } else {
123         OnReceiveEventInner(data);
124     }
125 }
126 
OnReceiveEventInner(const EventFwk::CommonEventData & data)127 void SystemEventObserver::OnReceiveEventInner(const EventFwk::CommonEventData &data)
128 {
129     std::string action = data.GetWant().GetAction();
130     if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) == 0) {
131         return OnBundleAddEventInner(data);
132     }
133     if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) == 0) {
134         return OnBundleUpdateEventInner(data);
135     }
136     if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED) == 0) {
137         return OnBootSystemCompletedEventInner(data);
138     }
139 }
140 
OnBundleAddEventInner(const EventFwk::CommonEventData & data)141 void SystemEventObserver::OnBundleAddEventInner(const EventFwk::CommonEventData &data)
142 {
143     if (callbacks_.onBundleAdd != nullptr) {
144         sptr<NotificationBundleOption> bundleOption = GetBundleOption(data.GetWant());
145         if (bundleOption != nullptr) {
146             callbacks_.onBundleAdd(bundleOption);
147         }
148     }
149 }
150 
OnBundleUpdateEventInner(const EventFwk::CommonEventData & data)151 void SystemEventObserver::OnBundleUpdateEventInner(const EventFwk::CommonEventData &data)
152 {
153     if (callbacks_.onBundleUpdate != nullptr) {
154         sptr<NotificationBundleOption> bundleOption = GetBundleOption(data.GetWant());
155         if (bundleOption != nullptr) {
156             callbacks_.onBundleUpdate(bundleOption);
157         }
158     }
159 }
160 
OnBootSystemCompletedEventInner(const EventFwk::CommonEventData & data)161 void SystemEventObserver::OnBootSystemCompletedEventInner(const EventFwk::CommonEventData &data)
162 {
163     if (callbacks_.onBootSystemCompleted != nullptr) {
164         callbacks_.onBootSystemCompleted();
165     }
166 }
167 }  // namespace Notification
168 }  // namespace OHOS
169