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_slot.h"
16
17 #include "ans_log_wrapper.h"
18 #include "sts_throw_erro.h"
19 #include "sts_common.h"
20 #include "notification_helper.h"
21 #include "sts_bundle_option.h"
22 #include "sts_slot.h"
23 #include "notification_slot.h"
24 #include "sts_notification_manager.h"
25
26 namespace OHOS {
27 namespace NotificationManagerSts {
28 namespace {
29 constexpr int32_t RETURN_EXCEPTION_VALUE = -1;
30 } // namespace
31
AniGetSlotsByBundle(ani_env * env,ani_object bundleOption)32 ani_object AniGetSlotsByBundle(ani_env *env, ani_object bundleOption)
33 {
34 ANS_LOGD("sts GetSlotsByBundle enter");
35 int returncode = ERR_OK;
36 std::vector<sptr<Notification::NotificationSlot>> slots;
37 BundleOption option;
38 if (NotificationSts::UnwrapBundleOption(env, bundleOption, option)) {
39 returncode = Notification::NotificationHelper::GetNotificationSlotsForBundle(option, slots);
40 } else {
41 NotificationSts::ThrowErrorWithMsg(env, "sts GetSlotsByBundle ERROR_INTERNAL_ERROR");
42 return nullptr;
43 }
44
45 if (returncode != ERR_OK) {
46 int externalCode = NotificationSts::GetExternalCode(returncode);
47 ANS_LOGE("sts GetSlotsByBundle error, errorCode: %{public}d", externalCode);
48 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
49 return nullptr;
50 }
51 ani_object outAniObj;
52 if (!NotificationSts::WrapNotificationSlotArray(env, slots, outAniObj)) {
53 NotificationSts::ThrowErrorWithMsg(env, "GetSlotsByBundle:failed to WrapNotificationSlotArray");
54 return nullptr;
55 }
56 return outAniObj;
57 }
58
AniAddSlots(ani_env * env,ani_object notificationSlotArrayObj)59 void AniAddSlots(ani_env *env, ani_object notificationSlotArrayObj)
60 {
61 ANS_LOGD("AniAddSlots enter");
62 std::vector<Notification::NotificationSlot> slots;
63 if (!NotificationSts::UnwrapNotificationSlotArrayByAniObj(env, notificationSlotArrayObj, slots)) {
64 ANS_LOGE("UnwrapNotificationSlotArrayByAniObj failed");
65 NotificationSts::ThrowErrorWithMsg(env, "sts AddSlots ERROR_INTERNAL_ERROR");
66 return;
67 }
68 int returncode = Notification::NotificationHelper::AddNotificationSlots(slots);
69 if (returncode != ERR_OK) {
70 int externalCode = NotificationSts::GetExternalCode(returncode);
71 ANS_LOGE("AniAddSlots -> error, errorCode: %{public}d", externalCode);
72 NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
73 return;
74 }
75 ANS_LOGD("AniAddSlots leave");
76 }
77
AniAddSlotByNotificationSlot(ani_env * env,ani_object notificationSlotObj)78 void AniAddSlotByNotificationSlot(ani_env *env, ani_object notificationSlotObj)
79 {
80 ANS_LOGD("AniAddSlotByNotificationSlot enter");
81 int returncode = ERR_OK;
82 Notification::NotificationSlot slot;
83 if (NotificationSts::UnwrapNotificationSlot(env, notificationSlotObj, slot)) {
84 returncode = Notification::NotificationHelper::AddNotificationSlot(slot);
85 } else {
86 NotificationSts::ThrowErrorWithMsg(env, "sts AddSlot ERROR_INTERNAL_ERROR");
87 return;
88 }
89 if (returncode != ERR_OK) {
90 int externalCode = NotificationSts::GetExternalCode(returncode);
91 ANS_LOGE("sts AddSlot error, errorCode: %{public}d", externalCode);
92 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
93 return;
94 }
95 ANS_LOGD("AniAddSlotByNotificationSlot leave");
96 }
97
AniAddSlotBySlotType(ani_env * env,ani_enum_item enumObj)98 void AniAddSlotBySlotType(ani_env *env, ani_enum_item enumObj)
99 {
100 ANS_LOGD("AniAddSlotBySlotType enter");
101 Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType::OTHER;
102 if (!NotificationSts::SlotTypeEtsToC(env, enumObj, slotType)) {
103 NotificationSts::ThrowErrorWithMsg(env, "AddSlotByType ERROR_INTERNAL_ERROR");
104 return;
105 }
106 int returncode = Notification::NotificationHelper::AddSlotByType(slotType);
107 if (returncode != ERR_OK) {
108 int externalCode = NotificationSts::GetExternalCode(returncode);
109 ANS_LOGE("AniAddSlotBySlotType -> error, errorCode: %{public}d", externalCode);
110 NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
111 }
112 ANS_LOGD("AniAddSlotBySlotType leave");
113 return;
114 }
115
AniGetSlot(ani_env * env,ani_enum_item enumObj)116 ani_object AniGetSlot(ani_env *env, ani_enum_item enumObj)
117 {
118 ANS_LOGD("AniGetSlot enter");
119 Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType::OTHER;
120 if (!NotificationSts::SlotTypeEtsToC(env, enumObj, slotType)) {
121 ANS_LOGE("SlotTypeEtsToC failed");
122 NotificationSts::ThrowErrorWithMsg(env, "sts GetSlot ERROR_INTERNAL_ERROR");
123 return nullptr;
124 }
125 sptr<Notification::NotificationSlot> slot = nullptr;
126 int returncode = Notification::NotificationHelper::GetNotificationSlot(slotType, slot);
127 if (returncode != ERR_OK) {
128 int externalCode = NotificationSts::GetExternalCode(returncode);
129 ANS_LOGE("AniGetSlot -> error, errorCode: %{public}d", externalCode);
130 NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
131 }
132 if (slot == nullptr) {
133 ANS_LOGD("AniGetSlot -> slot is nullptr");
134 NotificationSts::ThrowError(env, RETURN_EXCEPTION_VALUE, "slot is null");
135 return nullptr;
136 }
137 ani_object slotObj;
138 if (!NotificationSts::WrapNotificationSlot(env, slot, slotObj)) {
139 ANS_LOGE("WrapNotificationSlot faild");
140 NotificationSts::ThrowErrorWithMsg(env, "sts GetSlot ERROR_INTERNAL_ERROR");
141 return nullptr;
142 }
143 ANS_LOGD("AniGetSlot leave");
144 return slotObj;
145 }
146
AniGetSlots(ani_env * env)147 ani_object AniGetSlots(ani_env *env)
148 {
149 ANS_LOGD("AniGetSlots enter");
150 std::vector<sptr<Notification::NotificationSlot>> slots;
151 int returncode = Notification::NotificationHelper::GetNotificationSlots(slots);
152 if (returncode != ERR_OK) {
153 int externalCode = NotificationSts::GetExternalCode(returncode);
154 ANS_LOGE("AniGetSlots -> error, errorCode: %{public}d", externalCode);
155 NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
156 return nullptr;
157 }
158 ani_object outAniObj;
159 if (!NotificationSts::WrapNotificationSlotArray(env, slots, outAniObj)) {
160 ANS_LOGE("WrapNotificationSlotArray faild");
161 NotificationSts::ThrowErrorWithMsg(env, "AniGetSlots:failed to WrapNotificationSlotArray");
162 return nullptr;
163 }
164 ANS_LOGD("AniGetSlots leave");
165 return outAniObj;
166 }
167
AniRemoveSlot(ani_env * env,ani_enum_item enumObj)168 void AniRemoveSlot(ani_env *env, ani_enum_item enumObj)
169 {
170 ANS_LOGD("AniRemoveSlot enter");
171 Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType::OTHER;
172 if (!NotificationSts::SlotTypeEtsToC(env, enumObj, slotType)) {
173 ANS_LOGE("SlotTypeEtsToC failed");
174 NotificationSts::ThrowErrorWithMsg(env, "sts GetSlot ERROR_INTERNAL_ERROR");
175 return;
176 }
177 int returncode = Notification::NotificationHelper::RemoveNotificationSlot(slotType);
178 if (returncode != ERR_OK) {
179 int externalCode = NotificationSts::GetExternalCode(returncode);
180 ANS_LOGE("AniRemoveSlot -> error, errorCode: %{public}d", externalCode);
181 NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
182 return;
183 }
184 ANS_LOGD("AniRemoveSlot leave");
185 }
186
AniRemoveAllSlots(ani_env * env)187 void AniRemoveAllSlots(ani_env *env)
188 {
189 ANS_LOGD("AniRemoveAllSlots enter");
190 int returncode = Notification::NotificationHelper::RemoveAllSlots();
191 if (returncode != ERR_OK) {
192 int externalCode = NotificationSts::GetExternalCode(returncode);
193 ANS_LOGE("AniRemoveAllSlots -> error, errorCode: %{public}d", externalCode);
194 NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
195 return;
196 }
197 ANS_LOGD("AniRemoveAllSlots leave");
198 }
199
AniSetSlotByBundle(ani_env * env,ani_object bundleOptionObj,ani_object slotObj)200 void AniSetSlotByBundle(ani_env *env, ani_object bundleOptionObj, ani_object slotObj)
201 {
202 ANS_LOGD("AniSetSlotByBundle enter");
203 Notification::NotificationBundleOption option;
204 if (!NotificationSts::UnwrapBundleOption(env, bundleOptionObj, option)) {
205 ANS_LOGE("UnwrapBundleOption failed");
206 NotificationSts::ThrowErrorWithMsg(env, "AniSetNotificationEnableSlot ERROR_INTERNAL_ERROR");
207 return;
208 }
209
210 Notification::NotificationSlot slot;
211 if (!NotificationSts::UnwrapNotificationSlot(env, slotObj, slot)) {
212 ANS_LOGE("UnwrapNotificationSlot failed");
213 NotificationSts::ThrowErrorWithMsg(env, "sts SetSlotByBundle ERROR_INTERNAL_ERROR");
214 return;
215 }
216
217 std::vector<sptr<Notification::NotificationSlot>> slotsVct;
218 sptr<Notification::NotificationSlot> slotPtr = new (std::nothrow) Notification::NotificationSlot(slot);
219 if (slotPtr == nullptr) {
220 ANS_LOGE("Failed to create NotificationSlot ptr");
221 NotificationSts::ThrowErrorWithMsg(env, "sts AniSetSlotByBundle ERROR_INTERNAL_ERROR");
222 return;
223 }
224 slotsVct.emplace_back(slotPtr);
225
226 int returncode = Notification::NotificationHelper::UpdateNotificationSlots(option, slotsVct);
227 if (returncode != ERR_OK) {
228 int externalCode = NotificationSts::GetExternalCode(returncode);
229 ANS_LOGE("sts SetSlotByBundle error, errorCode: %{public}d", externalCode);
230 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
231 return;
232 }
233 ANS_LOGD("AniSetSlotByBundle leave");
234 }
235
AniGetSlotNumByBundle(ani_env * env,ani_object bundleOption)236 ani_double AniGetSlotNumByBundle(ani_env *env, ani_object bundleOption)
237 {
238 ANS_LOGD("AniGetSlotNumByBundle enter");
239 Notification::NotificationBundleOption option;
240 if (!NotificationSts::UnwrapBundleOption(env, bundleOption, option)) {
241 ANS_LOGE("UnwrapBundleOption failed");
242 NotificationSts::ThrowErrorWithMsg(env, "AniGetSlotNumByBundle ERROR_INTERNAL_ERROR");
243 return RETURN_EXCEPTION_VALUE;
244 }
245 uint64_t num = 0;
246 int returncode = Notification::NotificationHelper::GetNotificationSlotNumAsBundle(option, num);
247 if (returncode != ERR_OK) {
248 int externalCode = NotificationSts::GetExternalCode(returncode);
249 ANS_LOGE("sts GetSlotNumByBundle error, errorCode: %{public}d", externalCode);
250 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
251 return RETURN_EXCEPTION_VALUE;
252 }
253 ani_double retNum = static_cast<ani_double>(num);
254 ANS_LOGD("AniGetSlotNumByBundle leave");
255 return retNum;
256 }
AniSetNotificationEnableSlot(ani_env * env,ani_object bundleOption,ani_enum_item type,ani_boolean enable)257 void AniSetNotificationEnableSlot(ani_env *env, ani_object bundleOption, ani_enum_item type, ani_boolean enable)
258 {
259 ANS_LOGD("AniSetNotificationEnableSlot enter ");
260 Notification::NotificationBundleOption option;
261 if (!NotificationSts::UnwrapBundleOption(env, bundleOption, option)) {
262 NotificationSts::ThrowErrorWithMsg(env, "AniSetNotificationEnableSlot ERROR_INTERNAL_ERROR");
263 return;
264 }
265 Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType::OTHER;
266 if (!NotificationSts::SlotTypeEtsToC(env, type, slotType)) {
267 NotificationSts::ThrowErrorWithMsg(env, "AniSetNotificationEnableSlot ERROR_INTERNAL_ERROR");
268 return;
269 }
270 int returncode = 0;
271 bool isForceControl = false;
272 returncode = Notification::NotificationHelper::SetEnabledForBundleSlot(option, slotType,
273 NotificationSts::AniBooleanToBool(enable), isForceControl);
274 if (returncode != ERR_OK) {
275 int externalCode = NotificationSts::GetExternalCode(returncode);
276 ANS_LOGE("AniSetNotificationEnableSlot error, errorCode: %{public}d", externalCode);
277 NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
278 return;
279 }
280 ANS_LOGD("AniSetNotificationEnableSlot end");
281 }
282
AniSetNotificationEnableSlotWithForce(ani_env * env,ani_object bundleOption,ani_enum_item type,ani_boolean enable,ani_boolean isForceControl)283 void AniSetNotificationEnableSlotWithForce(ani_env *env,
284 ani_object bundleOption, ani_enum_item type, ani_boolean enable, ani_boolean isForceControl)
285 {
286 ANS_LOGD("AniSetNotificationEnableSlotWithForce enter ");
287 Notification::NotificationBundleOption option;
288 Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType::OTHER;
289 if (!(NotificationSts::SlotTypeEtsToC(env, type, slotType))
290 || !(NotificationSts::UnwrapBundleOption(env, bundleOption, option))) {
291 NotificationSts::ThrowErrorWithMsg(env, "SetNotificationEnableSlotWithForce ERROR_INTERNAL_ERROR");
292 return;
293 }
294 int returncode = Notification::NotificationHelper::SetEnabledForBundleSlot(option, slotType,
295 NotificationSts::AniBooleanToBool(enable), NotificationSts::AniBooleanToBool(isForceControl));
296 if (returncode != ERR_OK) {
297 int externalCode = NotificationSts::GetExternalCode(returncode);
298 ANS_LOGE("AniSetNotificationEnableSlotSync error, errorCode: %{public}d", externalCode);
299 NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
300 }
301 }
302
AniIsNotificationSlotEnabled(ani_env * env,ani_object bundleOption,ani_enum_item type)303 ani_boolean AniIsNotificationSlotEnabled(ani_env *env, ani_object bundleOption, ani_enum_item type)
304 {
305 ANS_LOGD("IsNotificationSlotEnabled enter");
306 Notification::NotificationBundleOption option;
307 Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType::OTHER;
308 if (!NotificationSts::UnwrapBundleOption(env, bundleOption, option)
309 || !NotificationSts::SlotTypeEtsToC(env, type, slotType)) {
310 NotificationSts::ThrowErrorWithMsg(env, "IsNotificationSlotEnabled : erro arguments.");
311 return ANI_FALSE;
312 }
313
314 bool isEnable = false;
315 int returncode = Notification::NotificationHelper::GetEnabledForBundleSlot(option, slotType, isEnable);
316 if (returncode != ERR_OK) {
317 int externalCode = NotificationSts::GetExternalCode(returncode);
318 ANS_LOGE("IsNotificationSlotEnabled -> error, errorCode: %{public}d", externalCode);
319 NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
320 }
321 return isEnable ? ANI_TRUE : ANI_FALSE;
322 }
323
AniGetSlotFlagsByBundle(ani_env * env,ani_object obj)324 ani_int AniGetSlotFlagsByBundle(ani_env *env, ani_object obj)
325 {
326 ANS_LOGD("sts getSlotFlagsByBundle call");
327 Notification::NotificationBundleOption option;
328 if (!NotificationSts::UnwrapBundleOption(env, obj, option)) {
329 NotificationSts::ThrowErrorWithMsg(env, "AniGetSlotFlagsByBundle : erro arguments.");
330 return ANI_FALSE;
331 }
332 uint32_t slotFlags = 0;
333 int returncode = Notification::NotificationHelper::GetNotificationSlotFlagsAsBundle(option, slotFlags);
334 if (returncode != ERR_OK) {
335 int externalCode = NotificationSts::GetExternalCode(returncode);
336 ANS_LOGE("AniGetSlotFlagsByBundle -> error, errorCode: %{public}d", externalCode);
337 NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
338 }
339 return slotFlags;
340 }
341
AniSetSlotFlagsByBundle(ani_env * env,ani_object obj,ani_double slotFlags)342 void AniSetSlotFlagsByBundle(ani_env *env, ani_object obj, ani_double slotFlags)
343 {
344 ANS_LOGD("sts setSlotFlagsByBundle call");
345 Notification::NotificationBundleOption option;
346 if (!NotificationSts::UnwrapBundleOption(env, obj, option)) {
347 NotificationSts::ThrowErrorWithMsg(env, "AniSetSlotFlagsByBundle : erro arguments.");
348 return;
349 }
350 int returncode =
351 Notification::NotificationHelper::SetNotificationSlotFlagsAsBundle(option, static_cast<int32_t>(slotFlags));
352 if (returncode != ERR_OK) {
353 int externalCode = NotificationSts::GetExternalCode(returncode);
354 ANS_LOGE("AniSetSlotFlagsByBundle -> error, errorCode: %{public}d", externalCode);
355 NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
356 }
357 }
358
AniGetSlotByBundle(ani_env * env,ani_object bundleOption,ani_enum_item type)359 ani_object AniGetSlotByBundle(ani_env *env, ani_object bundleOption, ani_enum_item type)
360 {
361 ANS_LOGD("AniGetSlotByBundle enter");
362 Notification::NotificationBundleOption option;
363 Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType::OTHER;
364 if (!NotificationSts::UnwrapBundleOption(env, bundleOption, option)
365 || !NotificationSts::SlotTypeEtsToC(env, type, slotType)) {
366 NotificationSts::ThrowErrorWithMsg(env, "GetSlotByBundle : erro arguments.");
367 return nullptr;
368 }
369 sptr<Notification::NotificationSlot> slot = nullptr;
370 int returncode = Notification::NotificationHelper::GetNotificationSlotForBundle(option, slotType, slot);
371 if (returncode != ERR_OK) {
372 int externalCode = NotificationSts::GetExternalCode(returncode);
373 ANS_LOGE("GetSlotByBundle -> error, errorCode: %{public}d", externalCode);
374 NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
375 return nullptr;
376 }
377 ani_object infoObj;
378 if (!NotificationSts::WrapNotificationSlot(env, slot, infoObj) || infoObj == nullptr) {
379 NotificationSts::ThrowErrorWithMsg(env, "WrapNotificationSlot Failed");
380 return nullptr;
381 }
382 return infoObj;
383 }
384 }
385 }