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