1 /*
2 * Copyright (c) 2023-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "common.h"
17 #include "ans_inner_errors.h"
18 #include "ans_log_wrapper.h"
19 #include "js_native_api.h"
20 #include "js_native_api_types.h"
21 #include "napi_common.h"
22 #include "napi_common_util.h"
23 #include "notification_action_button.h"
24 #include "notification_capsule.h"
25 #include "notification_constant.h"
26 #include "notification_local_live_view_content.h"
27 #include "notification_progress.h"
28 #include "notification_time.h"
29 #include "pixel_map_napi.h"
30
31 namespace OHOS {
32 namespace NotificationNapi {
GetPropertyNameByContentType(ContentType type)33 const char *Common::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
SetNotificationContentDetailed(const napi_env & env,const ContentType & type,const std::shared_ptr<NotificationContent> & content,napi_value & result)56 napi_value Common::SetNotificationContentDetailed(const napi_env &env, const ContentType &type,
57 const std::shared_ptr<NotificationContent> &content, napi_value &result)
58 {
59 ANS_LOGD("enter");
60 napi_value ret = NapiGetBoolean(env, false);
61 if (!content) {
62 ANS_LOGE("content is null");
63 return ret;
64 }
65
66 std::shared_ptr<NotificationBasicContent> basicContent = content->GetNotificationContent();
67 if (basicContent == nullptr) {
68 ANS_LOGE("content is null");
69 return ret;
70 }
71
72 napi_value contentResult = nullptr;
73 napi_create_object(env, &contentResult);
74 switch (type) {
75 case ContentType::NOTIFICATION_CONTENT_BASIC_TEXT: // normal?: NotificationBasicContent
76 ret = SetNotificationBasicContent(env, basicContent.get(), contentResult);
77 break;
78 case ContentType::NOTIFICATION_CONTENT_LONG_TEXT: // longText?: NotificationLongTextContent
79 ret = SetNotificationLongTextContent(env, basicContent.get(), contentResult);
80 break;
81 case ContentType::NOTIFICATION_CONTENT_PICTURE: // picture?: NotificationPictureContent
82 ret = SetNotificationPictureContent(env, basicContent.get(), contentResult);
83 break;
84 case ContentType::NOTIFICATION_CONTENT_CONVERSATION: // conversation?: NotificationConversationalContent
85 ret = SetNotificationConversationalContent(env, basicContent.get(), contentResult);
86 break;
87 case ContentType::NOTIFICATION_CONTENT_MULTILINE: // multiLine?: NotificationMultiLineContent
88 ret = SetNotificationMultiLineContent(env, basicContent.get(), contentResult);
89 break;
90 case ContentType::NOTIFICATION_CONTENT_LOCAL_LIVE_VIEW: // systemLiveView?: NotificationLocalLiveViewContent
91 ret = SetNotificationLocalLiveViewContent(env, basicContent.get(), contentResult);
92 break;
93 case ContentType::NOTIFICATION_CONTENT_LIVE_VIEW: // liveView?: NotificationLiveViewContent
94 ret = SetNotificationLiveViewContent(env, basicContent.get(), contentResult);
95 break;
96 default:
97 ANS_LOGE("ContentType is does not exist");
98 return nullptr;
99 }
100 if (ret) {
101 const char *propertyName = GetPropertyNameByContentType(type);
102 napi_set_named_property(env, result, propertyName, contentResult);
103 }
104
105 return ret;
106 }
107
SetNotificationContent(const napi_env & env,const std::shared_ptr<NotificationContent> & content,napi_value & result)108 napi_value Common::SetNotificationContent(
109 const napi_env &env, const std::shared_ptr<NotificationContent> &content, napi_value &result)
110 {
111 ANS_LOGD("enter");
112 napi_value value = nullptr;
113 if (content == nullptr) {
114 ANS_LOGE("content is null");
115 return NapiGetBoolean(env, false);
116 }
117
118 // contentType: ContentType
119 NotificationContent::Type type = content->GetContentType();
120 ContentType outType = ContentType::NOTIFICATION_CONTENT_BASIC_TEXT;
121 if (!ContentTypeCToJS(type, outType)) {
122 return NapiGetBoolean(env, false);
123 }
124 napi_create_int32(env, static_cast<int32_t>(outType), &value);
125 napi_set_named_property(env, result, "contentType", value);
126 napi_set_named_property(env, result, "notificationContentType", value);
127
128 if (!SetNotificationContentDetailed(env, outType, content, result)) {
129 return NapiGetBoolean(env, false);
130 }
131
132 return NapiGetBoolean(env, true);
133 }
134
SetNotificationBasicContent(const napi_env & env,const NotificationBasicContent * basicContent,napi_value & result)135 napi_value Common::SetNotificationBasicContent(
136 const napi_env &env, const NotificationBasicContent *basicContent, napi_value &result)
137 {
138 ANS_LOGD("enter");
139 napi_value value = nullptr;
140 if (basicContent == nullptr) {
141 ANS_LOGE("basicContent is null");
142 return NapiGetBoolean(env, false);
143 }
144
145 // title: string
146 napi_create_string_utf8(env, basicContent->GetTitle().c_str(), NAPI_AUTO_LENGTH, &value);
147 napi_set_named_property(env, result, "title", value);
148
149 // text: string
150 napi_create_string_utf8(env, basicContent->GetText().c_str(), NAPI_AUTO_LENGTH, &value);
151 napi_set_named_property(env, result, "text", value);
152
153 // additionalText?: string
154 napi_create_string_utf8(env, basicContent->GetAdditionalText().c_str(), NAPI_AUTO_LENGTH, &value);
155 napi_set_named_property(env, result, "additionalText", value);
156
157 return NapiGetBoolean(env, true);
158 }
159
SetNotificationLongTextContent(const napi_env & env,NotificationBasicContent * basicContent,napi_value & result)160 napi_value Common::SetNotificationLongTextContent(
161 const napi_env &env, NotificationBasicContent *basicContent, napi_value &result)
162 {
163 ANS_LOGD("enter");
164 napi_value value = nullptr;
165 if (basicContent == nullptr) {
166 ANS_LOGE("basicContent is null");
167 return NapiGetBoolean(env, false);
168 }
169
170 OHOS::Notification::NotificationLongTextContent *longTextContent =
171 static_cast<OHOS::Notification::NotificationLongTextContent *>(basicContent);
172 if (longTextContent == nullptr) {
173 ANS_LOGE("longTextContent is null");
174 return NapiGetBoolean(env, false);
175 }
176
177 if (!SetNotificationBasicContent(env, longTextContent, result)) {
178 ANS_LOGE("SetNotificationBasicContent call failed");
179 return NapiGetBoolean(env, false);
180 }
181
182 // longText: string
183 napi_create_string_utf8(env, longTextContent->GetLongText().c_str(), NAPI_AUTO_LENGTH, &value);
184 napi_set_named_property(env, result, "longText", value);
185
186 // briefText: string
187 napi_create_string_utf8(env, longTextContent->GetBriefText().c_str(), NAPI_AUTO_LENGTH, &value);
188 napi_set_named_property(env, result, "briefText", value);
189
190 // expandedTitle: string
191 napi_create_string_utf8(env, longTextContent->GetExpandedTitle().c_str(), NAPI_AUTO_LENGTH, &value);
192 napi_set_named_property(env, result, "expandedTitle", value);
193
194 return NapiGetBoolean(env, true);
195 }
196
SetNotificationPictureContent(const napi_env & env,NotificationBasicContent * basicContent,napi_value & result)197 napi_value Common::SetNotificationPictureContent(
198 const napi_env &env, NotificationBasicContent *basicContent, napi_value &result)
199 {
200 ANS_LOGD("enter");
201 napi_value value = nullptr;
202 if (basicContent == nullptr) {
203 ANS_LOGE("basicContent is null");
204 return NapiGetBoolean(env, false);
205 }
206 OHOS::Notification::NotificationPictureContent *pictureContent =
207 static_cast<OHOS::Notification::NotificationPictureContent *>(basicContent);
208 if (pictureContent == nullptr) {
209 ANS_LOGE("pictureContent is null");
210 return NapiGetBoolean(env, false);
211 }
212
213 if (!SetNotificationBasicContent(env, pictureContent, result)) {
214 ANS_LOGE("SetNotificationBasicContent call failed");
215 return NapiGetBoolean(env, false);
216 }
217
218 // briefText: string
219 napi_create_string_utf8(env, pictureContent->GetBriefText().c_str(), NAPI_AUTO_LENGTH, &value);
220 napi_set_named_property(env, result, "briefText", value);
221
222 // expandedTitle: string
223 napi_create_string_utf8(env, pictureContent->GetExpandedTitle().c_str(), NAPI_AUTO_LENGTH, &value);
224 napi_set_named_property(env, result, "expandedTitle", value);
225
226 // picture: image.PixelMap
227 std::shared_ptr<Media::PixelMap> picture = pictureContent->GetBigPicture();
228 if (picture) {
229 napi_value pictureResult = nullptr;
230 napi_valuetype valuetype = napi_undefined;
231 pictureResult = Media::PixelMapNapi::CreatePixelMap(env, picture);
232 NAPI_CALL(env, napi_typeof(env, pictureResult, &valuetype));
233 if (valuetype == napi_undefined) {
234 ANS_LOGW("pictureResult is undefined");
235 napi_set_named_property(env, result, "picture", NapiGetNull(env));
236 } else {
237 napi_set_named_property(env, result, "picture", pictureResult);
238 }
239 }
240 return NapiGetBoolean(env, true);
241 }
242
SetNotificationConversationalContent(const napi_env & env,NotificationBasicContent * basicContent,napi_value & result)243 napi_value Common::SetNotificationConversationalContent(const napi_env &env,
244 NotificationBasicContent *basicContent, napi_value &result)
245 {
246 ANS_LOGD("enter");
247 napi_value value = nullptr;
248 if (basicContent == nullptr) {
249 ANS_LOGE("basicContent is null");
250 return NapiGetBoolean(env, false);
251 }
252 OHOS::Notification::NotificationConversationalContent *conversationalContent =
253 static_cast<OHOS::Notification::NotificationConversationalContent *>(basicContent);
254 if (conversationalContent == nullptr) {
255 ANS_LOGE("conversationalContent is null");
256 return NapiGetBoolean(env, false);
257 }
258
259 if (!SetNotificationBasicContent(env, conversationalContent, result)) {
260 ANS_LOGE("SetNotificationBasicContent call failed");
261 return NapiGetBoolean(env, false);
262 }
263
264 // conversationTitle: string
265 napi_create_string_utf8(env, conversationalContent->GetConversationTitle().c_str(), NAPI_AUTO_LENGTH, &value);
266 napi_set_named_property(env, result, "conversationTitle", value);
267
268 // conversationGroup: boolean
269 napi_get_boolean(env, conversationalContent->IsConversationGroup(), &value);
270 napi_set_named_property(env, result, "conversationGroup", value);
271
272 // messages: Array<ConversationalMessage>
273 napi_value arr = nullptr;
274 if (!SetConversationalMessages(env, conversationalContent, arr)) {
275 ANS_LOGE("SetConversationalMessages call failed");
276 return NapiGetBoolean(env, false);
277 }
278 napi_set_named_property(env, result, "messages", arr);
279
280 // user: MessageUser
281 napi_value messageUserResult = nullptr;
282 napi_create_object(env, &messageUserResult);
283 if (!SetMessageUser(env, conversationalContent->GetMessageUser(), messageUserResult)) {
284 ANS_LOGE("SetMessageUser call failed");
285 return NapiGetBoolean(env, false);
286 }
287 napi_set_named_property(env, result, "user", messageUserResult);
288
289 return NapiGetBoolean(env, true);
290 }
291
SetNotificationMultiLineContent(const napi_env & env,NotificationBasicContent * basicContent,napi_value & result)292 napi_value Common::SetNotificationMultiLineContent(
293 const napi_env &env, NotificationBasicContent *basicContent, napi_value &result)
294 {
295 ANS_LOGD("enter");
296 napi_value value = nullptr;
297 if (basicContent == nullptr) {
298 ANS_LOGE("basicContent is null");
299 return NapiGetBoolean(env, false);
300 }
301 OHOS::Notification::NotificationMultiLineContent *multiLineContent =
302 static_cast<OHOS::Notification::NotificationMultiLineContent *>(basicContent);
303 if (multiLineContent == nullptr) {
304 ANS_LOGE("multiLineContent is null");
305 return NapiGetBoolean(env, false);
306 }
307
308 if (!SetNotificationBasicContent(env, multiLineContent, result)) {
309 ANS_LOGE("SetNotificationBasicContent call failed");
310 return NapiGetBoolean(env, false);
311 }
312
313 // briefText: string
314 napi_create_string_utf8(env, multiLineContent->GetBriefText().c_str(), NAPI_AUTO_LENGTH, &value);
315 napi_set_named_property(env, result, "briefText", value);
316
317 // longTitle: string
318 napi_create_string_utf8(env, multiLineContent->GetExpandedTitle().c_str(), NAPI_AUTO_LENGTH, &value);
319 napi_set_named_property(env, result, "longTitle", value);
320
321 // lines: Array<String>
322 napi_value arr = nullptr;
323 int count = 0;
324 napi_create_array(env, &arr);
325 for (auto vec : multiLineContent->GetAllLines()) {
326 napi_create_string_utf8(env, vec.c_str(), NAPI_AUTO_LENGTH, &value);
327 napi_set_element(env, arr, count, value);
328 count++;
329 }
330 napi_set_named_property(env, result, "lines", arr);
331
332 return NapiGetBoolean(env, true);
333 }
334
SetMessageUser(const napi_env & env,const MessageUser & messageUser,napi_value & result)335 napi_value Common::SetMessageUser(const napi_env &env, const MessageUser &messageUser, napi_value &result)
336 {
337 ANS_LOGD("enter");
338
339 napi_value value = nullptr;
340 // name: string
341 napi_create_string_utf8(env, messageUser.GetName().c_str(), NAPI_AUTO_LENGTH, &value);
342 napi_set_named_property(env, result, "name", value);
343
344 // key: string
345 napi_create_string_utf8(env, messageUser.GetKey().c_str(), NAPI_AUTO_LENGTH, &value);
346 napi_set_named_property(env, result, "key", value);
347
348 // uri: string
349 napi_create_string_utf8(env, messageUser.GetUri().ToString().c_str(), NAPI_AUTO_LENGTH, &value);
350 napi_set_named_property(env, result, "uri", value);
351
352 // isMachine: boolean
353 napi_get_boolean(env, messageUser.IsMachine(), &value);
354 napi_set_named_property(env, result, "isMachine", value);
355
356 // isUserImportant: boolean
357 napi_get_boolean(env, messageUser.IsUserImportant(), &value);
358 napi_set_named_property(env, result, "isUserImportant", value);
359
360 // icon?: image.PixelMap
361 std::shared_ptr<Media::PixelMap> icon = messageUser.GetPixelMap();
362 if (icon) {
363 napi_value iconResult = nullptr;
364 napi_valuetype valuetype = napi_undefined;
365 iconResult = Media::PixelMapNapi::CreatePixelMap(env, icon);
366 NAPI_CALL(env, napi_typeof(env, iconResult, &valuetype));
367 if (valuetype == napi_undefined) {
368 ANS_LOGW("iconResult is undefined");
369 napi_set_named_property(env, result, "icon", NapiGetNull(env));
370 } else {
371 napi_set_named_property(env, result, "icon", iconResult);
372 }
373 }
374 return NapiGetBoolean(env, true);
375 }
376
SetConversationalMessages(const napi_env & env,const OHOS::Notification::NotificationConversationalContent * conversationalContent,napi_value & arr)377 napi_value Common::SetConversationalMessages(const napi_env &env,
378 const OHOS::Notification::NotificationConversationalContent *conversationalContent, napi_value &arr)
379 {
380 ANS_LOGD("enter");
381 if (!conversationalContent) {
382 ANS_LOGE("conversationalContent is null");
383 return NapiGetBoolean(env, false);
384 }
385
386 int count = 0;
387 napi_create_array(env, &arr);
388 std::vector<std::shared_ptr<NotificationConversationalMessage>> messages =
389 conversationalContent->GetAllConversationalMessages();
390 for (auto vec : messages) {
391 if (!vec) {
392 continue;
393 }
394 napi_value conversationalMessageResult = nullptr;
395 napi_create_object(env, &conversationalMessageResult);
396 if (!SetConversationalMessage(env, vec, conversationalMessageResult)) {
397 ANS_LOGE("SetConversationalMessage call failed");
398 return NapiGetBoolean(env, false);
399 }
400 napi_set_element(env, arr, count, conversationalMessageResult);
401 count++;
402 }
403 return NapiGetBoolean(env, true);
404 }
405
SetConversationalMessage(const napi_env & env,const std::shared_ptr<NotificationConversationalMessage> & conversationalMessage,napi_value & result)406 napi_value Common::SetConversationalMessage(const napi_env &env,
407 const std::shared_ptr<NotificationConversationalMessage> &conversationalMessage, napi_value &result)
408 {
409 ANS_LOGD("enter");
410 napi_value value = nullptr;
411 if (conversationalMessage == nullptr) {
412 ANS_LOGE("conversationalMessage is null");
413 return NapiGetBoolean(env, false);
414 }
415
416 // text: string
417 napi_create_string_utf8(env, conversationalMessage->GetText().c_str(), NAPI_AUTO_LENGTH, &value);
418 napi_set_named_property(env, result, "text", value);
419
420 // timestamp: number
421 napi_create_int64(env, conversationalMessage->GetArrivedTime(), &value);
422 napi_set_named_property(env, result, "timestamp", value);
423
424 // sender: MessageUser
425 napi_value messageUserResult = nullptr;
426 napi_create_object(env, &messageUserResult);
427 if (!SetMessageUser(env, conversationalMessage->GetSender(), messageUserResult)) {
428 ANS_LOGE("SetMessageUser call failed");
429 return NapiGetBoolean(env, false);
430 }
431 napi_set_named_property(env, result, "sender", messageUserResult);
432
433 // mimeType: string
434 napi_create_string_utf8(env, conversationalMessage->GetMimeType().c_str(), NAPI_AUTO_LENGTH, &value);
435 napi_set_named_property(env, result, "mimeType", value);
436
437 // uri: string
438 napi_create_string_utf8(env, conversationalMessage->GetUri()->ToString().c_str(), NAPI_AUTO_LENGTH, &value);
439 napi_set_named_property(env, result, "uri", value);
440
441 return NapiGetBoolean(env, true);
442 }
443
GetNotificationContent(const napi_env & env,const napi_value & value,NotificationRequest & request)444 napi_value Common::GetNotificationContent(const napi_env &env, const napi_value &value, NotificationRequest &request)
445 {
446 ANS_LOGD("enter");
447
448 napi_value result = AppExecFwk::GetPropertyValueByPropertyName(env, value, "content", napi_object);
449 if (result == nullptr) {
450 ANS_LOGE("No content.");
451 return nullptr;
452 }
453
454 int32_t type = 0;
455 if (GetNotificationContentType(env, result, type) == nullptr) {
456 return nullptr;
457 }
458 NotificationContent::Type outType = NotificationContent::Type::NONE;
459 if (!ContentTypeJSToC(ContentType(type), outType)) {
460 return nullptr;
461 }
462 switch (outType) {
463 case NotificationContent::Type::BASIC_TEXT:
464 if (GetNotificationBasicContent(env, result, request) == nullptr) {
465 return nullptr;
466 }
467 break;
468 case NotificationContent::Type::LONG_TEXT:
469 if (GetNotificationLongTextContent(env, result, request) == nullptr) {
470 return nullptr;
471 }
472 break;
473 case NotificationContent::Type::PICTURE:
474 if (GetNotificationPictureContent(env, result, request) == nullptr) {
475 return nullptr;
476 }
477 break;
478 case NotificationContent::Type::CONVERSATION:
479 if (GetNotificationConversationalContent(env, result, request) == nullptr) {
480 return nullptr;
481 }
482 break;
483 case NotificationContent::Type::MULTILINE:
484 if (GetNotificationMultiLineContent(env, result, request) == nullptr) {
485 return nullptr;
486 }
487 break;
488 case NotificationContent::Type::LOCAL_LIVE_VIEW:
489 if (GetNotificationLocalLiveViewContent(env, result, request) == nullptr) {
490 return nullptr;
491 }
492 break;
493 case NotificationContent::Type::LIVE_VIEW:
494 if (GetNotificationLiveViewContent(env, result, request) == nullptr) {
495 return nullptr;
496 }
497 break;
498 default:
499 return nullptr;
500 }
501
502 return NapiGetNull(env);
503 }
504
GetNotificationBasicContent(const napi_env & env,const napi_value & result,NotificationRequest & request)505 napi_value Common::GetNotificationBasicContent(
506 const napi_env &env, const napi_value &result, NotificationRequest &request)
507 {
508 ANS_LOGD("enter");
509
510 napi_valuetype valuetype = napi_undefined;
511 napi_value contentResult = nullptr;
512 bool hasProperty = false;
513 NAPI_CALL(env, napi_has_named_property(env, result, "normal", &hasProperty));
514 if (!hasProperty) {
515 ANS_LOGE("Property normal expected.");
516 return nullptr;
517 }
518 napi_get_named_property(env, result, "normal", &contentResult);
519 NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
520 if (valuetype != napi_object) {
521 ANS_LOGE("Wrong argument type. Object expected.");
522 return nullptr;
523 }
524
525 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
526 if (normalContent == nullptr) {
527 ANS_LOGE("normalContent is null");
528 return nullptr;
529 }
530
531 if (GetNotificationBasicContentDetailed(env, contentResult, normalContent) == nullptr) {
532 return nullptr;
533 }
534
535 request.SetContent(std::make_shared<NotificationContent>(normalContent));
536
537 return NapiGetNull(env);
538 }
539
GetNotificationBasicContentDetailed(const napi_env & env,const napi_value & contentResult,std::shared_ptr<NotificationBasicContent> basicContent)540 napi_value Common::GetNotificationBasicContentDetailed(
541 const napi_env &env, const napi_value &contentResult, std::shared_ptr<NotificationBasicContent> basicContent)
542 {
543 ANS_LOGD("enter");
544
545 napi_valuetype valuetype = napi_undefined;
546 napi_value value = nullptr;
547 bool hasProperty = false;
548 char str[STR_MAX_SIZE] = {0};
549 size_t strLen = 0;
550
551 // title: string
552 NAPI_CALL(env, napi_has_named_property(env, contentResult, "title", &hasProperty));
553 if (!hasProperty) {
554 ANS_LOGE("Property title expected.");
555 return nullptr;
556 }
557 napi_get_named_property(env, contentResult, "title", &value);
558 NAPI_CALL(env, napi_typeof(env, value, &valuetype));
559 if (valuetype != napi_string) {
560 ANS_LOGE("Wrong argument type. String expected.");
561 return nullptr;
562 }
563 NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
564 if (std::strlen(str) == 0) {
565 ANS_LOGE("Property title is empty");
566 return nullptr;
567 }
568 basicContent->SetTitle(str);
569 ANS_LOGD("normal::title = %{public}s", str);
570
571 // text: string
572 NAPI_CALL(env, napi_has_named_property(env, contentResult, "text", &hasProperty));
573 if (!hasProperty) {
574 ANS_LOGE("Property text expected.");
575 return nullptr;
576 }
577 napi_get_named_property(env, contentResult, "text", &value);
578 NAPI_CALL(env, napi_typeof(env, value, &valuetype));
579 if (valuetype != napi_string) {
580 ANS_LOGE("Wrong argument type. String expected.");
581 return nullptr;
582 }
583 NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
584 if (std::strlen(str) == 0) {
585 ANS_LOGE("Property text is empty");
586 return nullptr;
587 }
588 basicContent->SetText(str);
589 ANS_LOGD("normal::text = %{public}s", str);
590
591 // additionalText?: string
592 NAPI_CALL(env, napi_has_named_property(env, contentResult, "additionalText", &hasProperty));
593 if (hasProperty) {
594 napi_get_named_property(env, contentResult, "additionalText", &value);
595 NAPI_CALL(env, napi_typeof(env, value, &valuetype));
596 if (valuetype != napi_string) {
597 ANS_LOGE("Wrong argument type. String expected.");
598 return nullptr;
599 }
600 NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
601 basicContent->SetAdditionalText(str);
602 ANS_LOGI("normal::additionalText = %{public}s", str);
603 }
604
605 return NapiGetNull(env);
606 }
607
GetNotificationLongTextContent(const napi_env & env,const napi_value & result,NotificationRequest & request)608 napi_value Common::GetNotificationLongTextContent(
609 const napi_env &env, const napi_value &result, NotificationRequest &request)
610 {
611 ANS_LOGD("enter");
612
613 napi_valuetype valuetype = napi_undefined;
614 napi_value contentResult = nullptr;
615 bool hasProperty = false;
616
617 NAPI_CALL(env, napi_has_named_property(env, result, "longText", &hasProperty));
618 if (!hasProperty) {
619 ANS_LOGE("Property longText expected.");
620 return nullptr;
621 }
622
623 napi_get_named_property(env, result, "longText", &contentResult);
624 NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
625 if (valuetype != napi_object) {
626 ANS_LOGE("Wrong argument type. Object expected.");
627 return nullptr;
628 }
629
630 std::shared_ptr<OHOS::Notification::NotificationLongTextContent> longContent =
631 std::make_shared<OHOS::Notification::NotificationLongTextContent>();
632 if (longContent == nullptr) {
633 ANS_LOGE("longContent is null");
634 return nullptr;
635 }
636
637 if (GetNotificationLongTextContentDetailed(env, contentResult, longContent) == nullptr) {
638 return nullptr;
639 }
640
641 request.SetContent(std::make_shared<NotificationContent>(longContent));
642
643 return NapiGetNull(env);
644 }
645
GetNotificationLongTextContentDetailed(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLongTextContent> & longContent)646 napi_value Common::GetNotificationLongTextContentDetailed(
647 const napi_env &env, const napi_value &contentResult,
648 std::shared_ptr<OHOS::Notification::NotificationLongTextContent> &longContent)
649 {
650 ANS_LOGD("enter");
651
652 napi_valuetype valuetype = napi_undefined;
653 napi_value longContentResult = nullptr;
654 bool hasProperty = false;
655 char str[STR_MAX_SIZE] = {0};
656 char long_str[LONG_STR_MAX_SIZE + 1] = {0};
657 size_t strLen = 0;
658
659 if (GetNotificationBasicContentDetailed(env, contentResult, longContent) == nullptr) {
660 return nullptr;
661 }
662
663 // longText: string
664 NAPI_CALL(env, napi_has_named_property(env, contentResult, "longText", &hasProperty));
665 if (!hasProperty) {
666 ANS_LOGE("Property longText expected.");
667 return nullptr;
668 }
669 napi_get_named_property(env, contentResult, "longText", &longContentResult);
670 NAPI_CALL(env, napi_typeof(env, longContentResult, &valuetype));
671 if (valuetype != napi_string) {
672 ANS_LOGE("Wrong argument type. String expected.");
673 return nullptr;
674 }
675 NAPI_CALL(env, napi_get_value_string_utf8(env, longContentResult, long_str, LONG_STR_MAX_SIZE, &strLen));
676 if (std::strlen(long_str) == 0) {
677 ANS_LOGE("Property longText is empty");
678 return nullptr;
679 }
680 longContent->SetLongText(long_str);
681 ANS_LOGD("longText::longText = %{public}s", long_str);
682
683 // briefText: string
684 NAPI_CALL(env, napi_has_named_property(env, contentResult, "briefText", &hasProperty));
685 if (!hasProperty) {
686 ANS_LOGE("Property briefText expected.");
687 return nullptr;
688 }
689 napi_get_named_property(env, contentResult, "briefText", &longContentResult);
690 NAPI_CALL(env, napi_typeof(env, longContentResult, &valuetype));
691 if (valuetype != napi_string) {
692 ANS_LOGE("Wrong argument type. String expected.");
693 return nullptr;
694 }
695 NAPI_CALL(env, napi_get_value_string_utf8(env, longContentResult, str, STR_MAX_SIZE - 1, &strLen));
696 if (std::strlen(str) == 0) {
697 ANS_LOGE("Property briefText is empty");
698 return nullptr;
699 }
700 longContent->SetBriefText(str);
701 ANS_LOGD("longText::briefText = %{public}s", str);
702
703 // expandedTitle: string
704 NAPI_CALL(env, napi_has_named_property(env, contentResult, "expandedTitle", &hasProperty));
705 if (!hasProperty) {
706 ANS_LOGE("Property expandedTitle expected.");
707 return nullptr;
708 }
709 napi_get_named_property(env, contentResult, "expandedTitle", &longContentResult);
710 NAPI_CALL(env, napi_typeof(env, longContentResult, &valuetype));
711 if (valuetype != napi_string) {
712 ANS_LOGE("Wrong argument type. String expected.");
713 return nullptr;
714 }
715 NAPI_CALL(env, napi_get_value_string_utf8(env, longContentResult, str, STR_MAX_SIZE - 1, &strLen));
716 if (std::strlen(str) == 0) {
717 ANS_LOGE("Property expandedTitle is empty");
718 return nullptr;
719 }
720 longContent->SetExpandedTitle(str);
721 ANS_LOGD("longText::expandedTitle = %{public}s", str);
722
723 return NapiGetNull(env);
724 }
725
GetNotificationPictureContent(const napi_env & env,const napi_value & result,NotificationRequest & request)726 napi_value Common::GetNotificationPictureContent(
727 const napi_env &env, const napi_value &result, NotificationRequest &request)
728 {
729 ANS_LOGD("enter");
730
731 napi_valuetype valuetype = napi_undefined;
732 napi_value contentResult = nullptr;
733 bool hasProperty = false;
734
735 NAPI_CALL(env, napi_has_named_property(env, result, "picture", &hasProperty));
736 if (!hasProperty) {
737 ANS_LOGE("Property picture expected.");
738 return nullptr;
739 }
740 napi_get_named_property(env, result, "picture", &contentResult);
741 NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
742 if (valuetype != napi_object) {
743 ANS_LOGE("Wrong argument type. Object expected.");
744 return nullptr;
745 }
746
747 std::shared_ptr<OHOS::Notification::NotificationPictureContent> pictureContent =
748 std::make_shared<OHOS::Notification::NotificationPictureContent>();
749 if (pictureContent == nullptr) {
750 ANS_LOGE("pictureContent is null");
751 return nullptr;
752 }
753 if (GetNotificationPictureContentDetailed(env, contentResult, pictureContent) == nullptr) {
754 return nullptr;
755 }
756
757 request.SetContent(std::make_shared<NotificationContent>(pictureContent));
758
759 return NapiGetNull(env);
760 }
761
GetNotificationPictureContentDetailed(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationPictureContent> & pictureContent)762 napi_value Common::GetNotificationPictureContentDetailed(const napi_env &env,
763 const napi_value &contentResult, std::shared_ptr<OHOS::Notification::NotificationPictureContent> &pictureContent)
764 {
765 ANS_LOGD("enter");
766
767 napi_valuetype valuetype = napi_undefined;
768 napi_value pictureContentResult = nullptr;
769 bool hasProperty = false;
770 char str[STR_MAX_SIZE] = {0};
771 size_t strLen = 0;
772
773 if (GetNotificationBasicContentDetailed(env, contentResult, pictureContent) == nullptr) {
774 return nullptr;
775 }
776
777 // briefText: string
778 NAPI_CALL(env, napi_has_named_property(env, contentResult, "briefText", &hasProperty));
779 if (!hasProperty) {
780 ANS_LOGE("Property briefText expected.");
781 return nullptr;
782 }
783 napi_get_named_property(env, contentResult, "briefText", &pictureContentResult);
784 NAPI_CALL(env, napi_typeof(env, pictureContentResult, &valuetype));
785 if (valuetype != napi_string) {
786 ANS_LOGE("Wrong argument type. String expected.");
787 return nullptr;
788 }
789 NAPI_CALL(env, napi_get_value_string_utf8(env, pictureContentResult, str, STR_MAX_SIZE - 1, &strLen));
790 if (std::strlen(str) == 0) {
791 ANS_LOGE("Property briefText is empty");
792 return nullptr;
793 }
794 pictureContent->SetBriefText(str);
795
796 // expandedTitle: string
797 NAPI_CALL(env, napi_has_named_property(env, contentResult, "expandedTitle", &hasProperty));
798 if (!hasProperty) {
799 ANS_LOGE("Property expandedTitle expected.");
800 return nullptr;
801 }
802 napi_get_named_property(env, contentResult, "expandedTitle", &pictureContentResult);
803 NAPI_CALL(env, napi_typeof(env, pictureContentResult, &valuetype));
804 if (valuetype != napi_string) {
805 ANS_LOGE("Wrong argument type. String expected.");
806 return nullptr;
807 }
808 NAPI_CALL(env, napi_get_value_string_utf8(env, pictureContentResult, str, STR_MAX_SIZE - 1, &strLen));
809 if (std::strlen(str) == 0) {
810 ANS_LOGE("Property expandedTitle is empty");
811 return nullptr;
812 }
813 pictureContent->SetExpandedTitle(str);
814
815 // picture: image.PixelMap
816 NAPI_CALL(env, napi_has_named_property(env, contentResult, "picture", &hasProperty));
817 if (!hasProperty) {
818 ANS_LOGE("Property picture expected.");
819 return nullptr;
820 }
821 napi_get_named_property(env, contentResult, "picture", &pictureContentResult);
822 NAPI_CALL(env, napi_typeof(env, pictureContentResult, &valuetype));
823 if (valuetype != napi_object) {
824 ANS_LOGE("Wrong argument type. Object expected.");
825 return nullptr;
826 }
827 std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
828 pixelMap = Media::PixelMapNapi::GetPixelMap(env, pictureContentResult);
829 if (pixelMap == nullptr) {
830 ANS_LOGE("Invalid object pixelMap");
831 return nullptr;
832 }
833 pictureContent->SetBigPicture(pixelMap);
834
835 return Common::NapiGetNull(env);
836 }
837
GetNotificationConversationalContent(const napi_env & env,const napi_value & result,NotificationRequest & request)838 napi_value Common::GetNotificationConversationalContent(
839 const napi_env &env, const napi_value &result, NotificationRequest &request)
840 {
841 ANS_LOGD("enter");
842
843 napi_valuetype valuetype = napi_undefined;
844 napi_value contentResult = nullptr;
845 bool hasProperty = false;
846 MessageUser user;
847
848 NAPI_CALL(env, napi_has_named_property(env, result, "conversation", &hasProperty));
849 if (!hasProperty) {
850 ANS_LOGE("Property conversation expected.");
851 return nullptr;
852 }
853 napi_get_named_property(env, result, "conversation", &contentResult);
854 NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
855 if (valuetype != napi_object) {
856 ANS_LOGE("Wrong argument type. Object expected.");
857 return nullptr;
858 }
859
860 if (GetNotificationConversationalContentByUser(env, contentResult, user) == nullptr) {
861 return nullptr;
862 }
863
864 std::shared_ptr<OHOS::Notification::NotificationConversationalContent> conversationalContent =
865 std::make_shared<OHOS::Notification::NotificationConversationalContent>(user);
866 if (conversationalContent == nullptr) {
867 ANS_LOGE("conversationalContent is null");
868 return nullptr;
869 }
870
871 if (GetNotificationBasicContentDetailed(env, contentResult, conversationalContent) == nullptr) {
872 return nullptr;
873 }
874 if (GetNotificationConversationalContentTitle(env, contentResult, conversationalContent) == nullptr) {
875 return nullptr;
876 }
877 if (GetNotificationConversationalContentGroup(env, contentResult, conversationalContent) == nullptr) {
878 return nullptr;
879 }
880 if (GetNotificationConversationalContentMessages(env, contentResult, conversationalContent) == nullptr) {
881 return nullptr;
882 }
883
884 request.SetContent(std::make_shared<NotificationContent>(conversationalContent));
885
886 return NapiGetNull(env);
887 }
888
GetNotificationConversationalContentByUser(const napi_env & env,const napi_value & contentResult,MessageUser & user)889 napi_value Common::GetNotificationConversationalContentByUser(
890 const napi_env &env, const napi_value &contentResult, MessageUser &user)
891 {
892 ANS_LOGD("enter");
893
894 napi_valuetype valuetype = napi_undefined;
895 bool hasProperty = false;
896
897 // user: MessageUser
898 NAPI_CALL(env, napi_has_named_property(env, contentResult, "user", &hasProperty));
899 if (!hasProperty) {
900 ANS_LOGE("Property user expected.");
901 return nullptr;
902 }
903 napi_value userResult = nullptr;
904 napi_get_named_property(env, contentResult, "user", &userResult);
905 NAPI_CALL(env, napi_typeof(env, userResult, &valuetype));
906 if (valuetype != napi_object) {
907 ANS_LOGE("Wrong argument type. Object expected.");
908 return nullptr;
909 }
910 if (!GetMessageUser(env, userResult, user)) {
911 return nullptr;
912 }
913
914 return NapiGetNull(env);
915 }
916
GetMessageUser(const napi_env & env,const napi_value & result,MessageUser & messageUser)917 napi_value Common::GetMessageUser(const napi_env &env, const napi_value &result, MessageUser &messageUser)
918 {
919 ANS_LOGD("enter");
920
921 if (GetMessageUserByString(env, result, messageUser) == nullptr) {
922 return nullptr;
923 }
924
925 if (GetMessageUserByBool(env, result, messageUser) == nullptr) {
926 return nullptr;
927 }
928
929 if (GetMessageUserByCustom(env, result, messageUser) == nullptr) {
930 return nullptr;
931 }
932
933 return NapiGetNull(env);
934 }
935
GetMessageUserByString(const napi_env & env,const napi_value & result,MessageUser & messageUser)936 napi_value Common::GetMessageUserByString(const napi_env &env, const napi_value &result, MessageUser &messageUser)
937 {
938 ANS_LOGD("enter");
939
940 napi_valuetype valuetype = napi_undefined;
941 bool hasProperty = false;
942 char str[STR_MAX_SIZE] = {0};
943 size_t strLen = 0;
944
945 // name: string
946 NAPI_CALL(env, napi_has_named_property(env, result, "name", &hasProperty));
947 if (!hasProperty) {
948 ANS_LOGE("Property name expected.");
949 return nullptr;
950 }
951 napi_value nameResult = nullptr;
952 napi_get_named_property(env, result, "name", &nameResult);
953 NAPI_CALL(env, napi_typeof(env, nameResult, &valuetype));
954 if (valuetype != napi_string) {
955 ANS_LOGE("Wrong argument type. String expected.");
956 return nullptr;
957 }
958 NAPI_CALL(env, napi_get_value_string_utf8(env, nameResult, str, STR_MAX_SIZE - 1, &strLen));
959 messageUser.SetName(str);
960 ANS_LOGI("MessageUser::name = %{public}s", str);
961
962 // key: string
963 NAPI_CALL(env, napi_has_named_property(env, result, "key", &hasProperty));
964 if (!hasProperty) {
965 ANS_LOGE("Property key expected.");
966 return nullptr;
967 }
968 napi_value keyResult = nullptr;
969 napi_get_named_property(env, result, "key", &keyResult);
970 NAPI_CALL(env, napi_typeof(env, keyResult, &valuetype));
971 if (valuetype != napi_string) {
972 ANS_LOGE("Wrong argument type. String expected.");
973 return nullptr;
974 }
975 NAPI_CALL(env, napi_get_value_string_utf8(env, keyResult, str, STR_MAX_SIZE - 1, &strLen));
976 messageUser.SetKey(str);
977 ANS_LOGI("MessageUser::key = %{public}s", str);
978
979 // uri: string
980 NAPI_CALL(env, napi_has_named_property(env, result, "uri", &hasProperty));
981 if (!hasProperty) {
982 ANS_LOGE("Property uri expected.");
983 return nullptr;
984 }
985 napi_value uriResult = nullptr;
986 napi_get_named_property(env, result, "uri", &uriResult);
987 NAPI_CALL(env, napi_typeof(env, uriResult, &valuetype));
988 if (valuetype != napi_string) {
989 ANS_LOGE("Wrong argument type. String expected.");
990 return nullptr;
991 }
992 NAPI_CALL(env, napi_get_value_string_utf8(env, uriResult, str, STR_MAX_SIZE - 1, &strLen));
993 Uri uri(str);
994 messageUser.SetUri(uri);
995
996 return NapiGetNull(env);
997 }
998
GetMessageUserByBool(const napi_env & env,const napi_value & result,MessageUser & messageUser)999 napi_value Common::GetMessageUserByBool(const napi_env &env, const napi_value &result, MessageUser &messageUser)
1000 {
1001 ANS_LOGD("enter");
1002
1003 napi_valuetype valuetype = napi_undefined;
1004 bool hasProperty = false;
1005
1006 // isMachine: boolean
1007 NAPI_CALL(env, napi_has_named_property(env, result, "isMachine", &hasProperty));
1008 if (!hasProperty) {
1009 ANS_LOGE("Property isMachine expected.");
1010 return nullptr;
1011 }
1012 napi_value machineResult = nullptr;
1013 napi_get_named_property(env, result, "isMachine", &machineResult);
1014 NAPI_CALL(env, napi_typeof(env, machineResult, &valuetype));
1015 if (valuetype != napi_boolean) {
1016 ANS_LOGE("Wrong argument type. Bool expected.");
1017 return nullptr;
1018 }
1019 bool machine = false;
1020 napi_get_value_bool(env, machineResult, &machine);
1021 messageUser.SetMachine(machine);
1022 ANS_LOGD("MessageUser::isMachine = %{public}d", machine);
1023
1024 // isUserImportant: boolean
1025 NAPI_CALL(env, napi_has_named_property(env, result, "isUserImportant", &hasProperty));
1026 if (!hasProperty) {
1027 ANS_LOGE("Property isUserImportant expected.");
1028 return nullptr;
1029 }
1030 napi_value importantResult = nullptr;
1031 napi_get_named_property(env, result, "isUserImportant", &importantResult);
1032 NAPI_CALL(env, napi_typeof(env, importantResult, &valuetype));
1033 if (valuetype != napi_boolean) {
1034 ANS_LOGE("Wrong argument type. Bool expected.");
1035 return nullptr;
1036 }
1037 bool important = false;
1038 napi_get_value_bool(env, importantResult, &important);
1039 messageUser.SetUserAsImportant(important);
1040 ANS_LOGI("MessageUser::isUserImportant = %{public}d", important);
1041
1042 return NapiGetNull(env);
1043 }
1044
GetMessageUserByCustom(const napi_env & env,const napi_value & result,MessageUser & messageUser)1045 napi_value Common::GetMessageUserByCustom(const napi_env &env, const napi_value &result, MessageUser &messageUser)
1046 {
1047 ANS_LOGD("enter");
1048
1049 napi_valuetype valuetype = napi_undefined;
1050 bool hasProperty = false;
1051
1052 // icon?: image.PixelMap
1053 NAPI_CALL(env, napi_has_named_property(env, result, "icon", &hasProperty));
1054 if (hasProperty) {
1055 napi_value iconResult = nullptr;
1056 napi_get_named_property(env, result, "icon", &iconResult);
1057 NAPI_CALL(env, napi_typeof(env, iconResult, &valuetype));
1058 if (valuetype != napi_object) {
1059 ANS_LOGE("Wrong argument type. Object expected.");
1060 return nullptr;
1061 }
1062 std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
1063 pixelMap = Media::PixelMapNapi::GetPixelMap(env, iconResult);
1064 if (pixelMap == nullptr) {
1065 ANS_LOGE("Invalid object pixelMap");
1066 return nullptr;
1067 }
1068 messageUser.SetPixelMap(pixelMap);
1069 }
1070
1071 return NapiGetNull(env);
1072 }
1073
GetNotificationConversationalContentTitle(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationConversationalContent> & conversationalContent)1074 napi_value Common::GetNotificationConversationalContentTitle(
1075 const napi_env &env, const napi_value &contentResult,
1076 std::shared_ptr<OHOS::Notification::NotificationConversationalContent> &conversationalContent)
1077 {
1078 ANS_LOGD("enter");
1079 napi_valuetype valuetype = napi_undefined;
1080 napi_value conversationalContentResult = nullptr;
1081 bool hasProperty = false;
1082 char str[STR_MAX_SIZE] = {0};
1083 size_t strLen = 0;
1084
1085 // conversationTitle: string
1086 NAPI_CALL(env, napi_has_named_property(env, contentResult, "conversationTitle", &hasProperty));
1087 if (!hasProperty) {
1088 ANS_LOGE("Property conversationTitle expected.");
1089 return nullptr;
1090 }
1091 napi_get_named_property(env, contentResult, "conversationTitle", &conversationalContentResult);
1092 NAPI_CALL(env, napi_typeof(env, conversationalContentResult, &valuetype));
1093 if (valuetype != napi_string) {
1094 ANS_LOGE("Wrong argument type. String expected.");
1095 return nullptr;
1096 }
1097 NAPI_CALL(env, napi_get_value_string_utf8(env, conversationalContentResult, str, STR_MAX_SIZE - 1, &strLen));
1098 conversationalContent->SetConversationTitle(str);
1099 ANS_LOGD("conversationTitle = %{public}s", str);
1100
1101 return NapiGetNull(env);
1102 }
1103
GetNotificationConversationalContentGroup(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationConversationalContent> & conversationalContent)1104 napi_value Common::GetNotificationConversationalContentGroup(
1105 const napi_env &env, const napi_value &contentResult,
1106 std::shared_ptr<OHOS::Notification::NotificationConversationalContent> &conversationalContent)
1107 {
1108 ANS_LOGD("enter");
1109 napi_valuetype valuetype = napi_undefined;
1110 napi_value conversationalContentResult = nullptr;
1111 bool hasProperty = false;
1112
1113 // conversationGroup: boolean
1114 NAPI_CALL(env, napi_has_named_property(env, contentResult, "conversationGroup", &hasProperty));
1115 if (!hasProperty) {
1116 ANS_LOGE("Property conversationGroup expected.");
1117 return nullptr;
1118 }
1119 napi_get_named_property(env, contentResult, "conversationGroup", &conversationalContentResult);
1120 NAPI_CALL(env, napi_typeof(env, conversationalContentResult, &valuetype));
1121 if (valuetype != napi_boolean) {
1122 ANS_LOGE("Wrong argument type. Bool expected.");
1123 return nullptr;
1124 }
1125 bool conversationGroup = false;
1126 napi_get_value_bool(env, conversationalContentResult, &conversationGroup);
1127 conversationalContent->SetConversationGroup(conversationGroup);
1128 ANS_LOGI("conversationalText::conversationGroup = %{public}d", conversationGroup);
1129
1130 return NapiGetNull(env);
1131 }
1132
GetNotificationConversationalContentMessages(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationConversationalContent> & conversationalContent)1133 napi_value Common::GetNotificationConversationalContentMessages(
1134 const napi_env &env, const napi_value &contentResult,
1135 std::shared_ptr<OHOS::Notification::NotificationConversationalContent> &conversationalContent)
1136 {
1137 ANS_LOGD("enter");
1138 napi_valuetype valuetype = napi_undefined;
1139 napi_value conversationalContentResult = nullptr;
1140 bool hasProperty = false;
1141
1142 // messages: Array<ConversationalMessage>
1143 NAPI_CALL(env, napi_has_named_property(env, contentResult, "messages", &hasProperty));
1144 if (!hasProperty) {
1145 ANS_LOGE("Property messages expected.");
1146 return nullptr;
1147 }
1148 napi_get_named_property(env, contentResult, "messages", &conversationalContentResult);
1149 bool isArray = false;
1150 napi_is_array(env, conversationalContentResult, &isArray);
1151 if (!isArray) {
1152 ANS_LOGE("Property messages is expected to be an array.");
1153 return nullptr;
1154 }
1155 uint32_t length = 0;
1156 napi_get_array_length(env, conversationalContentResult, &length);
1157 if (length == 0) {
1158 ANS_LOGE("The array is empty.");
1159 return nullptr;
1160 }
1161 for (size_t i = 0; i < length; i++) {
1162 napi_value conversationalMessage = nullptr;
1163 napi_get_element(env, conversationalContentResult, i, &conversationalMessage);
1164 NAPI_CALL(env, napi_typeof(env, conversationalMessage, &valuetype));
1165 if (valuetype != napi_object) {
1166 ANS_LOGE("Wrong argument type. Object expected.");
1167 return nullptr;
1168 }
1169 std::shared_ptr<NotificationConversationalMessage> message = nullptr;
1170 if (!GetConversationalMessage(env, conversationalMessage, message)) {
1171 return nullptr;
1172 }
1173 conversationalContent->AddConversationalMessage(message);
1174 }
1175
1176 return NapiGetNull(env);
1177 }
1178
GetConversationalMessage(const napi_env & env,const napi_value & conversationalMessage,std::shared_ptr<NotificationConversationalMessage> & message)1179 napi_value Common::GetConversationalMessage(const napi_env &env, const napi_value &conversationalMessage,
1180 std::shared_ptr<NotificationConversationalMessage> &message)
1181 {
1182 ANS_LOGD("enter");
1183
1184 if (GetConversationalMessageBasicInfo(env, conversationalMessage, message) == nullptr) {
1185 return nullptr;
1186 }
1187 if (GetConversationalMessageOtherInfo(env, conversationalMessage, message) == nullptr) {
1188 return nullptr;
1189 }
1190 return NapiGetNull(env);
1191 }
1192
GetConversationalMessageBasicInfo(const napi_env & env,const napi_value & conversationalMessage,std::shared_ptr<NotificationConversationalMessage> & message)1193 napi_value Common::GetConversationalMessageBasicInfo(const napi_env &env, const napi_value &conversationalMessage,
1194 std::shared_ptr<NotificationConversationalMessage> &message)
1195 {
1196 ANS_LOGD("enter");
1197
1198 napi_valuetype valuetype = napi_undefined;
1199 bool hasProperty = false;
1200 char str[STR_MAX_SIZE] = {0};
1201 size_t strLen = 0;
1202 std::string text;
1203 int64_t timestamp = 0;
1204 MessageUser sender;
1205
1206 // text: string
1207 NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "text", &hasProperty));
1208 if (!hasProperty) {
1209 ANS_LOGE("Property text expected.");
1210 return nullptr;
1211 }
1212 napi_value textResult = nullptr;
1213 napi_get_named_property(env, conversationalMessage, "text", &textResult);
1214 NAPI_CALL(env, napi_typeof(env, textResult, &valuetype));
1215 if (valuetype != napi_string) {
1216 ANS_LOGE("Wrong argument type. String expected.");
1217 return nullptr;
1218 }
1219 NAPI_CALL(env, napi_get_value_string_utf8(env, textResult, str, STR_MAX_SIZE - 1, &strLen));
1220 text = str;
1221 ANS_LOGI("conversationalMessage::text = %{public}s", str);
1222
1223 // timestamp: number
1224 NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "timestamp", &hasProperty));
1225 if (!hasProperty) {
1226 ANS_LOGE("Property timestamp expected.");
1227 return nullptr;
1228 }
1229 napi_value timestampResult = nullptr;
1230 napi_get_named_property(env, conversationalMessage, "timestamp", ×tampResult);
1231 NAPI_CALL(env, napi_typeof(env, timestampResult, &valuetype));
1232 if (valuetype != napi_number) {
1233 ANS_LOGE("Wrong argument type. Number expected.");
1234 return nullptr;
1235 }
1236 napi_get_value_int64(env, timestampResult, ×tamp);
1237 ANS_LOGI("conversationalMessage::timestamp = %{public}" PRId64, timestamp);
1238
1239 // sender: MessageUser
1240 NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "sender", &hasProperty));
1241 if (!hasProperty) {
1242 ANS_LOGE("Property sender expected.");
1243 return nullptr;
1244 }
1245 napi_value senderResult = nullptr;
1246 napi_get_named_property(env, conversationalMessage, "sender", &senderResult);
1247 NAPI_CALL(env, napi_typeof(env, senderResult, &valuetype));
1248 if (valuetype != napi_object) {
1249 ANS_LOGE("Wrong argument type. Object expected.");
1250 return nullptr;
1251 }
1252 if (!GetMessageUser(env, senderResult, sender)) {
1253 return nullptr;
1254 }
1255
1256 message = std::make_shared<NotificationConversationalMessage>(text, timestamp, sender);
1257 if (!message) {
1258 ANS_LOGE("Failed to create NotificationConversationalMessage object");
1259 return nullptr;
1260 }
1261
1262 return NapiGetNull(env);
1263 }
1264
GetConversationalMessageOtherInfo(const napi_env & env,const napi_value & conversationalMessage,std::shared_ptr<NotificationConversationalMessage> & message)1265 napi_value Common::GetConversationalMessageOtherInfo(const napi_env &env, const napi_value &conversationalMessage,
1266 std::shared_ptr<NotificationConversationalMessage> &message)
1267 {
1268 ANS_LOGD("enter");
1269
1270 napi_valuetype valuetype = napi_undefined;
1271 bool hasProperty = false;
1272 char str[STR_MAX_SIZE] = {0};
1273 size_t strLen = 0;
1274 std::string mimeType;
1275 std::string uri;
1276
1277 // mimeType: string
1278 NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "mimeType", &hasProperty));
1279 if (!hasProperty) {
1280 ANS_LOGE("Property mimeType expected.");
1281 return nullptr;
1282 }
1283 napi_value mimeTypeResult = nullptr;
1284 napi_get_named_property(env, conversationalMessage, "mimeType", &mimeTypeResult);
1285 NAPI_CALL(env, napi_typeof(env, mimeTypeResult, &valuetype));
1286 if (valuetype != napi_string) {
1287 ANS_LOGE("Wrong argument type. String expected.");
1288 return nullptr;
1289 }
1290 NAPI_CALL(env, napi_get_value_string_utf8(env, mimeTypeResult, str, STR_MAX_SIZE - 1, &strLen));
1291 mimeType = str;
1292 ANS_LOGI("conversationalMessage::mimeType = %{public}s", str);
1293
1294 // uri?: string
1295 NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "uri", &hasProperty));
1296 if (hasProperty) {
1297 napi_value uriResult = nullptr;
1298 napi_get_named_property(env, conversationalMessage, "uri", &uriResult);
1299 NAPI_CALL(env, napi_typeof(env, uriResult, &valuetype));
1300 if (valuetype != napi_string) {
1301 ANS_LOGE("Wrong argument type. String expected.");
1302 return nullptr;
1303 }
1304 NAPI_CALL(env, napi_get_value_string_utf8(env, uriResult, str, STR_MAX_SIZE - 1, &strLen));
1305 uri = str;
1306 }
1307
1308 std::shared_ptr<Uri> uriPtr = std::make_shared<Uri>(uri);
1309 message->SetData(mimeType, uriPtr);
1310
1311 return NapiGetNull(env);
1312 }
1313
GetNotificationMultiLineContent(const napi_env & env,const napi_value & result,NotificationRequest & request)1314 napi_value Common::GetNotificationMultiLineContent(
1315 const napi_env &env, const napi_value &result, NotificationRequest &request)
1316 {
1317 ANS_LOGD("enter");
1318
1319 napi_valuetype valuetype = napi_undefined;
1320 napi_value contentResult = nullptr;
1321 napi_value multiLineContentResult = nullptr;
1322 bool hasProperty = false;
1323 char str[STR_MAX_SIZE] = {0};
1324 size_t strLen = 0;
1325
1326 NAPI_CALL(env, napi_has_named_property(env, result, "multiLine", &hasProperty));
1327 if (!hasProperty) {
1328 ANS_LOGE("Property multiLine expected.");
1329 return nullptr;
1330 }
1331 napi_get_named_property(env, result, "multiLine", &contentResult);
1332 NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
1333 if (valuetype != napi_object) {
1334 ANS_LOGE("Wrong argument type. Object expected.");
1335 return nullptr;
1336 }
1337
1338 std::shared_ptr<OHOS::Notification::NotificationMultiLineContent> multiLineContent =
1339 std::make_shared<OHOS::Notification::NotificationMultiLineContent>();
1340 if (multiLineContent == nullptr) {
1341 ANS_LOGE("multiLineContent is null");
1342 return nullptr;
1343 }
1344
1345 if (GetNotificationBasicContentDetailed(env, contentResult, multiLineContent) == nullptr) {
1346 return nullptr;
1347 }
1348
1349 // briefText: string
1350 NAPI_CALL(env, napi_has_named_property(env, contentResult, "briefText", &hasProperty));
1351 if (!hasProperty) {
1352 ANS_LOGE("Property briefText expected.");
1353 return nullptr;
1354 }
1355 napi_get_named_property(env, contentResult, "briefText", &multiLineContentResult);
1356 NAPI_CALL(env, napi_typeof(env, multiLineContentResult, &valuetype));
1357 if (valuetype != napi_string) {
1358 ANS_LOGE("Wrong argument type. String expected.");
1359 return nullptr;
1360 }
1361 NAPI_CALL(env, napi_get_value_string_utf8(env, multiLineContentResult, str, STR_MAX_SIZE - 1, &strLen));
1362 if (std::strlen(str) == 0) {
1363 ANS_LOGE("Property briefText is empty");
1364 return nullptr;
1365 }
1366 multiLineContent->SetBriefText(str);
1367 ANS_LOGD("multiLine: briefText = %{public}s", str);
1368
1369 // longTitle: string
1370 NAPI_CALL(env, napi_has_named_property(env, contentResult, "longTitle", &hasProperty));
1371 if (!hasProperty) {
1372 ANS_LOGE("Property longTitle expected.");
1373 return nullptr;
1374 }
1375 napi_get_named_property(env, contentResult, "longTitle", &multiLineContentResult);
1376 NAPI_CALL(env, napi_typeof(env, multiLineContentResult, &valuetype));
1377 if (valuetype != napi_string) {
1378 ANS_LOGE("Wrong argument type. String expected.");
1379 return nullptr;
1380 }
1381 NAPI_CALL(env, napi_get_value_string_utf8(env, multiLineContentResult, str, STR_MAX_SIZE - 1, &strLen));
1382 if (std::strlen(str) == 0) {
1383 ANS_LOGE("Property longTitle is empty");
1384 return nullptr;
1385 }
1386 multiLineContent->SetExpandedTitle(str);
1387 ANS_LOGD("multiLine: longTitle = %{public}s", str);
1388
1389 // lines: Array<String>
1390 NAPI_CALL(env, napi_has_named_property(env, contentResult, "lines", &hasProperty));
1391 if (!hasProperty) {
1392 ANS_LOGE("Property lines expected.");
1393 return nullptr;
1394 }
1395 if (GetNotificationMultiLineContentLines(env, contentResult, multiLineContent) == nullptr) {
1396 return nullptr;
1397 }
1398
1399 request.SetContent(std::make_shared<NotificationContent>(multiLineContent));
1400
1401 ANS_LOGD("end");
1402 return NapiGetNull(env);
1403 }
1404
GetNotificationMultiLineContentLines(const napi_env & env,const napi_value & result,std::shared_ptr<OHOS::Notification::NotificationMultiLineContent> & multiLineContent)1405 napi_value Common::GetNotificationMultiLineContentLines(const napi_env &env, const napi_value &result,
1406 std::shared_ptr<OHOS::Notification::NotificationMultiLineContent> &multiLineContent)
1407 {
1408 ANS_LOGD("enter");
1409
1410 bool isArray = false;
1411 napi_valuetype valuetype = napi_undefined;
1412 napi_value multilines = nullptr;
1413 char str[STR_MAX_SIZE] = {0};
1414 size_t strLen = 0;
1415 uint32_t length = 0;
1416
1417 napi_get_named_property(env, result, "lines", &multilines);
1418 napi_is_array(env, multilines, &isArray);
1419 if (!isArray) {
1420 ANS_LOGE("Property lines is expected to be an array.");
1421 return nullptr;
1422 }
1423
1424 napi_get_array_length(env, multilines, &length);
1425 if (length == 0) {
1426 ANS_LOGE("The array is empty.");
1427 return nullptr;
1428 }
1429 for (size_t i = 0; i < length; i++) {
1430 napi_value line = nullptr;
1431 napi_get_element(env, multilines, i, &line);
1432 NAPI_CALL(env, napi_typeof(env, line, &valuetype));
1433 if (valuetype != napi_string) {
1434 ANS_LOGE("Wrong argument type. String expected.");
1435 return nullptr;
1436 }
1437 NAPI_CALL(env, napi_get_value_string_utf8(env, line, str, STR_MAX_SIZE - 1, &strLen));
1438 multiLineContent->AddSingleLine(str);
1439 ANS_LOGI("multiLine: lines : addSingleLine = %{public}s", str);
1440 }
1441
1442 return NapiGetNull(env);
1443 }
1444 }
1445 }
1446