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