• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2023 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 "common.h"
17 #include "ans_inner_errors.h"
18 #include "ans_log_wrapper.h"
19 #include "js_native_api.h"
20 #include "js_native_api_types.h"
21 #include "napi_common.h"
22 #include "napi_common_util.h"
23 #include "notification_action_button.h"
24 #include "notification_capsule.h"
25 #include "notification_constant.h"
26 #include "notification_local_live_view_content.h"
27 #include "notification_progress.h"
28 #include "notification_time.h"
29 #include "pixel_map_napi.h"
30 
31 namespace OHOS {
32 namespace NotificationNapi {
SetNotificationRequestByString(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)33 napi_value Common::SetNotificationRequestByString(
34     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
35 {
36     ANS_LOGD("enter");
37 
38     napi_value value = nullptr;
39 
40     if (request == nullptr) {
41         ANS_LOGE("request is nullptr");
42         return NapiGetBoolean(env, false);
43     }
44 
45     // classification?: string
46     napi_create_string_utf8(env, request->GetClassification().c_str(), NAPI_AUTO_LENGTH, &value);
47     napi_set_named_property(env, result, "classification", value);
48 
49     // statusBarText?: string
50     napi_create_string_utf8(env, request->GetStatusBarText().c_str(), NAPI_AUTO_LENGTH, &value);
51     napi_set_named_property(env, result, "statusBarText", value);
52 
53     // label?: string
54     napi_create_string_utf8(env, request->GetLabel().c_str(), NAPI_AUTO_LENGTH, &value);
55     napi_set_named_property(env, result, "label", value);
56 
57     // groupName?: string
58     napi_create_string_utf8(env, request->GetGroupName().c_str(), NAPI_AUTO_LENGTH, &value);
59     napi_set_named_property(env, result, "groupName", value);
60 
61     // readonly creatorBundleName?: string
62     napi_create_string_utf8(env, request->GetCreatorBundleName().c_str(), NAPI_AUTO_LENGTH, &value);
63     napi_set_named_property(env, result, "creatorBundleName", value);
64 
65     return NapiGetBoolean(env, true);
66 }
67 
SetNotificationRequestByNumber(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)68 napi_value Common::SetNotificationRequestByNumber(
69     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
70 {
71     ANS_LOGD("enter");
72 
73     napi_value value = nullptr;
74 
75     if (request == nullptr) {
76         ANS_LOGE("request is nullptr");
77         return NapiGetBoolean(env, false);
78     }
79 
80     // id?: number
81     napi_create_int32(env, request->GetNotificationId(), &value);
82     napi_set_named_property(env, result, "id", value);
83 
84     // slotType?: SlotType
85     SlotType outType = SlotType::UNKNOWN_TYPE;
86     if (!SlotTypeCToJS(request->GetSlotType(), outType)) {
87         return NapiGetBoolean(env, false);
88     }
89     napi_create_int32(env, static_cast<int32_t>(outType), &value);
90     napi_set_named_property(env, result, "slotType", value);
91     napi_set_named_property(env, result, "notificationSlotType", value);
92 
93     // deliveryTime?: number
94     napi_create_int64(env, request->GetDeliveryTime(), &value);
95     napi_set_named_property(env, result, "deliveryTime", value);
96 
97     // autoDeletedTime?: number
98     napi_create_int64(env, request->GetAutoDeletedTime(), &value);
99     napi_set_named_property(env, result, "autoDeletedTime", value);
100 
101     // color ?: number
102     napi_create_uint32(env, request->GetColor(), &value);
103     napi_set_named_property(env, result, "color", value);
104 
105     // badgeIconStyle ?: number
106     auto badgeIconStyle = static_cast<int32_t>(request->GetBadgeIconStyle());
107     napi_create_int32(env, badgeIconStyle, &value);
108     napi_set_named_property(env, result, "badgeIconStyle", value);
109 
110     // readonly creatorUid?: number
111     napi_create_int32(env, request->GetCreatorUid(), &value);
112     napi_set_named_property(env, result, "creatorUid", value);
113 
114     // readonly creatorPid?: number
115     napi_create_int32(env, request->GetCreatorPid(), &value);
116     napi_set_named_property(env, result, "creatorPid", value);
117 
118     // badgeNumber?: number
119     napi_create_uint32(env, request->GetBadgeNumber(), &value);
120     napi_set_named_property(env, result, "badgeNumber", value);
121 
122     return NapiGetBoolean(env, true);
123 }
124 
SetNotificationRequestByBool(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)125 napi_value Common::SetNotificationRequestByBool(
126     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
127 {
128     ANS_LOGD("enter");
129 
130     napi_value value = nullptr;
131 
132     if (request == nullptr) {
133         ANS_LOGE("request is nullptr");
134         return NapiGetBoolean(env, false);
135     }
136     // isOngoing?: boolean
137     napi_get_boolean(env, request->IsInProgress(), &value);
138     napi_set_named_property(env, result, "isOngoing", value);
139 
140     // isUnremovable?: boolean
141     napi_get_boolean(env, request->IsUnremovable(), &value);
142     napi_set_named_property(env, result, "isUnremovable", value);
143 
144     // tapDismissed?: boolean
145     napi_get_boolean(env, request->IsTapDismissed(), &value);
146     napi_set_named_property(env, result, "tapDismissed", value);
147 
148     // colorEnabled?: boolean
149     napi_get_boolean(env, request->IsColorEnabled(), &value);
150     napi_set_named_property(env, result, "colorEnabled", value);
151 
152     // isAlertOnce?: boolean
153     napi_get_boolean(env, request->IsAlertOneTime(), &value);
154     napi_set_named_property(env, result, "isAlertOnce", value);
155 
156     // isStopwatch?: boolean
157     napi_get_boolean(env, request->IsShowStopwatch(), &value);
158     napi_set_named_property(env, result, "isStopwatch", value);
159 
160     // isCountDown?: boolean
161     napi_get_boolean(env, request->IsCountdownTimer(), &value);
162     napi_set_named_property(env, result, "isCountDown", value);
163 
164     // isFloatingIcon?: boolean
165     napi_get_boolean(env, request->IsFloatingIcon(), &value);
166     napi_set_named_property(env, result, "isFloatingIcon", value);
167 
168     // showDeliveryTime?: boolean
169     napi_get_boolean(env, request->IsShowDeliveryTime(), &value);
170     napi_set_named_property(env, result, "showDeliveryTime", value);
171 
172     return NapiGetBoolean(env, true);
173 }
174 
SetNotificationRequestByWantAgent(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)175 napi_value Common::SetNotificationRequestByWantAgent(
176     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
177 {
178     ANS_LOGD("enter");
179     if (request == nullptr) {
180         ANS_LOGE("request is nullptr");
181         return NapiGetBoolean(env, false);
182     }
183     // wantAgent?: WantAgent
184     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> agent = request->GetWantAgent();
185     if (agent) {
186         napi_value wantAgent = nullptr;
187         wantAgent = CreateWantAgentByJS(env, agent);
188         napi_set_named_property(env, result, "wantAgent", wantAgent);
189     } else {
190         napi_set_named_property(env, result, "wantAgent", NapiGetNull(env));
191     }
192 
193     // removalWantAgent?: WantAgent
194     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> removalAgent = request->GetRemovalWantAgent();
195     if (removalAgent) {
196         napi_value wantAgent = nullptr;
197         wantAgent = CreateWantAgentByJS(env, removalAgent);
198         napi_set_named_property(env, result, "removalWantAgent", wantAgent);
199     } else {
200         napi_set_named_property(env, result, "removalWantAgent", NapiGetNull(env));
201     }
202 
203     // maxScreenWantAgent?: WantAgent
204     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> maxScreenAgent = request->GetMaxScreenWantAgent();
205     if (maxScreenAgent) {
206         napi_value wantAgent = nullptr;
207         wantAgent = CreateWantAgentByJS(env, maxScreenAgent);
208         napi_set_named_property(env, result, "maxScreenWantAgent", wantAgent);
209     } else {
210         napi_set_named_property(env, result, "maxScreenWantAgent", NapiGetNull(env));
211     }
212 
213     return NapiGetBoolean(env, true);
214 }
215 
SetNotificationRequestByPixelMap(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)216 napi_value Common::SetNotificationRequestByPixelMap(
217     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
218 {
219     ANS_LOGD("enter");
220 
221     if (request == nullptr) {
222         ANS_LOGE("request is nullptr");
223         return NapiGetBoolean(env, false);
224     }
225 
226     // smallIcon?: image.PixelMap
227     std::shared_ptr<Media::PixelMap> littleIcon = request->GetLittleIcon();
228     if (littleIcon) {
229         napi_value smallIconResult = nullptr;
230         napi_valuetype valuetype = napi_undefined;
231         smallIconResult = Media::PixelMapNapi::CreatePixelMap(env, littleIcon);
232         NAPI_CALL(env, napi_typeof(env, smallIconResult, &valuetype));
233         if (valuetype == napi_undefined) {
234             ANS_LOGI("smallIconResult is undefined");
235             napi_set_named_property(env, result, "smallIcon", NapiGetNull(env));
236         } else {
237             napi_set_named_property(env, result, "smallIcon", smallIconResult);
238         }
239     }
240 
241     // largeIcon?: image.PixelMap
242     std::shared_ptr<Media::PixelMap> largeIcon = request->GetBigIcon();
243     if (largeIcon) {
244         napi_value largeIconResult = nullptr;
245         napi_valuetype valuetype = napi_undefined;
246         largeIconResult = Media::PixelMapNapi::CreatePixelMap(env, largeIcon);
247         NAPI_CALL(env, napi_typeof(env, largeIconResult, &valuetype));
248         if (valuetype == napi_undefined) {
249             ANS_LOGI("largeIconResult is undefined");
250             napi_set_named_property(env, result, "largeIcon", NapiGetNull(env));
251         } else {
252             napi_set_named_property(env, result, "largeIcon", largeIconResult);
253         }
254     }
255 
256     // overlayIcon?: image.PixelMap
257     std::shared_ptr<Media::PixelMap> overlayIcon = request->GetOverlayIcon();
258     if (overlayIcon) {
259         napi_value overlayIconResult = nullptr;
260         napi_valuetype valuetype = napi_undefined;
261         overlayIconResult = Media::PixelMapNapi::CreatePixelMap(env, overlayIcon);
262         NAPI_CALL(env, napi_typeof(env, overlayIconResult, &valuetype));
263         if (valuetype == napi_undefined) {
264             ANS_LOGI("overlayIconResult is undefined");
265             napi_set_named_property(env, result, "overlayIcon", NapiGetNull(env));
266         } else {
267             napi_set_named_property(env, result, "overlayIcon", overlayIconResult);
268         }
269     }
270 
271     return NapiGetBoolean(env, true);
272 }
273 
SetNotificationRequestByCustom(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)274 napi_value Common::SetNotificationRequestByCustom(
275     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
276 {
277     ANS_LOGD("enter");
278 
279     if (request == nullptr) {
280         ANS_LOGE("request is nullptr");
281         return NapiGetBoolean(env, false);
282     }
283 
284     // content: NotificationContent
285     std::shared_ptr<NotificationContent> content = request->GetContent();
286     if (content) {
287         napi_value contentResult = nullptr;
288         napi_create_object(env, &contentResult);
289         if (!SetNotificationContent(env, content, contentResult)) {
290             ANS_LOGE("SetNotificationContent call failed");
291             return NapiGetBoolean(env, false);
292         }
293         napi_set_named_property(env, result, "content", contentResult);
294     } else {
295         ANS_LOGE("content is nullptr");
296         return NapiGetBoolean(env, false);
297     }
298 
299     // extraInfo?: {[key:string] : any}
300     std::shared_ptr<AAFwk::WantParams> additionalData = request->GetAdditionalData();
301     if (additionalData) {
302         napi_value extraInfo = nullptr;
303         extraInfo = OHOS::AppExecFwk::WrapWantParams(env, *additionalData);
304         napi_set_named_property(env, result, "extraInfo", extraInfo);
305     }
306 
307     // actionButtons?: Array<NotificationActionButton>
308     napi_value arr = nullptr;
309     uint32_t count = 0;
310     napi_create_array(env, &arr);
311     for (auto vec : request->GetActionButtons()) {
312         if (vec) {
313             napi_value actionButtonResult = nullptr;
314             napi_create_object(env, &actionButtonResult);
315             if (SetNotificationActionButton(env, vec, actionButtonResult)) {
316                 napi_set_element(env, arr, count, actionButtonResult);
317                 count++;
318             }
319         }
320     }
321     if (count != 0) {
322         napi_set_named_property(env, result, "actionButtons", arr);
323     }
324 
325     // template?: NotificationTemplate
326     std::shared_ptr<NotificationTemplate> templ = request->GetTemplate();
327     if (templ) {
328         napi_value templateResult = nullptr;
329         napi_create_object(env, &templateResult);
330         if (!SetNotificationTemplateInfo(env, templ, templateResult)) {
331             ANS_LOGE("SetNotificationTemplate call failed");
332             return NapiGetBoolean(env, false);
333         }
334         napi_set_named_property(env, result, "template", templateResult);
335     }
336 
337     // readonly notificationFlags?: NotificationFlags
338     std::shared_ptr<NotificationFlags> flags = request->GetFlags();
339     if (flags) {
340         napi_value flagsResult = nullptr;
341         napi_create_object(env, &flagsResult);
342         if (!SetNotificationFlags(env, flags, flagsResult)) {
343             ANS_LOGE("SetNotificationFlags call failed");
344             return NapiGetBoolean(env, false);
345         }
346         napi_set_named_property(env, result, "notificationFlags", flagsResult);
347     }
348 
349     return NapiGetBoolean(env, true);
350 }
351 
SetNotificationActionButton(const napi_env & env,const std::shared_ptr<NotificationActionButton> & actionButton,napi_value & result)352 napi_value Common::SetNotificationActionButton(
353     const napi_env &env, const std::shared_ptr<NotificationActionButton> &actionButton, napi_value &result)
354 {
355     ANS_LOGD("enter");
356     if (actionButton == nullptr) {
357         ANS_LOGE("actionButton is null");
358         return NapiGetBoolean(env, false);
359     }
360 
361     napi_value value = nullptr;
362 
363     // title: string
364     napi_create_string_utf8(env, actionButton->GetTitle().c_str(), NAPI_AUTO_LENGTH, &value);
365     napi_set_named_property(env, result, "title", value);
366 
367     // wantAgent: WantAgent
368     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> agent = actionButton->GetWantAgent();
369     if (agent == nullptr) {
370         ANS_LOGI("wantAgent is null");
371         napi_set_named_property(env, result, "wantAgent", NapiGetNull(env));
372         return NapiGetBoolean(env, false);
373     }
374     napi_value wantAgent = nullptr;
375     wantAgent = CreateWantAgentByJS(env, agent);
376     napi_set_named_property(env, result, "wantAgent", wantAgent);
377 
378     // icon?: image.PixelMap
379     std::shared_ptr<Media::PixelMap> icon = actionButton->GetIcon();
380     if (icon) {
381         napi_value iconResult = nullptr;
382         napi_valuetype valuetype = napi_undefined;
383         iconResult = Media::PixelMapNapi::CreatePixelMap(env, icon);
384         NAPI_CALL(env, napi_typeof(env, iconResult, &valuetype));
385         if (valuetype == napi_undefined) {
386             ANS_LOGW("icon result is undefined");
387             napi_set_named_property(env, result, "icon", NapiGetNull(env));
388         } else {
389             napi_set_named_property(env, result, "icon", iconResult);
390         }
391     }
392 
393     if (!SetNotificationActionButtonByExtras(env, actionButton, result)) {
394         return NapiGetBoolean(env, false);
395     }
396 
397     // userInput?: NotificationUserInput
398     napi_value userInputResult = nullptr;
399     napi_create_object(env, &userInputResult);
400     if (!SetNotificationActionButtonByUserInput(env, actionButton->GetUserInput(), userInputResult)) {
401         return NapiGetBoolean(env, false);
402     }
403     napi_set_named_property(env, result, "userInput", userInputResult);
404 
405     return NapiGetBoolean(env, true);
406 }
407 
SetNotificationActionButtonByExtras(const napi_env & env,const std::shared_ptr<NotificationActionButton> & actionButton,napi_value & result)408 napi_value Common::SetNotificationActionButtonByExtras(
409     const napi_env &env, const std::shared_ptr<NotificationActionButton> &actionButton, napi_value &result)
410 {
411     ANS_LOGD("enter");
412     if (!actionButton) {
413         ANS_LOGE("actionButton is null");
414         return NapiGetBoolean(env, false);
415     }
416     // extras?: {[key: string]: any}
417     auto extras = actionButton->GetAdditionalData();
418     if (extras) {
419         napi_value nExtras = nullptr;
420         nExtras = OHOS::AppExecFwk::WrapWantParams(env, *extras);
421         napi_set_named_property(env, result, "extras", nExtras);
422     }
423     return NapiGetBoolean(env, true);
424 }
425 
SetNotificationActionButtonByUserInput(const napi_env & env,const std::shared_ptr<NotificationUserInput> & userInput,napi_value & result)426 napi_value Common::SetNotificationActionButtonByUserInput(
427     const napi_env &env, const std::shared_ptr<NotificationUserInput> &userInput, napi_value &result)
428 {
429     ANS_LOGD("enter");
430 
431     if (!userInput) {
432         ANS_LOGE("userInput is null");
433         return NapiGetBoolean(env, false);
434     }
435 
436     napi_value value = nullptr;
437     napi_value arr = nullptr;
438     int count = 0;
439 
440     // inputKey: string
441     napi_create_string_utf8(env, userInput->GetInputKey().c_str(), NAPI_AUTO_LENGTH, &value);
442     napi_set_named_property(env, result, "inputKey", value);
443 
444     // tag: string
445     napi_create_string_utf8(env, userInput->GetTag().c_str(), NAPI_AUTO_LENGTH, &value);
446     napi_set_named_property(env, result, "tag", value);
447 
448     // options: Array<string>
449     napi_create_array(env, &arr);
450     for (auto vec : userInput->GetOptions()) {
451         napi_create_string_utf8(env, vec.c_str(), NAPI_AUTO_LENGTH, &value);
452         napi_set_element(env, arr, count, value);
453         count++;
454     }
455     if (count > 0) {
456         napi_set_named_property(env, result, "options", arr);
457     }
458 
459     // permitFreeFormInput?: boolean
460     napi_get_boolean(env, userInput->IsPermitFreeFormInput(), &value);
461     napi_set_named_property(env, result, "permitFreeFormInput", value);
462 
463     // permitMimeTypes?: Array<string>
464     count = 0;
465     napi_create_array(env, &arr);
466     for (auto vec : userInput->GetPermitMimeTypes()) {
467         napi_create_string_utf8(env, vec.c_str(), NAPI_AUTO_LENGTH, &value);
468         napi_set_element(env, arr, count, value);
469         count++;
470     }
471     if (count > 0) {
472         napi_set_named_property(env, result, "permitMimeTypes", arr);
473     }
474 
475     // editType?: number
476     napi_create_int64(env, userInput->GetEditType(), &value);
477     napi_set_named_property(env, result, "editType", value);
478 
479     // additionalData?: {[key: string]: Object}
480     auto additionalData = userInput->GetAdditionalData();
481     if (additionalData) {
482         napi_value nAdditionalData = nullptr;
483         nAdditionalData = OHOS::AppExecFwk::WrapWantParams(env, *additionalData);
484         napi_set_named_property(env, result, "additionalData", nAdditionalData);
485     }
486 
487     return NapiGetBoolean(env, true);
488 }
489 
SetNotificationRequest(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)490 napi_value Common::SetNotificationRequest(
491     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
492 {
493     ANS_LOGD("enter");
494 
495     if (request == nullptr) {
496         ANS_LOGE("request is nullptr");
497         return NapiGetBoolean(env, false);
498     }
499 
500     if (!SetNotificationRequestByString(env, request, result)) {
501         return NapiGetBoolean(env, false);
502     }
503     if (!SetNotificationRequestByNumber(env, request, result)) {
504         return NapiGetBoolean(env, false);
505     }
506     if (!SetNotificationRequestByBool(env, request, result)) {
507         return NapiGetBoolean(env, false);
508     }
509     if (!SetNotificationRequestByWantAgent(env, request, result)) {
510         return NapiGetBoolean(env, false);
511     }
512     if (!SetNotificationRequestByPixelMap(env, request, result)) {
513         return NapiGetBoolean(env, false);
514     }
515     if (!SetNotificationRequestByCustom(env, request, result)) {
516         return NapiGetBoolean(env, false);
517     }
518 
519     return NapiGetBoolean(env, true);
520 }
521 
522 
GetNotificationRequestByNumber(const napi_env & env,const napi_value & value,NotificationRequest & request)523 napi_value Common::GetNotificationRequestByNumber(
524     const napi_env &env, const napi_value &value, NotificationRequest &request)
525 {
526     ANS_LOGD("enter");
527     // id?: number
528     if (GetNotificationId(env, value, request) == nullptr) {
529         return nullptr;
530     }
531     // deliveryTime?: number
532     if (GetNotificationDeliveryTime(env, value, request) == nullptr) {
533         return nullptr;
534     }
535     // autoDeletedTime?: number
536     if (GetNotificationAutoDeletedTime(env, value, request) == nullptr) {
537         return nullptr;
538     }
539     // color?: number
540     if (GetNotificationColor(env, value, request) == nullptr) {
541         return nullptr;
542     }
543     // badgeIconStyle?: number
544     if (GetNotificationBadgeIconStyle(env, value, request) == nullptr) {
545         return nullptr;
546     }
547     // badgeNumber?: number
548     if (GetNotificationBadgeNumber(env, value, request) == nullptr) {
549         return nullptr;
550     }
551 
552     return NapiGetNull(env);
553 }
554 
GetNotificationRequestByString(const napi_env & env,const napi_value & value,NotificationRequest & request)555 napi_value Common::GetNotificationRequestByString(
556     const napi_env &env, const napi_value &value, NotificationRequest &request)
557 {
558     ANS_LOGD("enter");
559     // classification?: string
560     if (GetNotificationClassification(env, value, request) == nullptr) {
561         return nullptr;
562     }
563     // statusBarText?: string
564     if (GetNotificationStatusBarText(env, value, request) == nullptr) {
565         return nullptr;
566     }
567     // label?: string
568     if (GetNotificationLabel(env, value, request) == nullptr) {
569         return nullptr;
570     }
571     // groupName?: string
572     if (GetNotificationGroupName(env, value, request) == nullptr) {
573         return nullptr;
574     }
575     return NapiGetNull(env);
576 }
577 
GetNotificationRequestByBool(const napi_env & env,const napi_value & value,NotificationRequest & request)578 napi_value Common::GetNotificationRequestByBool(
579     const napi_env &env, const napi_value &value, NotificationRequest &request)
580 {
581     ANS_LOGD("enter");
582     // isOngoing?: boolean
583     if (GetNotificationIsOngoing(env, value, request) == nullptr) {
584         return nullptr;
585     }
586     // isUnremovable?: boolean
587     if (GetNotificationIsUnremovable(env, value, request) == nullptr) {
588         return nullptr;
589     }
590     // tapDismissed?: boolean
591     if (GetNotificationtapDismissed(env, value, request) == nullptr) {
592         return nullptr;
593     }
594     // colorEnabled?: boolean
595     if (GetNotificationColorEnabled(env, value, request) == nullptr) {
596         return nullptr;
597     }
598     // isAlertOnce?: boolean
599     if (GetNotificationIsAlertOnce(env, value, request) == nullptr) {
600         return nullptr;
601     }
602     // isStopwatch?: boolean
603     if (GetNotificationIsStopwatch(env, value, request) == nullptr) {
604         return nullptr;
605     }
606     // isCountDown?: boolean
607     if (GetNotificationIsCountDown(env, value, request) == nullptr) {
608         return nullptr;
609     }
610     // showDeliveryTime?: boolean
611     if (GetNotificationShowDeliveryTime(env, value, request) == nullptr) {
612         return nullptr;
613     }
614 
615     GetNotificationIsRemoveAllowed(env, value, request);
616 
617     return NapiGetNull(env);
618 }
619 
GetNotificationRequestByCustom(const napi_env & env,const napi_value & value,NotificationRequest & request)620 napi_value Common::GetNotificationRequestByCustom(
621     const napi_env &env, const napi_value &value, NotificationRequest &request)
622 {
623     ANS_LOGD("enter");
624     // content: NotificationContent
625     if (GetNotificationContent(env, value, request) == nullptr) {
626         return nullptr;
627     }
628     // slotType?: notification.SlotType
629     if (GetNotificationSlotType(env, value, request) == nullptr) {
630         return nullptr;
631     }
632     // wantAgent?: WantAgent
633     if (GetNotificationWantAgent(env, value, request) == nullptr) {
634         return nullptr;
635     }
636     // extraInfo?: {[key: string]: any}
637     if (GetNotificationExtraInfo(env, value, request) == nullptr) {
638         return nullptr;
639     }
640     // removalWantAgent?: WantAgent
641     if (GetNotificationRemovalWantAgent(env, value, request) == nullptr) {
642         return nullptr;
643     }
644     // maxScreenWantAgent?: WantAgent
645     if (GetNotificationMaxScreenWantAgent(env, value, request) == nullptr) {
646         return nullptr;
647     }
648     // actionButtons?: Array<NotificationActionButton>
649     if (GetNotificationActionButtons(env, value, request) == nullptr) {
650         return nullptr;
651     }
652     // smallIcon?: image.PixelMap
653     if (GetNotificationSmallIcon(env, value, request) == nullptr) {
654         return nullptr;
655     }
656     // largeIcon?: image.PixelMap
657     if (GetNotificationLargeIcon(env, value, request) == nullptr) {
658         return nullptr;
659     }
660     // overlayIcon?: image.PixelMap
661     if (GetNotificationOverlayIcon(env, value, request) == nullptr) {
662         return nullptr;
663     }
664     // distributedOption?:DistributedOptions
665     if (GetNotificationRequestDistributedOptions(env, value, request) == nullptr) {
666         return nullptr;
667     }
668     // template?: NotificationTemplate
669     if (GetNotificationTemplate(env, value, request) == nullptr) {
670         return nullptr;
671     }
672     return NapiGetNull(env);
673 }
674 
GetNotificationRequest(const napi_env & env,const napi_value & value,NotificationRequest & request)675 napi_value Common::GetNotificationRequest(const napi_env &env, const napi_value &value, NotificationRequest &request)
676 {
677     ANS_LOGD("enter");
678     if (!GetNotificationRequestByNumber(env, value, request)) {
679         return nullptr;
680     }
681     if (!GetNotificationRequestByString(env, value, request)) {
682         return nullptr;
683     }
684     if (!GetNotificationRequestByBool(env, value, request)) {
685         return nullptr;
686     }
687     if (!GetNotificationRequestByCustom(env, value, request)) {
688         return nullptr;
689     }
690     return NapiGetNull(env);
691 }
692 
GetNotificationSmallIcon(const napi_env & env,const napi_value & value,NotificationRequest & request)693 napi_value Common::GetNotificationSmallIcon(const napi_env &env, const napi_value &value, NotificationRequest &request)
694 {
695     ANS_LOGD("enter");
696 
697     napi_valuetype valuetype = napi_undefined;
698     napi_value result = nullptr;
699     bool hasProperty = false;
700 
701     NAPI_CALL(env, napi_has_named_property(env, value, "smallIcon", &hasProperty));
702     if (hasProperty) {
703         napi_get_named_property(env, value, "smallIcon", &result);
704         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
705         if (valuetype != napi_object) {
706             ANS_LOGE("Argument type is not object.");
707             return nullptr;
708         }
709         std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
710         pixelMap = Media::PixelMapNapi::GetPixelMap(env, result);
711         if (pixelMap == nullptr) {
712             ANS_LOGE("Invalid object pixelMap");
713             return nullptr;
714         }
715         request.SetLittleIcon(pixelMap);
716     }
717 
718     return NapiGetNull(env);
719 }
720 
GetNotificationLargeIcon(const napi_env & env,const napi_value & value,NotificationRequest & request)721 napi_value Common::GetNotificationLargeIcon(const napi_env &env, const napi_value &value, NotificationRequest &request)
722 {
723     ANS_LOGD("enter");
724 
725     napi_valuetype valuetype = napi_undefined;
726     napi_value result = nullptr;
727     bool hasProperty = false;
728 
729     NAPI_CALL(env, napi_has_named_property(env, value, "largeIcon", &hasProperty));
730     if (hasProperty) {
731         napi_get_named_property(env, value, "largeIcon", &result);
732         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
733         if (valuetype != napi_object) {
734             ANS_LOGE("Wrong argument type. Object expected.");
735             return nullptr;
736         }
737         std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
738         pixelMap = Media::PixelMapNapi::GetPixelMap(env, result);
739         if (pixelMap == nullptr) {
740             ANS_LOGE("Invalid object pixelMap");
741             return nullptr;
742         }
743         request.SetBigIcon(pixelMap);
744     }
745 
746     return NapiGetNull(env);
747 }
748 
GetNotificationOverlayIcon(const napi_env & env,const napi_value & value,NotificationRequest & request)749 napi_value Common::GetNotificationOverlayIcon(
750     const napi_env &env, const napi_value &value, NotificationRequest &request)
751 {
752     ANS_LOGD("enter");
753 
754     napi_valuetype valuetype = napi_undefined;
755     napi_value result = nullptr;
756     bool hasProperty = false;
757 
758     NAPI_CALL(env, napi_has_named_property(env, value, "overlayIcon", &hasProperty));
759     if (hasProperty) {
760         napi_get_named_property(env, value, "overlayIcon", &result);
761         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
762         if (valuetype != napi_object) {
763             ANS_LOGE("Wrong argument type. Object expected.");
764             return nullptr;
765         }
766         std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
767         pixelMap = Media::PixelMapNapi::GetPixelMap(env, result);
768         if (pixelMap == nullptr) {
769             ANS_LOGE("Invalid object pixelMap");
770             return nullptr;
771         }
772         request.SetOverlayIcon(pixelMap);
773     }
774 
775     return NapiGetNull(env);
776 }
777 
GetNotificationSupportDisplayDevices(const napi_env & env,const napi_value & value,NotificationRequest & request)778 napi_value Common::GetNotificationSupportDisplayDevices(
779     const napi_env &env, const napi_value &value, NotificationRequest &request)
780 {
781     ANS_LOGD("enter");
782 
783     bool isArray = false;
784     bool hasProperty = false;
785     napi_valuetype valuetype = napi_undefined;
786     napi_value supportDisplayDevices = nullptr;
787     size_t strLen = 0;
788     uint32_t length = 0;
789 
790     NAPI_CALL(env, napi_has_named_property(env, value, "supportDisplayDevices", &hasProperty));
791     if (hasProperty) {
792         napi_get_named_property(env, value, "supportDisplayDevices", &supportDisplayDevices);
793         napi_is_array(env, supportDisplayDevices, &isArray);
794         if (!isArray) {
795             ANS_LOGE("Property supportDisplayDevices is expected to be an array.");
796             return nullptr;
797         }
798 
799         napi_get_array_length(env, supportDisplayDevices, &length);
800         if (length == 0) {
801             ANS_LOGE("The array is empty.");
802             return nullptr;
803         }
804         std::vector<std::string> devices;
805         for (size_t i = 0; i < length; i++) {
806             napi_value line = nullptr;
807             napi_get_element(env, supportDisplayDevices, i, &line);
808             NAPI_CALL(env, napi_typeof(env, line, &valuetype));
809             if (valuetype != napi_string) {
810                 ANS_LOGE("Wrong argument type. String expected.");
811                 return nullptr;
812             }
813             char str[STR_MAX_SIZE] = {0};
814             NAPI_CALL(env, napi_get_value_string_utf8(env, line, str, STR_MAX_SIZE - 1, &strLen));
815             devices.emplace_back(str);
816             ANS_LOGI("supportDisplayDevices = %{public}s", str);
817         }
818         request.SetDevicesSupportDisplay(devices);
819     }
820     return NapiGetNull(env);
821 }
822 
GetNotificationSupportOperateDevices(const napi_env & env,const napi_value & value,NotificationRequest & request)823 napi_value Common::GetNotificationSupportOperateDevices(
824     const napi_env &env, const napi_value &value, NotificationRequest &request)
825 {
826     ANS_LOGD("enter");
827 
828     bool isArray = false;
829     bool hasProperty = false;
830     napi_valuetype valuetype = napi_undefined;
831     napi_value supportOperateDevices = nullptr;
832     size_t strLen = 0;
833     uint32_t length = 0;
834 
835     NAPI_CALL(env, napi_has_named_property(env, value, "supportOperateDevices", &hasProperty));
836     if (hasProperty) {
837         napi_get_named_property(env, value, "supportOperateDevices", &supportOperateDevices);
838         napi_is_array(env, supportOperateDevices, &isArray);
839         if (!isArray) {
840             ANS_LOGE("Property supportOperateDevices is expected to be an array.");
841             return nullptr;
842         }
843 
844         napi_get_array_length(env, supportOperateDevices, &length);
845         if (length == 0) {
846             ANS_LOGE("The array is empty.");
847             return nullptr;
848         }
849         std::vector<std::string> devices;
850         for (size_t i = 0; i < length; i++) {
851             napi_value line = nullptr;
852             napi_get_element(env, supportOperateDevices, i, &line);
853             NAPI_CALL(env, napi_typeof(env, line, &valuetype));
854             if (valuetype != napi_string) {
855                 ANS_LOGE("Wrong argument type. String expected.");
856                 return nullptr;
857             }
858             char str[STR_MAX_SIZE] = {0};
859             NAPI_CALL(env, napi_get_value_string_utf8(env, line, str, STR_MAX_SIZE - 1, &strLen));
860             devices.emplace_back(str);
861             ANS_LOGI("supportOperateDevices = %{public}s", str);
862         }
863         request.SetDevicesSupportOperate(devices);
864     }
865 
866     return NapiGetNull(env);
867 }
868 
GetNotificationId(const napi_env & env,const napi_value & value,NotificationRequest & request)869 napi_value Common::GetNotificationId(const napi_env &env, const napi_value &value, NotificationRequest &request)
870 {
871     ANS_LOGD("enter");
872 
873     napi_valuetype valuetype = napi_undefined;
874     napi_value result = nullptr;
875     bool hasProperty = false;
876     int32_t notificationId = 0;
877 
878     NAPI_CALL(env, napi_has_named_property(env, value, "id", &hasProperty));
879     if (hasProperty) {
880         napi_get_named_property(env, value, "id", &result);
881         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
882         if (valuetype != napi_number) {
883             ANS_LOGE("Wrong argument type. Number expected.");
884             return nullptr;
885         }
886         napi_get_value_int32(env, result, &notificationId);
887         request.SetNotificationId(notificationId);
888         ANS_LOGI("notificationId = %{public}d", notificationId);
889     } else {
890         ANS_LOGI("default notificationId = 0");
891         request.SetNotificationId(0);
892     }
893 
894     return NapiGetNull(env);
895 }
896 
GetNotificationSlotType(const napi_env & env,const napi_value & value,NotificationRequest & request)897 napi_value Common::GetNotificationSlotType(const napi_env &env, const napi_value &value, NotificationRequest &request)
898 {
899     ANS_LOGD("enter");
900 
901     napi_valuetype valuetype = napi_undefined;
902     napi_value result = nullptr;
903     bool hasSlotType = false;
904     bool hasNotificationSlotType = false;
905     int32_t slotType = 0;
906 
907     NAPI_CALL(env, napi_has_named_property(env, value, "notificationSlotType", &hasNotificationSlotType));
908     if (hasNotificationSlotType) {
909         napi_get_named_property(env, value, "notificationSlotType", &result);
910         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
911         if (valuetype != napi_number) {
912             ANS_LOGE("Wrong argument type. Number expected.");
913             return nullptr;
914         }
915         napi_get_value_int32(env, result, &slotType);
916 
917         NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
918         if (!SlotTypeJSToC(SlotType(slotType), outType)) {
919             return nullptr;
920         }
921         request.SetSlotType(outType);
922         ANS_LOGI("notificationSlotType = %{public}d", slotType);
923         return NapiGetNull(env);
924     }
925 
926     NAPI_CALL(env, napi_has_named_property(env, value, "slotType", &hasSlotType));
927     if (hasSlotType) {
928         napi_get_named_property(env, value, "slotType", &result);
929         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
930         if (valuetype != napi_number) {
931             ANS_LOGE("Wrong argument type. Number expected.");
932             return nullptr;
933         }
934         napi_get_value_int32(env, result, &slotType);
935 
936         NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
937         if (!SlotTypeJSToC(SlotType(slotType), outType)) {
938             return nullptr;
939         }
940         request.SetSlotType(outType);
941         ANS_LOGI("slotType = %{public}d", slotType);
942     } else {
943         ANS_LOGI("default slotType = OTHER");
944         request.SetSlotType(NotificationConstant::OTHER);
945     }
946 
947     return NapiGetNull(env);
948 }
949 
GetNotificationIsOngoing(const napi_env & env,const napi_value & value,NotificationRequest & request)950 napi_value Common::GetNotificationIsOngoing(const napi_env &env, const napi_value &value, NotificationRequest &request)
951 {
952     ANS_LOGD("enter");
953 
954     napi_valuetype valuetype = napi_undefined;
955     napi_value result = nullptr;
956     bool hasProperty = false;
957     bool isOngoing = false;
958 
959     NAPI_CALL(env, napi_has_named_property(env, value, "isOngoing", &hasProperty));
960     if (hasProperty) {
961         napi_get_named_property(env, value, "isOngoing", &result);
962         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
963         if (valuetype != napi_boolean) {
964             ANS_LOGE("Wrong argument type. Bool expected.");
965             return nullptr;
966         }
967         napi_get_value_bool(env, result, &isOngoing);
968         request.SetInProgress(isOngoing);
969     }
970 
971     return NapiGetNull(env);
972 }
973 
GetNotificationIsUnremovable(const napi_env & env,const napi_value & value,NotificationRequest & request)974 napi_value Common::GetNotificationIsUnremovable(
975     const napi_env &env, const napi_value &value, NotificationRequest &request)
976 {
977     ANS_LOGD("enter");
978 
979     napi_valuetype valuetype = napi_undefined;
980     napi_value result = nullptr;
981     bool hasProperty = false;
982     bool isUnremovable = false;
983 
984     NAPI_CALL(env, napi_has_named_property(env, value, "isUnremovable", &hasProperty));
985     if (hasProperty) {
986         napi_get_named_property(env, value, "isUnremovable", &result);
987         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
988         if (valuetype != napi_boolean) {
989             ANS_LOGE("Wrong argument type. Bool expected.");
990             return nullptr;
991         }
992         napi_get_value_bool(env, result, &isUnremovable);
993         request.SetUnremovable(isUnremovable);
994     }
995 
996     return NapiGetNull(env);
997 }
998 
GetNotificationDeliveryTime(const napi_env & env,const napi_value & value,NotificationRequest & request)999 napi_value Common::GetNotificationDeliveryTime(
1000     const napi_env &env, const napi_value &value, NotificationRequest &request)
1001 {
1002     ANS_LOGD("enter");
1003 
1004     napi_valuetype valuetype = napi_undefined;
1005     napi_value result = nullptr;
1006     bool hasProperty = false;
1007     int64_t deliveryTime = 0;
1008 
1009     NAPI_CALL(env, napi_has_named_property(env, value, "deliveryTime", &hasProperty));
1010     if (hasProperty) {
1011         napi_get_named_property(env, value, "deliveryTime", &result);
1012         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1013         if (valuetype != napi_number) {
1014             ANS_LOGE("Wrong argument type. Number expected.");
1015             return nullptr;
1016         }
1017         napi_get_value_int64(env, result, &deliveryTime);
1018         request.SetDeliveryTime(deliveryTime);
1019     }
1020 
1021     return NapiGetNull(env);
1022 }
1023 
GetNotificationtapDismissed(const napi_env & env,const napi_value & value,NotificationRequest & request)1024 napi_value Common::GetNotificationtapDismissed(
1025     const napi_env &env, const napi_value &value, NotificationRequest &request)
1026 {
1027     ANS_LOGD("enter");
1028 
1029     napi_valuetype valuetype = napi_undefined;
1030     napi_value result = nullptr;
1031     bool hasProperty = false;
1032     bool tapDismissed = true;
1033 
1034     NAPI_CALL(env, napi_has_named_property(env, value, "tapDismissed", &hasProperty));
1035     if (hasProperty) {
1036         napi_get_named_property(env, value, "tapDismissed", &result);
1037         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1038         if (valuetype != napi_boolean) {
1039             ANS_LOGE("Wrong argument type. Bool expected.");
1040             return nullptr;
1041         }
1042         napi_get_value_bool(env, result, &tapDismissed);
1043         request.SetTapDismissed(tapDismissed);
1044     }
1045 
1046     return NapiGetNull(env);
1047 }
1048 
GetNotificationWantAgent(const napi_env & env,const napi_value & value,NotificationRequest & request)1049 napi_value Common::GetNotificationWantAgent(const napi_env &env, const napi_value &value, NotificationRequest &request)
1050 {
1051     ANS_LOGD("enter");
1052 
1053     bool hasProperty = false;
1054     AbilityRuntime::WantAgent::WantAgent *wantAgent = nullptr;
1055     napi_value result = nullptr;
1056     napi_valuetype valuetype = napi_undefined;
1057 
1058     NAPI_CALL(env, napi_has_named_property(env, value, "wantAgent", &hasProperty));
1059     if (hasProperty) {
1060         napi_get_named_property(env, value, "wantAgent", &result);
1061         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1062         if (valuetype != napi_object) {
1063             ANS_LOGE("Wrong argument type. Object expected.");
1064             return nullptr;
1065         }
1066         napi_unwrap(env, result, (void **)&wantAgent);
1067         if (wantAgent == nullptr) {
1068             ANS_LOGE("Invalid object wantAgent");
1069             return nullptr;
1070         }
1071         std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> sWantAgent =
1072             std::make_shared<AbilityRuntime::WantAgent::WantAgent>(*wantAgent);
1073         request.SetWantAgent(sWantAgent);
1074     }
1075 
1076     return NapiGetNull(env);
1077 }
1078 
GetNotificationExtraInfo(const napi_env & env,const napi_value & value,NotificationRequest & request)1079 napi_value Common::GetNotificationExtraInfo(const napi_env &env, const napi_value &value, NotificationRequest &request)
1080 {
1081     ANS_LOGD("enter");
1082 
1083     napi_valuetype valuetype = napi_undefined;
1084     napi_value result = nullptr;
1085     bool hasProperty = false;
1086 
1087     NAPI_CALL(env, napi_has_named_property(env, value, "extraInfo", &hasProperty));
1088     if (hasProperty) {
1089         napi_get_named_property(env, value, "extraInfo", &result);
1090         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1091         if (valuetype != napi_object) {
1092             ANS_LOGE("Wrong argument type. Object expected.");
1093             return nullptr;
1094         }
1095         AAFwk::WantParams wantParams;
1096         if (!OHOS::AppExecFwk::UnwrapWantParams(env, result, wantParams)) {
1097             return nullptr;
1098         }
1099 
1100         std::shared_ptr<AAFwk::WantParams> extras = std::make_shared<AAFwk::WantParams>(wantParams);
1101         request.SetAdditionalData(extras);
1102     }
1103 
1104     return NapiGetNull(env);
1105 }
1106 
GetNotificationGroupName(const napi_env & env,const napi_value & value,NotificationRequest & request)1107 napi_value Common::GetNotificationGroupName(const napi_env &env, const napi_value &value, NotificationRequest &request)
1108 {
1109     ANS_LOGD("enter");
1110 
1111     napi_valuetype valuetype = napi_undefined;
1112     napi_value result = nullptr;
1113     bool hasProperty = false;
1114     size_t strLen = 0;
1115 
1116     NAPI_CALL(env, napi_has_named_property(env, value, "groupName", &hasProperty));
1117     if (hasProperty) {
1118         napi_get_named_property(env, value, "groupName", &result);
1119         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1120         if (valuetype != napi_string) {
1121             ANS_LOGE("Wrong argument type. String expected.");
1122             return nullptr;
1123         }
1124         char str[STR_MAX_SIZE] = {0};
1125         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1126         request.SetGroupName(str);
1127     }
1128 
1129     return NapiGetNull(env);
1130 }
1131 
GetNotificationRemovalWantAgent(const napi_env & env,const napi_value & value,NotificationRequest & request)1132 napi_value Common::GetNotificationRemovalWantAgent(
1133     const napi_env &env, const napi_value &value, NotificationRequest &request)
1134 {
1135     ANS_LOGD("enter");
1136 
1137     bool hasProperty = false;
1138     AbilityRuntime::WantAgent::WantAgent *wantAgent = nullptr;
1139     napi_value result = nullptr;
1140     napi_valuetype valuetype = napi_undefined;
1141 
1142     NAPI_CALL(env, napi_has_named_property(env, value, "removalWantAgent", &hasProperty));
1143     if (hasProperty) {
1144         napi_get_named_property(env, value, "removalWantAgent", &result);
1145         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1146         if (valuetype != napi_object) {
1147             ANS_LOGE("Wrong argument type. Object expected.");
1148             return nullptr;
1149         }
1150         napi_unwrap(env, result, (void **)&wantAgent);
1151         if (wantAgent == nullptr) {
1152             ANS_LOGE("Invalid object removalWantAgent");
1153             return nullptr;
1154         }
1155         std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> removeWantAgent =
1156             std::make_shared<AbilityRuntime::WantAgent::WantAgent>(*wantAgent);
1157         request.SetRemovalWantAgent(removeWantAgent);
1158     }
1159 
1160     return NapiGetNull(env);
1161 }
1162 
GetNotificationMaxScreenWantAgent(const napi_env & env,const napi_value & value,NotificationRequest & request)1163 napi_value Common::GetNotificationMaxScreenWantAgent(
1164     const napi_env &env, const napi_value &value, NotificationRequest &request)
1165 {
1166     ANS_LOGD("enter");
1167 
1168     bool hasProperty = false;
1169     AbilityRuntime::WantAgent::WantAgent *wantAgent = nullptr;
1170     napi_value result = nullptr;
1171     napi_valuetype valuetype = napi_undefined;
1172 
1173     NAPI_CALL(env, napi_has_named_property(env, value, "maxScreenWantAgent", &hasProperty));
1174     if (hasProperty) {
1175         napi_get_named_property(env, value, "maxScreenWantAgent", &result);
1176         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1177         if (valuetype != napi_object) {
1178             ANS_LOGE("Wrong argument type. Object expected.");
1179             return nullptr;
1180         }
1181         napi_unwrap(env, result, (void **)&wantAgent);
1182         if (wantAgent == nullptr) {
1183             ANS_LOGE("Invalid object maxScreenWantAgent");
1184             return nullptr;
1185         }
1186         std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> maxScreenWantAgent =
1187             std::make_shared<AbilityRuntime::WantAgent::WantAgent>(*wantAgent);
1188         request.SetMaxScreenWantAgent(maxScreenWantAgent);
1189     }
1190 
1191     return NapiGetNull(env);
1192 }
1193 
GetNotificationAutoDeletedTime(const napi_env & env,const napi_value & value,NotificationRequest & request)1194 napi_value Common::GetNotificationAutoDeletedTime(
1195     const napi_env &env, const napi_value &value, NotificationRequest &request)
1196 {
1197     ANS_LOGD("enter");
1198 
1199     napi_valuetype valuetype = napi_undefined;
1200     napi_value result = nullptr;
1201     bool hasProperty = false;
1202     int64_t autoDeletedTime = 0;
1203 
1204     NAPI_CALL(env, napi_has_named_property(env, value, "autoDeletedTime", &hasProperty));
1205     if (hasProperty) {
1206         napi_get_named_property(env, value, "autoDeletedTime", &result);
1207         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1208         if (valuetype != napi_number) {
1209             ANS_LOGE("Wrong argument type. Number expected.");
1210             return nullptr;
1211         }
1212         napi_get_value_int64(env, result, &autoDeletedTime);
1213         request.SetAutoDeletedTime(autoDeletedTime);
1214     }
1215 
1216     return NapiGetNull(env);
1217 }
1218 
GetNotificationClassification(const napi_env & env,const napi_value & value,NotificationRequest & request)1219 napi_value Common::GetNotificationClassification(
1220     const napi_env &env, const napi_value &value, NotificationRequest &request)
1221 {
1222     ANS_LOGD("enter");
1223 
1224     napi_valuetype valuetype = napi_undefined;
1225     napi_value result = nullptr;
1226     bool hasProperty = false;
1227     size_t strLen = 0;
1228 
1229     NAPI_CALL(env, napi_has_named_property(env, value, "classification", &hasProperty));
1230     if (hasProperty) {
1231         napi_get_named_property(env, value, "classification", &result);
1232         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1233         if (valuetype != napi_string) {
1234             ANS_LOGE("Wrong argument type. String expected.");
1235             return nullptr;
1236         }
1237         char str[STR_MAX_SIZE] = {0};
1238         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1239         request.SetClassification(str);
1240     }
1241 
1242     return NapiGetNull(env);
1243 }
1244 
GetNotificationColor(const napi_env & env,const napi_value & value,NotificationRequest & request)1245 napi_value Common::GetNotificationColor(const napi_env &env, const napi_value &value, NotificationRequest &request)
1246 {
1247     ANS_LOGD("enter");
1248 
1249     napi_valuetype valuetype = napi_undefined;
1250     napi_value result = nullptr;
1251     bool hasProperty = false;
1252     int32_t color = 0;
1253 
1254     NAPI_CALL(env, napi_has_named_property(env, value, "color", &hasProperty));
1255     if (hasProperty) {
1256         napi_get_named_property(env, value, "color", &result);
1257         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1258         if (valuetype != napi_number) {
1259             ANS_LOGE("Wrong argument type. Number expected.");
1260             return nullptr;
1261         }
1262         napi_get_value_int32(env, result, &color);
1263         if (color < 0) {
1264             ANS_LOGE("Wrong argument type. Natural number expected.");
1265             return nullptr;
1266         }
1267         request.SetColor(color);
1268     }
1269 
1270     return NapiGetNull(env);
1271 }
1272 
GetNotificationColorEnabled(const napi_env & env,const napi_value & value,NotificationRequest & request)1273 napi_value Common::GetNotificationColorEnabled(
1274     const napi_env &env, const napi_value &value, NotificationRequest &request)
1275 {
1276     ANS_LOGD("enter");
1277 
1278     napi_valuetype valuetype = napi_undefined;
1279     napi_value result = nullptr;
1280     bool hasProperty = false;
1281     bool colorEnabled = false;
1282 
1283     NAPI_CALL(env, napi_has_named_property(env, value, "colorEnabled", &hasProperty));
1284     if (hasProperty) {
1285         napi_get_named_property(env, value, "colorEnabled", &result);
1286         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1287         if (valuetype != napi_boolean) {
1288             ANS_LOGE("Wrong argument type. Bool expected.");
1289             return nullptr;
1290         }
1291         napi_get_value_bool(env, result, &colorEnabled);
1292         request.SetColorEnabled(colorEnabled);
1293     }
1294 
1295     return NapiGetNull(env);
1296 }
1297 
GetNotificationIsAlertOnce(const napi_env & env,const napi_value & value,NotificationRequest & request)1298 napi_value Common::GetNotificationIsAlertOnce(
1299     const napi_env &env, const napi_value &value, NotificationRequest &request)
1300 {
1301     ANS_LOGD("enter");
1302 
1303     napi_valuetype valuetype = napi_undefined;
1304     napi_value result = nullptr;
1305     bool hasProperty = false;
1306     bool isAlertOnce = false;
1307 
1308     NAPI_CALL(env, napi_has_named_property(env, value, "isAlertOnce", &hasProperty));
1309     if (hasProperty) {
1310         napi_get_named_property(env, value, "isAlertOnce", &result);
1311         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1312         if (valuetype != napi_boolean) {
1313             ANS_LOGE("Wrong argument type. Bool expected.");
1314             return nullptr;
1315         }
1316         napi_get_value_bool(env, result, &isAlertOnce);
1317         request.SetAlertOneTime(isAlertOnce);
1318     }
1319 
1320     return NapiGetNull(env);
1321 }
1322 
GetNotificationIsStopwatch(const napi_env & env,const napi_value & value,NotificationRequest & request)1323 napi_value Common::GetNotificationIsStopwatch(
1324     const napi_env &env, const napi_value &value, NotificationRequest &request)
1325 {
1326     ANS_LOGD("enter");
1327 
1328     napi_valuetype valuetype = napi_undefined;
1329     napi_value result = nullptr;
1330     bool hasProperty = false;
1331     bool isStopwatch = false;
1332 
1333     NAPI_CALL(env, napi_has_named_property(env, value, "isStopwatch", &hasProperty));
1334     if (hasProperty) {
1335         napi_get_named_property(env, value, "isStopwatch", &result);
1336         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1337         if (valuetype != napi_boolean) {
1338             ANS_LOGE("Wrong argument type. Bool expected.");
1339             return nullptr;
1340         }
1341         napi_get_value_bool(env, result, &isStopwatch);
1342         request.SetShowStopwatch(isStopwatch);
1343     }
1344 
1345     return NapiGetNull(env);
1346 }
1347 
GetNotificationIsCountDown(const napi_env & env,const napi_value & value,NotificationRequest & request)1348 napi_value Common::GetNotificationIsCountDown(
1349     const napi_env &env, const napi_value &value, NotificationRequest &request)
1350 {
1351     ANS_LOGD("enter");
1352 
1353     napi_valuetype valuetype = napi_undefined;
1354     napi_value result = nullptr;
1355     bool hasProperty = false;
1356     bool isCountDown = false;
1357 
1358     NAPI_CALL(env, napi_has_named_property(env, value, "isCountDown", &hasProperty));
1359     if (hasProperty) {
1360         napi_get_named_property(env, value, "isCountDown", &result);
1361         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1362         if (valuetype != napi_boolean) {
1363             ANS_LOGE("Wrong argument type. Bool expected.");
1364             return nullptr;
1365         }
1366         napi_get_value_bool(env, result, &isCountDown);
1367         request.SetCountdownTimer(isCountDown);
1368     }
1369 
1370     return NapiGetNull(env);
1371 }
1372 
GetNotificationStatusBarText(const napi_env & env,const napi_value & value,NotificationRequest & request)1373 napi_value Common::GetNotificationStatusBarText(
1374     const napi_env &env, const napi_value &value, NotificationRequest &request)
1375 {
1376     ANS_LOGD("enter");
1377 
1378     napi_valuetype valuetype = napi_undefined;
1379     napi_value result = nullptr;
1380     bool hasProperty = false;
1381     size_t strLen = 0;
1382 
1383     NAPI_CALL(env, napi_has_named_property(env, value, "statusBarText", &hasProperty));
1384     if (hasProperty) {
1385         napi_get_named_property(env, value, "statusBarText", &result);
1386         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1387         if (valuetype != napi_string) {
1388             ANS_LOGE("Wrong argument type. String expected.");
1389             return nullptr;
1390         }
1391         char str[STR_MAX_SIZE] = {0};
1392         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1393         request.SetStatusBarText(str);
1394     }
1395 
1396     return NapiGetNull(env);
1397 }
1398 
GetNotificationLabel(const napi_env & env,const napi_value & value,NotificationRequest & request)1399 napi_value Common::GetNotificationLabel(const napi_env &env, const napi_value &value, NotificationRequest &request)
1400 {
1401     ANS_LOGD("enter");
1402 
1403     napi_valuetype valuetype = napi_undefined;
1404     napi_value result = nullptr;
1405     bool hasProperty = false;
1406     size_t strLen = 0;
1407 
1408     NAPI_CALL(env, napi_has_named_property(env, value, "label", &hasProperty));
1409     if (hasProperty) {
1410         napi_get_named_property(env, value, "label", &result);
1411         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1412         if (valuetype != napi_string) {
1413             ANS_LOGE("Wrong argument type. String expected.");
1414             return nullptr;
1415         }
1416         char str[STR_MAX_SIZE] = {0};
1417         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1418         request.SetLabel(str);
1419     }
1420 
1421     return NapiGetNull(env);
1422 }
1423 
GetNotificationBadgeIconStyle(const napi_env & env,const napi_value & value,NotificationRequest & request)1424 napi_value Common::GetNotificationBadgeIconStyle(
1425     const napi_env &env, const napi_value &value, NotificationRequest &request)
1426 {
1427     ANS_LOGD("enter");
1428 
1429     napi_valuetype valuetype = napi_undefined;
1430     napi_value result = nullptr;
1431     bool hasProperty = false;
1432     int32_t badgeIconStyle = 0;
1433 
1434     NAPI_CALL(env, napi_has_named_property(env, value, "badgeIconStyle", &hasProperty));
1435     if (hasProperty) {
1436         napi_get_named_property(env, value, "badgeIconStyle", &result);
1437         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1438         if (valuetype != napi_number) {
1439             ANS_LOGE("Wrong argument type. Number expected.");
1440             return nullptr;
1441         }
1442         napi_get_value_int32(env, result, &badgeIconStyle);
1443         request.SetBadgeIconStyle(static_cast<NotificationRequest::BadgeStyle>(badgeIconStyle));
1444     }
1445 
1446     return NapiGetNull(env);
1447 }
1448 
GetNotificationShowDeliveryTime(const napi_env & env,const napi_value & value,NotificationRequest & request)1449 napi_value Common::GetNotificationShowDeliveryTime(
1450     const napi_env &env, const napi_value &value, NotificationRequest &request)
1451 {
1452     ANS_LOGD("enter");
1453 
1454     napi_valuetype valuetype = napi_undefined;
1455     napi_value result = nullptr;
1456     bool hasProperty = false;
1457     bool showDeliveryTime = false;
1458 
1459     NAPI_CALL(env, napi_has_named_property(env, value, "showDeliveryTime", &hasProperty));
1460     if (hasProperty) {
1461         napi_get_named_property(env, value, "showDeliveryTime", &result);
1462         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1463         if (valuetype != napi_boolean) {
1464             ANS_LOGE("Wrong argument type. Bool expected.");
1465             return nullptr;
1466         }
1467         napi_get_value_bool(env, result, &showDeliveryTime);
1468         request.SetShowDeliveryTime(showDeliveryTime);
1469     }
1470 
1471     return NapiGetNull(env);
1472 }
1473 
GetNotificationIsRemoveAllowed(const napi_env & env,const napi_value & value,NotificationRequest & request)1474 napi_value Common::GetNotificationIsRemoveAllowed(
1475     const napi_env &env, const napi_value &value, NotificationRequest &request)
1476 {
1477     ANS_LOGD("enter");
1478 
1479     napi_valuetype valuetype = napi_undefined;
1480     napi_value result = nullptr;
1481     bool hasProperty = false;
1482     bool isRemoveAllowed = true;
1483 
1484     NAPI_CALL(env, napi_has_named_property(env, value, "isRemoveAllowed", &hasProperty));
1485     if (hasProperty) {
1486         napi_get_named_property(env, value, "isRemoveAllowed", &result);
1487         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1488         if (valuetype != napi_boolean) {
1489             ANS_LOGE("Wrong argument type. Bool expected.");
1490             return nullptr;
1491         }
1492         napi_get_value_bool(env, result, &isRemoveAllowed);
1493         request.SetRemoveAllowed(isRemoveAllowed);
1494     }
1495 
1496     return NapiGetNull(env);
1497 }
1498 
GetNotificationActionButtons(const napi_env & env,const napi_value & value,NotificationRequest & request)1499 napi_value Common::GetNotificationActionButtons(
1500     const napi_env &env, const napi_value &value, NotificationRequest &request)
1501 {
1502     ANS_LOGD("enter");
1503 
1504     bool isArray = false;
1505     napi_valuetype valuetype = napi_undefined;
1506     napi_value actionButtons = nullptr;
1507     uint32_t length = 0;
1508     bool hasProperty = false;
1509 
1510     napi_has_named_property(env, value, "actionButtons", &hasProperty);
1511     if (!hasProperty) {
1512         return Common::NapiGetNull(env);
1513     }
1514 
1515     request.SetIsCoverActionButtons(true);
1516     napi_get_named_property(env, value, "actionButtons", &actionButtons);
1517     napi_is_array(env, actionButtons, &isArray);
1518     if (!isArray) {
1519         ANS_LOGE("Property actionButtons is expected to be an array.");
1520         return nullptr;
1521     }
1522     napi_get_array_length(env, actionButtons, &length);
1523     if (length == 0) {
1524         ANS_LOGI("The array is empty.");
1525         return Common::NapiGetNull(env);
1526     }
1527     for (size_t i = 0; i < length; i++) {
1528         napi_value actionButton = nullptr;
1529         napi_get_element(env, actionButtons, i, &actionButton);
1530         NAPI_CALL(env, napi_typeof(env, actionButton, &valuetype));
1531         if (valuetype != napi_object) {
1532             ANS_LOGE("Wrong argument type. Object expected.");
1533             return nullptr;
1534         }
1535 
1536         std::shared_ptr<NotificationActionButton> pActionButton = nullptr;
1537         if (GetNotificationActionButtonsDetailed(env, actionButton, pActionButton) == nullptr) {
1538             return nullptr;
1539         }
1540         request.AddActionButton(pActionButton);
1541     }
1542 
1543     return NapiGetNull(env);
1544 }
1545 
GetNotificationActionButtonsDetailed(const napi_env & env,const napi_value & actionButton,std::shared_ptr<NotificationActionButton> & pActionButton)1546 napi_value Common::GetNotificationActionButtonsDetailed(
1547     const napi_env &env, const napi_value &actionButton, std::shared_ptr<NotificationActionButton> &pActionButton)
1548 {
1549     ANS_LOGD("enter");
1550 
1551     if (!GetNotificationActionButtonsDetailedBasicInfo(env, actionButton, pActionButton)) {
1552         return nullptr;
1553     }
1554     if (!GetNotificationActionButtonsDetailedByExtras(env, actionButton, pActionButton)) {
1555         return nullptr;
1556     }
1557     if (!GetNotificationUserInput(env, actionButton, pActionButton)) {
1558         return nullptr;
1559     }
1560     return NapiGetNull(env);
1561 }
1562 
GetNotificationActionButtonsDetailedBasicInfo(const napi_env & env,const napi_value & actionButton,std::shared_ptr<NotificationActionButton> & pActionButton)1563 napi_value Common::GetNotificationActionButtonsDetailedBasicInfo(
1564     const napi_env &env, const napi_value &actionButton, std::shared_ptr<NotificationActionButton> &pActionButton)
1565 {
1566     ANS_LOGD("enter");
1567     napi_valuetype valuetype = napi_undefined;
1568     bool hasProperty = false;
1569     char str[STR_MAX_SIZE] = {0};
1570     size_t strLen = 0;
1571     napi_value value = nullptr;
1572     std::string title;
1573     AbilityRuntime::WantAgent::WantAgent *wantAgentPtr = nullptr;
1574     std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
1575     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent;
1576 
1577     // title: string
1578     NAPI_CALL(env, napi_has_named_property(env, actionButton, "title", &hasProperty));
1579     if (!hasProperty) {
1580         ANS_LOGE("Property title expected.");
1581         return nullptr;
1582     }
1583     napi_get_named_property(env, actionButton, "title", &value);
1584     NAPI_CALL(env, napi_typeof(env, value, &valuetype));
1585     if (valuetype != napi_string) {
1586         ANS_LOGE("Wrong argument type. String expected.");
1587         return nullptr;
1588     }
1589     NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
1590     title = str;
1591 
1592     // wantAgent: WantAgent
1593     NAPI_CALL(env, napi_has_named_property(env, actionButton, "wantAgent", &hasProperty));
1594     if (!hasProperty) {
1595         ANS_LOGE("Property wantAgent expected.");
1596         return nullptr;
1597     }
1598     napi_get_named_property(env, actionButton, "wantAgent", &value);
1599     NAPI_CALL(env, napi_typeof(env, value, &valuetype));
1600     if (valuetype != napi_object) {
1601         ANS_LOGE("Wrong argument type. Object expected.");
1602         return nullptr;
1603     }
1604     napi_unwrap(env, value, (void **)&wantAgentPtr);
1605     if (wantAgentPtr == nullptr) {
1606         ANS_LOGE("Invalid object wantAgent");
1607         return nullptr;
1608     }
1609     wantAgent = std::make_shared<AbilityRuntime::WantAgent::WantAgent>(*wantAgentPtr);
1610 
1611     // icon?: image.PixelMap
1612     NAPI_CALL(env, napi_has_named_property(env, actionButton, "icon", &hasProperty));
1613     if (hasProperty) {
1614         napi_get_named_property(env, actionButton, "icon", &value);
1615         NAPI_CALL(env, napi_typeof(env, value, &valuetype));
1616         if (valuetype != napi_object) {
1617             ANS_LOGE("Wrong argument type. Object expected.");
1618             return nullptr;
1619         }
1620         pixelMap = Media::PixelMapNapi::GetPixelMap(env, value);
1621         if (pixelMap == nullptr) {
1622             ANS_LOGE("Invalid object pixelMap");
1623             return nullptr;
1624         }
1625     }
1626     pActionButton = NotificationActionButton::Create(pixelMap, title, wantAgent);
1627 
1628     return NapiGetNull(env);
1629 }
1630 
GetNotificationActionButtonsDetailedByExtras(const napi_env & env,const napi_value & actionButton,std::shared_ptr<NotificationActionButton> & pActionButton)1631 napi_value Common::GetNotificationActionButtonsDetailedByExtras(
1632     const napi_env &env, const napi_value &actionButton, std::shared_ptr<NotificationActionButton> &pActionButton)
1633 {
1634     ANS_LOGD("enter");
1635 
1636     napi_valuetype valuetype = napi_undefined;
1637     napi_value result = nullptr;
1638     bool hasProperty = false;
1639 
1640     if (!pActionButton) {
1641         ANS_LOGE("pActionButton is nullptr");
1642         return nullptr;
1643     }
1644 
1645     // extras?: {[key: string]: any}
1646     NAPI_CALL(env, napi_has_named_property(env, actionButton, "extras", &hasProperty));
1647     if (hasProperty) {
1648         napi_get_named_property(env, actionButton, "extras", &result);
1649         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1650         if (valuetype != napi_object) {
1651             ANS_LOGE("Wrong argument type. Object expected.");
1652             return nullptr;
1653         }
1654         AAFwk::WantParams wantParams;
1655         if (!OHOS::AppExecFwk::UnwrapWantParams(env, result, wantParams)) {
1656             return nullptr;
1657         }
1658         pActionButton->AddAdditionalData(wantParams);
1659     }
1660     return NapiGetNull(env);
1661 }
1662 
GetNotificationBadgeNumber(const napi_env & env,const napi_value & value,NotificationRequest & request)1663 napi_value Common::GetNotificationBadgeNumber(
1664     const napi_env &env, const napi_value &value, NotificationRequest &request)
1665 {
1666     ANS_LOGD("enter");
1667 
1668     napi_valuetype valuetype = napi_undefined;
1669     napi_value result = nullptr;
1670     bool hasProperty = false;
1671     int32_t badgeNumber = 0;
1672 
1673     NAPI_CALL(env, napi_has_named_property(env, value, "badgeNumber", &hasProperty));
1674     if (hasProperty) {
1675         napi_get_named_property(env, value, "badgeNumber", &result);
1676         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1677         if (valuetype != napi_number) {
1678             ANS_LOGE("Wrong argument type. Number expected.");
1679             return nullptr;
1680         }
1681 
1682         napi_get_value_int32(env, result, &badgeNumber);
1683         if (badgeNumber < 0) {
1684             ANS_LOGE("Wrong badge number.");
1685             return nullptr;
1686         }
1687 
1688         request.SetBadgeNumber(badgeNumber);
1689     }
1690 
1691     return NapiGetNull(env);
1692 }
1693 }
1694 }
1695