1 /*
2 * Copyright (c) 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 "notification_napi.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 Location {
GetPropertyNameByContentType(ContentType type)33 const char *NotificationNapi::GetPropertyNameByContentType(ContentType type)
34 {
35 switch (type) {
36 case ContentType::NOTIFICATION_CONTENT_BASIC_TEXT: // normal?: NotificationBasicContent
37 return "normal";
38 case ContentType::NOTIFICATION_CONTENT_LONG_TEXT: // longText?: NotificationLongTextContent
39 return "longText";
40 case ContentType::NOTIFICATION_CONTENT_PICTURE: // picture?: NotificationPictureContent
41 return "picture";
42 case ContentType::NOTIFICATION_CONTENT_CONVERSATION: // conversation?: NotificationConversationalContent
43 return "conversation";
44 case ContentType::NOTIFICATION_CONTENT_MULTILINE: // multiLine?: NotificationMultiLineContent
45 return "multiLine";
46 case ContentType::NOTIFICATION_CONTENT_LOCAL_LIVE_VIEW: // systemLiveView?: NotificationLocalLiveViewContent
47 return "systemLiveView";
48 case ContentType::NOTIFICATION_CONTENT_LIVE_VIEW: // liveView?: NotificationLiveViewContent
49 return "liveView";
50 default:
51 ANS_LOGE("ContentType is does not exist");
52 return "null";
53 }
54 }
55
GetNotificationContent(const napi_env & env,const napi_value & value,NotificationRequest & request)56 napi_value NotificationNapi::GetNotificationContent(
57 const napi_env &env, const napi_value &value, NotificationRequest &request)
58 {
59 ANS_LOGD("enter");
60
61 napi_value result = AppExecFwk::GetPropertyValueByPropertyName(env, value, "content", napi_object);
62 if (result == nullptr) {
63 ANS_LOGE("No content.");
64 return nullptr;
65 }
66
67 int32_t type = 0;
68 if (GetNotificationContentType(env, result, type) == nullptr) {
69 return nullptr;
70 }
71 NotificationContent::Type outType = NotificationContent::Type::NONE;
72 if (!AnsEnumUtil::ContentTypeJSToC(ContentType(type), outType)) {
73 return nullptr;
74 }
75 switch (outType) {
76 case NotificationContent::Type::BASIC_TEXT:
77 if (GetNotificationBasicContent(env, result, request) == nullptr) {
78 return nullptr;
79 }
80 break;
81 case NotificationContent::Type::LONG_TEXT:
82 if (GetNotificationLongTextContent(env, result, request) == nullptr) {
83 return nullptr;
84 }
85 break;
86 case NotificationContent::Type::PICTURE:
87 if (GetNotificationPictureContent(env, result, request) == nullptr) {
88 return nullptr;
89 }
90 break;
91 case NotificationContent::Type::CONVERSATION:
92 if (GetNotificationConversationalContent(env, result, request) == nullptr) {
93 return nullptr;
94 }
95 break;
96 case NotificationContent::Type::MULTILINE:
97 if (GetNotificationMultiLineContent(env, result, request) == nullptr) {
98 return nullptr;
99 }
100 break;
101 case NotificationContent::Type::LOCAL_LIVE_VIEW:
102 if (GetNotificationLocalLiveViewContent(env, result, request) == nullptr) {
103 return nullptr;
104 }
105 break;
106 case NotificationContent::Type::LIVE_VIEW:
107 if (GetNotificationLiveViewContent(env, result, request) == nullptr) {
108 return nullptr;
109 }
110 break;
111 default:
112 return nullptr;
113 }
114
115 return NapiGetNull(env);
116 }
117
GetNotificationBasicContent(const napi_env & env,const napi_value & result,NotificationRequest & request)118 napi_value NotificationNapi::GetNotificationBasicContent(
119 const napi_env &env, const napi_value &result, NotificationRequest &request)
120 {
121 ANS_LOGD("enter");
122
123 napi_valuetype valuetype = napi_undefined;
124 napi_value contentResult = nullptr;
125 bool hasProperty = false;
126 NAPI_CALL(env, napi_has_named_property(env, result, "normal", &hasProperty));
127 if (!hasProperty) {
128 ANS_LOGE("Property normal expected.");
129 return nullptr;
130 }
131 napi_get_named_property(env, result, "normal", &contentResult);
132 NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
133 if (valuetype != napi_object) {
134 ANS_LOGE("Wrong argument type. Object expected.");
135 return nullptr;
136 }
137
138 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
139 if (GetNotificationBasicContentDetailed(env, contentResult, normalContent) == nullptr) {
140 return nullptr;
141 }
142
143 request.SetContent(std::make_shared<NotificationContent>(normalContent));
144
145 return NapiGetNull(env);
146 }
147
GetNotificationBasicContentDetailed(const napi_env & env,const napi_value & contentResult,std::shared_ptr<NotificationBasicContent> basicContent)148 napi_value NotificationNapi::GetNotificationBasicContentDetailed(
149 const napi_env &env, const napi_value &contentResult, std::shared_ptr<NotificationBasicContent> basicContent)
150 {
151 ANS_LOGD("enter");
152
153 bool hasProperty = false;
154 char str[STR_MAX_SIZE] = {0};
155 size_t strLen = 0;
156
157 // title: string
158 auto value = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult, "title", napi_string);
159 if (value == nullptr) {
160 ANS_LOGE("Failed to get title from js.");
161 return nullptr;
162 }
163 NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
164 if (std::strlen(str) == 0) {
165 ANS_LOGE("Property title is empty");
166 return nullptr;
167 }
168 basicContent->SetTitle(str);
169 ANS_LOGD("normal::title = %{public}s", str);
170
171 // text: string
172 value = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult, "text", napi_string);
173 if (value == nullptr) {
174 ANS_LOGE("Failed to get text from js.");
175 return nullptr;
176 }
177 NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
178 if (std::strlen(str) == 0) {
179 ANS_LOGE("Property text is empty");
180 return nullptr;
181 }
182 basicContent->SetText(str);
183 ANS_LOGD("normal::text = %{public}s", str);
184
185 // additionalText?: string
186 NAPI_CALL(env, napi_has_named_property(env, contentResult, "additionalText", &hasProperty));
187 if (hasProperty) {
188 value = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult, "additionalText", napi_string);
189 if (value == nullptr) {
190 ANS_LOGE("Failed to get additionalText from js.");
191 return nullptr;
192 }
193 NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
194 basicContent->SetAdditionalText(str);
195 ANS_LOGD("normal::additionalText = %{public}s", str);
196 }
197
198 // lockScreenPicture?: pixelMap
199 return GetLockScreenPicture(env, contentResult, basicContent);
200 }
201
GetNotificationLongTextContent(const napi_env & env,const napi_value & result,NotificationRequest & request)202 napi_value NotificationNapi::GetNotificationLongTextContent(
203 const napi_env &env, const napi_value &result, NotificationRequest &request)
204 {
205 ANS_LOGD("enter");
206
207 napi_valuetype valuetype = napi_undefined;
208 napi_value contentResult = nullptr;
209 bool hasProperty = false;
210
211 NAPI_CALL(env, napi_has_named_property(env, result, "longText", &hasProperty));
212 if (!hasProperty) {
213 ANS_LOGE("Property longText expected.");
214 return nullptr;
215 }
216
217 napi_get_named_property(env, result, "longText", &contentResult);
218 NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
219 if (valuetype != napi_object) {
220 ANS_LOGE("Wrong argument type. Object expected.");
221 return nullptr;
222 }
223
224 std::shared_ptr<OHOS::Notification::NotificationLongTextContent> longContent =
225 std::make_shared<OHOS::Notification::NotificationLongTextContent>();
226 if (GetNotificationLongTextContentDetailed(env, contentResult, longContent) == nullptr) {
227 return nullptr;
228 }
229
230 request.SetContent(std::make_shared<NotificationContent>(longContent));
231
232 return NapiGetNull(env);
233 }
234
GetNotificationLongTextContentDetailed(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLongTextContent> & longContent)235 napi_value NotificationNapi::GetNotificationLongTextContentDetailed(
236 const napi_env &env, const napi_value &contentResult,
237 std::shared_ptr<OHOS::Notification::NotificationLongTextContent> &longContent)
238 {
239 ANS_LOGD("enter");
240
241 napi_valuetype valuetype = napi_undefined;
242 napi_value longContentResult = nullptr;
243 bool hasProperty = false;
244 char str[STR_MAX_SIZE] = {0};
245 char long_str[LONG_STR_MAX_SIZE + 1] = {0};
246 size_t strLen = 0;
247
248 if (GetNotificationBasicContentDetailed(env, contentResult, longContent) == nullptr) {
249 return nullptr;
250 }
251
252 // longText: string
253 NAPI_CALL(env, napi_has_named_property(env, contentResult, "longText", &hasProperty));
254 if (!hasProperty) {
255 ANS_LOGE("Property longText expected.");
256 return nullptr;
257 }
258 napi_get_named_property(env, contentResult, "longText", &longContentResult);
259 NAPI_CALL(env, napi_typeof(env, longContentResult, &valuetype));
260 if (valuetype != napi_string) {
261 ANS_LOGE("Wrong argument type. String expected.");
262 return nullptr;
263 }
264 NAPI_CALL(env, napi_get_value_string_utf8(env, longContentResult, long_str, LONG_STR_MAX_SIZE, &strLen));
265 if (std::strlen(long_str) == 0) {
266 ANS_LOGE("Property longText is empty");
267 return nullptr;
268 }
269 longContent->SetLongText(long_str);
270 ANS_LOGD("longText::longText = %{public}s", long_str);
271
272 // briefText: string
273 NAPI_CALL(env, napi_has_named_property(env, contentResult, "briefText", &hasProperty));
274 if (!hasProperty) {
275 ANS_LOGE("Property briefText expected.");
276 return nullptr;
277 }
278 napi_get_named_property(env, contentResult, "briefText", &longContentResult);
279 NAPI_CALL(env, napi_typeof(env, longContentResult, &valuetype));
280 if (valuetype != napi_string) {
281 ANS_LOGE("Wrong argument type. String expected.");
282 return nullptr;
283 }
284 NAPI_CALL(env, napi_get_value_string_utf8(env, longContentResult, str, STR_MAX_SIZE - 1, &strLen));
285 if (std::strlen(str) == 0) {
286 ANS_LOGE("Property briefText is empty");
287 return nullptr;
288 }
289 longContent->SetBriefText(str);
290 ANS_LOGD("longText::briefText = %{public}s", str);
291
292 // expandedTitle: string
293 NAPI_CALL(env, napi_has_named_property(env, contentResult, "expandedTitle", &hasProperty));
294 if (!hasProperty) {
295 ANS_LOGE("Property expandedTitle expected.");
296 return nullptr;
297 }
298 napi_get_named_property(env, contentResult, "expandedTitle", &longContentResult);
299 NAPI_CALL(env, napi_typeof(env, longContentResult, &valuetype));
300 if (valuetype != napi_string) {
301 ANS_LOGE("Wrong argument type. String expected.");
302 return nullptr;
303 }
304 NAPI_CALL(env, napi_get_value_string_utf8(env, longContentResult, str, STR_MAX_SIZE - 1, &strLen));
305 if (std::strlen(str) == 0) {
306 ANS_LOGE("Property expandedTitle is empty");
307 return nullptr;
308 }
309 longContent->SetExpandedTitle(str);
310 ANS_LOGD("longText::expandedTitle = %{public}s", str);
311
312 return NapiGetNull(env);
313 }
314
GetNotificationPictureContent(const napi_env & env,const napi_value & result,NotificationRequest & request)315 napi_value NotificationNapi::GetNotificationPictureContent(
316 const napi_env &env, const napi_value &result, NotificationRequest &request)
317 {
318 ANS_LOGD("enter");
319
320 napi_valuetype valuetype = napi_undefined;
321 napi_value contentResult = nullptr;
322 bool hasProperty = false;
323
324 NAPI_CALL(env, napi_has_named_property(env, result, "picture", &hasProperty));
325 if (!hasProperty) {
326 ANS_LOGE("Property picture expected.");
327 return nullptr;
328 }
329 napi_get_named_property(env, result, "picture", &contentResult);
330 NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
331 if (valuetype != napi_object) {
332 ANS_LOGE("Wrong argument type. Object expected.");
333 return nullptr;
334 }
335
336 std::shared_ptr<OHOS::Notification::NotificationPictureContent> pictureContent =
337 std::make_shared<OHOS::Notification::NotificationPictureContent>();
338 if (GetNotificationPictureContentDetailed(env, contentResult, pictureContent) == nullptr) {
339 return nullptr;
340 }
341
342 request.SetContent(std::make_shared<NotificationContent>(pictureContent));
343
344 return NapiGetNull(env);
345 }
346
GetNotificationPictureContentDetailed(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationPictureContent> & pictureContent)347 napi_value NotificationNapi::GetNotificationPictureContentDetailed(const napi_env &env,
348 const napi_value &contentResult, std::shared_ptr<OHOS::Notification::NotificationPictureContent> &pictureContent)
349 {
350 ANS_LOGD("enter");
351
352 napi_valuetype valuetype = napi_undefined;
353 napi_value pictureContentResult = nullptr;
354 bool hasProperty = false;
355 char str[STR_MAX_SIZE] = {0};
356 size_t strLen = 0;
357
358 if (GetNotificationBasicContentDetailed(env, contentResult, pictureContent) == nullptr) {
359 return nullptr;
360 }
361
362 // briefText: string
363 NAPI_CALL(env, napi_has_named_property(env, contentResult, "briefText", &hasProperty));
364 if (!hasProperty) {
365 ANS_LOGE("Property briefText expected.");
366 return nullptr;
367 }
368 napi_get_named_property(env, contentResult, "briefText", &pictureContentResult);
369 NAPI_CALL(env, napi_typeof(env, pictureContentResult, &valuetype));
370 if (valuetype != napi_string) {
371 ANS_LOGE("Wrong argument type. String expected.");
372 return nullptr;
373 }
374 NAPI_CALL(env, napi_get_value_string_utf8(env, pictureContentResult, str, STR_MAX_SIZE - 1, &strLen));
375 if (std::strlen(str) == 0) {
376 ANS_LOGE("Property briefText is empty");
377 return nullptr;
378 }
379 pictureContent->SetBriefText(str);
380
381 // expandedTitle: string
382 NAPI_CALL(env, napi_has_named_property(env, contentResult, "expandedTitle", &hasProperty));
383 if (!hasProperty) {
384 ANS_LOGE("Property expandedTitle expected.");
385 return nullptr;
386 }
387 napi_get_named_property(env, contentResult, "expandedTitle", &pictureContentResult);
388 NAPI_CALL(env, napi_typeof(env, pictureContentResult, &valuetype));
389 if (valuetype != napi_string) {
390 ANS_LOGE("Wrong argument type. String expected.");
391 return nullptr;
392 }
393 NAPI_CALL(env, napi_get_value_string_utf8(env, pictureContentResult, str, STR_MAX_SIZE - 1, &strLen));
394 if (std::strlen(str) == 0) {
395 ANS_LOGE("Property expandedTitle is empty");
396 return nullptr;
397 }
398 pictureContent->SetExpandedTitle(str);
399
400 // picture: image.PixelMap
401 NAPI_CALL(env, napi_has_named_property(env, contentResult, "picture", &hasProperty));
402 if (!hasProperty) {
403 ANS_LOGE("Property picture expected.");
404 return nullptr;
405 }
406 napi_get_named_property(env, contentResult, "picture", &pictureContentResult);
407 NAPI_CALL(env, napi_typeof(env, pictureContentResult, &valuetype));
408 if (valuetype != napi_object) {
409 ANS_LOGE("Wrong argument type. Object expected.");
410 return nullptr;
411 }
412 std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
413 pixelMap = Media::PixelMapNapi::GetPixelMap(env, pictureContentResult);
414 if (pixelMap == nullptr) {
415 ANS_LOGE("Invalid object pixelMap");
416 return nullptr;
417 }
418 pictureContent->SetBigPicture(pixelMap);
419
420 return NotificationNapi::NapiGetNull(env);
421 }
422
GetNotificationConversationalContent(const napi_env & env,const napi_value & result,NotificationRequest & request)423 napi_value NotificationNapi::GetNotificationConversationalContent(
424 const napi_env &env, const napi_value &result, NotificationRequest &request)
425 {
426 ANS_LOGD("enter");
427
428 napi_valuetype valuetype = napi_undefined;
429 napi_value contentResult = nullptr;
430 bool hasProperty = false;
431 MessageUser user;
432
433 NAPI_CALL(env, napi_has_named_property(env, result, "conversation", &hasProperty));
434 if (!hasProperty) {
435 ANS_LOGE("Property conversation expected.");
436 return nullptr;
437 }
438 napi_get_named_property(env, result, "conversation", &contentResult);
439 NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
440 if (valuetype != napi_object) {
441 ANS_LOGE("Wrong argument type. Object expected.");
442 return nullptr;
443 }
444
445 if (GetNotificationConversationalContentByUser(env, contentResult, user) == nullptr) {
446 return nullptr;
447 }
448
449 std::shared_ptr<OHOS::Notification::NotificationConversationalContent> conversationalContent =
450 std::make_shared<OHOS::Notification::NotificationConversationalContent>(user);
451 if (GetNotificationBasicContentDetailed(env, contentResult, conversationalContent) == nullptr) {
452 return nullptr;
453 }
454 if (GetNotificationConversationalContentTitle(env, contentResult, conversationalContent) == nullptr) {
455 return nullptr;
456 }
457 if (GetNotificationConversationalContentGroup(env, contentResult, conversationalContent) == nullptr) {
458 return nullptr;
459 }
460 if (GetNotificationConversationalContentMessages(env, contentResult, conversationalContent) == nullptr) {
461 return nullptr;
462 }
463
464 request.SetContent(std::make_shared<NotificationContent>(conversationalContent));
465
466 return NapiGetNull(env);
467 }
468
GetNotificationConversationalContentByUser(const napi_env & env,const napi_value & contentResult,MessageUser & user)469 napi_value NotificationNapi::GetNotificationConversationalContentByUser(
470 const napi_env &env, const napi_value &contentResult, MessageUser &user)
471 {
472 ANS_LOGD("enter");
473
474 napi_valuetype valuetype = napi_undefined;
475 bool hasProperty = false;
476
477 // user: MessageUser
478 NAPI_CALL(env, napi_has_named_property(env, contentResult, "user", &hasProperty));
479 if (!hasProperty) {
480 ANS_LOGE("Property user expected.");
481 return nullptr;
482 }
483 napi_value userResult = nullptr;
484 napi_get_named_property(env, contentResult, "user", &userResult);
485 NAPI_CALL(env, napi_typeof(env, userResult, &valuetype));
486 if (valuetype != napi_object) {
487 ANS_LOGE("Wrong argument type. Object expected.");
488 return nullptr;
489 }
490 if (!GetMessageUser(env, userResult, user)) {
491 return nullptr;
492 }
493
494 return NapiGetNull(env);
495 }
496
GetMessageUser(const napi_env & env,const napi_value & result,MessageUser & messageUser)497 napi_value NotificationNapi::GetMessageUser(const napi_env &env, const napi_value &result, MessageUser &messageUser)
498 {
499 ANS_LOGD("enter");
500
501 if (GetMessageUserByString(env, result, messageUser) == nullptr) {
502 return nullptr;
503 }
504
505 if (GetMessageUserByBool(env, result, messageUser) == nullptr) {
506 return nullptr;
507 }
508
509 if (GetMessageUserByCustom(env, result, messageUser) == nullptr) {
510 return nullptr;
511 }
512
513 return NapiGetNull(env);
514 }
515
GetMessageUserByString(const napi_env & env,const napi_value & result,MessageUser & messageUser)516 napi_value NotificationNapi::GetMessageUserByString(
517 const napi_env &env, const napi_value &result, MessageUser &messageUser)
518 {
519 ANS_LOGD("enter");
520
521 napi_valuetype valuetype = napi_undefined;
522 bool hasProperty = false;
523 char str[STR_MAX_SIZE] = {0};
524 size_t strLen = 0;
525
526 // name: string
527 NAPI_CALL(env, napi_has_named_property(env, result, "name", &hasProperty));
528 if (!hasProperty) {
529 ANS_LOGE("Property name expected.");
530 return nullptr;
531 }
532 napi_value nameResult = nullptr;
533 napi_get_named_property(env, result, "name", &nameResult);
534 NAPI_CALL(env, napi_typeof(env, nameResult, &valuetype));
535 if (valuetype != napi_string) {
536 ANS_LOGE("Wrong argument type. String expected.");
537 return nullptr;
538 }
539 NAPI_CALL(env, napi_get_value_string_utf8(env, nameResult, str, STR_MAX_SIZE - 1, &strLen));
540 messageUser.SetName(str);
541 ANS_LOGI("MessageUser::name = %{public}s", str);
542
543 // key: string
544 NAPI_CALL(env, napi_has_named_property(env, result, "key", &hasProperty));
545 if (!hasProperty) {
546 ANS_LOGE("Property key expected.");
547 return nullptr;
548 }
549 napi_value keyResult = nullptr;
550 napi_get_named_property(env, result, "key", &keyResult);
551 NAPI_CALL(env, napi_typeof(env, keyResult, &valuetype));
552 if (valuetype != napi_string) {
553 ANS_LOGE("Wrong argument type. String expected.");
554 return nullptr;
555 }
556 NAPI_CALL(env, napi_get_value_string_utf8(env, keyResult, str, STR_MAX_SIZE - 1, &strLen));
557 messageUser.SetKey(str);
558 ANS_LOGI("MessageUser::key = %{public}s", str);
559
560 // uri: string
561 NAPI_CALL(env, napi_has_named_property(env, result, "uri", &hasProperty));
562 if (!hasProperty) {
563 ANS_LOGE("Property uri expected.");
564 return nullptr;
565 }
566 napi_value uriResult = nullptr;
567 napi_get_named_property(env, result, "uri", &uriResult);
568 NAPI_CALL(env, napi_typeof(env, uriResult, &valuetype));
569 if (valuetype != napi_string) {
570 ANS_LOGE("Wrong argument type. String expected.");
571 return nullptr;
572 }
573 NAPI_CALL(env, napi_get_value_string_utf8(env, uriResult, str, STR_MAX_SIZE - 1, &strLen));
574 Uri uri(str);
575 messageUser.SetUri(uri);
576
577 return NapiGetNull(env);
578 }
579
GetMessageUserByBool(const napi_env & env,const napi_value & result,MessageUser & messageUser)580 napi_value NotificationNapi::GetMessageUserByBool(
581 const napi_env &env, const napi_value &result, MessageUser &messageUser)
582 {
583 ANS_LOGD("enter");
584
585 napi_valuetype valuetype = napi_undefined;
586 bool hasProperty = false;
587
588 // isMachine: boolean
589 NAPI_CALL(env, napi_has_named_property(env, result, "isMachine", &hasProperty));
590 if (!hasProperty) {
591 ANS_LOGE("Property isMachine expected.");
592 return nullptr;
593 }
594 napi_value machineResult = nullptr;
595 napi_get_named_property(env, result, "isMachine", &machineResult);
596 NAPI_CALL(env, napi_typeof(env, machineResult, &valuetype));
597 if (valuetype != napi_boolean) {
598 ANS_LOGE("Wrong argument type. Bool expected.");
599 return nullptr;
600 }
601 bool machine = false;
602 napi_get_value_bool(env, machineResult, &machine);
603 messageUser.SetMachine(machine);
604
605 // isUserImportant: boolean
606 NAPI_CALL(env, napi_has_named_property(env, result, "isUserImportant", &hasProperty));
607 if (!hasProperty) {
608 ANS_LOGE("Property isUserImportant expected.");
609 return nullptr;
610 }
611 napi_value importantResult = nullptr;
612 napi_get_named_property(env, result, "isUserImportant", &importantResult);
613 NAPI_CALL(env, napi_typeof(env, importantResult, &valuetype));
614 if (valuetype != napi_boolean) {
615 ANS_LOGE("Wrong argument type. Bool expected.");
616 return nullptr;
617 }
618 bool important = false;
619 napi_get_value_bool(env, importantResult, &important);
620 messageUser.SetUserAsImportant(important);
621 ANS_LOGI("MessageUser::isUserImportant = %{public}d", important);
622
623 return NapiGetNull(env);
624 }
625
GetMessageUserByCustom(const napi_env & env,const napi_value & result,MessageUser & messageUser)626 napi_value NotificationNapi::GetMessageUserByCustom(
627 const napi_env &env, const napi_value &result, MessageUser &messageUser)
628 {
629 ANS_LOGD("enter");
630
631 napi_valuetype valuetype = napi_undefined;
632 bool hasProperty = false;
633
634 // icon?: image.PixelMap
635 NAPI_CALL(env, napi_has_named_property(env, result, "icon", &hasProperty));
636 if (hasProperty) {
637 napi_value iconResult = nullptr;
638 napi_get_named_property(env, result, "icon", &iconResult);
639 NAPI_CALL(env, napi_typeof(env, iconResult, &valuetype));
640 if (valuetype != napi_object) {
641 ANS_LOGE("Wrong argument type. Object expected.");
642 return nullptr;
643 }
644 std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
645 pixelMap = Media::PixelMapNapi::GetPixelMap(env, iconResult);
646 if (pixelMap == nullptr) {
647 ANS_LOGE("Invalid object pixelMap");
648 return nullptr;
649 }
650 messageUser.SetPixelMap(pixelMap);
651 }
652
653 return NapiGetNull(env);
654 }
655
GetNotificationConversationalContentTitle(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationConversationalContent> & conversationalContent)656 napi_value NotificationNapi::GetNotificationConversationalContentTitle(
657 const napi_env &env, const napi_value &contentResult,
658 std::shared_ptr<OHOS::Notification::NotificationConversationalContent> &conversationalContent)
659 {
660 ANS_LOGD("enter");
661 napi_valuetype valuetype = napi_undefined;
662 napi_value conversationalContentResult = nullptr;
663 bool hasProperty = false;
664 char str[STR_MAX_SIZE] = {0};
665 size_t strLen = 0;
666
667 // conversationTitle: string
668 NAPI_CALL(env, napi_has_named_property(env, contentResult, "conversationTitle", &hasProperty));
669 if (!hasProperty) {
670 ANS_LOGE("Property conversationTitle expected.");
671 return nullptr;
672 }
673 napi_get_named_property(env, contentResult, "conversationTitle", &conversationalContentResult);
674 NAPI_CALL(env, napi_typeof(env, conversationalContentResult, &valuetype));
675 if (valuetype != napi_string) {
676 ANS_LOGE("Wrong argument type. String expected.");
677 return nullptr;
678 }
679 NAPI_CALL(env, napi_get_value_string_utf8(env, conversationalContentResult, str, STR_MAX_SIZE - 1, &strLen));
680 conversationalContent->SetConversationTitle(str);
681 ANS_LOGD("conversationTitle = %{public}s", str);
682
683 return NapiGetNull(env);
684 }
685
GetNotificationConversationalContentGroup(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationConversationalContent> & conversationalContent)686 napi_value NotificationNapi::GetNotificationConversationalContentGroup(
687 const napi_env &env, const napi_value &contentResult,
688 std::shared_ptr<OHOS::Notification::NotificationConversationalContent> &conversationalContent)
689 {
690 ANS_LOGD("enter");
691 napi_valuetype valuetype = napi_undefined;
692 napi_value conversationalContentResult = nullptr;
693 bool hasProperty = false;
694
695 // conversationGroup: boolean
696 NAPI_CALL(env, napi_has_named_property(env, contentResult, "conversationGroup", &hasProperty));
697 if (!hasProperty) {
698 ANS_LOGE("Property conversationGroup expected.");
699 return nullptr;
700 }
701 napi_get_named_property(env, contentResult, "conversationGroup", &conversationalContentResult);
702 NAPI_CALL(env, napi_typeof(env, conversationalContentResult, &valuetype));
703 if (valuetype != napi_boolean) {
704 ANS_LOGE("Wrong argument type. Bool expected.");
705 return nullptr;
706 }
707 bool conversationGroup = false;
708 napi_get_value_bool(env, conversationalContentResult, &conversationGroup);
709 conversationalContent->SetConversationGroup(conversationGroup);
710 ANS_LOGI("conversationalText::conversationGroup = %{public}d", conversationGroup);
711
712 return NapiGetNull(env);
713 }
714
GetNotificationConversationalContentMessages(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationConversationalContent> & conversationalContent)715 napi_value NotificationNapi::GetNotificationConversationalContentMessages(
716 const napi_env &env, const napi_value &contentResult,
717 std::shared_ptr<OHOS::Notification::NotificationConversationalContent> &conversationalContent)
718 {
719 ANS_LOGD("enter");
720 napi_valuetype valuetype = napi_undefined;
721 napi_value conversationalContentResult = nullptr;
722 bool hasProperty = false;
723
724 // messages: Array<ConversationalMessage>
725 NAPI_CALL(env, napi_has_named_property(env, contentResult, "messages", &hasProperty));
726 if (!hasProperty) {
727 ANS_LOGE("Property messages expected.");
728 return nullptr;
729 }
730 napi_get_named_property(env, contentResult, "messages", &conversationalContentResult);
731 bool isArray = false;
732 napi_is_array(env, conversationalContentResult, &isArray);
733 if (!isArray) {
734 ANS_LOGE("Property messages is expected to be an array.");
735 return nullptr;
736 }
737 uint32_t length = 0;
738 napi_get_array_length(env, conversationalContentResult, &length);
739 if (length == 0) {
740 ANS_LOGE("The array is empty.");
741 return nullptr;
742 }
743 for (size_t i = 0; i < length; i++) {
744 napi_value conversationalMessage = nullptr;
745 napi_get_element(env, conversationalContentResult, i, &conversationalMessage);
746 NAPI_CALL(env, napi_typeof(env, conversationalMessage, &valuetype));
747 if (valuetype != napi_object) {
748 ANS_LOGE("Wrong argument type. Object expected.");
749 return nullptr;
750 }
751 std::shared_ptr<NotificationConversationalMessage> message = nullptr;
752 if (!GetConversationalMessage(env, conversationalMessage, message)) {
753 return nullptr;
754 }
755 conversationalContent->AddConversationalMessage(message);
756 }
757
758 return NapiGetNull(env);
759 }
760
GetConversationalMessage(const napi_env & env,const napi_value & conversationalMessage,std::shared_ptr<NotificationConversationalMessage> & message)761 napi_value NotificationNapi::GetConversationalMessage(const napi_env &env, const napi_value &conversationalMessage,
762 std::shared_ptr<NotificationConversationalMessage> &message)
763 {
764 ANS_LOGD("enter");
765
766 if (GetConversationalMessageBasicInfo(env, conversationalMessage, message) == nullptr) {
767 return nullptr;
768 }
769 if (GetConversationalMessageOtherInfo(env, conversationalMessage, message) == nullptr) {
770 return nullptr;
771 }
772 return NapiGetNull(env);
773 }
774
GetConversationalMessageBasicInfo(const napi_env & env,const napi_value & conversationalMessage,std::shared_ptr<NotificationConversationalMessage> & message)775 napi_value NotificationNapi::GetConversationalMessageBasicInfo(
776 const napi_env &env, const napi_value &conversationalMessage,
777 std::shared_ptr<NotificationConversationalMessage> &message)
778 {
779 ANS_LOGD("enter");
780
781 napi_valuetype valuetype = napi_undefined;
782 bool hasProperty = false;
783 char str[STR_MAX_SIZE] = {0};
784 size_t strLen = 0;
785 std::string text;
786 int64_t timestamp = 0;
787 MessageUser sender;
788
789 // text: string
790 NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "text", &hasProperty));
791 if (!hasProperty) {
792 ANS_LOGE("Property text expected.");
793 return nullptr;
794 }
795 napi_value textResult = nullptr;
796 napi_get_named_property(env, conversationalMessage, "text", &textResult);
797 NAPI_CALL(env, napi_typeof(env, textResult, &valuetype));
798 if (valuetype != napi_string) {
799 ANS_LOGE("Wrong argument type. String expected.");
800 return nullptr;
801 }
802 NAPI_CALL(env, napi_get_value_string_utf8(env, textResult, str, STR_MAX_SIZE - 1, &strLen));
803 text = str;
804 ANS_LOGI("conversationalMessage::text = %{public}s", str);
805
806 // timestamp: number
807 NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "timestamp", &hasProperty));
808 if (!hasProperty) {
809 ANS_LOGE("Property timestamp expected.");
810 return nullptr;
811 }
812 napi_value timestampResult = nullptr;
813 napi_get_named_property(env, conversationalMessage, "timestamp", ×tampResult);
814 NAPI_CALL(env, napi_typeof(env, timestampResult, &valuetype));
815 if (valuetype != napi_number) {
816 ANS_LOGE("Wrong argument type. Number expected.");
817 return nullptr;
818 }
819 napi_get_value_int64(env, timestampResult, ×tamp);
820 ANS_LOGI("conversationalMessage::timestamp = %{public}" PRId64, timestamp);
821
822 // sender: MessageUser
823 NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "sender", &hasProperty));
824 if (!hasProperty) {
825 ANS_LOGE("Property sender expected.");
826 return nullptr;
827 }
828 napi_value senderResult = nullptr;
829 napi_get_named_property(env, conversationalMessage, "sender", &senderResult);
830 NAPI_CALL(env, napi_typeof(env, senderResult, &valuetype));
831 if (valuetype != napi_object) {
832 ANS_LOGE("Wrong argument type. Object expected.");
833 return nullptr;
834 }
835 if (!GetMessageUser(env, senderResult, sender)) {
836 return nullptr;
837 }
838
839 message = std::make_shared<NotificationConversationalMessage>(text, timestamp, sender);
840 if (!message) {
841 ANS_LOGE("Failed to create NotificationConversationalMessage object");
842 return nullptr;
843 }
844
845 return NapiGetNull(env);
846 }
847
GetConversationalMessageOtherInfo(const napi_env & env,const napi_value & conversationalMessage,std::shared_ptr<NotificationConversationalMessage> & message)848 napi_value NotificationNapi::GetConversationalMessageOtherInfo(
849 const napi_env &env, const napi_value &conversationalMessage,
850 std::shared_ptr<NotificationConversationalMessage> &message)
851 {
852 ANS_LOGD("enter");
853 if (message == nullptr) {
854 return nullptr;
855 }
856 napi_valuetype valuetype = napi_undefined;
857 bool hasProperty = false;
858 char str[STR_MAX_SIZE] = {0};
859 size_t strLen = 0;
860 std::string mimeType;
861 std::string uri;
862
863 // mimeType: string
864 NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "mimeType", &hasProperty));
865 if (!hasProperty) {
866 ANS_LOGE("Property mimeType expected.");
867 return nullptr;
868 }
869 napi_value mimeTypeResult = nullptr;
870 napi_get_named_property(env, conversationalMessage, "mimeType", &mimeTypeResult);
871 NAPI_CALL(env, napi_typeof(env, mimeTypeResult, &valuetype));
872 if (valuetype != napi_string) {
873 ANS_LOGE("Wrong argument type. String expected.");
874 return nullptr;
875 }
876 NAPI_CALL(env, napi_get_value_string_utf8(env, mimeTypeResult, str, STR_MAX_SIZE - 1, &strLen));
877 mimeType = str;
878 ANS_LOGI("conversationalMessage::mimeType = %{public}s", str);
879
880 // uri?: string
881 NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "uri", &hasProperty));
882 if (hasProperty) {
883 napi_value uriResult = nullptr;
884 napi_get_named_property(env, conversationalMessage, "uri", &uriResult);
885 NAPI_CALL(env, napi_typeof(env, uriResult, &valuetype));
886 if (valuetype != napi_string) {
887 ANS_LOGE("Wrong argument type. String expected.");
888 return nullptr;
889 }
890 NAPI_CALL(env, napi_get_value_string_utf8(env, uriResult, str, STR_MAX_SIZE - 1, &strLen));
891 uri = str;
892 }
893
894 std::shared_ptr<Uri> uriPtr = std::make_shared<Uri>(uri);
895 message->SetData(mimeType, uriPtr);
896
897 return NapiGetNull(env);
898 }
899
GetNotificationMultiLineContent(const napi_env & env,const napi_value & result,NotificationRequest & request)900 napi_value NotificationNapi::GetNotificationMultiLineContent(
901 const napi_env &env, const napi_value &result, NotificationRequest &request)
902 {
903 ANS_LOGD("enter");
904
905 napi_valuetype valuetype = napi_undefined;
906 napi_value contentResult = nullptr;
907 napi_value multiLineContentResult = nullptr;
908 bool hasProperty = false;
909 char str[STR_MAX_SIZE] = {0};
910 size_t strLen = 0;
911
912 NAPI_CALL(env, napi_has_named_property(env, result, "multiLine", &hasProperty));
913 if (!hasProperty) {
914 ANS_LOGE("Property multiLine expected.");
915 return nullptr;
916 }
917 napi_get_named_property(env, result, "multiLine", &contentResult);
918 NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
919 if (valuetype != napi_object) {
920 ANS_LOGE("Wrong argument type. Object expected.");
921 return nullptr;
922 }
923
924 std::shared_ptr<OHOS::Notification::NotificationMultiLineContent> multiLineContent =
925 std::make_shared<OHOS::Notification::NotificationMultiLineContent>();
926 if (GetNotificationBasicContentDetailed(env, contentResult, multiLineContent) == nullptr) {
927 return nullptr;
928 }
929
930 // briefText: string
931 NAPI_CALL(env, napi_has_named_property(env, contentResult, "briefText", &hasProperty));
932 if (!hasProperty) {
933 ANS_LOGE("Property briefText expected.");
934 return nullptr;
935 }
936 napi_get_named_property(env, contentResult, "briefText", &multiLineContentResult);
937 NAPI_CALL(env, napi_typeof(env, multiLineContentResult, &valuetype));
938 if (valuetype != napi_string) {
939 ANS_LOGE("Wrong argument type. String expected.");
940 return nullptr;
941 }
942 NAPI_CALL(env, napi_get_value_string_utf8(env, multiLineContentResult, str, STR_MAX_SIZE - 1, &strLen));
943 if (std::strlen(str) == 0) {
944 ANS_LOGE("Property briefText is empty");
945 return nullptr;
946 }
947 multiLineContent->SetBriefText(str);
948 ANS_LOGD("multiLine: briefText = %{public}s", str);
949
950 // longTitle: string
951 NAPI_CALL(env, napi_has_named_property(env, contentResult, "longTitle", &hasProperty));
952 if (!hasProperty) {
953 ANS_LOGE("Property longTitle expected.");
954 return nullptr;
955 }
956 napi_get_named_property(env, contentResult, "longTitle", &multiLineContentResult);
957 NAPI_CALL(env, napi_typeof(env, multiLineContentResult, &valuetype));
958 if (valuetype != napi_string) {
959 ANS_LOGE("Wrong argument type. String expected.");
960 return nullptr;
961 }
962 NAPI_CALL(env, napi_get_value_string_utf8(env, multiLineContentResult, str, STR_MAX_SIZE - 1, &strLen));
963 if (std::strlen(str) == 0) {
964 ANS_LOGE("Property longTitle is empty");
965 return nullptr;
966 }
967 multiLineContent->SetExpandedTitle(str);
968 ANS_LOGD("multiLine: longTitle = %{public}s", str);
969
970 // lines: Array<String>
971 NAPI_CALL(env, napi_has_named_property(env, contentResult, "lines", &hasProperty));
972 if (!hasProperty) {
973 ANS_LOGE("Property lines expected.");
974 return nullptr;
975 }
976 if (GetNotificationMultiLineContentLines(env, contentResult, multiLineContent) == nullptr) {
977 return nullptr;
978 }
979
980 request.SetContent(std::make_shared<NotificationContent>(multiLineContent));
981
982 ANS_LOGD("end");
983 return NapiGetNull(env);
984 }
985
GetNotificationMultiLineContentLines(const napi_env & env,const napi_value & result,std::shared_ptr<OHOS::Notification::NotificationMultiLineContent> & multiLineContent)986 napi_value NotificationNapi::GetNotificationMultiLineContentLines(const napi_env &env, const napi_value &result,
987 std::shared_ptr<OHOS::Notification::NotificationMultiLineContent> &multiLineContent)
988 {
989 ANS_LOGD("enter");
990
991 bool isArray = false;
992 napi_valuetype valuetype = napi_undefined;
993 napi_value multilines = nullptr;
994 char str[STR_MAX_SIZE] = {0};
995 size_t strLen = 0;
996 uint32_t length = 0;
997
998 napi_get_named_property(env, result, "lines", &multilines);
999 napi_is_array(env, multilines, &isArray);
1000 if (!isArray) {
1001 ANS_LOGE("Property lines is expected to be an array.");
1002 return nullptr;
1003 }
1004
1005 napi_get_array_length(env, multilines, &length);
1006 if (length == 0) {
1007 ANS_LOGE("The array is empty.");
1008 return nullptr;
1009 }
1010 for (size_t i = 0; i < length; i++) {
1011 napi_value line = nullptr;
1012 napi_get_element(env, multilines, i, &line);
1013 NAPI_CALL(env, napi_typeof(env, line, &valuetype));
1014 if (valuetype != napi_string) {
1015 ANS_LOGE("Wrong argument type. String expected.");
1016 return nullptr;
1017 }
1018 NAPI_CALL(env, napi_get_value_string_utf8(env, line, str, STR_MAX_SIZE - 1, &strLen));
1019 multiLineContent->AddSingleLine(str);
1020 ANS_LOGI("multiLine: lines : addSingleLine = %{public}s", str);
1021 }
1022
1023 return NapiGetNull(env);
1024 }
1025
GetLockScreenPicture(const napi_env & env,const napi_value & contentResult,std::shared_ptr<NotificationBasicContent> basicContent)1026 napi_value NotificationNapi::GetLockScreenPicture(
1027 const napi_env &env, const napi_value &contentResult, std::shared_ptr<NotificationBasicContent> basicContent)
1028 {
1029 bool hasProperty = false;
1030 NAPI_CALL(env, napi_has_named_property(env, contentResult, "lockscreenPicture", &hasProperty));
1031 if (hasProperty) {
1032 auto value = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult, "lockscreenPicture", napi_object);
1033 if (value == nullptr) {
1034 ANS_LOGE("Failed to get lockScreenPicture from js.");
1035 return nullptr;
1036 }
1037 auto pixelMap = Media::PixelMapNapi::GetPixelMap(env, value);
1038 if (pixelMap == nullptr) {
1039 ANS_LOGE("Invalid object pixelMap");
1040 return nullptr;
1041 }
1042 basicContent->SetLockScreenPicture(pixelMap);
1043 }
1044
1045 return NapiGetNull(env);
1046 }
1047 }
1048 }
1049