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