• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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.h"
17 
18 #include <functional>
19 #include <iomanip>
20 #include <sstream>
21 
22 #include "accesstoken_kit.h"
23 #include "ans_const_define.h"
24 #include "ans_inner_errors.h"
25 #include "ans_log_wrapper.h"
26 #include "common_event_manager.h"
27 #include "common_event_publish_info.h"
28 #include "errors.h"
29 
30 #include "ipc_skeleton.h"
31 #include "notification_constant.h"
32 #include "os_account_info.h"
33 #include "os_account_manager.h"
34 #include "os_account_manager_helper.h"
35 #include "hitrace_meter_adapter.h"
36 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
37 #include "distributed_notification_manager.h"
38 #include "distributed_preferences.h"
39 #include "distributed_screen_status_manager.h"
40 #endif
41 
42 #include "../advanced_notification_inline.cpp"
43 #include "notification_analytics_util.h"
44 #include "notification_operation_service.h"
45 #include "distributed_device_data_service.h"
46 #ifdef ALL_SCENARIO_COLLABORATION
47 #include "distributed_extension_service.h"
48 #endif
49 
50 namespace OHOS {
51 namespace Notification {
52 using namespace OHOS::AccountSA;
53 
54 const static std::string NOTIFICATION_EVENT_DISTRIBUTED_DEVICE_TYPES_CHANGE =
55     "notification.event.DISTRIBUTED_DEVICE_TYPES_CHANGE";
56 
IsDistributedEnabled(bool & enabled)57 ErrCode AdvancedNotificationService::IsDistributedEnabled(bool &enabled)
58 {
59     ANS_LOGD("%{public}s", __FUNCTION__);
60 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
61     if (notificationSvrQueue_ == nullptr) {
62         ANS_LOGE("Serial queue is invalid.");
63         return ERR_ANS_INVALID_PARAM;
64     }
65     ErrCode result = ERR_OK;
66     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
67         ANS_LOGD("ffrt enter!");
68         result = DistributedPreferences::GetInstance()->GetDistributedEnable(enabled);
69         if (result != ERR_OK) {
70             result = ERR_OK;
71             enabled = false;
72         }
73     }));
74     notificationSvrQueue_->wait(handler);
75     return result;
76 #else
77     return ERR_INVALID_OPERATION;
78 #endif
79 }
80 
SetDistributedEnabledBySlot(int32_t slotTypeInt,const std::string & deviceType,bool enabled)81 ErrCode AdvancedNotificationService::SetDistributedEnabledBySlot(
82     int32_t slotTypeInt, const std::string &deviceType, bool enabled)
83 {
84     ANS_LOGD("%{public}s", __FUNCTION__);
85     NotificationConstant::SlotType slotType = static_cast<NotificationConstant::SlotType>(slotTypeInt);
86     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_8, EventBranchId::BRANCH_7);
87     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
88     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
89         ANS_LOGE("IsSystemApp is false.");
90         message.ErrorCode(ERR_ANS_NON_SYSTEM_APP).Append("Not SystemApp");
91         NotificationAnalyticsUtil::ReportModifyEvent(message);
92         return ERR_ANS_NON_SYSTEM_APP;
93     }
94 
95     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
96         ANS_LOGE("Permission Denied.");
97         message.ErrorCode(ERR_ANS_PERMISSION_DENIED).Append("No permission");
98         NotificationAnalyticsUtil::ReportModifyEvent(message);
99         return ERR_ANS_PERMISSION_DENIED;
100     }
101 
102     ErrCode result = NotificationPreferences::GetInstance()->SetDistributedEnabledBySlot(slotType,
103         deviceType, enabled);
104 #ifdef ALL_SCENARIO_COLLABORATION
105     if (result == ERR_OK && slotType == NotificationConstant::SlotType::LIVE_VIEW) {
106         NotificationConstant::SWITCH_STATE notification = NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_OFF;
107         if (NotificationPreferences::GetInstance()->IsDistributedEnabled(deviceType,
108             notification) != ERR_OK) {
109             ANS_LOGW("Get notification distributed failed %{public}s!", deviceType.c_str());
110         }
111         DeviceStatueChangeInfo changeInfo;
112         changeInfo.enableChange = (notification == NotificationConstant::SWITCH_STATE::USER_MODIFIED_ON) ? true : false;
113         changeInfo.liveViewChange = enabled;
114         changeInfo.changeType = DeviceStatueChangeType::NOTIFICATION_ENABLE_CHANGE;
115         DistributedExtensionService::GetInstance().DeviceStatusChange(changeInfo);
116     }
117 
118     if (result == ERR_OK && !enabled) {
119         RemoveDistributedNotifications(slotType,
120             NotificationConstant::DISTRIBUTED_ENABLE_CLOSE_DELETE,
121             NotificationConstant::DistributedDeleteType::SLOT);
122     }
123 #endif
124     ANS_LOGI("SetDistributedEnabledBySlot %{public}d, deviceType: %{public}s, enabled: %{public}s, "
125         "SetDistributedEnabledBySlot result: %{public}d",
126         slotType, deviceType.c_str(), std::to_string(enabled).c_str(), result);
127     message.ErrorCode(result);
128     NotificationAnalyticsUtil::ReportModifyEvent(message);
129 
130     return result;
131 }
132 
IsDistributedEnabledBySlot(int32_t slotTypeInt,const std::string & deviceType,bool & enabled)133 ErrCode AdvancedNotificationService::IsDistributedEnabledBySlot(
134     int32_t slotTypeInt, const std::string &deviceType, bool &enabled)
135 {
136     ANS_LOGD("%{public}s", __FUNCTION__);
137     NotificationConstant::SlotType slotType = static_cast<NotificationConstant::SlotType>(slotTypeInt);
138     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
139     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
140         ANS_LOGD("IsSystemApp is bogus.");
141         return ERR_ANS_NON_SYSTEM_APP;
142     }
143 
144     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
145         ANS_LOGE("no permission");
146         return ERR_ANS_PERMISSION_DENIED;
147     }
148 
149     return NotificationPreferences::GetInstance()->IsDistributedEnabledBySlot(slotType, deviceType, enabled);
150 }
151 
EnableDistributed(bool enabled)152 ErrCode AdvancedNotificationService::EnableDistributed(bool enabled)
153 {
154     ANS_LOGD("%{public}s", __FUNCTION__);
155 
156 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
157     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
158     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
159         ANS_LOGD("VerifyNativeToken and IsSystemApp is false.");
160         return ERR_ANS_NON_SYSTEM_APP;
161     }
162 
163     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
164         return ERR_ANS_PERMISSION_DENIED;
165     }
166 
167     if (notificationSvrQueue_ == nullptr) {
168         ANS_LOGE("Serial queue is invalidity.");
169         return ERR_ANS_INVALID_PARAM;
170     }
171     ErrCode result = ERR_OK;
172     ffrt::task_handle handler = notificationSvrQueue_->submit_h(
173         std::bind([&]() {
174             result = DistributedPreferences::GetInstance()->SetDistributedEnable(enabled);
175             ANS_LOGE("ffrt enter!");
176         }));
177     notificationSvrQueue_->wait(handler);
178     return result;
179 #else
180     return ERR_INVALID_OPERATION;
181 #endif
182 }
183 
EnableDistributedByBundle(const sptr<NotificationBundleOption> & bundleOption,bool enabled)184 ErrCode AdvancedNotificationService::EnableDistributedByBundle(
185     const sptr<NotificationBundleOption> &bundleOption, bool enabled)
186 {
187     ANS_LOGD("%{public}s", __FUNCTION__);
188 
189 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
190     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
191     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
192         return ERR_ANS_NON_SYSTEM_APP;
193     }
194 
195     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
196         ANS_LOGD("AccessTokenHelper::CheckPermission is false.");
197         return ERR_ANS_PERMISSION_DENIED;
198     }
199 
200     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
201     if (bundle == nullptr) {
202         ANS_LOGD("Create bundle failed.");
203         return ERR_ANS_INVALID_BUNDLE;
204     }
205 
206     bool appInfoEnable = true;
207     GetDistributedEnableInApplicationInfo(bundle, appInfoEnable);
208     if (!appInfoEnable) {
209         ANS_LOGD("Get from bms is %{public}d", appInfoEnable);
210         return ERR_ANS_PERMISSION_DENIED;
211     }
212 
213     if (notificationSvrQueue_ == nullptr) {
214         ANS_LOGE("Serial queue is invalid.");
215         return ERR_ANS_INVALID_PARAM;
216     }
217     ErrCode result = ERR_OK;
218     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
219         ANS_LOGD("ffrt enter!");
220         result = DistributedPreferences::GetInstance()->SetDistributedBundleEnable(bundle, enabled);
221         if (result != ERR_OK) {
222             result = ERR_OK;
223             enabled = false;
224         }
225     }));
226     notificationSvrQueue_->wait(handler);
227     return result;
228 #else
229     return ERR_INVALID_OPERATION;
230 #endif
231 }
232 
EnableDistributedSelf(const bool enabled)233 ErrCode AdvancedNotificationService::EnableDistributedSelf(const bool enabled)
234 {
235     ANS_LOGD("%{public}s", __FUNCTION__);
236 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
237     sptr<NotificationBundleOption> bundleOption = GenerateBundleOption();
238     if (bundleOption == nullptr) {
239         return ERR_ANS_INVALID_BUNDLE;
240     }
241 
242     bool appInfoEnable = true;
243     GetDistributedEnableInApplicationInfo(bundleOption, appInfoEnable);
244     if (!appInfoEnable) {
245         ANS_LOGD("Get from bms is %{public}d", appInfoEnable);
246         return ERR_ANS_PERMISSION_DENIED;
247     }
248 
249     if (notificationSvrQueue_ == nullptr) {
250         ANS_LOGE("notificationSvrQueue_ is nullptr.");
251         return ERR_ANS_INVALID_PARAM;
252     }
253     ErrCode result = ERR_OK;
254     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind(
255         [&]() {
256             ANS_LOGD("ffrt enter!");
257             result = DistributedPreferences::GetInstance()->SetDistributedBundleEnable(bundleOption, enabled);
258         }));
259     notificationSvrQueue_->wait(handler);
260     return result;
261 #else
262     return ERR_INVALID_OPERATION;
263 #endif
264 }
265 
IsDistributedEnableByBundle(const sptr<NotificationBundleOption> & bundleOption,bool & enabled)266 ErrCode AdvancedNotificationService::IsDistributedEnableByBundle(
267     const sptr<NotificationBundleOption> &bundleOption, bool &enabled)
268 {
269     ANS_LOGD("%{public}s", __FUNCTION__);
270 
271 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
272     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
273     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
274         return ERR_ANS_NON_SYSTEM_APP;
275     }
276 
277     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
278         return ERR_ANS_PERMISSION_DENIED;
279     }
280 
281     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
282     if (bundle == nullptr) {
283         ANS_LOGD("Failed to create bundle.");
284         return ERR_ANS_INVALID_BUNDLE;
285     }
286 
287     bool appInfoEnable = true;
288     GetDistributedEnableInApplicationInfo(bundle, appInfoEnable);
289     if (!appInfoEnable) {
290         ANS_LOGD("Get from bms is %{public}d", appInfoEnable);
291         enabled = appInfoEnable;
292         return ERR_OK;
293     }
294 
295     if (notificationSvrQueue_ == nullptr) {
296         ANS_LOGE("Serial queue is invalid.");
297         return ERR_ANS_INVALID_PARAM;
298     }
299     ErrCode result = ERR_OK;
300     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
301         ANS_LOGD("ffrt enter!");
302         result = DistributedPreferences::GetInstance()->GetDistributedBundleEnable(bundle, enabled);
303         if (result != ERR_OK) {
304             result = ERR_OK;
305             enabled = false;
306         }
307     }));
308     notificationSvrQueue_->wait(handler);
309     return result;
310 #else
311     return ERR_INVALID_OPERATION;
312 #endif
313 }
314 
GetTargetDeviceStatus(const std::string & deviceType,int32_t & status)315 ErrCode AdvancedNotificationService::GetTargetDeviceStatus(const std::string &deviceType, int32_t &status)
316 {
317     ANS_LOGD("%{public}s", __FUNCTION__);
318     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
319     if (!isSubsystem) {
320         ANS_LOGD("isSubsystem is bogus.");
321         return ERR_ANS_NON_SYSTEM_APP;
322     }
323     if (deviceType.empty()) {
324         return ERR_ANS_INVALID_PARAM;
325     }
326 
327     uint32_t result = DelayedSingleton<DistributedDeviceStatus>::GetInstance()->GetDeviceStatus(deviceType);
328     status = static_cast<int32_t>(result);
329     ANS_LOGI("Get %{public}s status %{public}u", deviceType.c_str(), status);
330     return ERR_OK;
331 }
332 
333 
DistributeOperationParamCheck(const sptr<NotificationOperationInfo> & operationInfo,const sptr<IAnsOperationCallback> & callback)334 ErrCode DistributeOperationParamCheck(const sptr<NotificationOperationInfo>& operationInfo,
335     const sptr<IAnsOperationCallback> &callback)
336 {
337     if (operationInfo == nullptr || operationInfo->GetHashCode().empty()) {
338         ANS_LOGE("hashCode is empty.");
339         return ERR_ANS_INVALID_PARAM;
340     }
341 
342     OperationType operationType = operationInfo->GetOperationType();
343     if (operationType != OperationType::DISTRIBUTE_OPERATION_JUMP &&
344         operationType != OperationType::DISTRIBUTE_OPERATION_REPLY &&
345         operationType != OperationType::DISTRIBUTE_OPERATION_JUMP_BY_TYPE) {
346         ANS_LOGE("operation type is error.");
347         return ERR_ANS_INVALID_PARAM;
348     }
349 
350     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
351     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
352         ANS_LOGE("is not system app.");
353         return ERR_ANS_NON_SYSTEM_APP;
354     }
355 
356     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
357         ANS_LOGE("not have permission.");
358         return ERR_ANS_PERMISSION_DENIED;
359     }
360     return ERR_OK;
361 }
362 
DistributeOperation(const sptr<NotificationOperationInfo> & operationInfo,const sptr<IAnsOperationCallback> & callback)363 ErrCode AdvancedNotificationService::DistributeOperation(const sptr<NotificationOperationInfo>& operationInfo,
364     const sptr<IAnsOperationCallback> &callback)
365 {
366 #ifdef ALL_SCENARIO_COLLABORATION
367     ErrCode result = DistributeOperationParamCheck(operationInfo, callback);
368     if (result != ERR_OK) {
369         return result;
370     }
371 
372     if (notificationSvrQueue_ == nullptr) {
373         ANS_LOGE("Serial queue is invalidated");
374         return ERR_ANS_INVALID_PARAM;
375     }
376 
377     OperationType operationType = operationInfo->GetOperationType();
378     if (operationType == OperationType::DISTRIBUTE_OPERATION_REPLY) {
379         operationInfo->SetEventId(std::to_string(GetCurrentTime()));
380         std::string key = operationInfo->GetHashCode() + operationInfo->GetEventId();
381         DistributedOperationService::GetInstance().AddOperation(key, callback);
382     }
383     ANS_LOGI("DistributeOperation trigger hashcode %{public}s.", operationInfo->GetHashCode().c_str());
384     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
385         std::string hashCode = operationInfo->GetHashCode();
386         for (auto record : notificationList_) {
387             if (record->notification->GetKey() != hashCode) {
388                 continue;
389             }
390             if (record->notification->GetNotificationRequestPoint() == nullptr) {
391                 continue;
392             }
393             auto request = record->notification->GetNotificationRequestPoint();
394             if (!request->GetDistributedCollaborate()) {
395                 ANS_LOGI("Not collaborate hashcode %{public}s.", hashCode.c_str());
396                 continue;
397             }
398             result = NotificationSubscriberManager::GetInstance()->DistributeOperation(operationInfo, request);
399             return;
400         }
401         ANS_LOGI("DistributeOperation not exist hashcode.");
402         result = ERR_ANS_INVALID_PARAM;
403     }));
404     notificationSvrQueue_->wait(handler);
405     if (result != ERR_OK && operationType == OperationType::DISTRIBUTE_OPERATION_REPLY) {
406         std::string key = operationInfo->GetHashCode() + operationInfo->GetEventId();
407         DistributedOperationService::GetInstance().RemoveOperationResponse(key);
408     }
409     return result;
410 #else
411     return ERR_ANS_INVALID_PARAM;
412 #endif
413 }
414 
ReplyDistributeOperation(const std::string & hashCode,const int32_t result)415 ErrCode AdvancedNotificationService::ReplyDistributeOperation(const std::string& hashCode, const int32_t result)
416 {
417 #ifdef ALL_SCENARIO_COLLABORATION
418     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
419     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
420         return ERR_ANS_NON_SYSTEM_APP;
421     }
422 
423     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
424         ANS_LOGD("Check permission is false.");
425         return ERR_ANS_PERMISSION_DENIED;
426     }
427 
428     if (hashCode.empty()) {
429         ANS_LOGE("Hash code is invalid.");
430         return ERR_ANS_INVALID_PARAM;
431     }
432     ANS_LOGI("Reply operation key %{public}s %{public}d.", hashCode.c_str(), result);
433     DistributedOperationService::GetInstance().ReplyOperationResponse(hashCode, result);
434     return ERR_OK;
435 #else
436     return ERR_ANS_INVALID_PARAM;
437 #endif
438 }
439 
SetTargetDeviceStatus(const std::string & deviceType,uint32_t status,const std::string & deviceId)440 ErrCode AdvancedNotificationService::SetTargetDeviceStatus(const std::string &deviceType, uint32_t status,
441     const std::string &deviceId)
442 {
443     ANS_LOGD("%{public}s", __FUNCTION__);
444     uint32_t status_ = status;
445     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
446     if (!isSubsystem) {
447         ANS_LOGD("isSubsystem is bogus.");
448         return ERR_ANS_NON_SYSTEM_APP;
449     }
450     if (deviceType.empty()) {
451         return ERR_ANS_INVALID_PARAM;
452     }
453 
454     DelayedSingleton<DistributedDeviceStatus>::GetInstance()->SetDeviceStatus(deviceType, status_,
455         DistributedDeviceStatus::DISTURB_DEFAULT_FLAG);
456     return ERR_OK;
457 }
458 
SetTargetDeviceStatus(const std::string & deviceType,uint32_t status,uint32_t controlFlag,const std::string & deviceId,int32_t userId)459 ErrCode AdvancedNotificationService::SetTargetDeviceStatus(const std::string &deviceType, uint32_t status,
460     uint32_t controlFlag, const std::string &deviceId, int32_t userId)
461 {
462     ANS_LOGD("%{public}s", __FUNCTION__);
463     if (deviceType.empty()) {
464         return ERR_ANS_INVALID_PARAM;
465     }
466 
467     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
468     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
469         ANS_LOGD("isSubsystem is bogus.");
470         return ERR_ANS_NON_SYSTEM_APP;
471     }
472 
473     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
474         ANS_LOGD("Check permission is false.");
475         return ERR_ANS_PERMISSION_DENIED;
476     }
477 
478     if (deviceType == NotificationConstant::PAD_DEVICE_TYPE || deviceType == NotificationConstant::PC_DEVICE_TYPE) {
479         return DelayedSingleton<DistributedDeviceStatus>::GetInstance()->SetDeviceStatus(deviceType, status,
480             controlFlag, deviceId, userId);
481     }
482 
483     DelayedSingleton<DistributedDeviceStatus>::GetInstance()->SetDeviceStatus(deviceType, status, controlFlag);
484     ANS_LOGI("update %{public}s status %{public}u %{public}u", deviceType.c_str(), status, controlFlag);
485     return ERR_OK;
486 }
487 
SetTargetDeviceBundleList(const std::string & deviceType,const std::string & deviceId,int operatorType,const std::vector<std::string> & bundleList,const std::vector<std::string> & labelList)488 ErrCode AdvancedNotificationService::SetTargetDeviceBundleList(const std::string& deviceType,
489     const std::string& deviceId, int operatorType, const std::vector<std::string>& bundleList,
490     const std::vector<std::string>& labelList)
491 {
492 #ifdef ALL_SCENARIO_COLLABORATION
493     if (!AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID())) {
494         return ERR_ANS_NON_SYSTEM_APP;
495     }
496 
497     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
498         ANS_LOGD("AccessTokenHelper::CheckPermission is false.");
499         return ERR_ANS_PERMISSION_DENIED;
500     }
501     return DistributedDeviceDataService::GetInstance().SetTargetDeviceBundleList(deviceType, deviceId,
502         operatorType, bundleList, labelList);
503 #else
504     return ERR_ANS_INVALID_PARAM;
505 #endif
506 }
507 
GetMutilDeviceStatus(const std::string & deviceType,const uint32_t status,std::string & deviceId,int32_t & userId)508 ErrCode AdvancedNotificationService::GetMutilDeviceStatus(const std::string &deviceType, const uint32_t status,
509     std::string& deviceId, int32_t& userId)
510 {
511     if (deviceType.empty()) {
512         return ERR_ANS_INVALID_PARAM;
513     }
514 
515     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
516     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
517         ANS_LOGD("isSubsystem is bogus.");
518         return ERR_ANS_NON_SYSTEM_APP;
519     }
520 
521     DeviceStatus deviceStatus = DelayedSingleton<DistributedDeviceStatus>::GetInstance()->GetMultiDeviceStatus(
522         deviceType, status);
523     userId = deviceStatus.userId;
524     deviceId = deviceStatus.deviceId;
525     return ERR_OK;
526 }
527 
GetTargetDeviceBundleList(const std::string & deviceType,const std::string & deviceId,std::vector<std::string> & bundleList,std::vector<std::string> & labelList)528 ErrCode AdvancedNotificationService::GetTargetDeviceBundleList(const std::string& deviceType,
529     const std::string& deviceId, std::vector<std::string>& bundleList, std::vector<std::string>& labelList)
530 {
531     if (deviceType.empty() || deviceId.empty()) {
532         return ERR_ANS_INVALID_PARAM;
533     }
534 
535     if (!AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID())) {
536         return ERR_ANS_NON_SYSTEM_APP;
537     }
538 
539     return DistributedDeviceDataService::GetInstance().GetTargetDeviceBundleList(deviceType, deviceId,
540         bundleList, labelList);
541 }
542 
SetTargetDeviceSwitch(const std::string & deviceType,const std::string & deviceId,bool notificaitonEnable,bool liveViewEnable)543 ErrCode AdvancedNotificationService::SetTargetDeviceSwitch(const std::string& deviceType,
544     const std::string& deviceId, bool notificaitonEnable, bool liveViewEnable)
545 {
546 #ifdef ALL_SCENARIO_COLLABORATION
547     if (!AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID())) {
548         return ERR_ANS_NON_SYSTEM_APP;
549     }
550 
551     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
552         ANS_LOGD("AccessTokenHelper::CheckPermission is false.");
553         return ERR_ANS_PERMISSION_DENIED;
554     }
555     return DistributedDeviceDataService::GetInstance().SetDeviceSyncSwitch(deviceType, deviceId,
556         notificaitonEnable, liveViewEnable);
557 #else
558     return ERR_ANS_INVALID_PARAM;
559 #endif
560 }
561 
GetAllDistribuedEnabledBundles(const std::string & deviceType,std::vector<NotificationBundleOption> & bundleOption)562 ErrCode AdvancedNotificationService::GetAllDistribuedEnabledBundles(
563     const std::string& deviceType, std::vector<NotificationBundleOption> &bundleOption)
564 {
565     ANS_LOGD("Called.");
566     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
567         ANS_LOGE("Permission denied.");
568         return ERR_ANS_PERMISSION_DENIED;
569     }
570     if (notificationSvrQueue_ == nullptr) {
571         ANS_LOGE("Serial queue is invalid.");
572         return ERR_ANS_INVALID_PARAM;
573     }
574 
575     int32_t userId = 100;
576     OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId);
577     ErrCode result = ERR_OK;
578     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&, userId, deviceType]() {
579         ANS_LOGD("ffrt enter!");
580         result = NotificationPreferences::GetInstance()->GetAllDistribuedEnabledBundles(userId,
581             deviceType, bundleOption);
582         if (result != ERR_OK) {
583             ANS_LOGE("Get all notification enable status failed");
584             return;
585         }
586     }));
587     notificationSvrQueue_->wait(handler);
588 
589     return result;
590 }
591 
SetSmartReminderEnabled(const std::string & deviceType,const bool enabled)592 ErrCode AdvancedNotificationService::SetSmartReminderEnabled(const std::string &deviceType, const bool enabled)
593 {
594     ANS_LOGD("%{public}s", __FUNCTION__);
595     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_8, EventBranchId::BRANCH_6);
596     message.Message(" enabled:" + std::to_string(enabled) + " deviceType:" + deviceType);
597     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
598     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
599         ANS_LOGE("IsSystemApp is false.");
600         message.ErrorCode(ERR_ANS_NON_SYSTEM_APP).Append(" Not SystemApp");
601         NotificationAnalyticsUtil::ReportModifyEvent(message);
602         return ERR_ANS_NON_SYSTEM_APP;
603     }
604 
605     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
606         ANS_LOGE("Permission Denied.");
607         message.ErrorCode(ERR_ANS_PERMISSION_DENIED).Append(" Permission Denied");
608         NotificationAnalyticsUtil::ReportModifyEvent(message);
609         return ERR_ANS_PERMISSION_DENIED;
610     }
611     ErrCode result = NotificationPreferences::GetInstance()->SetSmartReminderEnabled(deviceType, enabled);
612 
613     ANS_LOGI("enabled: %{public}s, deviceType: %{public}s,Set smart reminder enabled: %{public}d",
614         std::to_string(enabled).c_str(), deviceType.c_str(), result);
615     message.ErrorCode(result);
616     NotificationAnalyticsUtil::ReportModifyEvent(message);
617     return result;
618 }
619 
IsSmartReminderEnabled(const std::string & deviceType,bool & enabled)620 ErrCode AdvancedNotificationService::IsSmartReminderEnabled(const std::string &deviceType, bool &enabled)
621 {
622     ANS_LOGD("%{public}s", __FUNCTION__);
623     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
624     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
625         ANS_LOGD("IsSystemApp is bogus.");
626         return ERR_ANS_NON_SYSTEM_APP;
627     }
628 
629     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
630         ANS_LOGE("no permission");
631         return ERR_ANS_PERMISSION_DENIED;
632     }
633 
634     return NotificationPreferences::GetInstance()->IsSmartReminderEnabled(deviceType, enabled);
635 }
636 
SetDistributedEnabledByBundle(const sptr<NotificationBundleOption> & bundleOption,const std::string & deviceType,const bool enabled)637 ErrCode AdvancedNotificationService::SetDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
638     const std::string &deviceType, const bool enabled)
639 {
640     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_13, EventBranchId::BRANCH_10);
641     ANS_LOGD("%{public}s", __FUNCTION__);
642     if (bundleOption == nullptr) {
643         ANS_LOGE("BundleOption is null.");
644         NotificationAnalyticsUtil::ReportModifyEvent(message.ErrorCode(ERR_ANS_INVALID_BUNDLE));
645         return ERR_ANS_INVALID_BUNDLE;
646     }
647 
648     message.Message(bundleOption->GetBundleName() + "_" + std::to_string(bundleOption->GetUid()) +
649         " en:" + std::to_string(enabled) + " dT:" + deviceType);
650     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
651     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
652         ANS_LOGE("IsSystemApp is false.");
653         NotificationAnalyticsUtil::ReportModifyEvent(message.ErrorCode(ERR_ANS_NON_SYSTEM_APP).BranchId(BRANCH_11));
654         return ERR_ANS_NON_SYSTEM_APP;
655     }
656 
657     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
658         ANS_LOGE("Permission Denied.");
659         NotificationAnalyticsUtil::ReportModifyEvent(message.ErrorCode(ERR_ANS_PERMISSION_DENIED).BranchId(BRANCH_12));
660         return ERR_ANS_PERMISSION_DENIED;
661     }
662 
663     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
664     if (bundle == nullptr) {
665         ANS_LOGE("bundle is nullptr");
666         return ERR_ANS_INVALID_BUNDLE;
667     }
668 
669     ErrCode result = NotificationPreferences::GetInstance()->SetDistributedEnabledByBundle(bundle,
670         deviceType, enabled);
671 
672     ANS_LOGI("%{public}s_%{public}d, deviceType: %{public}s, enabled: %{public}s, "
673         "SetDistributedEnabledByBundle result: %{public}d", bundleOption->GetBundleName().c_str(),
674         bundleOption->GetUid(), deviceType.c_str(), std::to_string(enabled).c_str(), result);
675     NotificationAnalyticsUtil::ReportModifyEvent(message.ErrorCode(result).BranchId(BRANCH_13));
676 
677     return result;
678 }
679 
SetDistributedBundleOption(const std::vector<sptr<DistributedBundleOption>> & bundles,const std::string & deviceType)680 ErrCode AdvancedNotificationService::SetDistributedBundleOption(
681     const std::vector<sptr<DistributedBundleOption>> &bundles,
682     const std::string &deviceType)
683 {
684     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_13, EventBranchId::BRANCH_10);
685     ANS_LOGD("%{public}s", __FUNCTION__);
686     if (bundles.empty()) {
687         ANS_LOGE("bundles is null.");
688         NotificationAnalyticsUtil::ReportModifyEvent(message.Message("bundles null").ErrorCode(ERR_ANS_INVALID_PARAM));
689         return ERR_ANS_INVALID_PARAM;
690     }
691 
692     if (deviceType.empty()) {
693         ANS_LOGE("deviceType is null.");
694         NotificationAnalyticsUtil::ReportModifyEvent(message.Message("device null").ErrorCode(ERR_ANS_INVALID_PARAM));
695         return ERR_ANS_INVALID_PARAM;
696     }
697 
698     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
699     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
700         ANS_LOGE("IsSystemApp is false.");
701         NotificationAnalyticsUtil::ReportModifyEvent(message.ErrorCode(ERR_ANS_NON_SYSTEM_APP).BranchId(BRANCH_11));
702         return ERR_ANS_NON_SYSTEM_APP;
703     }
704 
705     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
706         ANS_LOGE("Permission Denied.");
707         NotificationAnalyticsUtil::ReportModifyEvent(message.ErrorCode(ERR_ANS_PERMISSION_DENIED).BranchId(BRANCH_12));
708         return ERR_ANS_PERMISSION_DENIED;
709     }
710 
711     std::vector<sptr<DistributedBundleOption>> affectBundleOption;
712     for (auto distributedBundle : bundles) {
713         std::string bundleName = distributedBundle->GetBundle()->GetBundleName();
714         if (bundleName.empty()) {
715             ANS_LOGW("unaffet bundle. empty bundle");
716             continue;
717         }
718         int32_t uid = distributedBundle->GetBundle()->GetUid();
719         sptr<NotificationBundleOption> bundleOption = new (std::nothrow) NotificationBundleOption(bundleName, uid);
720         sptr<NotificationBundleOption> returnOption = GenerateValidBundleOption(bundleOption);
721         if (returnOption == nullptr) {
722             ANS_LOGW("unaffet bundle. %{public}s %{public}d", bundleName.c_str(), uid);
723             continue;
724         }
725         distributedBundle->GetBundle()->SetUid(returnOption->GetUid());
726         affectBundleOption.emplace_back(distributedBundle);
727     }
728 
729     if (affectBundleOption.empty()) {
730         ANS_LOGE("no bundle is afffect");
731         NotificationAnalyticsUtil::ReportModifyEvent(message.ErrorCode(
732             ERR_ANS_DISTRIBUTED_OPERATION_FAILED).BranchId(BRANCH_13));
733         return ERR_ANS_DISTRIBUTED_OPERATION_FAILED;
734     }
735 
736     ErrCode result = NotificationPreferences::GetInstance()->SetDistributedBundleOption(
737         affectBundleOption, deviceType);
738 
739     ANS_LOGI("SetDistributedBundleOption result: %{public}s, %{public}d",  deviceType.c_str(), result);
740     NotificationAnalyticsUtil::ReportModifyEvent(
741         message.Message("batch").ErrorCode(result).BranchId(BRANCH_13));
742 
743     return result;
744 }
745 
IsDistributedEnabledByBundle(const sptr<NotificationBundleOption> & bundleOption,const std::string & deviceType,bool & enabled)746 ErrCode AdvancedNotificationService::IsDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
747     const std::string &deviceType, bool &enabled)
748 {
749     ANS_LOGD("%{public}s", __FUNCTION__);
750 
751     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
752     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
753         ANS_LOGD("IsSystemApp is bogus.");
754         return ERR_ANS_NON_SYSTEM_APP;
755     }
756 
757     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
758         ANS_LOGE("no permission");
759         return ERR_ANS_PERMISSION_DENIED;
760     }
761 
762     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
763     if (bundle == nullptr) {
764         return ERR_ANS_INVALID_BUNDLE;
765     }
766 
767     return NotificationPreferences::GetInstance()->IsDistributedEnabledByBundle(bundle, deviceType, enabled);
768 }
769 
SetDistributedEnabled(const std::string & deviceType,const bool enabled)770 ErrCode AdvancedNotificationService::SetDistributedEnabled(const std::string &deviceType, const bool enabled)
771 {
772     ANS_LOGD("%{public}s", __FUNCTION__);
773     bool isSubSystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
774     if (!isSubSystem && !AccessTokenHelper::IsSystemApp()) {
775         ANS_LOGW("Not system app or SA!");
776         return ERR_ANS_NON_SYSTEM_APP;
777     }
778 
779     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
780         ANS_LOGE("no permission");
781         return ERR_ANS_PERMISSION_DENIED;
782     }
783 
784     auto result = NotificationPreferences::GetInstance()->SetDistributedEnabled(deviceType,
785         enabled ? NotificationConstant::SWITCH_STATE::USER_MODIFIED_ON
786         : NotificationConstant::SWITCH_STATE::USER_MODIFIED_OFF);
787 
788     if (deviceType == NotificationConstant::LITEWEARABLE_DEVICE_TYPE) {
789         return result;
790     }
791 
792 #ifdef ALL_SCENARIO_COLLABORATION
793     if (result == ERR_OK) {
794         bool liveViewEnabled = false;
795         if (NotificationPreferences::GetInstance()->IsDistributedEnabledBySlot(
796             NotificationConstant::SlotType::LIVE_VIEW, deviceType, liveViewEnabled) != ERR_OK) {
797             ANS_LOGW("Get live view distributed failed %{public}s!", deviceType.c_str());
798         }
799         DeviceStatueChangeInfo changeInfo;
800         changeInfo.enableChange = enabled;
801         changeInfo.liveViewChange = liveViewEnabled;
802         changeInfo.changeType = DeviceStatueChangeType::NOTIFICATION_ENABLE_CHANGE;
803         DistributedExtensionService::GetInstance().DeviceStatusChange(changeInfo);
804     }
805 
806     if (result == ERR_OK && !enabled) {
807         RemoveDistributedNotifications(NotificationConstant::SlotType::LIVE_VIEW,
808             NotificationConstant::DISTRIBUTED_ENABLE_CLOSE_DELETE,
809             NotificationConstant::DistributedDeleteType::EXCLUDE_ONE_SLOT);
810     }
811 #endif
812     return result;
813 }
814 
IsDistributedEnabled(const std::string & deviceType,bool & enabled)815 ErrCode AdvancedNotificationService::IsDistributedEnabled(const std::string &deviceType, bool &enabled)
816 {
817     ANS_LOGD("%{public}s", __FUNCTION__);
818     bool isSubSystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
819     if (!isSubSystem && !AccessTokenHelper::IsSystemApp()) {
820         ANS_LOGW("Not system app or SA!");
821         return ERR_ANS_NON_SYSTEM_APP;
822     }
823 
824     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
825         ANS_LOGE("no permission");
826         return ERR_ANS_PERMISSION_DENIED;
827     }
828 
829     NotificationConstant::SWITCH_STATE enableStatus;
830     ErrCode errResult = NotificationPreferences::GetInstance()->IsDistributedEnabled(deviceType, enableStatus);
831     enabled = (enableStatus == NotificationConstant::SWITCH_STATE::USER_MODIFIED_ON);
832     return errResult;
833 }
834 
GetDistributedAbility(int32_t & abilityId)835 ErrCode AdvancedNotificationService::GetDistributedAbility(int32_t &abilityId)
836 {
837     ANS_LOGD("%{public}s", __FUNCTION__);
838 
839     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
840     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
841         ANS_LOGD("IsSystemApp is bogus.");
842         return ERR_ANS_NON_SYSTEM_APP;
843     }
844 
845     abilityId = static_cast<int32_t>(NotificationConstant::DANS_SUPPORT_STATUS::UNSUPPORT);
846     bool isPrivate = false;
847     ErrCode result = OsAccountManagerHelper::GetInstance().GetOsAccountPrivateStatus(isPrivate);
848     if (result != ERR_OK || isPrivate) {
849         return result;
850     }
851     abilityId = static_cast<int32_t>(NotificationConstant::DANS_SUPPORT_STATUS::SUPPORT);
852     return result;
853 }
854 
GetDistributedAuthStatus(const std::string & deviceType,const std::string & deviceId,int32_t userId,bool & isAuth)855 ErrCode AdvancedNotificationService::GetDistributedAuthStatus(
856     const std::string &deviceType, const std::string &deviceId, int32_t userId, bool &isAuth)
857 {
858     ANS_LOGD("%{public}s", __FUNCTION__);
859 
860     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
861     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
862         ANS_LOGD("IsSystemApp is bogus.");
863         return ERR_ANS_NON_SYSTEM_APP;
864     }
865 
866     return NotificationPreferences::GetInstance()->GetDistributedAuthStatus(deviceType, deviceId, userId, isAuth);
867 }
868 
UpdateDistributedDeviceList(const std::string & deviceType,int32_t userId)869 void AdvancedNotificationService::UpdateDistributedDeviceList(const std::string &deviceType, int32_t userId)
870 {
871     std::vector<std::string> deviceTypes;
872     auto result = NotificationPreferences::GetInstance()->GetDistributedDevicelist(deviceTypes);
873     if (result != ERR_OK) {
874         ANS_LOGE("Get distributed device list failed");
875         return;
876     }
877     auto it = std::find(deviceTypes.begin(), deviceTypes.end(), deviceType);
878     if (it == deviceTypes.end()) {
879         deviceTypes.push_back(deviceType);
880         result = NotificationPreferences::GetInstance()->SetDistributedDevicelist(deviceTypes, userId);
881         if (result != ERR_OK) {
882             ANS_LOGE("Set distributed device list failed");
883         }
884     }
885 }
886 
SetDistributedAuthStatus(const std::string & deviceType,const std::string & deviceId,int32_t userId,bool isAuth)887 ErrCode AdvancedNotificationService::SetDistributedAuthStatus(
888     const std::string &deviceType, const std::string &deviceId, int32_t userId, bool isAuth)
889 {
890     ANS_LOGD("%{public}s", __FUNCTION__);
891 
892     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
893     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
894         ANS_LOGD("IsSystemApp is bogus.");
895         return ERR_ANS_NON_SYSTEM_APP;
896     }
897 
898     auto result =
899         NotificationPreferences::GetInstance()->SetDistributedAuthStatus(deviceType, deviceId, userId, isAuth);
900     if (result == ERR_OK) {
901         if (isAuth) {
902             UpdateDistributedDeviceList(deviceType, userId);
903         }
904         EventFwk::Want want;
905         want.SetAction(NOTIFICATION_EVENT_DISTRIBUTED_DEVICE_TYPES_CHANGE);
906         EventFwk::CommonEventData commonData{ want };
907         EventFwk::CommonEventPublishInfo publishInfo;
908         publishInfo.SetSubscriberType(EventFwk::SubscriberType::SYSTEM_SUBSCRIBER_TYPE);
909         if (!EventFwk::CommonEventManager::PublishCommonEventAsUser(commonData, publishInfo, userId)) {
910             ANS_LOGE("Publish common event failed");
911         }
912     }
913     return result;
914 }
915 
GetDistributedDevicelist(std::vector<std::string> & deviceTypes)916 ErrCode AdvancedNotificationService::GetDistributedDevicelist(std::vector<std::string> &deviceTypes)
917 {
918     ANS_LOGD("%{public}s", __FUNCTION__);
919     bool isSubSystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
920     if (!isSubSystem && !AccessTokenHelper::IsSystemApp()) {
921         ANS_LOGW("Not system app or SA!");
922         return ERR_ANS_NON_SYSTEM_APP;
923     }
924 
925     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
926         ANS_LOGE("no permission");
927         return ERR_ANS_PERMISSION_DENIED;
928     }
929     return NotificationPreferences::GetInstance()->GetDistributedDevicelist(deviceTypes);
930 }
931 
GetDeviceRemindType(int32_t & remindTypeInt)932 ErrCode AdvancedNotificationService::GetDeviceRemindType(int32_t& remindTypeInt)
933 {
934     ANS_LOGD("%{public}s", __FUNCTION__);
935 
936     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
937     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
938         return ERR_ANS_NON_SYSTEM_APP;
939     }
940 
941     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
942         return ERR_ANS_PERMISSION_DENIED;
943     }
944 
945 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
946     if (notificationSvrQueue_ == nullptr) {
947         ANS_LOGE("Serial queue is invalid.");
948         return ERR_ANS_INVALID_PARAM;
949     }
950     ffrt::task_handle handler =
951         notificationSvrQueue_->submit_h(std::bind([&]() { remindTypeInt = static_cast<int32_t>(GetRemindType()); }));
952     notificationSvrQueue_->wait(handler);
953     return ERR_OK;
954 #else
955     return ERR_INVALID_OPERATION;
956 #endif
957 }
958 
SetSyncNotificationEnabledWithoutApp(const int32_t userId,const bool enabled)959 ErrCode AdvancedNotificationService::SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled)
960 {
961     ANS_LOGD("userId: %{public}d, enabled: %{public}d", userId, enabled);
962 
963 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
964     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
965     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
966         return ERR_ANS_NON_SYSTEM_APP;
967     }
968 
969     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
970         ANS_LOGD("AccessTokenHelper::CheckPermission is false.");
971         return ERR_ANS_PERMISSION_DENIED;
972     }
973 
974     if (notificationSvrQueue_ == nullptr) {
975         ANS_LOGE("Serial queue is invalidity.");
976         return ERR_ANS_INVALID_PARAM;
977     }
978     ErrCode result = ERR_OK;
979     ffrt::task_handle handler = notificationSvrQueue_->submit_h(
980         std::bind([&]() {
981             ANS_LOGD("ffrt enter!");
982             result = DistributedPreferences::GetInstance()->SetSyncEnabledWithoutApp(userId, enabled);
983         }));
984     notificationSvrQueue_->wait(handler);
985     return result;
986 #else
987     return ERR_INVALID_OPERATION;
988 #endif
989 }
990 
GetSyncNotificationEnabledWithoutApp(const int32_t userId,bool & enabled)991 ErrCode AdvancedNotificationService::GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled)
992 {
993     ANS_LOGD("userId: %{public}d", userId);
994 
995 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
996     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
997     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
998         return ERR_ANS_NON_SYSTEM_APP;
999     }
1000 
1001     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
1002         return ERR_ANS_PERMISSION_DENIED;
1003     }
1004 
1005     if (notificationSvrQueue_ == nullptr) {
1006         ANS_LOGE("Serial queue is invalid.");
1007         return ERR_ANS_INVALID_PARAM;
1008     }
1009     ErrCode result = ERR_OK;
1010     ffrt::task_handle handler = notificationSvrQueue_->submit_h(
1011         std::bind([&]() {
1012             ANS_LOGD("ffrt enter!");
1013             result = DistributedPreferences::GetInstance()->GetSyncEnabledWithoutApp(userId, enabled);
1014         }));
1015     notificationSvrQueue_->wait(handler);
1016     return result;
1017 #else
1018     return ERR_INVALID_OPERATION;
1019 #endif
1020 }
1021 }  // namespace Notification
1022 }  // namespa OHOS
1023