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 if (isDatashaReready_) {
99 return;
100 }
101 auto notificationService = AdvancedNotificationService::GetInstance();
102 if (notificationService == nullptr) {
103 return;
104 }
105 notificationService->ResetDistributedEnabled();
106 }
107 }
108
OnReceiveEvent(const EventFwk::CommonEventData & data)109 void AdvancedNotificationServiceAbility::OnReceiveEvent(const EventFwk::CommonEventData &data)
110 {
111 ANS_LOGI("CheckIfSettingsDataReady() ok!");
112 if (isDatashaReready_) {
113 return;
114 }
115 auto const &want = data.GetWant();
116 std::string action = want.GetAction();
117 if (action == "usual.event.DATA_SHARE_READY") {
118 isDatashaReready_ = true;
119 ANS_LOGI("COMMON_EVENT_SERVICE_ID OnReceiveEvent ok!");
120 EXTENTION_WRAPPER->CheckIfSetlocalSwitch();
121 }
122 }
123
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)124 void AdvancedNotificationServiceAbility::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
125 {
126 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
127 return;
128 }
129 }
130
OnExtension(const std::string & extension,MessageParcel & data,MessageParcel & reply)131 int32_t AdvancedNotificationServiceAbility::OnExtension(const std::string& extension,
132 MessageParcel& data, MessageParcel& reply)
133 {
134 ANS_LOGI("extension is %{public}s.", extension.c_str());
135 auto notificationService = AdvancedNotificationService::GetInstance();
136 if (notificationService == nullptr) {
137 ANS_LOGW("notification service is not initial.");
138 return ERR_OK;
139 }
140 if (extension == EXTENSION_BACKUP) {
141 return notificationService->OnBackup(data, reply);
142 } else if (extension == EXTENSION_RESTORE) {
143 return notificationService->OnRestore(data, reply);
144 }
145 return ERR_OK;
146 }
147 } // namespace Notification
148 } // namespace OHOS
149