• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "accesstoken_kit.h"
19 #include "access_token_helper.h"
20 #include "ans_const_define.h"
21 #include "ans_inner_errors.h"
22 #include "ans_log_wrapper.h"
23 #include "ans_trace_wrapper.h"
24 #include "ans_permission_def.h"
25 
26 #include "bundle_manager_helper.h"
27 #include "ipc_skeleton.h"
28 
29 #include "notification_preferences.h"
30 #include "notification_bundle_option.h"
31 #include "notification_analytics_util.h"
32 #include "os_account_manager_helper.h"
33 #include "notification_extension_wrapper.h"
34 #include "notification_config_parse.h"
35 #include "distributed_data_define.h"
36 
37 namespace OHOS {
38 namespace Notification {
39 
40 constexpr int32_t ANS_USERID = 5523;
41 const static std::string BUNDLE_NAME_ZYT = "com.zhuoyi.appstore.lite";
42 const static std::string BUNDLE_NAME_ABROAD = "com.easy.abroad";
43 const static std::string INSTALL_SOURCE_EASYABROAD = "com.easy.abroad";
44 constexpr int32_t ZERO_USER_ID = 0;
45 
RequestEnableNotification(const std::string & deviceId,const sptr<IAnsDialogCallback> & callback)46 ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string &deviceId,
47     const sptr<IAnsDialogCallback> &callback)
48 {
49     return RequestEnableNotification(deviceId, callback, nullptr);
50 }
51 
RequestEnableNotification(const std::string & deviceId,const sptr<IAnsDialogCallback> & callback,const sptr<IRemoteObject> & callerToken)52 ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string &deviceId,
53     const sptr<IAnsDialogCallback> &callback,
54     const sptr<IRemoteObject> &callerToken)
55 {
56     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_13, EventBranchId::BRANCH_4);
57     message.Message(" de:" + deviceId);
58     ANS_LOGD("%{public}s", __FUNCTION__);
59     if (callback == nullptr) {
60         ANS_LOGE("callback == nullptr");
61         message.ErrorCode(ERR_ANS_INVALID_PARAM);
62         NotificationAnalyticsUtil::ReportModifyEvent(message);
63         return ERR_ANS_INVALID_PARAM;
64     }
65     sptr<NotificationBundleOption> bundleOption = GenerateBundleOption();
66     if (bundleOption == nullptr) {
67         ANS_LOGE("bundleOption is nullptr.");
68         return ERROR_INTERNAL_ERROR;
69     }
70     return CommonRequestEnableNotification(deviceId, callback, callerToken, bundleOption, false, false);
71 }
72 
RequestEnableNotification(const std::string & bundleName,int32_t uid)73 ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string& bundleName, int32_t uid)
74 {
75     ANS_LOGI("bundleName = %{public}s uid = %{public}d", bundleName.c_str(), uid);
76     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
77         return ERR_ANS_PERMISSION_DENIED;
78     }
79     if (bundleName == BUNDLE_NAME_ZYT || bundleName == BUNDLE_NAME_ABROAD) {
80         ANS_LOGE("zyt or abroad");
81         return ERR_ANS_NOT_ALLOWED;
82     }
83 
84     AppExecFwk::BundleInfo bundleInfo;
85     bool ret = BundleManagerHelper::GetInstance()->GetBundleInfoV9(bundleName,
86         static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION),
87         bundleInfo, ZERO_USER_ID);
88     bool easyAbroad = false;
89     if (bundleInfo.applicationInfo.installSource == INSTALL_SOURCE_EASYABROAD) {
90         ANS_LOGW("abroad app");
91         easyAbroad = true;
92     }
93     sptr<NotificationBundleOption> bundleOption = new (std::nothrow) NotificationBundleOption(bundleName, uid);
94     if (bundleOption == nullptr) {
95         ANS_LOGE("bundleOption is nullptr.");
96         return ERROR_INTERNAL_ERROR;
97     }
98     return CommonRequestEnableNotification("", nullptr, nullptr, bundleOption, true, easyAbroad);
99 }
100 
CommonRequestEnableNotification(const std::string & deviceId,const sptr<IAnsDialogCallback> & callback,const sptr<IRemoteObject> & callerToken,const sptr<NotificationBundleOption> bundleOption,const bool innerLake,const bool easyAbroad)101 ErrCode AdvancedNotificationService::CommonRequestEnableNotification(const std::string &deviceId,
102     const sptr<IAnsDialogCallback> &callback,
103     const sptr<IRemoteObject> &callerToken,
104     const sptr<NotificationBundleOption> bundleOption,
105     const bool innerLake,
106     const bool easyAbroad)
107 {
108     ANS_LOGI("%{public}s", __FUNCTION__);
109     ErrCode result = ERR_OK;
110     // To get the permission
111     bool allowedNotify = false;
112     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_8, EventBranchId::BRANCH_5);
113     if (bundleOption == nullptr) {
114         ANS_LOGE("bundleOption is nullptr.");
115         return ERROR_INTERNAL_ERROR;
116     }
117     message.Message(bundleOption->GetBundleName() + "_" + std::to_string(bundleOption->GetUid()) +
118             " deviceId:" + deviceId);
119     result = IsAllowedNotifySelf(bundleOption, allowedNotify);
120     if (result != ERR_OK) {
121         ANS_LOGE("Not allowed notify self");
122         message.ErrorCode(result).Append(" Allow failed");
123         NotificationAnalyticsUtil::ReportModifyEvent(message);
124         return ERROR_INTERNAL_ERROR;
125     }
126     ANS_LOGI("allowedNotify = %{public}d, bundle = %{public}s", allowedNotify,
127         bundleOption->GetBundleName().c_str());
128     if (allowedNotify) {
129         message.ErrorCode(ERR_OK).Append(" Allow success");
130         NotificationAnalyticsUtil::ReportModifyEvent(message);
131         return ERR_OK;
132     }
133     // Check to see if it has been popover before
134     bool hasPopped = false;
135     result = GetHasPoppedDialog(bundleOption, hasPopped);
136     if (result != ERR_OK) {
137         ANS_LOGE("Get has popped dialog failed.");
138         message.ErrorCode(result).Append(" Get dialog failed.");
139         NotificationAnalyticsUtil::ReportModifyEvent(message);
140         return ERROR_INTERNAL_ERROR;
141     }
142     if (hasPopped) {
143         ANS_LOGW("Has popped is true.");
144 #ifdef ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER
145         int32_t userId = -1;
146         OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleOption->GetUid(), userId);
147         ANS_LOGD("GetOsAccountLocalIdFromUid PRI, %{public}d, %{public}d", bundleOption->GetUid(), userId);
148         if (!EXTENTION_WRAPPER->GetPrivilegeDialogPopped(bundleOption, userId)) {
149             ANS_LOGE("GetPrivilegeDialogPopped false.");
150             message.ErrorCode(ERR_ANS_NOT_ALLOWED).Append(" Has no permission popped");
151             NotificationAnalyticsUtil::ReportModifyEvent(message);
152             return ERR_ANS_NOT_ALLOWED;
153         } else {
154             ANS_LOGW("duplicated popped.");
155             message.Append(" duplicated popped.");
156         }
157 #else
158         message.ErrorCode(ERR_ANS_NOT_ALLOWED).Append(" Has popped");
159         NotificationAnalyticsUtil::ReportModifyEvent(message);
160         return ERR_ANS_NOT_ALLOWED;
161 #endif
162     }
163     if (!EXTENTION_WRAPPER->NotificationDialogControl()) {
164         return ERR_ANS_NOT_ALLOWED;
165     }
166 
167     if (!CreateDialogManager()) {
168         ANS_LOGE("Create dialog manager failed.");
169         message.ErrorCode(ERR_ANS_NOT_ALLOWED).Append(" Create dialog failed");
170         NotificationAnalyticsUtil::ReportModifyEvent(message);
171         return ERROR_INTERNAL_ERROR;
172     }
173 
174     result = dialogManager_->RequestEnableNotificationDailog(bundleOption,
175         callback, callerToken, innerLake, easyAbroad);
176     if (result == ERR_OK) {
177         result = ERR_ANS_DIALOG_POP_SUCCEEDED;
178     }
179 
180     ANS_LOGI("%{public}s_%{public}d, deviceId: %{public}s, result: %{public}d",
181         bundleOption->GetBundleName().c_str(), bundleOption->GetUid(), StringAnonymous(deviceId).c_str(), result);
182     message.ErrorCode(result);
183     if (!innerLake || result == ERR_ANS_DIALOG_POP_SUCCEEDED) {
184         NotificationAnalyticsUtil::ReportModifyEvent(message);
185     }
186     return result;
187 }
188 
SetNotificationsEnabledForBundle(const std::string & deviceId,bool enabled)189 ErrCode AdvancedNotificationService::SetNotificationsEnabledForBundle(const std::string &deviceId, bool enabled)
190 {
191     return ERR_INVALID_OPERATION;
192 }
193 
SetNotificationsEnabledForAllBundles(const std::string & deviceId,bool enabled)194 ErrCode AdvancedNotificationService::SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled)
195 {
196     ANS_LOGD("%{public}s", __FUNCTION__);
197 
198     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
199     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
200         ANS_LOGD("VerifyNativeToken and IsSystemApp is false.");
201         return ERR_ANS_NON_SYSTEM_APP;
202     }
203 
204     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
205         return ERR_ANS_PERMISSION_DENIED;
206     }
207 
208     int32_t userId = SUBSCRIBE_USER_INIT;
209     if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) {
210         return ERR_ANS_GET_ACTIVE_USER_FAILED;
211     }
212 
213     if (notificationSvrQueue_ == nullptr) {
214         ANS_LOGE("Serial queue is invalidity.");
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         if (deviceId.empty()) {
221             // Local device
222             result = NotificationPreferences::GetInstance()->SetNotificationsEnabled(userId, enabled);
223         } else {
224             // Remote device
225         }
226     }));
227     notificationSvrQueue_->wait(handler);
228     return result;
229 }
230 
SetNotificationsEnabledForSpecialBundle(const std::string & deviceId,const sptr<NotificationBundleOption> & bundleOption,bool enabled,bool updateUnEnableTime)231 ErrCode AdvancedNotificationService::SetNotificationsEnabledForSpecialBundle(
232     const std::string &deviceId, const sptr<NotificationBundleOption> &bundleOption, bool enabled,
233     bool updateUnEnableTime)
234 {
235     return SetNotificationsEnabledForSpecialBundleImpl(
236         deviceId, bundleOption, enabled, updateUnEnableTime, false);
237 }
238 
SetNotificationsSystemEnabledForSpecialBundle(const std::string & deviceId,const sptr<NotificationBundleOption> & bundleOption,bool enabled,bool updateUnEnableTime)239 ErrCode AdvancedNotificationService::SetNotificationsSystemEnabledForSpecialBundle(
240     const std::string &deviceId, const sptr<NotificationBundleOption> &bundleOption, bool enabled,
241     bool updateUnEnableTime)
242 {
243     return SetNotificationsEnabledForSpecialBundleImpl(
244         deviceId, bundleOption, enabled, updateUnEnableTime, true);
245 }
246 
SetNotificationsEnabledForSpecialBundleImpl(const std::string & deviceId,const sptr<NotificationBundleOption> & bundleOption,bool enabled,bool updateUnEnableTime,bool isSystemCall)247 ErrCode AdvancedNotificationService::SetNotificationsEnabledForSpecialBundleImpl(
248     const std::string &deviceId, const sptr<NotificationBundleOption> &bundleOption, bool enabled,
249     bool updateUnEnableTime, bool isSystemCall)
250 {
251     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_13, EventBranchId::BRANCH_5);
252     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
253     ANS_LOGD("%{public}s", __FUNCTION__);
254     if (bundleOption == nullptr) {
255         ANS_LOGE("BundleOption is null.");
256         message.ErrorCode(ERR_ANS_INVALID_BUNDLE);
257         NotificationAnalyticsUtil::ReportModifyEvent(message);
258         return ERR_ANS_INVALID_BUNDLE;
259     }
260 
261     NotificationConstant::SWITCH_STATE state = isSystemCall ?
262         (enabled ? NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_ON :
263             NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_OFF) :
264         (enabled ? NotificationConstant::SWITCH_STATE::USER_MODIFIED_ON :
265             NotificationConstant::SWITCH_STATE::USER_MODIFIED_OFF);
266 
267     message.Message(bundleOption->GetBundleName() + "_" + std::to_string(bundleOption->GetUid()) +
268             " st:" + std::to_string(static_cast<int32_t>(state)) +
269             " dId:" + deviceId);
270     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
271     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
272         ANS_LOGE("IsSystemApp is false.");
273         message.ErrorCode(ERR_ANS_NON_SYSTEM_APP).BranchId(BRANCH_6);
274         NotificationAnalyticsUtil::ReportModifyEvent(message);
275         return ERR_ANS_NON_SYSTEM_APP;
276     }
277 
278     int32_t callingUid = IPCSkeleton::GetCallingUid();
279     if (callingUid != ANS_USERID && !AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
280         ANS_LOGE("Permission Denied.");
281         message.ErrorCode(ERR_ANS_PERMISSION_DENIED).BranchId(BRANCH_7);
282         NotificationAnalyticsUtil::ReportModifyEvent(message);
283         return ERR_ANS_PERMISSION_DENIED;
284     }
285 
286     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
287     if (bundle == nullptr) {
288         message.ErrorCode(ERR_ANS_INVALID_BUNDLE).BranchId(BRANCH_8);
289         NotificationAnalyticsUtil::ReportModifyEvent(message);
290         ANS_LOGE(" Bundle is nullptr.");
291         return ERR_ANS_INVALID_BUNDLE;
292     }
293 
294     sptr<EnabledNotificationCallbackData> bundleData = new (std::nothrow)
295         EnabledNotificationCallbackData(bundle->GetBundleName(), bundle->GetUid(), enabled);
296     if (bundleData == nullptr) {
297         ANS_LOGE("Failed to create EnabledNotificationCallbackData instance");
298         return ERR_NO_MEMORY;
299     }
300 
301     ErrCode result = ERR_OK;
302     if (deviceId.empty()) {
303         // Local device
304         result = NotificationPreferences::GetInstance()->SetNotificationsEnabledForBundle(bundle, state);
305         if (result == ERR_OK) {
306             if (!enabled) {
307                 result = RemoveAllNotificationsForDisable(bundle);
308             }
309 #ifdef ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER
310             if (!enabled && result == ERR_OK && updateUnEnableTime) {
311                 SetDialogPoppedUnEnableTime(bundleOption);
312             }
313 #endif
314             SetSlotFlagsTrustlistsAsBundle(bundle);
315             NotificationSubscriberManager::GetInstance()->NotifyEnabledNotificationChanged(bundleData);
316             PublishSlotChangeCommonEvent(bundle);
317         }
318     } else {
319         // Remote device
320     }
321 
322     ANS_LOGI("%{public}s_%{public}d, deviceId: %{public}s, state: %{public}s, "
323         "result: %{public}d", bundleOption->GetBundleName().c_str(),
324         bundleOption->GetUid(), StringAnonymous(deviceId).c_str(),
325         std::to_string(static_cast<int32_t>(state)).c_str(), result);
326         message.ErrorCode(result).BranchId(BRANCH_9);
327     NotificationAnalyticsUtil::ReportModifyEvent(message);
328     SendEnableNotificationHiSysEvent(bundleOption, enabled, result);
329     return result;
330 }
331 
CanPopEnableNotificationDialog(const sptr<IAnsDialogCallback> & callback,bool & canPop,std::string & bundleName)332 ErrCode AdvancedNotificationService::CanPopEnableNotificationDialog(
333     const sptr<IAnsDialogCallback> &callback, bool &canPop, std::string &bundleName)
334 {
335     ANS_LOGD("%{public}s", __FUNCTION__);
336     canPop = false;
337     ErrCode result = ERR_OK;
338     sptr<NotificationBundleOption> bundleOption = GenerateBundleOption();
339     if (bundleOption == nullptr) {
340         ANS_LOGE("bundleOption is nullptr.");
341         return ERR_ANS_INVALID_BUNDLE;
342     }
343     // To get the permission
344     bool allowedNotify = false;
345     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_2, EventBranchId::BRANCH_2);
346     message.Message(bundleOption->GetBundleName() + "_" + std::to_string(bundleOption->GetUid()) +
347         " canPop:" + std::to_string(canPop));
348     result = IsAllowedNotifySelf(bundleOption, allowedNotify);
349     if (result != ERR_OK) {
350         ANS_LOGE("Not allowed Notify self.");
351         message.ErrorCode(result).Append(" Not Allow");
352         NotificationAnalyticsUtil::ReportModifyEvent(message);
353         return ERROR_INTERNAL_ERROR;
354     }
355     ANS_LOGI("allowedNotify = %{public}d, bundle = %{public}s", allowedNotify,
356         bundleOption->GetBundleName().c_str());
357     if (allowedNotify) {
358         message.ErrorCode(ERR_OK).Append(" Allow success");
359         NotificationAnalyticsUtil::ReportModifyEvent(message);
360         return ERR_OK;
361     }
362     // Check to see if it has been popover before
363     bool hasPopped = false;
364     result = GetHasPoppedDialog(bundleOption, hasPopped);
365     if (result != ERR_OK) {
366         ANS_LOGE("Get has popped dialog failed. result: %{public}d", result);
367         message.ErrorCode(result).Append(" Has popped");
368         NotificationAnalyticsUtil::ReportModifyEvent(message);
369         return ERROR_INTERNAL_ERROR;
370     }
371     if (hasPopped) {
372         ANS_LOGE("Has popped is true.");
373 #ifdef ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER
374         int32_t userId = -1;
375         OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleOption->GetUid(), userId);
376         ANS_LOGD("GetOsAccountLocalIdFromUid PRI, %{public}d, %{public}d", bundleOption->GetUid(), userId);
377         if (!EXTENTION_WRAPPER->GetPrivilegeDialogPopped(bundleOption, userId)) {
378             ANS_LOGE("GetPrivilegeDialogPopped false.");
379             message.ErrorCode(ERR_ANS_NOT_ALLOWED).Append(" Has no permission popped");
380             NotificationAnalyticsUtil::ReportModifyEvent(message);
381             return ERR_ANS_NOT_ALLOWED;
382         } else {
383             ANS_LOGI("duplicated popped.");
384             message.Append(" duplicated popped.");
385         }
386 #else
387         message.ErrorCode(ERR_ANS_NOT_ALLOWED).Append(" Has popped");
388         NotificationAnalyticsUtil::ReportModifyEvent(message);
389         return ERR_ANS_NOT_ALLOWED;
390 #endif
391     }
392     if (!EXTENTION_WRAPPER->NotificationDialogControl()) {
393         return ERR_ANS_NOT_ALLOWED;
394     }
395 
396     if (!CreateDialogManager()) {
397         ANS_LOGE("Create dialog manager failed.");
398         message.ErrorCode(ERR_ANS_NOT_ALLOWED).Append(" Create dialog failed");
399         NotificationAnalyticsUtil::ReportModifyEvent(message);
400         return ERROR_INTERNAL_ERROR;
401     }
402     result = dialogManager_->AddDialogInfo(bundleOption, callback);
403     if (result != ERR_OK) {
404         ANS_LOGI("AddDialogInfo result: %{public}d", result);
405         message.ErrorCode(result).Append(" AddDialogInfo");
406         NotificationAnalyticsUtil::ReportModifyEvent(message);
407         return result;
408     }
409 
410     canPop = true;
411     bundleName = bundleOption->GetBundleName();
412     ANS_LOGI("%{public}s_%{public}d, canPop: %{public}s, result: %{public}d",
413         bundleOption->GetBundleName().c_str(), bundleOption->GetUid(), std::to_string(canPop).c_str(), result);
414     message.ErrorCode(result).Append(" CanPopEnableNotificationDialog end");
415     NotificationAnalyticsUtil::ReportModifyEvent(message);
416     return ERR_OK;
417 }
418 
RemoveEnableNotificationDialog()419 ErrCode AdvancedNotificationService::RemoveEnableNotificationDialog()
420 {
421     ANS_LOGD("%{public}s", __FUNCTION__);
422     ErrCode result = ERR_OK;
423     sptr<NotificationBundleOption> bundleOption = GenerateBundleOption();
424     if (bundleOption == nullptr) {
425         ANS_LOGE("bundleOption == nullptr");
426         return ERR_ANS_INVALID_BUNDLE;
427     }
428     return RemoveEnableNotificationDialog(bundleOption);
429 }
430 
RemoveEnableNotificationDialog(const sptr<NotificationBundleOption> & bundleOption)431 ErrCode AdvancedNotificationService::RemoveEnableNotificationDialog(const sptr<NotificationBundleOption> &bundleOption)
432 {
433     ANS_LOGI("%{public}s, %{public}d",
434         bundleOption->GetBundleName().c_str(),
435         bundleOption->GetUid());
436     if (!CreateDialogManager()) {
437         return ERROR_INTERNAL_ERROR;
438     }
439     std::unique_ptr<NotificationDialogManager::DialogInfo> dialogInfoRemoved = nullptr;
440     dialogManager_->RemoveDialogInfoByBundleOption(bundleOption, dialogInfoRemoved);
441     return ERR_OK;
442 }
443 
GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> & bundleOption)444 ErrCode AdvancedNotificationService::GetAllNotificationEnabledBundles(
445     std::vector<NotificationBundleOption> &bundleOption)
446 {
447     ANS_LOGD("Called.");
448     if (!AccessTokenHelper::IsSystemApp()) {
449         ANS_LOGE("Is not system app.");
450         return ERR_ANS_NON_SYSTEM_APP;
451     }
452     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
453         ANS_LOGE("Permission denied.");
454         return ERR_ANS_PERMISSION_DENIED;
455     }
456     if (notificationSvrQueue_ == nullptr) {
457         ANS_LOGE("Serial queue is invalid.");
458         return ERR_ANS_INVALID_PARAM;
459     }
460     ErrCode result = ERR_OK;
461     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
462         ANS_LOGD("ffrt enter!");
463         result = NotificationPreferences::GetInstance()->GetAllNotificationEnabledBundles(bundleOption);
464         if (result != ERR_OK) {
465             ANS_LOGE("Get all notification enable status failed");
466             return;
467         }
468     }));
469     notificationSvrQueue_->wait(handler);
470 
471     return result;
472 }
473 
SetNotificationsEnabledByUser(int32_t userId,bool enabled)474 ErrCode AdvancedNotificationService::SetNotificationsEnabledByUser(int32_t userId, bool enabled)
475 {
476     ANS_LOGD("%{public}s", __FUNCTION__);
477 
478     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
479     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
480         return ERR_ANS_NON_SYSTEM_APP;
481     }
482 
483     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
484         return ERR_ANS_PERMISSION_DENIED;
485     }
486 
487     if (notificationSvrQueue_ == nullptr) {
488         ANS_LOGE("Serial queue is ineffectiveness.");
489         return ERR_ANS_INVALID_PARAM;
490     }
491     ErrCode result = ERR_OK;
492     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
493         ANS_LOGD("ffrt enter!");
494         result = NotificationPreferences::GetInstance()->SetNotificationsEnabled(userId, enabled);
495     }));
496     notificationSvrQueue_->wait(handler);
497     return result;
498 }
499 
IsAllowedNotify(bool & allowed)500 ErrCode AdvancedNotificationService::IsAllowedNotify(bool &allowed)
501 {
502     ANS_LOGD("%{public}s", __FUNCTION__);
503 
504     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
505     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
506         return ERR_ANS_NON_SYSTEM_APP;
507     }
508 
509     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
510         ANS_LOGD("AccessTokenHelper::CheckPermission is false");
511         return ERR_ANS_PERMISSION_DENIED;
512     }
513 
514     int32_t userId = SUBSCRIBE_USER_INIT;
515     if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) {
516         return ERR_ANS_GET_ACTIVE_USER_FAILED;
517     }
518 
519     if (notificationSvrQueue_ == nullptr) {
520         ANS_LOGE("Serial queue is invalid.");
521         return ERR_ANS_INVALID_PARAM;
522     }
523     ErrCode result = ERR_OK;
524     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
525         ANS_LOGD("ffrt enter!");
526         allowed = false;
527         result = NotificationPreferences::GetInstance()->GetNotificationsEnabled(userId, allowed);
528     }));
529     notificationSvrQueue_->wait(handler);
530     return result;
531 }
532 
IsAllowedNotifySelf(bool & allowed)533 ErrCode AdvancedNotificationService::IsAllowedNotifySelf(bool &allowed)
534 {
535     ANS_LOGD("%{public}s", __FUNCTION__);
536 
537     sptr<NotificationBundleOption> bundleOption = GenerateBundleOption();
538     if (bundleOption == nullptr) {
539         return ERR_ANS_INVALID_BUNDLE;
540     }
541     return IsAllowedNotifySelf(bundleOption, allowed);
542 }
543 
IsAllowedNotifySelf(const sptr<NotificationBundleOption> & bundleOption,bool & allowed)544 ErrCode AdvancedNotificationService::IsAllowedNotifySelf(const sptr<NotificationBundleOption> &bundleOption,
545     bool &allowed)
546 {
547     ANS_LOGD("%{public}s", __FUNCTION__);
548     if (bundleOption == nullptr) {
549         return ERR_ANS_INVALID_BUNDLE;
550     }
551 
552     int32_t userId = SUBSCRIBE_USER_INIT;
553     if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) {
554         ANS_LOGD("GetActiveUserId is false");
555         return ERR_ANS_GET_ACTIVE_USER_FAILED;
556     }
557 
558     ErrCode result = ERR_OK;
559     allowed = false;
560     NotificationConstant::SWITCH_STATE state = NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_OFF;
561     result = NotificationPreferences::GetInstance()->GetNotificationsEnabled(userId, allowed);
562     if (result == ERR_OK && allowed) {
563         result = NotificationPreferences::GetInstance()->GetNotificationsEnabledForBundle(bundleOption, state);
564         if (result == ERR_OK) {
565             allowed = (state == NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_ON ||
566                 state == NotificationConstant::SWITCH_STATE::USER_MODIFIED_ON);
567         }
568         if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
569             result = ERR_OK;
570             // FA model app can publish notification without user confirm
571             allowed = CheckApiCompatibility(bundleOption);
572             SetDefaultNotificationEnabled(bundleOption, allowed);
573         }
574     }
575     return result;
576 }
577 
IsSpecialUserAllowedNotify(int32_t userId,bool & allowed)578 ErrCode AdvancedNotificationService::IsSpecialUserAllowedNotify(int32_t userId, bool &allowed)
579 {
580     ANS_LOGD("%{public}s", __FUNCTION__);
581 
582     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
583     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
584         return ERR_ANS_NON_SYSTEM_APP;
585     }
586 
587     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
588         ANS_LOGD("Failed to checkPermission");
589         return ERR_ANS_PERMISSION_DENIED;
590     }
591 
592     if (notificationSvrQueue_ == nullptr) {
593         ANS_LOGE("NotificationSvrQueue_ is nullptr.");
594         return ERR_ANS_INVALID_PARAM;
595     }
596     ErrCode result = ERR_OK;
597     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
598         ANS_LOGD("ffrt enter!");
599         allowed = false;
600         result = NotificationPreferences::GetInstance()->GetNotificationsEnabled(userId, allowed);
601     }));
602     notificationSvrQueue_->wait(handler);
603     return result;
604 }
605 
IsSpecialBundleAllowedNotify(const sptr<NotificationBundleOption> & bundleOption,bool & allowed)606 ErrCode AdvancedNotificationService::IsSpecialBundleAllowedNotify(
607     const sptr<NotificationBundleOption> &bundleOption, bool &allowed)
608 {
609     ANS_LOGD("%{public}s", __FUNCTION__);
610 
611     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
612     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
613         ANS_LOGE("Not system application");
614         return ERR_ANS_NON_SYSTEM_APP;
615     }
616 
617     int32_t callingUid = IPCSkeleton::GetCallingUid();
618     if (callingUid != ANS_USERID && !AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
619         return ERR_ANS_PERMISSION_DENIED;
620     }
621 
622     sptr<NotificationBundleOption> targetBundle = nullptr;
623     if (isSubsystem) {
624         if (bundleOption != nullptr) {
625             targetBundle = GenerateValidBundleOption(bundleOption);
626         }
627     } else {
628         ErrCode result = GetAppTargetBundle(bundleOption, targetBundle);
629         if (result != ERR_OK) {
630             return result;
631         }
632     }
633 
634     if (targetBundle == nullptr) {
635         return ERR_ANS_INVALID_BUNDLE;
636     }
637 
638     int32_t userId = SUBSCRIBE_USER_INIT;
639     if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) {
640         return ERR_ANS_GET_ACTIVE_USER_FAILED;
641     }
642 
643     ErrCode result = ERR_OK;
644     allowed = false;
645     NotificationConstant::SWITCH_STATE state = NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_OFF;
646     result = NotificationPreferences::GetInstance()->GetNotificationsEnabled(userId, allowed);
647     if (result == ERR_OK && allowed) {
648         result = NotificationPreferences::GetInstance()->GetNotificationsEnabledForBundle(targetBundle, state);
649         if (result == ERR_OK) {
650             allowed = (state == NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_ON ||
651                 state == NotificationConstant::SWITCH_STATE::USER_MODIFIED_ON);
652         }
653         if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
654             result = ERR_OK;
655             allowed = CheckApiCompatibility(targetBundle);
656             SetNotificationsSystemEnabledForSpecialBundle("", bundleOption, allowed);
657         }
658     }
659     return result;
660 }
661 
CanPublishAsBundle(const std::string & representativeBundle,bool & canPublish)662 ErrCode AdvancedNotificationService::CanPublishAsBundle(const std::string &representativeBundle, bool &canPublish)
663 {
664     return ERR_INVALID_OPERATION;
665 }
666 
667 } // Notification
668 } // OHOS
669