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 "bundle_constants.h"
20 #include "common_event_manager.h"
21 #include "common_event_support.h"
22 #include "notification_preferences.h"
23
24 namespace OHOS {
25 namespace Notification {
SystemEventObserver(const ISystemEvent & callbacks)26 SystemEventObserver::SystemEventObserver(const ISystemEvent &callbacks) : callbacks_(callbacks)
27 {
28 EventFwk::MatchingSkills matchingSkills;
29 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
30 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
31 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
32 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
33 #endif
34 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
35 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
36 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED);
37 EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills);
38
39 subscriber_ = std::make_shared<SystemEventSubscriber>(
40 commonEventSubscribeInfo, std::bind(&SystemEventObserver::OnReceiveEvent, this, std::placeholders::_1));
41
42 EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
43 }
44
~SystemEventObserver()45 SystemEventObserver::~SystemEventObserver()
46 {
47 EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
48 }
49
GetBundleOption(AAFwk::Want want)50 sptr<NotificationBundleOption> SystemEventObserver::GetBundleOption(AAFwk::Want want)
51 {
52 auto element = want.GetElement();
53 std::string bundleName = element.GetBundleName();
54 int uid = want.GetIntParam(AppExecFwk::Constants::UID, -1);
55 sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption(bundleName, uid);
56 if (bundleOption == nullptr) {
57 ANS_LOGE("Failed to create bundleOption.");
58 }
59 return bundleOption;
60 }
61
OnReceiveEvent(const EventFwk::CommonEventData & data)62 void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data)
63 {
64 auto want = data.GetWant();
65 std::string action = want.GetAction();
66 ANS_LOGD("OnReceiveEvent action is %{public}s.", action.c_str());
67 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
68 if (callbacks_.onBundleRemoved != nullptr) {
69 sptr<NotificationBundleOption> bundleOption = GetBundleOption(want);
70 if (bundleOption != nullptr) {
71 callbacks_.onBundleRemoved(bundleOption);
72 }
73 }
74 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
75 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
76 if (callbacks_.onScreenOn != nullptr) {
77 callbacks_.onScreenOn();
78 }
79 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
80 if (callbacks_.onScreenOff != nullptr) {
81 callbacks_.onScreenOff();
82 }
83 #endif
84 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
85 NotificationPreferences::GetInstance().InitSettingFromDisturbDB();
86 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
87 int32_t userId = data.GetCode();
88 if (callbacks_.onResourceRemove != nullptr) {
89 callbacks_.onResourceRemove(userId);
90 }
91 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) {
92 if (callbacks_.onBundleDataCleared != nullptr) {
93 sptr<NotificationBundleOption> bundleOption = GetBundleOption(want);
94 if (bundleOption != nullptr) {
95 callbacks_.onBundleDataCleared(bundleOption);
96 }
97 }
98 }
99 }
100 } // namespace Notification
101 } // namespace OHOS