• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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     EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills);
39     commonEventSubscribeInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
40 
41     subscriber_ = std::make_shared<SystemEventSubscriber>(
42         commonEventSubscribeInfo, std::bind(&SystemEventObserver::OnReceiveEvent, this, std::placeholders::_1));
43 
44     EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
45 }
46 
~SystemEventObserver()47 SystemEventObserver::~SystemEventObserver()
48 {
49     EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
50 }
51 
GetBundleOption(AAFwk::Want want)52 sptr<NotificationBundleOption> SystemEventObserver::GetBundleOption(AAFwk::Want want)
53 {
54     auto element = want.GetElement();
55     std::string bundleName = element.GetBundleName();
56     int32_t uid = want.GetIntParam(AppExecFwk::Constants::UID, -1);
57     sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption(bundleName, uid);
58     if (bundleOption == nullptr) {
59         ANS_LOGE("Failed to create bundleOption.");
60     }
61     return bundleOption;
62 }
63 
OnReceiveEvent(const EventFwk::CommonEventData & data)64 void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data)
65 {
66     auto want = data.GetWant();
67     std::string action = want.GetAction();
68     ANS_LOGD("OnReceiveEvent action is %{public}s.", action.c_str());
69     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
70         if (callbacks_.onBundleRemoved != nullptr) {
71             sptr<NotificationBundleOption> bundleOption = GetBundleOption(want);
72             if (bundleOption != nullptr) {
73                 callbacks_.onBundleRemoved(bundleOption);
74             }
75         }
76 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
77     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
78         if (callbacks_.onScreenOn != nullptr) {
79             callbacks_.onScreenOn();
80         }
81     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
82         if (callbacks_.onScreenOff != nullptr) {
83             callbacks_.onScreenOff();
84         }
85 #endif
86     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
87         NotificationPreferences::GetInstance().InitSettingFromDisturbDB();
88     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
89         int32_t userId = data.GetCode();
90         if (userId <= DEFAULT_USER_ID) {
91             ANS_LOGE("Illegal userId, userId[%{public}d].", userId);
92             return;
93         }
94         if (callbacks_.onResourceRemove != nullptr) {
95             callbacks_.onResourceRemove(userId);
96         }
97     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) {
98         if (callbacks_.onBundleDataCleared != nullptr) {
99             sptr<NotificationBundleOption> bundleOption = GetBundleOption(want);
100             if (bundleOption != nullptr) {
101                 callbacks_.onBundleDataCleared(bundleOption);
102             }
103         }
104     }
105 }
106 }  // namespace Notification
107 }  // namespace OHOS