• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 str[STR_MAX_SIZE] = {0};
548     size_t strLen = 0;
549 
550     // title: string
551     auto value = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult, "title", napi_string);
552     if (value == nullptr) {
553         ANS_LOGE("Failed to get title from js.");
554         return nullptr;
555     }
556     NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
557     if (std::strlen(str) == 0) {
558         ANS_LOGE("Property title is empty");
559         return nullptr;
560     }
561     basicContent->SetTitle(str);
562     ANS_LOGD("normal::title = %{public}s", str);
563 
564     // text: string
565     value = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult, "text", napi_string);
566     if (value == nullptr) {
567         ANS_LOGE("Failed to get text from js.");
568         return nullptr;
569     }
570     NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
571     if (std::strlen(str) == 0) {
572         ANS_LOGE("Property text is empty");
573         return nullptr;
574     }
575     basicContent->SetText(str);
576     ANS_LOGD("normal::text = %{public}s", str);
577 
578     // additionalText?: string
579     NAPI_CALL(env, napi_has_named_property(env, contentResult, "additionalText", &hasProperty));
580     if (hasProperty) {
581         value = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult, "additionalText", napi_string);
582         if (value == nullptr) {
583             ANS_LOGE("Failed to get additionalText from js.");
584             return nullptr;
585         }
586         NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
587         basicContent->SetAdditionalText(str);
588         ANS_LOGD("normal::additionalText = %{public}s", str);
589     }
590 
591     // lockScreenPicture?: pixelMap
592     return GetLockScreenPicture(env, contentResult, basicContent);
593 }
594 
GetNotificationLongTextContent(const napi_env & env,const napi_value & result,NotificationRequest & request)595 napi_value Common::GetNotificationLongTextContent(
596     const napi_env &env, const napi_value &result, NotificationRequest &request)
597 {
598     ANS_LOGD("enter");
599 
600     napi_valuetype valuetype = napi_undefined;
601     napi_value contentResult = nullptr;
602     bool hasProperty = false;
603 
604     NAPI_CALL(env, napi_has_named_property(env, result, "longText", &hasProperty));
605     if (!hasProperty) {
606         ANS_LOGE("Property longText expected.");
607         return nullptr;
608     }
609 
610     napi_get_named_property(env, result, "longText", &contentResult);
611     NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
612     if (valuetype != napi_object) {
613         ANS_LOGE("Wrong argument type. Object expected.");
614         return nullptr;
615     }
616 
617     std::shared_ptr<OHOS::Notification::NotificationLongTextContent> longContent =
618         std::make_shared<OHOS::Notification::NotificationLongTextContent>();
619     if (longContent == nullptr) {
620         ANS_LOGE("longContent is null");
621         return nullptr;
622     }
623 
624     if (GetNotificationLongTextContentDetailed(env, contentResult, longContent) == nullptr) {
625         return nullptr;
626     }
627 
628     request.SetContent(std::make_shared<NotificationContent>(longContent));
629 
630     return NapiGetNull(env);
631 }
632 
GetNotificationLongTextContentDetailed(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLongTextContent> & longContent)633 napi_value Common::GetNotificationLongTextContentDetailed(
634     const napi_env &env, const napi_value &contentResult,
635     std::shared_ptr<OHOS::Notification::NotificationLongTextContent> &longContent)
636 {
637     ANS_LOGD("enter");
638 
639     napi_valuetype valuetype = napi_undefined;
640     napi_value longContentResult = nullptr;
641     bool hasProperty = false;
642     char str[STR_MAX_SIZE] = {0};
643     char long_str[LONG_STR_MAX_SIZE + 1] = {0};
644     size_t strLen = 0;
645 
646     if (GetNotificationBasicContentDetailed(env, contentResult, longContent) == nullptr) {
647         return nullptr;
648     }
649 
650     // longText: string
651     NAPI_CALL(env, napi_has_named_property(env, contentResult, "longText", &hasProperty));
652     if (!hasProperty) {
653         ANS_LOGE("Property longText expected.");
654         return nullptr;
655     }
656     napi_get_named_property(env, contentResult, "longText", &longContentResult);
657     NAPI_CALL(env, napi_typeof(env, longContentResult, &valuetype));
658     if (valuetype != napi_string) {
659         ANS_LOGE("Wrong argument type. String expected.");
660         return nullptr;
661     }
662     NAPI_CALL(env, napi_get_value_string_utf8(env, longContentResult, long_str, LONG_STR_MAX_SIZE, &strLen));
663     if (std::strlen(long_str) == 0) {
664         ANS_LOGE("Property longText is empty");
665         return nullptr;
666     }
667     longContent->SetLongText(long_str);
668     ANS_LOGD("longText::longText = %{public}s", long_str);
669 
670     // briefText: string
671     NAPI_CALL(env, napi_has_named_property(env, contentResult, "briefText", &hasProperty));
672     if (!hasProperty) {
673         ANS_LOGE("Property briefText expected.");
674         return nullptr;
675     }
676     napi_get_named_property(env, contentResult, "briefText", &longContentResult);
677     NAPI_CALL(env, napi_typeof(env, longContentResult, &valuetype));
678     if (valuetype != napi_string) {
679         ANS_LOGE("Wrong argument type. String expected.");
680         return nullptr;
681     }
682     NAPI_CALL(env, napi_get_value_string_utf8(env, longContentResult, str, STR_MAX_SIZE - 1, &strLen));
683     if (std::strlen(str) == 0) {
684         ANS_LOGE("Property briefText is empty");
685         return nullptr;
686     }
687     longContent->SetBriefText(str);
688     ANS_LOGD("longText::briefText = %{public}s", str);
689 
690     // expandedTitle: string
691     NAPI_CALL(env, napi_has_named_property(env, contentResult, "expandedTitle", &hasProperty));
692     if (!hasProperty) {
693         ANS_LOGE("Property expandedTitle expected.");
694         return nullptr;
695     }
696     napi_get_named_property(env, contentResult, "expandedTitle", &longContentResult);
697     NAPI_CALL(env, napi_typeof(env, longContentResult, &valuetype));
698     if (valuetype != napi_string) {
699         ANS_LOGE("Wrong argument type. String expected.");
700         return nullptr;
701     }
702     NAPI_CALL(env, napi_get_value_string_utf8(env, longContentResult, str, STR_MAX_SIZE - 1, &strLen));
703     if (std::strlen(str) == 0) {
704         ANS_LOGE("Property expandedTitle is empty");
705         return nullptr;
706     }
707     longContent->SetExpandedTitle(str);
708     ANS_LOGD("longText::expandedTitle = %{public}s", str);
709 
710     return NapiGetNull(env);
711 }
712 
GetNotificationPictureContent(const napi_env & env,const napi_value & result,NotificationRequest & request)713 napi_value Common::GetNotificationPictureContent(
714     const napi_env &env, const napi_value &result, NotificationRequest &request)
715 {
716     ANS_LOGD("enter");
717 
718     napi_valuetype valuetype = napi_undefined;
719     napi_value contentResult = nullptr;
720     bool hasProperty = false;
721 
722     NAPI_CALL(env, napi_has_named_property(env, result, "picture", &hasProperty));
723     if (!hasProperty) {
724         ANS_LOGE("Property picture expected.");
725         return nullptr;
726     }
727     napi_get_named_property(env, result, "picture", &contentResult);
728     NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
729     if (valuetype != napi_object) {
730         ANS_LOGE("Wrong argument type. Object expected.");
731         return nullptr;
732     }
733 
734     std::shared_ptr<OHOS::Notification::NotificationPictureContent> pictureContent =
735         std::make_shared<OHOS::Notification::NotificationPictureContent>();
736     if (pictureContent == nullptr) {
737         ANS_LOGE("pictureContent is null");
738         return nullptr;
739     }
740     if (GetNotificationPictureContentDetailed(env, contentResult, pictureContent) == nullptr) {
741         return nullptr;
742     }
743 
744     request.SetContent(std::make_shared<NotificationContent>(pictureContent));
745 
746     return NapiGetNull(env);
747 }
748 
GetNotificationPictureContentDetailed(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationPictureContent> & pictureContent)749 napi_value Common::GetNotificationPictureContentDetailed(const napi_env &env,
750     const napi_value &contentResult, std::shared_ptr<OHOS::Notification::NotificationPictureContent> &pictureContent)
751 {
752     ANS_LOGD("enter");
753 
754     napi_valuetype valuetype = napi_undefined;
755     napi_value pictureContentResult = nullptr;
756     bool hasProperty = false;
757     char str[STR_MAX_SIZE] = {0};
758     size_t strLen = 0;
759 
760     if (GetNotificationBasicContentDetailed(env, contentResult, pictureContent) == nullptr) {
761         return nullptr;
762     }
763 
764     // briefText: string
765     NAPI_CALL(env, napi_has_named_property(env, contentResult, "briefText", &hasProperty));
766     if (!hasProperty) {
767         ANS_LOGE("Property briefText expected.");
768         return nullptr;
769     }
770     napi_get_named_property(env, contentResult, "briefText", &pictureContentResult);
771     NAPI_CALL(env, napi_typeof(env, pictureContentResult, &valuetype));
772     if (valuetype != napi_string) {
773         ANS_LOGE("Wrong argument type. String expected.");
774         return nullptr;
775     }
776     NAPI_CALL(env, napi_get_value_string_utf8(env, pictureContentResult, str, STR_MAX_SIZE - 1, &strLen));
777     if (std::strlen(str) == 0) {
778         ANS_LOGE("Property briefText is empty");
779         return nullptr;
780     }
781     pictureContent->SetBriefText(str);
782 
783     // expandedTitle: string
784     NAPI_CALL(env, napi_has_named_property(env, contentResult, "expandedTitle", &hasProperty));
785     if (!hasProperty) {
786         ANS_LOGE("Property expandedTitle expected.");
787         return nullptr;
788     }
789     napi_get_named_property(env, contentResult, "expandedTitle", &pictureContentResult);
790     NAPI_CALL(env, napi_typeof(env, pictureContentResult, &valuetype));
791     if (valuetype != napi_string) {
792         ANS_LOGE("Wrong argument type. String expected.");
793         return nullptr;
794     }
795     NAPI_CALL(env, napi_get_value_string_utf8(env, pictureContentResult, str, STR_MAX_SIZE - 1, &strLen));
796     if (std::strlen(str) == 0) {
797         ANS_LOGE("Property expandedTitle is empty");
798         return nullptr;
799     }
800     pictureContent->SetExpandedTitle(str);
801 
802     // picture: image.PixelMap
803     NAPI_CALL(env, napi_has_named_property(env, contentResult, "picture", &hasProperty));
804     if (!hasProperty) {
805         ANS_LOGE("Property picture expected.");
806         return nullptr;
807     }
808     napi_get_named_property(env, contentResult, "picture", &pictureContentResult);
809     NAPI_CALL(env, napi_typeof(env, pictureContentResult, &valuetype));
810     if (valuetype != napi_object) {
811         ANS_LOGE("Wrong argument type. Object expected.");
812         return nullptr;
813     }
814     std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
815     pixelMap = Media::PixelMapNapi::GetPixelMap(env, pictureContentResult);
816     if (pixelMap == nullptr) {
817         ANS_LOGE("Invalid object pixelMap");
818         return nullptr;
819     }
820     pictureContent->SetBigPicture(pixelMap);
821 
822     return Common::NapiGetNull(env);
823 }
824 
GetNotificationConversationalContent(const napi_env & env,const napi_value & result,NotificationRequest & request)825 napi_value Common::GetNotificationConversationalContent(
826     const napi_env &env, const napi_value &result, NotificationRequest &request)
827 {
828     ANS_LOGD("enter");
829 
830     napi_valuetype valuetype = napi_undefined;
831     napi_value contentResult = nullptr;
832     bool hasProperty = false;
833     MessageUser user;
834 
835     NAPI_CALL(env, napi_has_named_property(env, result, "conversation", &hasProperty));
836     if (!hasProperty) {
837         ANS_LOGE("Property conversation expected.");
838         return nullptr;
839     }
840     napi_get_named_property(env, result, "conversation", &contentResult);
841     NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
842     if (valuetype != napi_object) {
843         ANS_LOGE("Wrong argument type. Object expected.");
844         return nullptr;
845     }
846 
847     if (GetNotificationConversationalContentByUser(env, contentResult, user) == nullptr) {
848         return nullptr;
849     }
850 
851     std::shared_ptr<OHOS::Notification::NotificationConversationalContent> conversationalContent =
852         std::make_shared<OHOS::Notification::NotificationConversationalContent>(user);
853     if (conversationalContent == nullptr) {
854         ANS_LOGE("conversationalContent is null");
855         return nullptr;
856     }
857 
858     if (GetNotificationBasicContentDetailed(env, contentResult, conversationalContent) == nullptr) {
859         return nullptr;
860     }
861     if (GetNotificationConversationalContentTitle(env, contentResult, conversationalContent) == nullptr) {
862         return nullptr;
863     }
864     if (GetNotificationConversationalContentGroup(env, contentResult, conversationalContent) == nullptr) {
865         return nullptr;
866     }
867     if (GetNotificationConversationalContentMessages(env, contentResult, conversationalContent) == nullptr) {
868         return nullptr;
869     }
870 
871     request.SetContent(std::make_shared<NotificationContent>(conversationalContent));
872 
873     return NapiGetNull(env);
874 }
875 
GetNotificationConversationalContentByUser(const napi_env & env,const napi_value & contentResult,MessageUser & user)876 napi_value Common::GetNotificationConversationalContentByUser(
877     const napi_env &env, const napi_value &contentResult, MessageUser &user)
878 {
879     ANS_LOGD("enter");
880 
881     napi_valuetype valuetype = napi_undefined;
882     bool hasProperty = false;
883 
884     // user: MessageUser
885     NAPI_CALL(env, napi_has_named_property(env, contentResult, "user", &hasProperty));
886     if (!hasProperty) {
887         ANS_LOGE("Property user expected.");
888         return nullptr;
889     }
890     napi_value userResult = nullptr;
891     napi_get_named_property(env, contentResult, "user", &userResult);
892     NAPI_CALL(env, napi_typeof(env, userResult, &valuetype));
893     if (valuetype != napi_object) {
894         ANS_LOGE("Wrong argument type. Object expected.");
895         return nullptr;
896     }
897     if (!GetMessageUser(env, userResult, user)) {
898         return nullptr;
899     }
900 
901     return NapiGetNull(env);
902 }
903 
GetMessageUser(const napi_env & env,const napi_value & result,MessageUser & messageUser)904 napi_value Common::GetMessageUser(const napi_env &env, const napi_value &result, MessageUser &messageUser)
905 {
906     ANS_LOGD("enter");
907 
908     if (GetMessageUserByString(env, result, messageUser) == nullptr) {
909         return nullptr;
910     }
911 
912     if (GetMessageUserByBool(env, result, messageUser) == nullptr) {
913         return nullptr;
914     }
915 
916     if (GetMessageUserByCustom(env, result, messageUser) == nullptr) {
917         return nullptr;
918     }
919 
920     return NapiGetNull(env);
921 }
922 
GetMessageUserByString(const napi_env & env,const napi_value & result,MessageUser & messageUser)923 napi_value Common::GetMessageUserByString(const napi_env &env, const napi_value &result, MessageUser &messageUser)
924 {
925     ANS_LOGD("enter");
926 
927     napi_valuetype valuetype = napi_undefined;
928     bool hasProperty = false;
929     char str[STR_MAX_SIZE] = {0};
930     size_t strLen = 0;
931 
932     // name: string
933     NAPI_CALL(env, napi_has_named_property(env, result, "name", &hasProperty));
934     if (!hasProperty) {
935         ANS_LOGE("Property name expected.");
936         return nullptr;
937     }
938     napi_value nameResult = nullptr;
939     napi_get_named_property(env, result, "name", &nameResult);
940     NAPI_CALL(env, napi_typeof(env, nameResult, &valuetype));
941     if (valuetype != napi_string) {
942         ANS_LOGE("Wrong argument type. String expected.");
943         return nullptr;
944     }
945     NAPI_CALL(env, napi_get_value_string_utf8(env, nameResult, str, STR_MAX_SIZE - 1, &strLen));
946     messageUser.SetName(str);
947     ANS_LOGI("MessageUser::name = %{public}s", str);
948 
949     // key: string
950     NAPI_CALL(env, napi_has_named_property(env, result, "key", &hasProperty));
951     if (!hasProperty) {
952         ANS_LOGE("Property key expected.");
953         return nullptr;
954     }
955     napi_value keyResult = nullptr;
956     napi_get_named_property(env, result, "key", &keyResult);
957     NAPI_CALL(env, napi_typeof(env, keyResult, &valuetype));
958     if (valuetype != napi_string) {
959         ANS_LOGE("Wrong argument type. String expected.");
960         return nullptr;
961     }
962     NAPI_CALL(env, napi_get_value_string_utf8(env, keyResult, str, STR_MAX_SIZE - 1, &strLen));
963     messageUser.SetKey(str);
964     ANS_LOGI("MessageUser::key = %{public}s", str);
965 
966     // uri: string
967     NAPI_CALL(env, napi_has_named_property(env, result, "uri", &hasProperty));
968     if (!hasProperty) {
969         ANS_LOGE("Property uri expected.");
970         return nullptr;
971     }
972     napi_value uriResult = nullptr;
973     napi_get_named_property(env, result, "uri", &uriResult);
974     NAPI_CALL(env, napi_typeof(env, uriResult, &valuetype));
975     if (valuetype != napi_string) {
976         ANS_LOGE("Wrong argument type. String expected.");
977         return nullptr;
978     }
979     NAPI_CALL(env, napi_get_value_string_utf8(env, uriResult, str, STR_MAX_SIZE - 1, &strLen));
980     Uri uri(str);
981     messageUser.SetUri(uri);
982 
983     return NapiGetNull(env);
984 }
985 
GetMessageUserByBool(const napi_env & env,const napi_value & result,MessageUser & messageUser)986 napi_value Common::GetMessageUserByBool(const napi_env &env, const napi_value &result, MessageUser &messageUser)
987 {
988     ANS_LOGD("enter");
989 
990     napi_valuetype valuetype = napi_undefined;
991     bool hasProperty = false;
992 
993     // isMachine: boolean
994     NAPI_CALL(env, napi_has_named_property(env, result, "isMachine", &hasProperty));
995     if (!hasProperty) {
996         ANS_LOGE("Property isMachine expected.");
997         return nullptr;
998     }
999     napi_value machineResult = nullptr;
1000     napi_get_named_property(env, result, "isMachine", &machineResult);
1001     NAPI_CALL(env, napi_typeof(env, machineResult, &valuetype));
1002     if (valuetype != napi_boolean) {
1003         ANS_LOGE("Wrong argument type. Bool expected.");
1004         return nullptr;
1005     }
1006     bool machine = false;
1007     napi_get_value_bool(env, machineResult, &machine);
1008     messageUser.SetMachine(machine);
1009 
1010     // isUserImportant: boolean
1011     NAPI_CALL(env, napi_has_named_property(env, result, "isUserImportant", &hasProperty));
1012     if (!hasProperty) {
1013         ANS_LOGE("Property isUserImportant expected.");
1014         return nullptr;
1015     }
1016     napi_value importantResult = nullptr;
1017     napi_get_named_property(env, result, "isUserImportant", &importantResult);
1018     NAPI_CALL(env, napi_typeof(env, importantResult, &valuetype));
1019     if (valuetype != napi_boolean) {
1020         ANS_LOGE("Wrong argument type. Bool expected.");
1021         return nullptr;
1022     }
1023     bool important = false;
1024     napi_get_value_bool(env, importantResult, &important);
1025     messageUser.SetUserAsImportant(important);
1026     ANS_LOGI("MessageUser::isUserImportant = %{public}d", important);
1027 
1028     return NapiGetNull(env);
1029 }
1030 
GetMessageUserByCustom(const napi_env & env,const napi_value & result,MessageUser & messageUser)1031 napi_value Common::GetMessageUserByCustom(const napi_env &env, const napi_value &result, MessageUser &messageUser)
1032 {
1033     ANS_LOGD("enter");
1034 
1035     napi_valuetype valuetype = napi_undefined;
1036     bool hasProperty = false;
1037 
1038     // icon?: image.PixelMap
1039     NAPI_CALL(env, napi_has_named_property(env, result, "icon", &hasProperty));
1040     if (hasProperty) {
1041         napi_value iconResult = nullptr;
1042         napi_get_named_property(env, result, "icon", &iconResult);
1043         NAPI_CALL(env, napi_typeof(env, iconResult, &valuetype));
1044         if (valuetype != napi_object) {
1045             ANS_LOGE("Wrong argument type. Object expected.");
1046             return nullptr;
1047         }
1048         std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
1049         pixelMap = Media::PixelMapNapi::GetPixelMap(env, iconResult);
1050         if (pixelMap == nullptr) {
1051             ANS_LOGE("Invalid object pixelMap");
1052             return nullptr;
1053         }
1054         messageUser.SetPixelMap(pixelMap);
1055     }
1056 
1057     return NapiGetNull(env);
1058 }
1059 
GetNotificationConversationalContentTitle(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationConversationalContent> & conversationalContent)1060 napi_value Common::GetNotificationConversationalContentTitle(
1061     const napi_env &env, const napi_value &contentResult,
1062     std::shared_ptr<OHOS::Notification::NotificationConversationalContent> &conversationalContent)
1063 {
1064     ANS_LOGD("enter");
1065     napi_valuetype valuetype = napi_undefined;
1066     napi_value conversationalContentResult = nullptr;
1067     bool hasProperty = false;
1068     char str[STR_MAX_SIZE] = {0};
1069     size_t strLen = 0;
1070 
1071     // conversationTitle: string
1072     NAPI_CALL(env, napi_has_named_property(env, contentResult, "conversationTitle", &hasProperty));
1073     if (!hasProperty) {
1074         ANS_LOGE("Property conversationTitle expected.");
1075         return nullptr;
1076     }
1077     napi_get_named_property(env, contentResult, "conversationTitle", &conversationalContentResult);
1078     NAPI_CALL(env, napi_typeof(env, conversationalContentResult, &valuetype));
1079     if (valuetype != napi_string) {
1080         ANS_LOGE("Wrong argument type. String expected.");
1081         return nullptr;
1082     }
1083     NAPI_CALL(env, napi_get_value_string_utf8(env, conversationalContentResult, str, STR_MAX_SIZE - 1, &strLen));
1084     conversationalContent->SetConversationTitle(str);
1085     ANS_LOGD("conversationTitle = %{public}s", str);
1086 
1087     return NapiGetNull(env);
1088 }
1089 
GetNotificationConversationalContentGroup(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationConversationalContent> & conversationalContent)1090 napi_value Common::GetNotificationConversationalContentGroup(
1091     const napi_env &env, const napi_value &contentResult,
1092     std::shared_ptr<OHOS::Notification::NotificationConversationalContent> &conversationalContent)
1093 {
1094     ANS_LOGD("enter");
1095     napi_valuetype valuetype = napi_undefined;
1096     napi_value conversationalContentResult = nullptr;
1097     bool hasProperty = false;
1098 
1099     // conversationGroup: boolean
1100     NAPI_CALL(env, napi_has_named_property(env, contentResult, "conversationGroup", &hasProperty));
1101     if (!hasProperty) {
1102         ANS_LOGE("Property conversationGroup expected.");
1103         return nullptr;
1104     }
1105     napi_get_named_property(env, contentResult, "conversationGroup", &conversationalContentResult);
1106     NAPI_CALL(env, napi_typeof(env, conversationalContentResult, &valuetype));
1107     if (valuetype != napi_boolean) {
1108         ANS_LOGE("Wrong argument type. Bool expected.");
1109         return nullptr;
1110     }
1111     bool conversationGroup = false;
1112     napi_get_value_bool(env, conversationalContentResult, &conversationGroup);
1113     conversationalContent->SetConversationGroup(conversationGroup);
1114     ANS_LOGI("conversationalText::conversationGroup = %{public}d", conversationGroup);
1115 
1116     return NapiGetNull(env);
1117 }
1118 
GetNotificationConversationalContentMessages(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationConversationalContent> & conversationalContent)1119 napi_value Common::GetNotificationConversationalContentMessages(
1120     const napi_env &env, const napi_value &contentResult,
1121     std::shared_ptr<OHOS::Notification::NotificationConversationalContent> &conversationalContent)
1122 {
1123     ANS_LOGD("enter");
1124     napi_valuetype valuetype = napi_undefined;
1125     napi_value conversationalContentResult = nullptr;
1126     bool hasProperty = false;
1127 
1128     // messages: Array<ConversationalMessage>
1129     NAPI_CALL(env, napi_has_named_property(env, contentResult, "messages", &hasProperty));
1130     if (!hasProperty) {
1131         ANS_LOGE("Property messages expected.");
1132         return nullptr;
1133     }
1134     napi_get_named_property(env, contentResult, "messages", &conversationalContentResult);
1135     bool isArray = false;
1136     napi_is_array(env, conversationalContentResult, &isArray);
1137     if (!isArray) {
1138         ANS_LOGE("Property messages is expected to be an array.");
1139         return nullptr;
1140     }
1141     uint32_t length = 0;
1142     napi_get_array_length(env, conversationalContentResult, &length);
1143     if (length == 0) {
1144         ANS_LOGE("The array is empty.");
1145         return nullptr;
1146     }
1147     for (size_t i = 0; i < length; i++) {
1148         napi_value conversationalMessage = nullptr;
1149         napi_get_element(env, conversationalContentResult, i, &conversationalMessage);
1150         NAPI_CALL(env, napi_typeof(env, conversationalMessage, &valuetype));
1151         if (valuetype != napi_object) {
1152             ANS_LOGE("Wrong argument type. Object expected.");
1153             return nullptr;
1154         }
1155         std::shared_ptr<NotificationConversationalMessage> message = nullptr;
1156         if (!GetConversationalMessage(env, conversationalMessage, message)) {
1157             return nullptr;
1158         }
1159         conversationalContent->AddConversationalMessage(message);
1160     }
1161 
1162     return NapiGetNull(env);
1163 }
1164 
GetConversationalMessage(const napi_env & env,const napi_value & conversationalMessage,std::shared_ptr<NotificationConversationalMessage> & message)1165 napi_value Common::GetConversationalMessage(const napi_env &env, const napi_value &conversationalMessage,
1166     std::shared_ptr<NotificationConversationalMessage> &message)
1167 {
1168     ANS_LOGD("enter");
1169 
1170     if (GetConversationalMessageBasicInfo(env, conversationalMessage, message) == nullptr) {
1171         return nullptr;
1172     }
1173     if (GetConversationalMessageOtherInfo(env, conversationalMessage, message) == nullptr) {
1174         return nullptr;
1175     }
1176     return NapiGetNull(env);
1177 }
1178 
GetConversationalMessageBasicInfo(const napi_env & env,const napi_value & conversationalMessage,std::shared_ptr<NotificationConversationalMessage> & message)1179 napi_value Common::GetConversationalMessageBasicInfo(const napi_env &env, const napi_value &conversationalMessage,
1180     std::shared_ptr<NotificationConversationalMessage> &message)
1181 {
1182     ANS_LOGD("enter");
1183 
1184     napi_valuetype valuetype = napi_undefined;
1185     bool hasProperty = false;
1186     char str[STR_MAX_SIZE] = {0};
1187     size_t strLen = 0;
1188     std::string text;
1189     int64_t timestamp = 0;
1190     MessageUser sender;
1191 
1192     // text: string
1193     NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "text", &hasProperty));
1194     if (!hasProperty) {
1195         ANS_LOGE("Property text expected.");
1196         return nullptr;
1197     }
1198     napi_value textResult = nullptr;
1199     napi_get_named_property(env, conversationalMessage, "text", &textResult);
1200     NAPI_CALL(env, napi_typeof(env, textResult, &valuetype));
1201     if (valuetype != napi_string) {
1202         ANS_LOGE("Wrong argument type. String expected.");
1203         return nullptr;
1204     }
1205     NAPI_CALL(env, napi_get_value_string_utf8(env, textResult, str, STR_MAX_SIZE - 1, &strLen));
1206     text = str;
1207     ANS_LOGI("conversationalMessage::text = %{public}s", str);
1208 
1209     // timestamp: number
1210     NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "timestamp", &hasProperty));
1211     if (!hasProperty) {
1212         ANS_LOGE("Property timestamp expected.");
1213         return nullptr;
1214     }
1215     napi_value timestampResult = nullptr;
1216     napi_get_named_property(env, conversationalMessage, "timestamp", &timestampResult);
1217     NAPI_CALL(env, napi_typeof(env, timestampResult, &valuetype));
1218     if (valuetype != napi_number) {
1219         ANS_LOGE("Wrong argument type. Number expected.");
1220         return nullptr;
1221     }
1222     napi_get_value_int64(env, timestampResult, &timestamp);
1223     ANS_LOGI("conversationalMessage::timestamp = %{public}" PRId64, timestamp);
1224 
1225     // sender: MessageUser
1226     NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "sender", &hasProperty));
1227     if (!hasProperty) {
1228         ANS_LOGE("Property sender expected.");
1229         return nullptr;
1230     }
1231     napi_value senderResult = nullptr;
1232     napi_get_named_property(env, conversationalMessage, "sender", &senderResult);
1233     NAPI_CALL(env, napi_typeof(env, senderResult, &valuetype));
1234     if (valuetype != napi_object) {
1235         ANS_LOGE("Wrong argument type. Object expected.");
1236         return nullptr;
1237     }
1238     if (!GetMessageUser(env, senderResult, sender)) {
1239         return nullptr;
1240     }
1241 
1242     message = std::make_shared<NotificationConversationalMessage>(text, timestamp, sender);
1243     if (!message) {
1244         ANS_LOGE("Failed to create NotificationConversationalMessage object");
1245         return nullptr;
1246     }
1247 
1248     return NapiGetNull(env);
1249 }
1250 
GetConversationalMessageOtherInfo(const napi_env & env,const napi_value & conversationalMessage,std::shared_ptr<NotificationConversationalMessage> & message)1251 napi_value Common::GetConversationalMessageOtherInfo(const napi_env &env, const napi_value &conversationalMessage,
1252     std::shared_ptr<NotificationConversationalMessage> &message)
1253 {
1254     ANS_LOGD("enter");
1255 
1256     napi_valuetype valuetype = napi_undefined;
1257     bool hasProperty = false;
1258     char str[STR_MAX_SIZE] = {0};
1259     size_t strLen = 0;
1260     std::string mimeType;
1261     std::string uri;
1262 
1263     // mimeType: string
1264     NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "mimeType", &hasProperty));
1265     if (!hasProperty) {
1266         ANS_LOGE("Property mimeType expected.");
1267         return nullptr;
1268     }
1269     napi_value mimeTypeResult = nullptr;
1270     napi_get_named_property(env, conversationalMessage, "mimeType", &mimeTypeResult);
1271     NAPI_CALL(env, napi_typeof(env, mimeTypeResult, &valuetype));
1272     if (valuetype != napi_string) {
1273         ANS_LOGE("Wrong argument type. String expected.");
1274         return nullptr;
1275     }
1276     NAPI_CALL(env, napi_get_value_string_utf8(env, mimeTypeResult, str, STR_MAX_SIZE - 1, &strLen));
1277     mimeType = str;
1278     ANS_LOGI("conversationalMessage::mimeType = %{public}s", str);
1279 
1280     // uri?: string
1281     NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "uri", &hasProperty));
1282     if (hasProperty) {
1283         napi_value uriResult = nullptr;
1284         napi_get_named_property(env, conversationalMessage, "uri", &uriResult);
1285         NAPI_CALL(env, napi_typeof(env, uriResult, &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, uriResult, str, STR_MAX_SIZE - 1, &strLen));
1291         uri = str;
1292     }
1293 
1294     std::shared_ptr<Uri> uriPtr = std::make_shared<Uri>(uri);
1295     message->SetData(mimeType, uriPtr);
1296 
1297     return NapiGetNull(env);
1298 }
1299 
GetNotificationMultiLineContent(const napi_env & env,const napi_value & result,NotificationRequest & request)1300 napi_value Common::GetNotificationMultiLineContent(
1301     const napi_env &env, const napi_value &result, NotificationRequest &request)
1302 {
1303     ANS_LOGD("enter");
1304 
1305     napi_valuetype valuetype = napi_undefined;
1306     napi_value contentResult = nullptr;
1307     napi_value multiLineContentResult = nullptr;
1308     bool hasProperty = false;
1309     char str[STR_MAX_SIZE] = {0};
1310     size_t strLen = 0;
1311 
1312     NAPI_CALL(env, napi_has_named_property(env, result, "multiLine", &hasProperty));
1313     if (!hasProperty) {
1314         ANS_LOGE("Property multiLine expected.");
1315         return nullptr;
1316     }
1317     napi_get_named_property(env, result, "multiLine", &contentResult);
1318     NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
1319     if (valuetype != napi_object) {
1320         ANS_LOGE("Wrong argument type. Object expected.");
1321         return nullptr;
1322     }
1323 
1324     std::shared_ptr<OHOS::Notification::NotificationMultiLineContent> multiLineContent =
1325         std::make_shared<OHOS::Notification::NotificationMultiLineContent>();
1326     if (multiLineContent == nullptr) {
1327         ANS_LOGE("multiLineContent is null");
1328         return nullptr;
1329     }
1330 
1331     if (GetNotificationBasicContentDetailed(env, contentResult, multiLineContent) == nullptr) {
1332         return nullptr;
1333     }
1334 
1335     // briefText: string
1336     NAPI_CALL(env, napi_has_named_property(env, contentResult, "briefText", &hasProperty));
1337     if (!hasProperty) {
1338         ANS_LOGE("Property briefText expected.");
1339         return nullptr;
1340     }
1341     napi_get_named_property(env, contentResult, "briefText", &multiLineContentResult);
1342     NAPI_CALL(env, napi_typeof(env, multiLineContentResult, &valuetype));
1343     if (valuetype != napi_string) {
1344         ANS_LOGE("Wrong argument type. String expected.");
1345         return nullptr;
1346     }
1347     NAPI_CALL(env, napi_get_value_string_utf8(env, multiLineContentResult, str, STR_MAX_SIZE - 1, &strLen));
1348     if (std::strlen(str) == 0) {
1349         ANS_LOGE("Property briefText is empty");
1350         return nullptr;
1351     }
1352     multiLineContent->SetBriefText(str);
1353     ANS_LOGD("multiLine: briefText = %{public}s", str);
1354 
1355     // longTitle: string
1356     NAPI_CALL(env, napi_has_named_property(env, contentResult, "longTitle", &hasProperty));
1357     if (!hasProperty) {
1358         ANS_LOGE("Property longTitle expected.");
1359         return nullptr;
1360     }
1361     napi_get_named_property(env, contentResult, "longTitle", &multiLineContentResult);
1362     NAPI_CALL(env, napi_typeof(env, multiLineContentResult, &valuetype));
1363     if (valuetype != napi_string) {
1364         ANS_LOGE("Wrong argument type. String expected.");
1365         return nullptr;
1366     }
1367     NAPI_CALL(env, napi_get_value_string_utf8(env, multiLineContentResult, str, STR_MAX_SIZE - 1, &strLen));
1368     if (std::strlen(str) == 0) {
1369         ANS_LOGE("Property longTitle is empty");
1370         return nullptr;
1371     }
1372     multiLineContent->SetExpandedTitle(str);
1373     ANS_LOGD("multiLine: longTitle = %{public}s", str);
1374 
1375     // lines: Array<String>
1376     NAPI_CALL(env, napi_has_named_property(env, contentResult, "lines", &hasProperty));
1377     if (!hasProperty) {
1378         ANS_LOGE("Property lines expected.");
1379         return nullptr;
1380     }
1381     if (GetNotificationMultiLineContentLines(env, contentResult, multiLineContent) == nullptr) {
1382         return nullptr;
1383     }
1384 
1385     request.SetContent(std::make_shared<NotificationContent>(multiLineContent));
1386 
1387     ANS_LOGD("end");
1388     return NapiGetNull(env);
1389 }
1390 
GetNotificationMultiLineContentLines(const napi_env & env,const napi_value & result,std::shared_ptr<OHOS::Notification::NotificationMultiLineContent> & multiLineContent)1391 napi_value Common::GetNotificationMultiLineContentLines(const napi_env &env, const napi_value &result,
1392     std::shared_ptr<OHOS::Notification::NotificationMultiLineContent> &multiLineContent)
1393 {
1394     ANS_LOGD("enter");
1395 
1396     bool isArray = false;
1397     napi_valuetype valuetype = napi_undefined;
1398     napi_value multilines = nullptr;
1399     char str[STR_MAX_SIZE] = {0};
1400     size_t strLen = 0;
1401     uint32_t length = 0;
1402 
1403     napi_get_named_property(env, result, "lines", &multilines);
1404     napi_is_array(env, multilines, &isArray);
1405     if (!isArray) {
1406         ANS_LOGE("Property lines is expected to be an array.");
1407         return nullptr;
1408     }
1409 
1410     napi_get_array_length(env, multilines, &length);
1411     if (length == 0) {
1412         ANS_LOGE("The array is empty.");
1413         return nullptr;
1414     }
1415     for (size_t i = 0; i < length; i++) {
1416         napi_value line = nullptr;
1417         napi_get_element(env, multilines, i, &line);
1418         NAPI_CALL(env, napi_typeof(env, line, &valuetype));
1419         if (valuetype != napi_string) {
1420             ANS_LOGE("Wrong argument type. String expected.");
1421             return nullptr;
1422         }
1423         NAPI_CALL(env, napi_get_value_string_utf8(env, line, str, STR_MAX_SIZE - 1, &strLen));
1424         multiLineContent->AddSingleLine(str);
1425         ANS_LOGI("multiLine: lines : addSingleLine = %{public}s", str);
1426     }
1427 
1428     return NapiGetNull(env);
1429 }
1430 
GetLockScreenPicture(const napi_env & env,const napi_value & contentResult,std::shared_ptr<NotificationBasicContent> basicContent)1431 napi_value Common::GetLockScreenPicture(
1432     const napi_env &env, const napi_value &contentResult, std::shared_ptr<NotificationBasicContent> basicContent)
1433 {
1434     bool hasProperty = false;
1435     NAPI_CALL(env, napi_has_named_property(env, contentResult, "lockscreenPicture", &hasProperty));
1436     if (hasProperty) {
1437         auto value = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult, "lockscreenPicture", napi_object);
1438         if (value == nullptr) {
1439             ANS_LOGE("Failed to get lockScreenPicture from js.");
1440             return nullptr;
1441         }
1442         auto pixelMap = Media::PixelMapNapi::GetPixelMap(env, value);
1443         if (pixelMap == nullptr) {
1444             ANS_LOGE("Invalid object pixelMap");
1445             return nullptr;
1446         }
1447         basicContent->SetLockScreenPicture(pixelMap);
1448     }
1449 
1450     return NapiGetNull(env);
1451 }
1452 
SetLockScreenPicture(const napi_env & env,const NotificationBasicContent * basicContent,napi_value & result)1453 napi_value Common::SetLockScreenPicture(
1454     const napi_env &env, const NotificationBasicContent *basicContent, napi_value &result)
1455 {
1456     if (basicContent->GetLockScreenPicture() == nullptr) {
1457         return NapiGetBoolean(env, true);
1458     }
1459 
1460     std::shared_ptr<Media::PixelMap> picture = basicContent->GetLockScreenPicture();
1461     napi_valuetype valuetype = napi_undefined;
1462     napi_value pictureValue = Media::PixelMapNapi::CreatePixelMap(env, picture);
1463     NAPI_CALL(env, napi_typeof(env, pictureValue, &valuetype));
1464     if (valuetype == napi_undefined) {
1465         ANS_LOGI("LockScreenPicture is undefined");
1466         napi_set_named_property(env, result, "lockscreenPicture", NapiGetNull(env));
1467     } else {
1468         napi_set_named_property(env, result, "lockscreenPicture", pictureValue);
1469     }
1470 
1471     return NapiGetBoolean(env, true);
1472 }
1473 }
1474 }
1475