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