• 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 #include "ani_notification_enable.h"
16 
17 #include "notification_helper.h"
18 #include "ans_log_wrapper.h"
19 #include "sts_throw_erro.h"
20 #include "sts_common.h"
21 #include "sts_bundle_option.h"
22 
23 namespace OHOS {
24 namespace NotificationManagerSts {
AniIsNotificationEnabled(ani_env * env)25 ani_boolean AniIsNotificationEnabled(ani_env *env)
26 {
27     ANS_LOGD("AniIsNotificationEnabled call");
28     bool allowed = false;
29     int returncode = Notification::NotificationHelper::IsAllowedNotifySelf(allowed);
30     if (returncode != ERR_OK) {
31         int externalCode = NotificationSts::GetExternalCode(returncode);
32         OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
33         ANS_LOGE("AniIsNotificationEnabled -> error, errorCode: %{public}d", externalCode);
34     }
35     ANS_LOGD("AniIsNotificationEnabled end");
36     return NotificationSts::BoolToAniBoolean(allowed);
37 }
38 
AniIsNotificationEnabledWithId(ani_env * env,ani_double userId)39 ani_boolean AniIsNotificationEnabledWithId(ani_env *env, ani_double userId)
40 {
41     ANS_LOGD("AniIsNotificationEnabledWithId call");
42     bool allowed = false;
43     int returncode = Notification::NotificationHelper::IsAllowedNotify(userId, allowed);
44     if (returncode != ERR_OK) {
45         int externalCode = NotificationSts::GetExternalCode(returncode);
46         OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
47         ANS_LOGE("AniIsNotificationEnabledWithId -> error, errorCode: %{public}d", externalCode);
48     }
49     ANS_LOGD("AniIsNotificationEnabledWithId end");
50     return NotificationSts::BoolToAniBoolean(allowed);
51 }
52 
AniIsNotificationEnabledWithBundleOption(ani_env * env,ani_object bundleOption)53 ani_boolean AniIsNotificationEnabledWithBundleOption(ani_env *env, ani_object bundleOption)
54 {
55     ANS_LOGD("AniIsNotificationEnabledWithBundleOption call");
56     int returncode = ERR_OK;
57     bool allowed = false;
58     BundleOption option;
59     if (NotificationSts::UnwrapBundleOption(env, bundleOption, option)) {
60         returncode = Notification::NotificationHelper::IsAllowedNotify(option, allowed);
61     } else {
62         NotificationSts::ThrowErrorWithMsg(env, "sts GetSlotsByBundle ERROR_INTERNAL_ERROR");
63         return ANI_FALSE;
64     }
65 
66     if (returncode != ERR_OK) {
67         int externalCode = NotificationSts::GetExternalCode(returncode);
68         ANS_LOGE("AniIsNotificationEnabledWithBundleOption -> error, errorCode: %{public}d", externalCode);
69         OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
70         return ANI_FALSE;
71     }
72     ANS_LOGD("AniIsNotificationEnabledWithBundleOption end");
73     return NotificationSts::BoolToAniBoolean(allowed);
74 }
75 
AniSetNotificationEnable(ani_env * env,ani_object bundleOption,ani_boolean enable)76 void AniSetNotificationEnable(ani_env *env, ani_object bundleOption, ani_boolean enable)
77 {
78     ANS_LOGD("AniSetNotificationEnable call");
79     Notification::NotificationBundleOption option;
80     if (!NotificationSts::UnwrapBundleOption(env, bundleOption, option)) {
81         NotificationSts::ThrowErrorWithMsg(env, "sts GetSlotsByBundle ERROR_INTERNAL_ERROR");
82         return ;
83     }
84     std::string deviceId {""};
85     int returncode = Notification::NotificationHelper::SetNotificationsEnabledForSpecifiedBundle(option, deviceId,
86         NotificationSts::AniBooleanToBool(enable));
87     if (returncode != ERR_OK) {
88         int externalCode = NotificationSts::GetExternalCode(returncode);
89         ANS_LOGE("AniSetNotificationEnable -> error, errorCode: %{public}d", externalCode);
90         OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
91     }
92     ANS_LOGD("AniSetNotificationEnable end");
93 }
94 
AniGetAllNotificationEnabledBundles(ani_env * env)95 ani_object AniGetAllNotificationEnabledBundles(ani_env *env)
96 {
97     ANS_LOGD("AniGetAllNotificationEnabledBundles call");
98     std::vector<BundleOption> bundleOptions = {};
99     int returncode = Notification::NotificationHelper::GetAllNotificationEnabledBundles(bundleOptions);
100     if (returncode != ERR_OK) {
101         int externalCode = NotificationSts::GetExternalCode(returncode);
102         ANS_LOGE("AniGetAllNotificationEnabledBundles -> error, errorCode: %{public}d", externalCode);
103         OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
104         return nullptr;
105     }
106     ani_object arrayBundles = NotificationSts::GetAniArrayBundleOption(env, bundleOptions);
107     if (arrayBundles == nullptr) {
108         ANS_LOGE("GetAniArrayBundleOption filed,arrayBundles is nullptr");
109         NotificationSts::ThrowErrorWithMsg(env, "GetAniArrayBundleOption ERROR_INTERNAL_ERROR");
110         return nullptr;
111     }
112     return arrayBundles;
113 }
114 
AniIsNotificationEnabledSync(ani_env * env)115 ani_boolean AniIsNotificationEnabledSync(ani_env *env)
116 {
117     ANS_LOGD("AniIsNotificationEnabledSync call");
118     bool allowed = false;
119     int returncode = Notification::NotificationHelper::IsAllowedNotifySelf(allowed);
120     if (returncode != ERR_OK) {
121         int externalCode = NotificationSts::GetExternalCode(returncode);
122         ANS_LOGE("AniIsNotificationEnabledSync -> error, errorCode: %{public}d", externalCode);
123         OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
124         return NotificationSts::BoolToAniBoolean(false);
125     }
126     return NotificationSts::BoolToAniBoolean(allowed);
127 }
128 
AniGetSyncNotificationEnabledWithoutApp(ani_env * env,ani_double userId)129 ani_boolean AniGetSyncNotificationEnabledWithoutApp(ani_env* env, ani_double userId)
130 {
131     ANS_LOGD("AniGetSyncNotificationEnabledWithoutApp call");
132     bool enabled = false;
133     int returncode = Notification::NotificationHelper::GetSyncNotificationEnabledWithoutApp(
134         static_cast<int32_t>(userId), enabled);
135     if (returncode != ERR_OK) {
136         int externalCode = NotificationSts::GetExternalCode(returncode);
137         ANS_LOGE("AniGetSyncNotificationEnabledWithoutApp -> error, errorCode: %{public}d", externalCode);
138         OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
139         return NotificationSts::BoolToAniBoolean(false);
140     }
141     ANS_LOGD("End success, enabled: %{public}d, returncode: %{public}d", enabled, returncode);
142     return NotificationSts::BoolToAniBoolean(enabled);
143 }
144 
AniSetSyncNotificationEnabledWithoutApp(ani_env * env,ani_double userId,ani_boolean enabled)145 void AniSetSyncNotificationEnabledWithoutApp(ani_env* env, ani_double userId, ani_boolean enabled)
146 {
147     ANS_LOGD("AniSetSyncNotificationEnabledWithoutApp call,enable : %{public}d", enabled);
148     int returncode = Notification::NotificationHelper::SetSyncNotificationEnabledWithoutApp(
149         static_cast<int32_t>(userId), NotificationSts::AniBooleanToBool(enabled));
150     if (returncode != ERR_OK) {
151         int externalCode = NotificationSts::GetExternalCode(returncode);
152         ANS_LOGE("AniSetSyncNotificationEnabledWithoutApp -> error, errorCode: %{public}d", externalCode);
153         OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
154         return;
155     }
156     ANS_LOGD("AniSetSyncNotificationEnabledWithoutApp end");
157 }
158 
AniDisableNotificationFeature(ani_env * env,ani_boolean disabled,ani_object bundleList)159 void AniDisableNotificationFeature(ani_env *env, ani_boolean disabled, ani_object bundleList)
160 {
161     ANS_LOGD("AniDisableNotificationFeature enter");
162     std::vector<std::string> bundleListStd;
163     if (!NotificationSts::GetStringArrayByAniObj(env, bundleList, bundleListStd)) {
164         std::string msg = "Invalid bundleList: must be an array of strings.";
165         ANS_LOGE("GetStringArrayByAniObj failed. msg: %{public}s", msg.c_str());
166         OHOS::NotificationSts::ThrowError(env, Notification::ERROR_PARAM_INVALID, msg);
167         return;
168     }
169     Notification::NotificationDisable param;
170     param.SetDisabled(NotificationSts::AniBooleanToBool(disabled));
171     param.SetBundleList(bundleListStd);
172 
173     int returncode = ERR_OK;
174     returncode = Notification::NotificationHelper::DisableNotificationFeature(param);
175     if (returncode != ERR_OK) {
176         int externalCode = NotificationSts::GetExternalCode(returncode);
177         ANS_LOGE("AniDisableNotificationFeature error, errorCode: %{public}d", externalCode);
178         NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
179     }
180 }
181 } // namespace NotificationManagerSts
182 } // namespace OHOS