1 /*
2 * Copyright (c) 2021 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 "advanced_notification_service_ability.h"
17 #include "notification_extension_wrapper.h"
18 #include "system_event_observer.h"
19 #include "common_event_manager.h"
20 #include "liveview_all_scenarios_extension_wrapper.h"
21 #include "distributed_device_manager.h"
22 #include "advanced_datashare_helper.h"
23
24 namespace OHOS {
25 namespace Notification {
26 namespace {
27 REGISTER_SYSTEM_ABILITY_BY_ID(AdvancedNotificationServiceAbility, ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID, true);
28 }
29
30 const std::string EXTENSION_BACKUP = "backup";
31 const std::string EXTENSION_RESTORE = "restore";
32
AdvancedNotificationServiceAbility(const int32_t systemAbilityId,bool runOnCreate)33 AdvancedNotificationServiceAbility::AdvancedNotificationServiceAbility(const int32_t systemAbilityId, bool runOnCreate)
34 : SystemAbility(systemAbilityId, runOnCreate), service_(nullptr)
35 {}
36
~AdvancedNotificationServiceAbility()37 AdvancedNotificationServiceAbility::~AdvancedNotificationServiceAbility()
38 {}
39
OnStart()40 void AdvancedNotificationServiceAbility::OnStart()
41 {
42 if (service_ != nullptr) {
43 return;
44 }
45
46 service_ = AdvancedNotificationService::GetInstance();
47 service_->CreateDialogManager();
48 service_->InitPublishProcess();
49
50 if (!Publish(service_)) {
51 return;
52 }
53
54 #ifdef ENABLE_ANS_EXT_WRAPPER
55 EXTENTION_WRAPPER->InitExtentionWrapper();
56 #else
57 ANS_LOGI("Not enabled ans_ext");
58 #endif
59 AddSystemAbilityListener(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
60 AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
61 AddSystemAbilityListener(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
62
63 #ifdef ENABLE_ANS_TELEPHONY_CUST_WRAPPER
64 TEL_EXTENTION_WRAPPER->InitTelExtentionWrapper();
65 #endif
66 AddSystemAbilityListener(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
67 LIVEVIEW_ALL_SCENARIOS_EXTENTION_WRAPPER->InitExtentionWrapper();
68 }
69
OnStop()70 void AdvancedNotificationServiceAbility::OnStop()
71 {
72 service_ = nullptr;
73 }
74
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)75 void AdvancedNotificationServiceAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
76 {
77 ANS_LOGD("SubSystemAbilityListener::OnAddSystemAbility enter !");
78 if (systemAbilityId == DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID) {
79 if (AdvancedDatashareObserver::GetInstance().CheckIfSettingsDataReady()) {
80 if (isDatashaReready_) {
81 return;
82 }
83 #ifdef ENABLE_ANS_AGGREATION
84 EXTENTION_WRAPPER->CheckIfSetlocalSwitch();
85 #endif
86 isDatashaReready_ = true;
87 }
88 } else if (systemAbilityId == COMMON_EVENT_SERVICE_ID) {
89 if (isDatashaReready_) {
90 return;
91 }
92 EventFwk::MatchingSkills matchingSkills;
93 matchingSkills.AddEvent("usual.event.DATA_SHARE_READY");
94 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
95 subscriber_ = std::make_shared<SystemEventSubscriber>(
96 subscribeInfo, std::bind(&AdvancedNotificationServiceAbility::OnReceiveEvent, this, std::placeholders::_1));
97 if (subscriber_ == nullptr) {
98 ANS_LOGD("subscriber_ is nullptr");
99 return;
100 }
101 EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
102 } else if (systemAbilityId == BUNDLE_MGR_SERVICE_SYS_ABILITY_ID) {
103 if (isDatashaReready_) {
104 return;
105 }
106 auto notificationService = AdvancedNotificationService::GetInstance();
107 if (notificationService == nullptr) {
108 return;
109 }
110 notificationService->ResetDistributedEnabled();
111 } else if (systemAbilityId == DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID) {
112 ANS_LOGW("DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID");
113 DistributedDeviceManager::GetInstance().RegisterDms(true);
114 }
115 }
116
OnReceiveEvent(const EventFwk::CommonEventData & data)117 void AdvancedNotificationServiceAbility::OnReceiveEvent(const EventFwk::CommonEventData &data)
118 {
119 ANS_LOGI("CheckIfSettingsDataReady() ok!");
120 if (isDatashaReready_) {
121 return;
122 }
123 auto const &want = data.GetWant();
124 std::string action = want.GetAction();
125 if (action == "usual.event.DATA_SHARE_READY") {
126 AdvancedDatashareHelper::SetIsDataShareReady(true);
127 isDatashaReready_ = true;
128 ANS_LOGI("COMMON_EVENT_SERVICE_ID OnReceiveEvent ok!");
129 #ifdef ENABLE_ANS_AGGREATION
130 EXTENTION_WRAPPER->CheckIfSetlocalSwitch();
131 #endif
132 }
133 }
134
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)135 void AdvancedNotificationServiceAbility::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
136 {
137 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
138 return;
139 }
140 }
141
OnExtension(const std::string & extension,MessageParcel & data,MessageParcel & reply)142 int32_t AdvancedNotificationServiceAbility::OnExtension(const std::string& extension,
143 MessageParcel& data, MessageParcel& reply)
144 {
145 ANS_LOGI("extension is %{public}s.", extension.c_str());
146 auto notificationService = AdvancedNotificationService::GetInstance();
147 if (notificationService == nullptr) {
148 ANS_LOGW("notification service is not initial.");
149 return ERR_OK;
150 }
151 if (extension == EXTENSION_BACKUP) {
152 return notificationService->OnBackup(data, reply);
153 } else if (extension == EXTENSION_RESTORE) {
154 return notificationService->OnRestore(data, reply);
155 }
156 return ERR_OK;
157 }
158 } // namespace Notification
159 } // namespace OHOS
160