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 "sts_notification_manager.h"
16 #include "sts_common.h"
17 #include "ani_common_util.h"
18 #include "ani_common_want.h"
19
20 namespace OHOS {
21 namespace NotificationSts {
SetDate(ani_env * env,ani_object obj,const char * name,int64_t time)22 bool SetDate(ani_env *env, ani_object obj, const char *name, int64_t time)
23 {
24 ANS_LOGD("SetDate call");
25 if (env == nullptr || obj == nullptr || name == nullptr) {
26 ANS_LOGE("SetDate failed, has nullptr");
27 return false;
28 }
29 ani_object timeObj;
30 if (!CreateDate(env, time, timeObj)) {
31 ANS_LOGE("CreateDate faild.");
32 return false;
33 }
34 if (!SetPropertyByRef(env, obj, name, timeObj)) {
35 ANS_LOGE("set '%{public}s' faild.", name);
36 return false;
37 }
38 ANS_LOGD("SetDate end");
39 return true;
40 }
41
StsToC(const STSSlotType inType,SlotType & outType)42 bool StsSlotTypeUtils::StsToC(const STSSlotType inType, SlotType &outType)
43 {
44 switch (inType) {
45 case STSSlotType::SOCIAL_COMMUNICATION:
46 outType = SlotType::SOCIAL_COMMUNICATION;
47 break;
48 case STSSlotType::SERVICE_INFORMATION:
49 outType = SlotType::SERVICE_REMINDER;
50 break;
51 case STSSlotType::CONTENT_INFORMATION:
52 outType = SlotType::CONTENT_INFORMATION;
53 break;
54 case STSSlotType::LIVE_VIEW:
55 outType = SlotType::LIVE_VIEW;
56 break;
57 case STSSlotType::CUSTOMER_SERVICE:
58 outType = SlotType::CUSTOMER_SERVICE;
59 break;
60 case STSSlotType::EMERGENCY_INFORMATION:
61 outType = SlotType::EMERGENCY_INFORMATION;
62 break;
63 case STSSlotType::UNKNOWN_TYPE:
64 case STSSlotType::OTHER_TYPES:
65 outType = SlotType::OTHER;
66 break;
67 default:
68 ANS_LOGE("SlotType %{public}d is an invalid value", inType);
69 return false;
70 }
71 return true;
72 }
73
CToSts(const SlotType inType,STSSlotType & outType)74 bool StsSlotTypeUtils::CToSts(const SlotType inType, STSSlotType &outType)
75 {
76 switch (inType) {
77 case SlotType::CUSTOM:
78 outType = STSSlotType::UNKNOWN_TYPE;
79 break;
80 case SlotType::SOCIAL_COMMUNICATION:
81 outType = STSSlotType::SOCIAL_COMMUNICATION;
82 break;
83 case SlotType::SERVICE_REMINDER:
84 outType = STSSlotType::SERVICE_INFORMATION;
85 break;
86 case SlotType::CONTENT_INFORMATION:
87 outType = STSSlotType::CONTENT_INFORMATION;
88 break;
89 case SlotType::LIVE_VIEW:
90 outType = STSSlotType::LIVE_VIEW;
91 break;
92 case SlotType::CUSTOMER_SERVICE:
93 outType = STSSlotType::CUSTOMER_SERVICE;
94 break;
95 case SlotType::EMERGENCY_INFORMATION:
96 outType = STSSlotType::EMERGENCY_INFORMATION;
97 break;
98 case SlotType::OTHER:
99 outType = STSSlotType::OTHER_TYPES;
100 break;
101 default:
102 ANS_LOGE("SlotType %{public}d is an invalid value", inType);
103 return false;
104 }
105 return true;
106 }
107
StsToC(const STSContentType inType,ContentType & outType)108 bool StsContentTypeUtils::StsToC(const STSContentType inType, ContentType &outType)
109 {
110 switch (inType) {
111 case STSContentType::NOTIFICATION_CONTENT_BASIC_TEXT:
112 outType = ContentType::BASIC_TEXT;
113 break;
114 case STSContentType::NOTIFICATION_CONTENT_LONG_TEXT:
115 outType = ContentType::LONG_TEXT;
116 break;
117 case STSContentType::NOTIFICATION_CONTENT_MULTILINE:
118 outType = ContentType::MULTILINE;
119 break;
120 case STSContentType::NOTIFICATION_CONTENT_PICTURE:
121 outType = ContentType::PICTURE;
122 break;
123 case STSContentType::NOTIFICATION_CONTENT_CONVERSATION:
124 outType = ContentType::CONVERSATION;
125 break;
126 case STSContentType::NOTIFICATION_CONTENT_SYSTEM_LIVE_VIEW:
127 outType = ContentType::LOCAL_LIVE_VIEW;
128 break;
129 case STSContentType::NOTIFICATION_CONTENT_LIVE_VIEW:
130 outType = ContentType::LIVE_VIEW;
131 break;
132 default:
133 ANS_LOGE("ContentType %{public}d is an invalid value", inType);
134 return false;
135 }
136 return true;
137 }
138
CToSts(const ContentType inType,STSContentType & outType)139 bool StsContentTypeUtils::CToSts(const ContentType inType, STSContentType &outType)
140 {
141 switch (inType) {
142 case ContentType::BASIC_TEXT:
143 outType = STSContentType::NOTIFICATION_CONTENT_BASIC_TEXT;
144 break;
145 case ContentType::LONG_TEXT:
146 outType = STSContentType::NOTIFICATION_CONTENT_LONG_TEXT;
147 break;
148 case ContentType::MULTILINE:
149 outType = STSContentType::NOTIFICATION_CONTENT_MULTILINE;
150 break;
151 case ContentType::PICTURE:
152 outType = STSContentType::NOTIFICATION_CONTENT_PICTURE;
153 break;
154 case ContentType::CONVERSATION:
155 outType = STSContentType::NOTIFICATION_CONTENT_CONVERSATION;
156 break;
157 case ContentType::LOCAL_LIVE_VIEW:
158 outType = STSContentType::NOTIFICATION_CONTENT_SYSTEM_LIVE_VIEW;
159 break;
160 case ContentType::LIVE_VIEW:
161 outType = STSContentType::NOTIFICATION_CONTENT_LIVE_VIEW;
162 break;
163 default:
164 ANS_LOGE("ContentType %{public}d is an invalid value", inType);
165 return false;
166 }
167 return true;
168 }
169
CToSts(const SlotLevel inLevel,STSSlotLevel & outLevel)170 bool StsSlotLevelUtils::CToSts(const SlotLevel inLevel, STSSlotLevel &outLevel)
171 {
172 switch (inLevel) {
173 case SlotLevel::LEVEL_NONE:
174 case SlotLevel::LEVEL_UNDEFINED:
175 outLevel = STSSlotLevel::LEVEL_NONE;
176 break;
177 case SlotLevel::LEVEL_MIN:
178 outLevel = STSSlotLevel::LEVEL_MIN;
179 break;
180 case SlotLevel::LEVEL_LOW:
181 outLevel = STSSlotLevel::LEVEL_LOW;
182 break;
183 case SlotLevel::LEVEL_DEFAULT:
184 outLevel = STSSlotLevel::LEVEL_DEFAULT;
185 break;
186 case SlotLevel::LEVEL_HIGH:
187 outLevel = STSSlotLevel::LEVEL_HIGH;
188 break;
189 default:
190 ANS_LOGE("SlotLevel %{public}d is an invalid value", inLevel);
191 return false;
192 }
193 return true;
194 }
195
StsToC(const STSSlotLevel inLevel,SlotLevel & outLevel)196 bool StsSlotLevelUtils::StsToC(const STSSlotLevel inLevel, SlotLevel &outLevel)
197 {
198 switch (inLevel) {
199 case STSSlotLevel::LEVEL_NONE:
200 outLevel = SlotLevel::LEVEL_NONE;
201 break;
202 case STSSlotLevel::LEVEL_MIN:
203 outLevel = SlotLevel::LEVEL_MIN;
204 break;
205 case STSSlotLevel::LEVEL_LOW:
206 outLevel = SlotLevel::LEVEL_LOW;
207 break;
208 case STSSlotLevel::LEVEL_DEFAULT:
209 outLevel = SlotLevel::LEVEL_DEFAULT;
210 break;
211 case STSSlotLevel::LEVEL_HIGH:
212 outLevel = SlotLevel::LEVEL_HIGH;
213 break;
214 default:
215 ANS_LOGE("SlotLevel %{public}d is an invalid value", inLevel);
216 return false;
217 }
218 return true;
219 }
220
StsToC(const STSDoNotDisturbType inType,OHOS::Notification::NotificationConstant::DoNotDisturbType & outType)221 bool StsDoNotDisturbTypeUtils::StsToC(const STSDoNotDisturbType inType,
222 OHOS::Notification::NotificationConstant::DoNotDisturbType &outType)
223 {
224 switch (inType) {
225 case STSDoNotDisturbType::TYPE_NONE:
226 outType = Notification::NotificationConstant::DoNotDisturbType::NONE;
227 break;
228 case STSDoNotDisturbType::TYPE_ONCE:
229 outType = Notification::NotificationConstant::DoNotDisturbType::ONCE;
230 break;
231 case STSDoNotDisturbType::TYPE_DAILY:
232 outType = Notification::NotificationConstant::DoNotDisturbType::DAILY;
233 break;
234 case STSDoNotDisturbType::TYPE_CLEARLY:
235 outType = Notification::NotificationConstant::DoNotDisturbType::CLEARLY;
236 break;
237 default:
238 ANS_LOGE("STSDoNotDisturbType %{public}d is an invalid value", inType);
239 return false;
240 }
241 return true;
242 }
243
StsToC(const STSRemindType inType,RemindType & outType)244 bool StsRemindTypeUtils::StsToC(const STSRemindType inType, RemindType &outType)
245 {
246 switch (inType) {
247 case STSRemindType::IDLE_DONOT_REMIND:
248 outType = RemindType::DEVICE_IDLE_DONOT_REMIND;
249 break;
250 case STSRemindType::IDLE_REMIND:
251 outType = RemindType::DEVICE_IDLE_REMIND;
252 break;
253 case STSRemindType::ACTIVE_DONOT_REMIND:
254 outType = RemindType::DEVICE_ACTIVE_DONOT_REMIND;
255 break;
256 case STSRemindType::ACTIVE_REMIND:
257 outType = RemindType::DEVICE_ACTIVE_REMIND;
258 break;
259 default:
260 ANS_LOGE("STSRemindType %{public}d is an invalid value", inType);
261 return false;
262 }
263 return true;
264 }
265
CToSts(const RemindType inType,STSRemindType & outType)266 bool StsRemindTypeUtils::CToSts(const RemindType inType, STSRemindType &outType)
267 {
268 switch (inType) {
269 case RemindType::NONE:
270 case RemindType::DEVICE_IDLE_DONOT_REMIND:
271 outType = STSRemindType::IDLE_DONOT_REMIND;
272 break;
273 case RemindType::DEVICE_IDLE_REMIND:
274 outType = STSRemindType::IDLE_REMIND;
275 break;
276 case RemindType::DEVICE_ACTIVE_DONOT_REMIND:
277 outType = STSRemindType::ACTIVE_DONOT_REMIND;
278 break;
279 case RemindType::DEVICE_ACTIVE_REMIND:
280 outType = STSRemindType::ACTIVE_REMIND;
281 break;
282 default:
283 ANS_LOGE("RemindType %{public}d is an invalid value", inType);
284 return false;
285 }
286 return true;
287 }
288
StsNotificationLocalLiveViewSubscriber()289 StsNotificationLocalLiveViewSubscriber::StsNotificationLocalLiveViewSubscriber()
290 {}
291
~StsNotificationLocalLiveViewSubscriber()292 StsNotificationLocalLiveViewSubscriber::~StsNotificationLocalLiveViewSubscriber()
293 {}
294
OnConnected()295 void StsNotificationLocalLiveViewSubscriber::OnConnected()
296 {}
297
OnDisconnected()298 void StsNotificationLocalLiveViewSubscriber::OnDisconnected()
299 {}
300
OnDied()301 void StsNotificationLocalLiveViewSubscriber::OnDied()
302 {}
303
OnResponse(int32_t notificationId,sptr<ButtonOption> buttonOption)304 void StsNotificationLocalLiveViewSubscriber::OnResponse(int32_t notificationId, sptr<ButtonOption> buttonOption)
305 {
306 ANS_LOGD("OnResponse call");
307 std::string functionName = "OnResponse";
308 ani_env *env = GetAniEnv();
309 if (env == nullptr || stsSubscriber_ == nullptr) {
310 ANS_LOGE("null env or stsSubscriber_");
311 return;
312 }
313 ani_status status = ANI_OK;
314 ani_object stsSubscriberObj = reinterpret_cast<ani_object>(stsSubscriber_->aniRef);
315 ani_ref funRef;
316 ani_boolean isUndefined = ANI_TRUE;
317 status = GetPropertyRef(env, stsSubscriberObj, functionName.c_str(), isUndefined, funRef);
318 if (status != ANI_OK || isUndefined == ANI_TRUE || funRef == nullptr) {
319 ANS_LOGE("Object_GetField_Ref failed");
320 return;
321 }
322 ani_object notificationIdAni = CreateDouble(env, notificationId);
323 ani_object buttonOptionObj = WarpNotificationButtonOption(env, buttonOption);
324 if (notificationIdAni == nullptr || buttonOptionObj == nullptr) {
325 ANS_LOGE("null args");
326 return;
327 }
328 ani_fn_object onFn = reinterpret_cast<ani_fn_object>(funRef);
329 ani_ref resutlt;
330 std::vector<ani_ref> argv;
331 argv.push_back(notificationIdAni);
332 argv.push_back(buttonOptionObj);
333 if ((status = env->FunctionalObject_Call(onFn, argv.size(), argv.data(), &resutlt)) != ANI_OK) {
334 ANS_LOGE("FunctionalObject_Call failed, status: %{public}d", status);
335 return;
336 }
337 }
338
SetStsNotificationLocalLiveViewSubscriber(ani_env * env,ani_object & localLiveViewSubscriberObj)339 void StsNotificationLocalLiveViewSubscriber::SetStsNotificationLocalLiveViewSubscriber(
340 ani_env *env, ani_object &localLiveViewSubscriberObj)
341 {
342 ANS_LOGD("SetStsNotificationLocalLiveViewSubscriber call");
343 if (env == nullptr) {
344 ANS_LOGE("Set failed, env is nullptr");
345 return;
346 }
347 stsSubscriber_ = std::make_unique<AppExecFwk::ETSNativeReference>();
348 if (stsSubscriber_ == nullptr) {
349 ANS_LOGE("stsSubscriber_ is nullptr");
350 return;
351 }
352 ani_ref objRef = nullptr;
353 if (env->GlobalReference_Create(localLiveViewSubscriberObj, &objRef) != ANI_OK) {
354 ANS_LOGE("create ref failed");
355 return;
356 }
357 ani_vm *aniVM = nullptr;
358 if (env->GetVM(&aniVM) != ANI_OK) {
359 ANS_LOGE("GetVM failed");
360 return;
361 }
362 vm_ = aniVM;
363 stsSubscriber_->aniObj = localLiveViewSubscriberObj;
364 stsSubscriber_->aniRef = objRef;
365 }
366
GetAniEnv()367 ani_env* StsNotificationLocalLiveViewSubscriber::GetAniEnv()
368 {
369 ANS_LOGD("GetAniEnv call");
370 if (vm_ == nullptr) {
371 ANS_LOGE("vm_ is nullptr");
372 return nullptr;
373 }
374 ani_env* aniEnv = nullptr;
375 if (vm_->GetEnv(ANI_VERSION_1, &aniEnv) != ANI_OK) {
376 ANS_LOGE("get env failed");
377 return nullptr;
378 }
379 return aniEnv;
380 }
381
SlotTypeEtsToC(ani_env * env,ani_enum_item enumItem,SlotType & slotType)382 bool SlotTypeEtsToC(ani_env *env, ani_enum_item enumItem, SlotType &slotType)
383 {
384 ANS_LOGD("SlotTypeEtsToC call");
385 STSSlotType stsSlotType = STSSlotType::UNKNOWN_TYPE;
386 if (!EnumConvertAniToNative(env, enumItem, stsSlotType) || !StsSlotTypeUtils::StsToC(stsSlotType, slotType)) {
387 ANS_LOGE("SlotTypeEtsToC failed");
388 return false;
389 }
390 return true;
391 }
392
SlotTypeCToEts(ani_env * env,SlotType slotType,ani_enum_item & enumItem)393 bool SlotTypeCToEts(ani_env *env, SlotType slotType, ani_enum_item &enumItem)
394 {
395 ANS_LOGD("SlotTypeCToEts call");
396 STSSlotType stsSlotType = STSSlotType::UNKNOWN_TYPE;
397 if (!StsSlotTypeUtils::CToSts(slotType, stsSlotType)
398 || !EnumConvertNativeToAni(
399 env, "L@ohos/notificationManager/notificationManager/SlotType;", stsSlotType, enumItem)) {
400 ANS_LOGE("SlotTypeCToEts failed");
401 return false;
402 }
403 return true;
404 }
405
SlotLevelEtsToC(ani_env * env,ani_enum_item enumItem,SlotLevel & slotLevel)406 bool SlotLevelEtsToC(ani_env *env, ani_enum_item enumItem, SlotLevel &slotLevel)
407 {
408 ANS_LOGD("SlotLevelEtsToC call");
409 STSSlotLevel stsSlotLevel = STSSlotLevel::LEVEL_NONE;
410 if (!EnumConvertAniToNative(env, enumItem, stsSlotLevel)
411 || !StsSlotLevelUtils::StsToC(stsSlotLevel, slotLevel)) {
412 ANS_LOGE("SlotLevelEtsToC failed");
413 return false;
414 }
415 return true;
416 }
SlotLevelCToEts(ani_env * env,SlotLevel slotLevel,ani_enum_item & enumItem)417 bool SlotLevelCToEts(ani_env *env, SlotLevel slotLevel, ani_enum_item &enumItem)
418 {
419 ANS_LOGD("SlotLevelCToEts call");
420 STSSlotLevel stsSlotLevel = STSSlotLevel::LEVEL_NONE;
421 if (!StsSlotLevelUtils::CToSts(slotLevel, stsSlotLevel) || !EnumConvertNativeToAni(env,
422 "L@ohos/notificationManager/notificationManager/SlotLevel;", stsSlotLevel, enumItem)) {
423 ANS_LOGE("SlotLevelCToEts failed");
424 return false;
425 }
426 return true;
427 }
428
ContentTypeEtsToC(ani_env * env,ani_enum_item enumItem,ContentType & contentType)429 bool ContentTypeEtsToC(ani_env *env, ani_enum_item enumItem, ContentType &contentType)
430 {
431 ANS_LOGD("ContentTypeEtsToC call");
432 STSContentType stsContentType = STSContentType::NOTIFICATION_CONTENT_BASIC_TEXT;
433 if (!EnumConvertAniToNative(env, enumItem, stsContentType)
434 || !StsContentTypeUtils::StsToC(stsContentType, contentType)) {
435 ANS_LOGE("ContentTypeEtsToC failed");
436 return false;
437 }
438 return true;
439 }
440
ContentTypeCToEts(ani_env * env,ContentType contentType,ani_enum_item & enumItem)441 bool ContentTypeCToEts(ani_env *env, ContentType contentType, ani_enum_item &enumItem)
442 {
443 ANS_LOGD("ContentTypeCToEts call");
444 STSContentType stsContentType = STSContentType::NOTIFICATION_CONTENT_BASIC_TEXT;
445 if (!StsContentTypeUtils::CToSts(contentType, stsContentType)
446 || !EnumConvertNativeToAni(env,
447 "L@ohos/notificationManager/notificationManager/ContentType;", stsContentType, enumItem)) {
448 ANS_LOGE("ContentTypeCToEts failed");
449 return false;
450 }
451 return true;
452 }
453
DoNotDisturbTypeEtsToC(ani_env * env,ani_enum_item enumItem,Notification::NotificationConstant::DoNotDisturbType & doNotDisturbType)454 bool DoNotDisturbTypeEtsToC(ani_env *env, ani_enum_item enumItem,
455 Notification::NotificationConstant::DoNotDisturbType &doNotDisturbType)
456 {
457 ANS_LOGD("DoNotDisturbTypeEtsToC call");
458 STSDoNotDisturbType stsDoNotDisturbType = TYPE_NONE;
459 if (!EnumConvertAniToNative(env, enumItem, stsDoNotDisturbType)
460 || !StsDoNotDisturbTypeUtils::StsToC(stsDoNotDisturbType, doNotDisturbType)) {
461 ANS_LOGE("DoNotDisturbTypeEtsToC failed");
462 return false;
463 }
464 return true;
465 }
466
DeviceRemindTypeCToEts(ani_env * env,RemindType remindType,ani_enum_item & enumItem)467 bool DeviceRemindTypeCToEts(ani_env *env, RemindType remindType, ani_enum_item &enumItem)
468 {
469 STSRemindType stsRemindType = STSRemindType::IDLE_DONOT_REMIND;
470 StsRemindTypeUtils::CToSts(remindType, stsRemindType);
471 EnumConvertNativeToAni(env,
472 "L@ohos/notificationManager/notificationManager/RemindType;", stsRemindType, enumItem);
473 return true;
474 }
475
DeviceRemindTypeEtsToC(ani_env * env,ani_enum_item enumItem,RemindType & remindType)476 bool DeviceRemindTypeEtsToC(ani_env *env, ani_enum_item enumItem, RemindType &remindType)
477 {
478 STSRemindType stsRemindType = STSRemindType::IDLE_DONOT_REMIND;
479 if (EnumConvertAniToNative(env, enumItem, stsRemindType)) {
480 StsRemindTypeUtils::StsToC(stsRemindType, remindType);
481 return true;
482 }
483 return false;
484 }
485
UnWarpNotificationButtonOption(ani_env * env,const ani_object buttonOptionObj,ButtonOption & buttonOption)486 ani_status UnWarpNotificationButtonOption(ani_env *env, const ani_object buttonOptionObj,
487 ButtonOption &buttonOption)
488 {
489 ANS_LOGD("UnWarpNotificationButtonOption call");
490 if (env == nullptr || buttonOptionObj == nullptr) {
491 ANS_LOGE("UnWarpNotificationButtonOption failed, has nullptr");
492 return ANI_ERROR;
493 }
494 ani_status status = ANI_ERROR;
495 ani_boolean isUndefind = ANI_TRUE;
496 std::string buttonName = "";
497 if ((status = GetPropertyString(env, buttonOptionObj, "buttonName", isUndefind, buttonName)) != ANI_OK
498 || isUndefind == ANI_TRUE) {
499 ANS_LOGE("UnWarpNotificationButtonOption: get buttonName failed");
500 return ANI_INVALID_ARGS;
501 }
502 buttonOption.SetButtonName(GetResizeStr(buttonName, STR_MAX_SIZE));
503 ANS_LOGD("UnWarpNotificationButtonOption end");
504 return status;
505 }
506
WarpNotificationButtonOption(ani_env * env,sptr<ButtonOption> buttonOption)507 ani_object WarpNotificationButtonOption(ani_env *env, sptr<ButtonOption> buttonOption)
508 {
509 ANS_LOGD("WarpNotificationButtonOption call");
510 if (env == nullptr || buttonOption == nullptr) {
511 ANS_LOGE("WarpNotificationButtonOption failed, has nullptr");
512 return nullptr;
513 }
514 ani_object optObj = nullptr;
515 ani_class optCls = nullptr;
516 if (!CreateClassObjByClassName(env,
517 "L@ohos/notificationManager/notificationManager/ButtonOptionsInner;", optCls, optObj) || optObj == nullptr) {
518 ANS_LOGE("WarpNotificationButtonOption: create class failed");
519 return nullptr;
520 }
521 // title: string;
522 if (!SetPropertyOptionalByString(env, optObj, "buttonName", buttonOption->GetButtonName())) {
523 ANS_LOGE("WarpNotificationButtonOption: set buttonName failed");
524 return nullptr;
525 }
526 ANS_LOGD("WarpNotificationButtonOption end");
527 return optObj;
528 }
529
WarpNotificationDoNotDisturbDate(ani_env * env,const std::shared_ptr<NotificationDoNotDisturbDate> & date,ani_object & outObj)530 bool WarpNotificationDoNotDisturbDate(
531 ani_env *env, const std::shared_ptr<NotificationDoNotDisturbDate> &date, ani_object &outObj)
532 {
533 ANS_LOGD("WarpNotificationDoNotDisturbDate call");
534 if (env == nullptr || date == nullptr) {
535 ANS_LOGE("WarpNotificationDoNotDisturbDate failed, has nullptr");
536 return false;
537 }
538 ani_class cls;
539 ani_enum_item stsEnumValue;
540 const char *className = "L@ohos/notificationManager/notificationManager/DoNotDisturbDateInner;";
541 if (!CreateClassObjByClassName(env, className, cls, outObj) || outObj == nullptr) {
542 ANS_LOGE("WarpNotificationDoNotDisturbDate: create class faild");
543 return false;
544 }
545 if (!EnumConvertNativeToAni(
546 env, "L@ohos/notificationManager/notificationManager/DoNotDisturbType;",
547 date->GetDoNotDisturbType(), stsEnumValue)) {
548 ANS_LOGE("EnumConvert_NativeToSts faild");
549 return false;
550 }
551 if (!SetPropertyByRef(env, outObj, "type", stsEnumValue)) {
552 ANS_LOGE("set type faild.");
553 return false;
554 }
555 if (!SetDate(env, outObj, "begin", date->GetBeginDate())) {
556 ANS_LOGE("SetDate 'begin' faild.");
557 return false;
558 }
559 if (!SetDate(env, outObj, "end", date->GetEndDate())) {
560 ANS_LOGE("SetDate 'end' faild.");
561 return false;
562 }
563 ANS_LOGD("WarpNotificationDoNotDisturbDate end");
564 return true;
565 }
566
SetCheckInfoContentType(ani_env * env,ani_object & obj,const std::string & name,ContentType type)567 bool SetCheckInfoContentType(ani_env *env, ani_object &obj, const std::string &name, ContentType type)
568 {
569 if (env == nullptr || obj == nullptr || name.empty()) {
570 ANS_LOGE("InvalidParam");
571 return false;
572 }
573 STSContentType stsType = NOTIFICATION_CONTENT_BASIC_TEXT;
574 ani_enum_item item;
575 if (!StsContentTypeUtils::CToSts(type, stsType)) {
576 ANS_LOGE("CToSts 'contentType' faild.");
577 return false;
578 }
579 if (!EnumConvertNativeToAni(env, "L@ohos/notificationManager/notificationManager/ContentType;", stsType, item)) {
580 ANS_LOGE("EnumConvertNativeToAni 'contentType' faild.");
581 return false;
582 }
583 if (!SetPropertyByRef(env, obj, name.c_str(), static_cast<ani_ref>(item))) {
584 ANS_LOGE("SetPropertyByRef 'contentType' faild.");
585 return false;
586 }
587 return true;
588 }
589
SetCheckInfoSlotType(ani_env * env,ani_object & obj,const std::string & name,SlotType type)590 bool SetCheckInfoSlotType(ani_env *env, ani_object &obj, const std::string &name, SlotType type)
591 {
592 if (env == nullptr || obj == nullptr || name.empty()) {
593 ANS_LOGE("InvalidParam");
594 return false;
595 }
596 STSSlotType stsType = UNKNOWN_TYPE;
597 ani_enum_item item;
598 if (!StsSlotTypeUtils::CToSts(type, stsType)) {
599 ANS_LOGE("CToSts 'slotType' faild.");
600 return false;
601 }
602 if (!EnumConvertNativeToAni(env, "L@ohos/notificationManager/notificationManager/SlotType;", stsType, item)) {
603 ANS_LOGE("EnumConvertNativeToAni 'slotType' faild.");
604 return false;
605 }
606 if (!SetPropertyByRef(env, obj, name.c_str(), static_cast<ani_ref>(item))) {
607 ANS_LOGE("SetPropertyByRef 'slotType' faild.");
608 return false;
609 }
610 return true;
611 }
612
SetNotificationCheckInfoNumber(ani_env * env,const std::shared_ptr<OHOS::Notification::NotificationCheckInfo> & data,ani_object & outObj)613 bool SetNotificationCheckInfoNumber(
614 ani_env *env, const std::shared_ptr<OHOS::Notification::NotificationCheckInfo> &data, ani_object &outObj)
615 {
616 ani_status status = ANI_OK;
617 // notificationId: number;
618 if (ANI_OK != (status = env->Object_SetPropertyByName_Double(
619 outObj, "notificationId", static_cast<ani_double>(data->GetNotifyId())))) {
620 ANS_LOGE("WarpNotificationCheckInfo. set 'notificationId' faild. status %{public}d", status);
621 return false;
622 }
623 // creatorUserId: number;
624 if (ANI_OK != (status = env->Object_SetPropertyByName_Double(
625 outObj, "creatorUserId", static_cast<ani_double>(data->GetCreatorUserId())))) {
626 ANS_LOGE("WarpNotificationCheckInfo. set 'creatorUserId' faild. status %{public}d", status);
627 return false;
628 }
629 return true;
630 }
631
SetNotificationCheckInfoString(ani_env * env,const std::shared_ptr<OHOS::Notification::NotificationCheckInfo> & data,ani_object & outObj)632 bool SetNotificationCheckInfoString(
633 ani_env *env, const std::shared_ptr<OHOS::Notification::NotificationCheckInfo> &data, ani_object &outObj)
634 {
635 // bundleName: string;
636 if (!SetPropertyOptionalByString(env, outObj, "bundleName", data->GetPkgName())) {
637 ANS_LOGE("WarpNotificationCheckInfo set 'bundleName' faild");
638 return false;
639 }
640 // label?: string;
641 if (!data->GetLabel().empty() && !SetPropertyOptionalByString(env, outObj, "label", data->GetLabel())) {
642 ANS_LOGE("WarpNotificationCheckInfo set 'label' faild");
643 return false;
644 }
645 return true;
646 }
647
SetNotificationCheckInfoEnum(ani_env * env,const std::shared_ptr<OHOS::Notification::NotificationCheckInfo> & data,ani_object & outObj)648 bool SetNotificationCheckInfoEnum(
649 ani_env *env, const std::shared_ptr<OHOS::Notification::NotificationCheckInfo> &data, ani_object &outObj)
650 {
651 // contentType: ContentType;
652 if (!SetCheckInfoContentType(env, outObj, "contentType", static_cast<ContentType>(data->GetContentType()))) {
653 ANS_LOGE("WarpNotificationCheckInfo set 'contentType' faild");
654 return false;
655 }
656 // slotType: SlotType;
657 if (!SetCheckInfoSlotType(env, outObj, "slotType", static_cast<SlotType>(data->GetSlotType()))) {
658 ANS_LOGE("WarpNotificationCheckInfo set 'slotType' faild");
659 return false;
660 }
661 return true;
662 }
663
SetNotificationCheckInfo(ani_env * env,const std::shared_ptr<OHOS::Notification::NotificationCheckInfo> & data,ani_object & outObj)664 bool SetNotificationCheckInfo(
665 ani_env *env, const std::shared_ptr<OHOS::Notification::NotificationCheckInfo> &data, ani_object &outObj)
666 {
667 if (!SetNotificationCheckInfoNumber(env, data, outObj)) {
668 ANS_LOGE("SetNotificationCheckInfoNumber faild");
669 return false;
670 }
671 if (!SetNotificationCheckInfoString(env, data, outObj)) {
672 ANS_LOGE("SetNotificationCheckInfoString faild");
673 return false;
674 }
675 if (!SetNotificationCheckInfoEnum(env, data, outObj)) {
676 ANS_LOGE("SetNotificationCheckInfoEnum faild");
677 return false;
678 }
679 // extraInfos?: Record<string, Object>;
680 if (data->GetExtraInfo() != nullptr) {
681 ani_ref extraInfos = OHOS::AppExecFwk::WrapWantParams(env, *(data->GetExtraInfo()));
682 if (extraInfos == nullptr) {
683 ANS_LOGE("WrapWantParams 'extraInfos' faild");
684 return false;
685 }
686 if (!SetPropertyByRef(env, outObj, "extraInfos", extraInfos)) {
687 ANS_LOGE("WarpNotificationCheckInfo set 'extraInfos' faild");
688 return false;
689 }
690 }
691 return true;
692 }
693
WarpNotificationCheckInfo(ani_env * env,const std::shared_ptr<OHOS::Notification::NotificationCheckInfo> & data,ani_object & outObj)694 bool WarpNotificationCheckInfo(
695 ani_env *env, const std::shared_ptr<OHOS::Notification::NotificationCheckInfo> &data, ani_object &outObj)
696 {
697 ani_object obj;
698 ani_class cls;
699 if (env == nullptr || data == nullptr) {
700 ANS_LOGE("InvalidParam");
701 return false;
702 }
703 if (!CreateClassObjByClassName(
704 env, "L@ohos/notificationManager/notificationManager/NotificationCheckInfoInner;", cls, obj)) {
705 ANS_LOGE("WarpNotificationCheckInfo create faild");
706 return false;
707 }
708 if (!SetNotificationCheckInfo(env, data, obj)) {
709 ANS_LOGE("SetNotificationCheckInfo faild");
710 return false;
711 }
712 outObj = obj;
713 return true;
714 }
715
GetDoNotDisturbDateByDoNotDisturbType(ani_env * env,ani_object obj,NotificationDoNotDisturbDate & doNotDisturbDate)716 void GetDoNotDisturbDateByDoNotDisturbType(ani_env *env, ani_object obj, NotificationDoNotDisturbDate &doNotDisturbDate)
717 {
718 ANS_LOGD("GetDoNotDisturbDateByDoNotDisturbType start");
719 if (env == nullptr || obj == nullptr) {
720 ANS_LOGE("GetDoNotDisturbDateByDoNotDisturbType failed, has nullptr");
721 return;
722 }
723 ani_status status = ANI_ERROR;
724 ani_boolean isUndefined = ANI_OK;
725 ani_ref doNotDisturbTypeRef = {};
726 if (ANI_OK != (status = GetPropertyRef(env, obj, "type", isUndefined, doNotDisturbTypeRef))
727 || isUndefined == ANI_TRUE || doNotDisturbTypeRef == nullptr) {
728 ANS_LOGE("GetDoNotDisturbDateByDoNotDisturbType: get Ref failed");
729 return;
730 }
731 NotificationConstant::DoNotDisturbType type = NotificationConstant::DoNotDisturbType::NONE;
732
733 if (!DoNotDisturbTypeEtsToC(env, static_cast<ani_enum_item>(doNotDisturbTypeRef), type)) {
734 ANS_LOGE("GetDoNotDisturbDateByDoNotDisturbType: SlotTypeEtsToC failed");
735 return;
736 }
737 doNotDisturbDate.SetDoNotDisturbType(type);
738 ANS_LOGD("GetDoNotDisturbDateByDoNotDisturbType end");
739 }
740
UnWarpNotificationDoNotDisturbDate(ani_env * env,const ani_object doNotDisturbDateObj,NotificationDoNotDisturbDate & doNotDisturbDate)741 bool UnWarpNotificationDoNotDisturbDate(
742 ani_env* env,
743 const ani_object doNotDisturbDateObj,
744 NotificationDoNotDisturbDate& doNotDisturbDate)
745 {
746 ani_boolean isUndefined = false;
747 ani_double mDouble = 0.0;
748 if (env == nullptr) {
749 ANS_LOGE("UnWarpNotificationDoNotDisturbDate: Invalid input parameters");
750 return false;
751 }
752 GetDoNotDisturbDateByDoNotDisturbType(env, doNotDisturbDateObj, doNotDisturbDate);
753
754 if (ANI_OK == GetPropertyDouble(env, doNotDisturbDateObj, "begin", isUndefined, mDouble)
755 && isUndefined == ANI_FALSE) {
756 doNotDisturbDate.SetBeginDate(static_cast<int32_t>(mDouble));
757 }
758 if (ANI_OK == GetPropertyDouble(env, doNotDisturbDateObj, "end", isUndefined, mDouble)
759 && isUndefined == ANI_FALSE) {
760 doNotDisturbDate.SetEndDate(static_cast<int32_t>(mDouble));
761 }
762 if (doNotDisturbDate.GetBeginDate() >= doNotDisturbDate.GetEndDate()) {
763 ANS_LOGE("Invalid time range");
764 return false;
765 }
766 ANS_LOGD("Successfully parsed DoNotDisturbDate");
767 return true;
768 }
769 } // namespace NotificationSts
770 } // OHOS