• 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 
25 namespace OHOS {
26 namespace Notification {
SystemEventObserver(const ISystemEvent & callbacks)27 SystemEventObserver::SystemEventObserver(const ISystemEvent &callbacks) : callbacks_(callbacks)
28 {
29     EventFwk::MatchingSkills matchingSkills;
30     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
31 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
32     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
33     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
34 #endif
35     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
36     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
37     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED);
38     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
39     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
40     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED);
41     EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills);
42     commonEventSubscribeInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
43 
44     subscriber_ = std::make_shared<SystemEventSubscriber>(
45         commonEventSubscribeInfo, std::bind(&SystemEventObserver::OnReceiveEvent, this, std::placeholders::_1));
46 
47     EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
48     InitEventList();
49 }
50 
~SystemEventObserver()51 SystemEventObserver::~SystemEventObserver()
52 {
53     EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
54 }
55 
GetBundleOption(AAFwk::Want want)56 sptr<NotificationBundleOption> SystemEventObserver::GetBundleOption(AAFwk::Want want)
57 {
58     auto element = want.GetElement();
59     std::string bundleName = element.GetBundleName();
60     int32_t uid = want.GetIntParam(AppExecFwk::Constants::UID, -1);
61     sptr<NotificationBundleOption> bundleOption = new (std::nothrow) NotificationBundleOption(bundleName, uid);
62     if (bundleOption == nullptr) {
63         ANS_LOGE("Failed to create bundleOption.");
64         return nullptr;
65     }
66     return bundleOption;
67 }
68 
OnReceiveEvent(const EventFwk::CommonEventData & data)69 void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data)
70 {
71     auto want = data.GetWant();
72     std::string action = want.GetAction();
73     ANS_LOGD("OnReceiveEvent action is %{public}s.", action.c_str());
74     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
75         if (callbacks_.onBundleRemoved != nullptr) {
76             sptr<NotificationBundleOption> bundleOption = GetBundleOption(want);
77             if (bundleOption != nullptr) {
78                 callbacks_.onBundleRemoved(bundleOption);
79             }
80         }
81 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
82     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
83         if (callbacks_.onScreenOn != nullptr) {
84             callbacks_.onScreenOn();
85         }
86     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
87         if (callbacks_.onScreenOff != nullptr) {
88             callbacks_.onScreenOff();
89         }
90 #endif
91     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
92         NotificationPreferences::GetInstance().InitSettingFromDisturbDB();
93     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
94         int32_t userId = data.GetCode();
95         if (userId <= DEFAULT_USER_ID) {
96             ANS_LOGE("Illegal userId, userId[%{public}d].", userId);
97             return;
98         }
99         if (callbacks_.onResourceRemove != nullptr) {
100             callbacks_.onResourceRemove(userId);
101         }
102     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) {
103         if (callbacks_.onBundleDataCleared != nullptr) {
104             sptr<NotificationBundleOption> bundleOption = GetBundleOption(want);
105             if (bundleOption != nullptr) {
106                 callbacks_.onBundleDataCleared(bundleOption);
107             }
108         }
109     } else {
110         OnReceiveEventInner(data);
111     }
112 }
113 
InitEventList()114 void SystemEventObserver::InitEventList()
115 {
116     memberFuncMap_[EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED] =
117         &SystemEventObserver::OnBundleAddEventInner;
118     memberFuncMap_[EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED] =
119         &SystemEventObserver::OnBundleUpdateEventInner;
120     memberFuncMap_[EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED] =
121         &SystemEventObserver::OnBootSystemCompletedEventInner;
122 }
123 
OnReceiveEventInner(const EventFwk::CommonEventData & data)124 void SystemEventObserver::OnReceiveEventInner(const EventFwk::CommonEventData &data)
125 {
126     std::string action = data.GetWant().GetAction();
127     auto itFunc = memberFuncMap_.find(action);
128     if (itFunc == memberFuncMap_.end()) {
129         ANS_LOGE("Action %{public}s callback is not found.", action.c_str());
130         return;
131     }
132 
133     if (itFunc->second == nullptr) {
134         ANS_LOGE("Action [%{public}s] callback is nullptr.", action.c_str());
135         return;
136     }
137 
138     (this->*(itFunc->second))(data);
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