• 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_distributed_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 #include "sts_notification_manager.h"
23 
24 namespace OHOS {
25 namespace NotificationManagerSts {
AniSetDistributedEnable(ani_env * env,ani_boolean enabled)26 void AniSetDistributedEnable(ani_env* env, ani_boolean enabled)
27 {
28     ANS_LOGD("AniSetDistributedEnable call,enable : %{public}d", enabled);
29     int returncode = Notification::NotificationHelper::EnableDistributed(NotificationSts::AniBooleanToBool(enabled));
30     if (returncode != ERR_OK) {
31         int externalCode = NotificationSts::GetExternalCode(returncode);
32         OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
33         ANS_LOGE("AniSetDistributedEnable -> error, errorCode: %{public}d", externalCode);
34         return;
35     }
36     ANS_LOGD("AniSetDistributedEnable end");
37 }
38 
AniIsDistributedEnabled(ani_env * env)39 ani_boolean AniIsDistributedEnabled(ani_env* env)
40 {
41     ANS_LOGD("AniIsDistributedEnabled call");
42     bool enabled = false;
43     int returncode = Notification::NotificationHelper::IsDistributedEnabled(enabled);
44     if (returncode != ERR_OK) {
45         int externalCode = NotificationSts::GetExternalCode(returncode);
46         ANS_LOGE("AniIsDistributedEnabled -> error, errorCode: %{public}d", externalCode);
47         OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
48         return NotificationSts::BoolToAniBoolean(false);
49     }
50     ANS_LOGD("AniIsDistributedEnabled end");
51     return NotificationSts::BoolToAniBoolean(enabled);
52 }
53 
AniIsDistributedEnabledByBundle(ani_env * env,ani_object obj)54 ani_boolean AniIsDistributedEnabledByBundle(ani_env* env, ani_object obj)
55 {
56     ANS_LOGD("AniIsDistributedEnabledByBundle call");
57     Notification::NotificationBundleOption option;
58     if (!NotificationSts::UnwrapBundleOption(env, obj, option)) {
59         NotificationSts::ThrowErrorWithMsg(env, "AniIsDistributedEnabledByBundle : erro arguments.");
60         return NotificationSts::BoolToAniBoolean(false);
61     }
62     bool enabled = false;
63     int returncode = Notification::NotificationHelper::IsDistributedEnableByBundle(option, enabled);
64     if (returncode != ERR_OK) {
65         int externalCode = NotificationSts::GetExternalCode(returncode);
66         ANS_LOGE("AniIsDistributedEnabledByBundle -> error, errorCode: %{public}d", externalCode);
67         NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
68     }
69     ANS_LOGD("AniIsDistributedEnabledByBundle end");
70     return NotificationSts::BoolToAniBoolean(enabled);
71 }
72 
AniIsDistributedEnabledByBundleType(ani_env * env,ani_object obj,ani_string deviceType)73 ani_boolean AniIsDistributedEnabledByBundleType(ani_env* env, ani_object obj, ani_string deviceType)
74 {
75     ANS_LOGD("AniIsDistributedEnabledByBundleType call");
76     Notification::NotificationBundleOption option;
77     if (!NotificationSts::UnwrapBundleOption(env, obj, option)) {
78         NotificationSts::ThrowErrorWithMsg(env, "AniIsDistributedEnabledByBundleType : erro arguments.");
79         return NotificationSts::BoolToAniBoolean(false);
80     }
81     std::string deviceTypeStr;
82     if (NotificationSts::GetStringByAniString(env, deviceType, deviceTypeStr) != ANI_OK) {
83         NotificationSts::ThrowErrorWithMsg(env, "deviceType parse failed!");
84         return NotificationSts::BoolToAniBoolean(false);
85     }
86     ANS_LOGD("Cancel by deviceType:%{public}s", deviceTypeStr.c_str());
87 
88     bool enabled = false;
89     int returncode = Notification::NotificationHelper::IsDistributedEnabledByBundle(option, deviceTypeStr, enabled);
90     if (returncode != ERR_OK) {
91         int externalCode = NotificationSts::GetExternalCode(returncode);
92         ANS_LOGE("AniIsDistributedEnabledByBundle -> error, errorCode: %{public}d", externalCode);
93         NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
94     }
95     ANS_LOGD("AniIsDistributedEnabledByBundle end");
96     return NotificationSts::BoolToAniBoolean(enabled);
97 }
98 
AniSetDistributedEnableByBundle(ani_env * env,ani_object obj,ani_boolean enable)99 void AniSetDistributedEnableByBundle(ani_env *env, ani_object obj, ani_boolean enable)
100 {
101     ANS_LOGD("setDistributedEnableByBundle call");
102     int returncode = ERR_OK;
103     Notification::NotificationBundleOption option;
104     bool bFlag = NotificationSts::UnwrapBundleOption(env, obj, option);
105     if (bFlag) {
106         returncode = Notification::NotificationHelper::EnableDistributedByBundle(
107             option, NotificationSts::AniBooleanToBool(enable));
108     } else {
109         ANS_LOGE("sts setDistributedEnableByBundle ERROR_INTERNAL_ERROR");
110         OHOS::NotificationSts::ThrowError(env, OHOS::Notification::ERROR_INTERNAL_ERROR,
111             NotificationSts::FindAnsErrMsg(OHOS::Notification::ERROR_INTERNAL_ERROR));
112         return;
113     }
114     if (returncode != ERR_OK) {
115         int externalCode = NotificationSts::GetExternalCode(returncode);
116         ANS_LOGE("sts setDistributedEnableByBundle error, errorCode: %{public}d", externalCode);
117         OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
118         return;
119     }
120     ANS_LOGD("sts setDistributedEnableByBundle end");
121 }
122 
AniSetDistributedEnableByBundleAndType(ani_env * env,ani_object obj,ani_string deviceType,ani_boolean enable)123 void AniSetDistributedEnableByBundleAndType(ani_env *env,
124     ani_object obj, ani_string deviceType, ani_boolean enable)
125 {
126     ANS_LOGD("sts setDistributedEnabledByBundle call");
127     std::string deviceTypeStr;
128     if (NotificationSts::GetStringByAniString(env, deviceType, deviceTypeStr) != ANI_OK) {
129         std::string msg = "Parameter verification failed";
130         ANS_LOGE("GetStringByAniString failed. msg: %{public}s", msg.c_str());
131         OHOS::NotificationSts::ThrowError(env, Notification::ERROR_PARAM_INVALID, msg);
132         return;
133     }
134     int returncode = ERR_OK;
135     Notification::NotificationBundleOption option;
136     bool bFlag = NotificationSts::UnwrapBundleOption(env, obj, option);
137     if (bFlag) {
138         returncode = Notification::NotificationHelper::SetDistributedEnabledByBundle(option,
139             deviceTypeStr, NotificationSts::AniBooleanToBool(enable));
140     } else {
141         ANS_LOGE("sts setDistributedEnabledByBundle ERROR_INTERNAL_ERROR");
142         OHOS::NotificationSts::ThrowError(env, OHOS::Notification::ERROR_INTERNAL_ERROR,
143             NotificationSts::FindAnsErrMsg(OHOS::Notification::ERROR_INTERNAL_ERROR));
144         return;
145     }
146     if (returncode != ERR_OK) {
147         int externalCode = NotificationSts::GetExternalCode(returncode);
148         ANS_LOGE("sts setDistributedEnabledByBundle error, errorCode: %{public}d", externalCode);
149         OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
150         return;
151     }
152     ANS_LOGD("sts setDistributedEnabledByBundle end");
153 }
154 
AniSetTargetDeviceStatus(ani_env * env,ani_string deviceType,ani_double status)155 void AniSetTargetDeviceStatus(ani_env* env, ani_string deviceType, ani_double status)
156 {
157     ANS_LOGD("sts setTargetDeviceStatus call, id:%{public}lf", status);
158     std::string deviceTypeStr;
159     if (NotificationSts::GetStringByAniString(env, deviceType, deviceTypeStr) != ANI_OK) {
160         std::string msg = "Parameter verification failed";
161         ANS_LOGE("GetStringByAniString failed. msg: %{public}s", msg.c_str());
162         OHOS::NotificationSts::ThrowError(env, Notification::ERROR_PARAM_INVALID, msg);
163         return;
164     }
165     ANS_LOGD("sts setTargetDeviceStatus id:%{public}lf deviceType:%{public}s", status, deviceTypeStr.c_str());
166     int32_t ret = Notification::NotificationHelper::SetTargetDeviceStatus(deviceTypeStr, status, DISTURB_DEFAULT_FLAG);
167     if (ret != ERR_OK) {
168         int externalCode = NotificationSts::GetExternalCode(ret);
169         ANS_LOGE("sts setTargetDeviceStatus error, errorCode: %{public}d", externalCode);
170         OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
171         return;
172     }
173     ANS_LOGD("sts setTargetDeviceStatus end");
174 }
175 
AniIsSmartReminderEnabled(ani_env * env,ani_string deviceType)176 ani_boolean AniIsSmartReminderEnabled(ani_env *env, ani_string deviceType)
177 {
178     ANS_LOGD("isSmartReminderEnabled call");
179     bool allowed = false;
180     std::string deviceTypeStr;
181     if (env == nullptr || deviceType == nullptr) {
182         ANS_LOGE("Invalid env or deviceType is null");
183         return ANI_FALSE;
184     }
185     if (NotificationSts::GetStringByAniString(env, deviceType, deviceTypeStr) != ANI_OK) {
186         std::string msg = "Parameter verification failed";
187         ANS_LOGE("GetStringByAniString failed. msg: %{public}s", msg.c_str());
188         OHOS::NotificationSts::ThrowError(env, Notification::ERROR_PARAM_INVALID, msg);
189         return ANI_FALSE;
190     }
191     int returncode = Notification::NotificationHelper::IsSmartReminderEnabled(deviceTypeStr, allowed);
192     if (returncode != ERR_OK) {
193         int externalCode = NotificationSts::GetExternalCode(returncode);
194         ANS_LOGE("isSmartReminderEnabled -> error, errorCode: %{public}d", externalCode);
195         OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
196     }
197     ANS_LOGD("isSmartReminderEnabled end");
198     return NotificationSts::BoolToAniBoolean(allowed);
199 }
200 
201 
AniSetSmartReminderEnable(ani_env * env,ani_string deviceType,ani_boolean enable)202 void AniSetSmartReminderEnable(ani_env *env, ani_string deviceType, ani_boolean enable)
203 {
204     ANS_LOGD("setSmartReminderEnabled call");
205     std::string deviceTypeStr;
206     if (env == nullptr || deviceType == nullptr) {
207         ANS_LOGE("Invalid env or deviceType is null");
208         return;
209     }
210 
211     if (NotificationSts::GetStringByAniString(env, deviceType, deviceTypeStr) != ANI_OK) {
212         std::string msg = "Parameter verification failed";
213         ANS_LOGE("GetStringByAniString failed. msg: %{public}s", msg.c_str());
214         OHOS::NotificationSts::ThrowError(env, Notification::ERROR_PARAM_INVALID, msg);
215         return;
216     }
217     int returncode = Notification::NotificationHelper::SetSmartReminderEnabled(deviceTypeStr,
218         NotificationSts::AniBooleanToBool(enable));
219     if (returncode != ERR_OK) {
220         int externalCode = NotificationSts::GetExternalCode(returncode);
221         ANS_LOGE("setSmartReminderEnabled -> error, errorCode: %{public}d", externalCode);
222         OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
223     }
224     ANS_LOGD("setSmartReminderEnabled end");
225 }
226 
AniSetDistributedEnableBySlot(ani_env * env,ani_enum_item slot,ani_string deviceType,ani_boolean enable)227 void AniSetDistributedEnableBySlot(ani_env *env, ani_enum_item slot, ani_string deviceType, ani_boolean enable)
228 {
229     ANS_LOGD("setDistributedEnabledBySlot enter ");
230     std::string deviceTypeStr;
231     Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType::OTHER;
232     if (!NotificationSts::SlotTypeEtsToC(env, slot, slotType)) {
233         std::string msg = "Parameter verification failed";
234         ANS_LOGE("SlotTypeEtsToC failed. msg: %{public}s", msg.c_str());
235         OHOS::NotificationSts::ThrowError(env, Notification::ERROR_PARAM_INVALID, msg);
236         return;
237     }
238     if (env == nullptr || deviceType == nullptr) {
239         ANS_LOGE("Invalid env or deviceType is null");
240         return;
241     }
242     if (NotificationSts::GetStringByAniString(env, deviceType, deviceTypeStr) != ANI_OK) {
243         std::string msg = "Parameter verification failed";
244         ANS_LOGE("GetStringByAniString failed. msg: %{public}s", msg.c_str());
245         OHOS::NotificationSts::ThrowError(env, Notification::ERROR_PARAM_INVALID, msg);
246         return;
247     }
248     int returncode = ERR_OK;
249     returncode = Notification::NotificationHelper::SetDistributedEnabledBySlot(slotType,
250         deviceTypeStr, NotificationSts::AniBooleanToBool(enable));
251     if (returncode != ERR_OK) {
252         int externalCode = NotificationSts::GetExternalCode(returncode);
253         ANS_LOGE("setDistributedEnabledBySlot error, errorCode: %{public}d", externalCode);
254         NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
255     }
256 }
257 
AniIsDistributedEnabledBySlot(ani_env * env,ani_enum_item slot,ani_string deviceType)258 ani_boolean AniIsDistributedEnabledBySlot(ani_env *env, ani_enum_item slot, ani_string deviceType)
259 {
260     ANS_LOGD("isDistributedEnabledBySlot enter");
261     std::string deviceTypeStr;
262 
263     Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType::OTHER;
264     if (!NotificationSts::SlotTypeEtsToC(env, slot, slotType)) {
265         std::string msg = "Parameter verification failed";
266         ANS_LOGE("SlotTypeEtsToC failed. msg: %{public}s", msg.c_str());
267         OHOS::NotificationSts::ThrowError(env, Notification::ERROR_PARAM_INVALID, msg);
268         return ANI_FALSE;
269     }
270     if (env == nullptr || deviceType == nullptr) {
271         ANS_LOGE("Invalid env or deviceType is null");
272         return ANI_FALSE;
273     }
274     if (NotificationSts::GetStringByAniString(env, deviceType, deviceTypeStr) != ANI_OK) {
275         std::string msg = "Parameter verification failed";
276         ANS_LOGE("GetStringByAniString failed. msg: %{public}s", msg.c_str());
277         OHOS::NotificationSts::ThrowError(env, Notification::ERROR_PARAM_INVALID, msg);
278         return ANI_FALSE;
279     }
280     bool isEnable = false;
281     int returncode = Notification::NotificationHelper::IsDistributedEnabledBySlot(slotType, deviceTypeStr, isEnable);
282     if (returncode != ERR_OK) {
283         int externalCode = NotificationSts::GetExternalCode(returncode);
284         ANS_LOGE("isDistributedEnabledBySlot -> error, errorCode: %{public}d", externalCode);
285         NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
286     }
287     return isEnable ? ANI_TRUE : ANI_FALSE;
288 }
289 }
290 }