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