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