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