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