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_permission_def.h"
24
25 #include "bundle_manager_helper.h"
26 #include "ipc_skeleton.h"
27
28 #include "notification_preferences.h"
29 #include "notification_bundle_option.h"
30 #include "notification_analytics_util.h"
31 #include "os_account_manager_helper.h"
32 #include "notification_extension_wrapper.h"
33
34 namespace OHOS {
35 namespace Notification {
36
SetSilentReminderEnabled(const sptr<NotificationBundleOption> & bundleOption,const bool enabled)37 ErrCode AdvancedNotificationService::SetSilentReminderEnabled(const sptr<NotificationBundleOption> &bundleOption,
38 const bool enabled)
39 {
40 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_25, EventBranchId::BRANCH_0);
41 ANS_LOGD("%{public}s", __FUNCTION__);
42 if (bundleOption == nullptr) {
43 ANS_LOGE("BundleOption is null.");
44 NotificationAnalyticsUtil::ReportModifyEvent(message.ErrorCode(ERR_ANS_INVALID_BUNDLE));
45 return ERR_ANS_INVALID_BUNDLE;
46 }
47
48 message.Message(bundleOption->GetBundleName() + "_" + std::to_string(bundleOption->GetUid()) +
49 " silentReminderEnabled:" + std::to_string(enabled));
50 bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
51 if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
52 ANS_LOGE("IsSystemApp is false.");
53 return ERR_ANS_NON_SYSTEM_APP;
54 }
55
56 if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
57 ANS_LOGE("Permission Denied.");
58 return ERR_ANS_PERMISSION_DENIED;
59 }
60
61 sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
62 if (bundle == nullptr) {
63 ANS_LOGE("bundle is nullptr");
64 NotificationAnalyticsUtil::ReportModifyEvent(message.ErrorCode(ERR_ANS_INVALID_BUNDLE));
65 return ERR_ANS_INVALID_BUNDLE;
66 }
67
68 if (notificationSvrQueue_ == nullptr) {
69 ANS_LOGE("Serial queue is invalidity.");
70 return ERR_ANS_INVALID_PARAM;
71 }
72 ErrCode result = ERR_OK;
73 ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
74 result = NotificationPreferences::GetInstance()->SetSilentReminderEnabled(bundle, enabled);
75 }));
76 notificationSvrQueue_->wait(handler);
77 ANS_LOGI("%{public}s_%{public}d, enabled: %{public}s, "
78 "SetSilentReminderEnabled result: %{public}d", bundleOption->GetBundleName().c_str(),
79 bundleOption->GetUid(), std::to_string(enabled).c_str(), result);
80 NotificationAnalyticsUtil::ReportModifyEvent(message.ErrorCode(result).BranchId(BRANCH_3));
81
82 return result;
83 }
84
IsSilentReminderEnabled(const sptr<NotificationBundleOption> & bundleOption,int32_t & enableStatusInt)85 ErrCode AdvancedNotificationService::IsSilentReminderEnabled(const sptr<NotificationBundleOption> &bundleOption,
86 int32_t &enableStatusInt)
87 {
88 ANS_LOGD("%{public}s", __FUNCTION__);
89 if (bundleOption == nullptr) {
90 ANS_LOGE("BundleOption is null.");
91 return ERR_ANS_INVALID_BUNDLE;
92 }
93
94 NotificationConstant::SWITCH_STATE enableStatus;
95 bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
96 if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
97 ANS_LOGD("IsSystemApp is bogus.");
98 return ERR_ANS_NON_SYSTEM_APP;
99 }
100
101 if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
102 ANS_LOGE("no permission");
103 return ERR_ANS_PERMISSION_DENIED;
104 }
105
106 sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
107 if (bundle == nullptr) {
108 return ERR_ANS_INVALID_BUNDLE;
109 }
110
111 if (notificationSvrQueue_ == nullptr) {
112 ANS_LOGE("Serial queue is invalidity.");
113 return ERR_ANS_INVALID_PARAM;
114 }
115
116 ErrCode result = ERR_OK;
117 ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
118 result = NotificationPreferences::GetInstance()->IsSilentReminderEnabled(bundle, enableStatus);
119 enableStatusInt = static_cast<int32_t>(enableStatus);
120 }));
121 notificationSvrQueue_->wait(handler);
122 return result;
123 }
124
125 } // Notification
126 } // OHOS