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