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