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_icon_button.h"
28 #include "notification_progress.h"
29 #include "notification_time.h"
30 #include "pixel_map_napi.h"
31
32 namespace OHOS {
33 using namespace Global::Resource;
34 namespace NotificationNapi {
SetNotificationLocalLiveViewContent(const napi_env & env,NotificationBasicContent * basicContent,napi_value & result)35 napi_value Common::SetNotificationLocalLiveViewContent(
36 const napi_env &env, NotificationBasicContent *basicContent, napi_value &result)
37 {
38 ANS_LOGD("enter");
39 napi_value value = nullptr;
40 if (basicContent == nullptr) {
41 ANS_LOGE("basicContent is null");
42 return NapiGetBoolean(env, false);
43 }
44 OHOS::Notification::NotificationLocalLiveViewContent *localLiveViewContent =
45 static_cast<OHOS::Notification::NotificationLocalLiveViewContent *>(basicContent);
46 if (localLiveViewContent == nullptr) {
47 ANS_LOGE("localLiveViewContent is null");
48 return NapiGetBoolean(env, false);
49 }
50
51 if (!SetNotificationBasicContent(env, localLiveViewContent, result)) {
52 ANS_LOGE("SetNotificationBasicContent call failed");
53 return NapiGetBoolean(env, false);
54 }
55
56 // typeCode: int32_t
57 napi_create_int32(env, localLiveViewContent->GetType(), &value);
58 napi_set_named_property(env, result, "typeCode", value);
59
60 // capsule: NotificationCapsule
61 if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE)) {
62 napi_value capsule = nullptr;
63 napi_create_object(env, &capsule);
64 if (!SetCapsule(env, localLiveViewContent->GetCapsule(), capsule)) {
65 ANS_LOGE("SetCapsule call failed");
66 return NapiGetBoolean(env, false);
67 }
68 napi_set_named_property(env, result, "capsule", capsule);
69 }
70
71 // button: NotificationLocalLiveViewButton
72 if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON)) {
73 napi_value button = nullptr;
74 napi_create_object(env, &button);
75 if (!SetButton(env, localLiveViewContent->GetButton(), button)) {
76 ANS_LOGE("SetButton call failed");
77 return NapiGetBoolean(env, false);
78 }
79 napi_set_named_property(env, result, "button", button);
80 }
81
82 // cardButtons?: Array<NotificationIconButton>;
83 if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::CARD_BUTTON)) {
84 napi_value cardBtn = nullptr;
85 napi_create_array_with_length(env, localLiveViewContent->GetCardButton().size(), &cardBtn);
86 if (!SetCardButton(env, localLiveViewContent->GetCardButton(), cardBtn)) {
87 ANS_LOGE("SetCardButton call failed");
88 return NapiGetBoolean(env, false);
89 }
90 napi_set_named_property(env, result, "cardButtons", cardBtn);
91 }
92
93 // liveViewType?: LiveViewTypes
94 LiveViewTypes outType = LiveViewTypes::LIVE_VIEW_ACTIVITY;
95 if (!AnsEnumUtil::LiveViewTypesCToJS(localLiveViewContent->GetLiveViewType(), outType)) {
96 ANS_LOGE("Liveview type is invalid");
97 return NapiGetBoolean(env, false);
98 }
99 napi_create_int32(env, static_cast<int32_t>(outType), &value);
100 napi_set_named_property(env, result, "liveViewType", value);
101
102 // progress: NotificationProgress
103 if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::PROGRESS)) {
104 napi_value progress = nullptr;
105 napi_create_object(env, &progress);
106 if (!SetProgress(env, localLiveViewContent->GetProgress(), progress)) {
107 ANS_LOGE("SetProgress call failed");
108 return NapiGetBoolean(env, false);
109 }
110 napi_set_named_property(env, result, "progress", progress);
111 }
112
113 // time: NotificationTime
114 if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::TIME)) {
115 napi_value time = nullptr;
116 napi_create_object(env, &time);
117 bool flag = localLiveViewContent->isFlagExist(
118 NotificationLocalLiveViewContent::LiveViewContentInner::INITIAL_TIME);
119 if (!SetTime(env, localLiveViewContent->GetTime(), time, flag)) {
120 ANS_LOGE("SetMessageUser call failed");
121 return NapiGetBoolean(env, false);
122 }
123 napi_set_named_property(env, result, "time", time);
124 }
125
126 return NapiGetBoolean(env, true);
127 }
128
SetCapsule(const napi_env & env,const NotificationCapsule & capsule,napi_value & result)129 napi_value Common::SetCapsule(const napi_env &env, const NotificationCapsule &capsule, napi_value &result)
130 {
131 ANS_LOGD("enter");
132
133 napi_value value = nullptr;
134 // title: string
135 napi_create_string_utf8(env, capsule.GetTitle().c_str(), NAPI_AUTO_LENGTH, &value);
136 napi_set_named_property(env, result, "title", value);
137
138 // backgroundColor: string
139 napi_create_string_utf8(env, capsule.GetBackgroundColor().c_str(), NAPI_AUTO_LENGTH, &value);
140 napi_set_named_property(env, result, "backgroundColor", value);
141
142 // content: string
143 napi_create_string_utf8(env, capsule.GetContent().c_str(), NAPI_AUTO_LENGTH, &value);
144 napi_set_named_property(env, result, "content", value);
145
146 // icon?: image.PixelMap
147 std::shared_ptr<Media::PixelMap> icon = capsule.GetIcon();
148 if (icon) {
149 napi_value iconResult = nullptr;
150 napi_valuetype valuetype = napi_undefined;
151 iconResult = Media::PixelMapNapi::CreatePixelMap(env, icon);
152 NAPI_CALL(env, napi_typeof(env, iconResult, &valuetype));
153 if (valuetype == napi_undefined) {
154 ANS_LOGW("iconResult is undefined");
155 napi_set_named_property(env, result, "icon", NapiGetNull(env));
156 } else {
157 napi_set_named_property(env, result, "icon", iconResult);
158 }
159 }
160
161 // time?: number
162 napi_create_int32(env, capsule.GetTime(), &value);
163 napi_set_named_property(env, result, "time", value);
164
165 // capsuleButtons?: Array<NotificationIconButton>;
166 napi_value cardBtn = nullptr;
167 napi_create_array_with_length(env, capsule.GetCapsuleButton().size(), &cardBtn);
168 if (!SetCardButton(env, capsule.GetCapsuleButton(), cardBtn)) {
169 ANS_LOGE("capsuleButton call failed");
170 return NapiGetBoolean(env, false);
171 }
172 napi_set_named_property(env, result, "capsuleButtons", cardBtn);
173
174 return NapiGetBoolean(env, true);
175 }
176
SetProgress(const napi_env & env,const NotificationProgress & progress,napi_value & result)177 napi_value Common::SetProgress(const napi_env &env, const NotificationProgress &progress, napi_value &result)
178 {
179 ANS_LOGD("enter");
180
181 napi_value value = nullptr;
182 // currentValue: int32_t
183 napi_create_int32(env, progress.GetCurrentValue(), &value);
184 napi_set_named_property(env, result, "currentValue", value);
185
186 // maxValue: int32_t
187 napi_create_int32(env, progress.GetMaxValue(), &value);
188 napi_set_named_property(env, result, "maxValue", value);
189
190 // isPercentage: bool
191 napi_get_boolean(env, progress.GetIsPercentage(), &value);
192 napi_set_named_property(env, result, "isPercentage", value);
193
194 return NapiGetBoolean(env, true);
195 }
196
SetTime(const napi_env & env,const NotificationTime & time,napi_value & result,bool isInitialTimeExist)197 napi_value Common::SetTime(const napi_env &env, const NotificationTime &time,
198 napi_value &result, bool isInitialTimeExist)
199 {
200 ANS_LOGD("enter");
201
202 napi_value value = nullptr;
203 // initialTime: int32_t
204 if (isInitialTimeExist) {
205 napi_create_int32(env, time.GetInitialTime(), &value);
206 napi_set_named_property(env, result, "initialTime", value);
207 }
208
209 // isCountDown: bool
210 napi_get_boolean(env, time.GetIsCountDown(), &value);
211 napi_set_named_property(env, result, "isCountDown", value);
212
213 // isPaused: bool
214 napi_get_boolean(env, time.GetIsPaused(), &value);
215 napi_set_named_property(env, result, "isPaused", value);
216
217 // isInTitle: bool
218 napi_get_boolean(env, time.GetIsInTitle(), &value);
219 napi_set_named_property(env, result, "isInTitle", value);
220
221 return NapiGetBoolean(env, true);
222 }
223
SetObjectStringProperty(const napi_env & env,napi_value & object,const std::string & key,const std::string & value)224 napi_value Common::SetObjectStringProperty(const napi_env& env, napi_value& object,
225 const std::string& key, const std::string& value)
226 {
227 napi_value property;
228 napi_status status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &property);
229 if (status != napi_ok) {
230 ANS_LOGE("Failed to create value.");
231 return nullptr;
232 }
233 status = napi_set_named_property(env, object, key.c_str(), property);
234 if (status != napi_ok) {
235 ANS_LOGE("Failed to set locale property");
236 return nullptr;
237 }
238 return NapiGetNull(env);
239 }
240
SetObjectUint32Property(const napi_env & env,napi_value & object,const std::string & key,uint32_t value)241 napi_value Common::SetObjectUint32Property(const napi_env &env, napi_value& object,
242 const std::string& key, uint32_t value)
243 {
244 napi_value property;
245 napi_status status = napi_create_uint32(env, value, &property);
246 if (status != napi_ok) {
247 ANS_LOGE("Failed to create value.");
248 return nullptr;
249 }
250
251 status = napi_set_named_property(env, object, key.c_str(), property);
252 if (status != napi_ok) {
253 ANS_LOGE("Failed to set locale property");
254 return nullptr;
255 }
256 return NapiGetNull(env);
257 }
258
SetResourceObject(napi_env env,const std::shared_ptr<ResourceManager::Resource> & resource,napi_value & object)259 napi_value Common::SetResourceObject(napi_env env, const std::shared_ptr<ResourceManager::Resource> &resource,
260 napi_value &object)
261 {
262 if (SetObjectStringProperty(env, object, "bundleName", resource->bundleName) == nullptr) {
263 ANS_LOGE("Failed to set property bundleName");
264 return NapiGetBoolean(env, false);
265 }
266 if (SetObjectStringProperty(env, object, "moduleName", resource->moduleName) == nullptr) {
267 ANS_LOGE("Failed to set property moduleName");
268 return NapiGetBoolean(env, false);
269 }
270 if (SetObjectUint32Property(env, object, "id", resource->id) == nullptr) {
271 ANS_LOGE("Failed to set property id");
272 return NapiGetBoolean(env, false);
273 }
274 return NapiGetBoolean(env, true);
275 }
276
SetButton(const napi_env & env,const NotificationLocalLiveViewButton & button,napi_value & result)277 napi_value Common::SetButton(const napi_env &env, const NotificationLocalLiveViewButton &button, napi_value &result)
278 {
279 ANS_LOGD("enter");
280
281 napi_value value = nullptr;
282
283 // buttonNames: Array<String>
284 napi_value arr = nullptr;
285 int count = 0;
286 napi_create_array(env, &arr);
287 for (auto vec : button.GetAllButtonNames()) {
288 napi_create_string_utf8(env, vec.c_str(), NAPI_AUTO_LENGTH, &value);
289 napi_set_element(env, arr, count, value);
290 count++;
291 }
292 napi_set_named_property(env, result, "names", arr);
293
294 // buttonIcons: Array<PixelMap>
295 napi_value iconArr = nullptr;
296 int iconCount = 0;
297 napi_create_array(env, &iconArr);
298
299 std::vector<std::shared_ptr<Media::PixelMap>> icons = button.GetAllButtonIcons();
300 for (auto vec : icons) {
301 if (!vec) {
302 continue;
303 }
304 // buttonIcon
305 napi_value iconResult = nullptr;
306 iconResult = Media::PixelMapNapi::CreatePixelMap(env, vec);
307 napi_set_element(env, iconArr, iconCount, iconResult);
308 iconCount++;
309 }
310 napi_set_named_property(env, result, "icons", iconArr);
311
312 // buttonIcons: Array<Resource>
313 iconCount = 0;
314 napi_value resourceArr = nullptr;
315 napi_create_array(env, &resourceArr);
316 auto iconResources = button.GetAllButtonIconResource();
317 for (auto resource : iconResources) {
318 napi_value object;
319 napi_status status = napi_create_object(env, &object);
320 if (status != napi_ok) {
321 ANS_LOGE("Failed to create Configuration object");
322 return NapiGetBoolean(env, false);
323 }
324 if (!SetResourceObject(env, resource, object)) {
325 return NapiGetBoolean(env, false);
326 }
327 napi_set_element(env, resourceArr, iconCount, object);
328 iconCount++;
329 }
330 napi_set_named_property(env, result, "iconsResource", resourceArr);
331
332 return NapiGetBoolean(env, true);
333 }
334
SetCardButton(const napi_env & env,const std::vector<NotificationIconButton> buttons,napi_value & result)335 napi_value Common::SetCardButton(const napi_env &env, const std::vector<NotificationIconButton> buttons,
336 napi_value &result)
337 {
338 ANS_LOGD("enter");
339
340 int iconCount = 0;
341 napi_value value = nullptr;
342 for (auto btn : buttons) {
343 // name: string
344 napi_value item = nullptr;
345 napi_status status = napi_create_object(env, &item);
346 if (status != napi_ok) {
347 ANS_LOGE("Failed to create card button item");
348 return NapiGetBoolean(env, false);
349 }
350 napi_create_string_utf8(env, btn.GetName().c_str(), NAPI_AUTO_LENGTH, &value);
351 napi_set_named_property(env, item, "name", value);
352
353 // text?: string;
354 napi_create_string_utf8(env, btn.GetText().c_str(), NAPI_AUTO_LENGTH, &value);
355 napi_set_named_property(env, item, "text", value);
356
357 // hidePanel?: boolean;
358 napi_get_boolean(env, btn.GetHidePanel(), &value);
359 napi_set_named_property(env, item, "hidePanel", value);
360
361 // iconResource: Resource;
362 napi_value object;
363 status = napi_create_object(env, &object);
364 if (status != napi_ok) {
365 ANS_LOGE("Failed to create card button item.resource");
366 return NapiGetBoolean(env, false);
367 }
368 // resource | pixelMap
369 std::shared_ptr<Media::PixelMap> icon = btn.GetIconImage();
370 if (icon) {
371 object = Media::PixelMapNapi::CreatePixelMap(env, icon);
372 } else {
373 if (!SetResourceObject(env, btn.GetIconResource(), object)) {
374 return NapiGetBoolean(env, false);
375 }
376 }
377
378 napi_set_named_property(env, item, "iconResource", object);
379 status = napi_set_element(env, result, iconCount, item);
380 iconCount++;
381 }
382 return NapiGetBoolean(env, true);
383 }
384
SetNotificationLiveViewContent(const napi_env & env,NotificationBasicContent * basicContent,napi_value & result)385 napi_value Common::SetNotificationLiveViewContent(
386 const napi_env &env, NotificationBasicContent *basicContent, napi_value &result)
387 {
388 ANS_LOGD("enter");
389 napi_value value = nullptr;
390 if (basicContent == nullptr) {
391 ANS_LOGE("BasicContent is null");
392 return NapiGetBoolean(env, false);
393 }
394
395 // lockScreenPicture?: pixelMap
396 if (!SetLockScreenPicture(env, basicContent, result)) {
397 ANS_LOGE("lockScreenPicture is null");
398 return NapiGetBoolean(env, false);
399 }
400
401 auto liveViewContent = static_cast<NotificationLiveViewContent *>(basicContent);
402 if (liveViewContent == nullptr) {
403 ANS_LOGE("LiveViewContent is null");
404 return NapiGetBoolean(env, false);
405 }
406
407 // status: LiveViewStatus
408 LiveViewStatus outType = LiveViewStatus::LIVE_VIEW_BUTT;
409 if (!AnsEnumUtil::LiveViewStatusCToJS(liveViewContent->GetLiveViewStatus(), outType)) {
410 ANS_LOGE("Liveview status is invalid");
411 return NapiGetBoolean(env, false);
412 }
413 napi_create_int32(env, static_cast<int32_t>(outType), &value);
414 napi_set_named_property(env, result, "status", value);
415
416 // version?: uint32_t
417 napi_create_int32(env, static_cast<int32_t>(liveViewContent->GetVersion()), &value);
418 napi_set_named_property(env, result, "version", value);
419
420 // extraInfo?: {[key:string] : any}
421 std::shared_ptr<AAFwk::WantParams> extraInfoData = liveViewContent->GetExtraInfo();
422 if (extraInfoData != nullptr) {
423 napi_value extraInfo = OHOS::AppExecFwk::WrapWantParams(env, *extraInfoData);
424 napi_set_named_property(env, result, "extraInfo", extraInfo);
425 }
426
427 // pictureInfo?: {[key, string]: Array<image.pixelMap>}
428 if (liveViewContent->GetPicture().empty()) {
429 ANS_LOGD("No pictures in live view.");
430 return NapiGetBoolean(env, true);
431 }
432
433 napi_value pictureMapObj = SetLiveViewPictureInfo(env, liveViewContent->GetPicture());
434 if (pictureMapObj == nullptr) {
435 ANS_LOGE("Set live view picture map failed.");
436 return NapiGetBoolean(env, false);
437 }
438 napi_set_named_property(env, result, "pictureInfo", pictureMapObj);
439
440 return NapiGetBoolean(env, true);
441 }
442
SetLiveViewPictureInfo(const napi_env & env,const std::map<std::string,std::vector<std::shared_ptr<Media::PixelMap>>> & pictureMap)443 napi_value Common::SetLiveViewPictureInfo(
444 const napi_env &env, const std::map<std::string, std::vector<std::shared_ptr<Media::PixelMap>>> &pictureMap)
445 {
446 ANS_LOGD("enter");
447
448 napi_value pictureMapObj = nullptr;
449 NAPI_CALL(env, napi_create_object(env, &pictureMapObj));
450
451 for (auto iter = pictureMap.begin(); iter != pictureMap.end(); iter++) {
452 int count = 0;
453 napi_value picturesObj = nullptr;
454 napi_create_array(env, &picturesObj);
455 for (auto picture : iter->second) {
456 napi_value pictureObj = Media::PixelMapNapi::CreatePixelMap(env, picture);
457 napi_set_element(env, picturesObj, count, pictureObj);
458 count++;
459 }
460
461 if (count > 0) {
462 napi_set_named_property(env, pictureMapObj, iter->first.c_str(), picturesObj);
463 }
464 }
465
466 return pictureMapObj;
467 }
468
GetNotificationLocalLiveViewContent(const napi_env & env,const napi_value & result,NotificationRequest & request)469 napi_value Common::GetNotificationLocalLiveViewContent(
470 const napi_env &env, const napi_value &result, NotificationRequest &request)
471 {
472 ANS_LOGD("enter");
473
474 napi_valuetype valuetype = napi_undefined;
475 napi_value contentResult = nullptr;
476 bool hasProperty = false;
477 NAPI_CALL(env, napi_has_named_property(env, result, "systemLiveView", &hasProperty));
478 if (!hasProperty) {
479 ANS_LOGE("Property localLiveView expected.");
480 return nullptr;
481 }
482 napi_get_named_property(env, result, "systemLiveView", &contentResult);
483 NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
484 if (valuetype != napi_object) {
485 ANS_LOGE("Wrong argument type. Object expected.");
486 return nullptr;
487 }
488
489 std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> localLiveViewContent =
490 std::make_shared<OHOS::Notification::NotificationLocalLiveViewContent>();
491 if (localLiveViewContent == nullptr) {
492 ANS_LOGE("localLiveViewContent is null");
493 return nullptr;
494 }
495
496 if (GetNotificationLocalLiveViewContentDetailed(env, contentResult, localLiveViewContent) == nullptr) {
497 return nullptr;
498 }
499
500 request.SetContent(std::make_shared<NotificationContent>(localLiveViewContent));
501
502 // set isOnGoing of live view true
503 request.SetInProgress(true);
504
505 return NapiGetNull(env);
506 }
507
GetNotificationLocalLiveViewCapsule(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)508 napi_value Common::GetNotificationLocalLiveViewCapsule(
509 const napi_env &env, const napi_value &contentResult,
510 std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
511 {
512 napi_value capsuleResult = nullptr;
513 napi_valuetype valuetype = napi_undefined;
514 bool hasProperty = false;
515 size_t strLen = 0;
516 char str[STR_MAX_SIZE] = {0};
517 std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
518 napi_value result = nullptr;
519 int32_t intValue;
520
521 ANS_LOGD("enter");
522
523 NAPI_CALL(env, napi_has_named_property(env, contentResult, "capsule", &hasProperty));
524
525 napi_get_named_property(env, contentResult, "capsule", &capsuleResult);
526 NAPI_CALL(env, napi_typeof(env, capsuleResult, &valuetype));
527 if (valuetype != napi_object) {
528 ANS_LOGE("Wrong argument type. Object expected.");
529 return nullptr;
530 }
531
532 NotificationCapsule capsule;
533
534 NAPI_CALL(env, napi_has_named_property(env, capsuleResult, "title", &hasProperty));
535 if (hasProperty) {
536 napi_get_named_property(env, capsuleResult, "title", &result);
537 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
538 if (valuetype != napi_string) {
539 ANS_LOGE("Wrong argument type. String expected.");
540 std::string msg = "Incorrect parameter types. The type of title must be string.";
541 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
542 return nullptr;
543 }
544
545 NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
546 capsule.SetTitle(str);
547 ANS_LOGD("capsule title = %{public}s", str);
548 }
549
550 NAPI_CALL(env, napi_has_named_property(env, capsuleResult, "backgroundColor", &hasProperty));
551 if (hasProperty) {
552 napi_get_named_property(env, capsuleResult, "backgroundColor", &result);
553 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
554 if (valuetype != napi_string) {
555 ANS_LOGE("Wrong argument type. String expected.");
556 std::string msg = "Incorrect parameter types. The type of backgroundColor must be string.";
557 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
558 return nullptr;
559 }
560 NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
561 capsule.SetBackgroundColor(str);
562 ANS_LOGD("capsule backgroundColor = %{public}s", str);
563 }
564
565 NAPI_CALL(env, napi_has_named_property(env, capsuleResult, "content", &hasProperty));
566 if (hasProperty) {
567 napi_get_named_property(env, capsuleResult, "content", &result);
568 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
569 if (valuetype != napi_string) {
570 ANS_LOGE("Wrong argument type. String expected.");
571 std::string msg = "Incorrect parameter types. The type of content must be string.";
572 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
573 return nullptr;
574 }
575 NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
576 capsule.SetContent(str);
577 ANS_LOGD("capsule content = %{public}s", str);
578 }
579
580 NAPI_CALL(env, napi_has_named_property(env, capsuleResult, "icon", &hasProperty));
581 if (hasProperty) {
582 napi_get_named_property(env, capsuleResult, "icon", &result);
583 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
584 if (valuetype != napi_object) {
585 ANS_LOGE("Wrong argument type. Object expected.");
586 std::string msg = "Incorrect parameter types. The type of icon must be object.";
587 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
588 return nullptr;
589 }
590 pixelMap = Media::PixelMapNapi::GetPixelMap(env, result);
591 if (pixelMap == nullptr) {
592 ANS_LOGE("Invalid object pixelMap");
593 return nullptr;
594 }
595 capsule.SetIcon(pixelMap);
596 ANS_LOGD("capsule icon = %{public}d", pixelMap->GetWidth());
597 }
598
599 //time?: number
600 NAPI_CALL(env, napi_has_named_property(env, capsuleResult, "time", &hasProperty));
601 if (hasProperty) {
602 napi_get_named_property(env, capsuleResult, "time", &result);
603 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
604 if (valuetype != napi_number) {
605 ANS_LOGE("Wrong argument type. Number expected.");
606 return nullptr;
607 }
608 napi_get_value_int32(env, result, &intValue);
609 capsule.SetTime(intValue);
610 }
611
612 NAPI_CALL(env, napi_has_named_property(env, capsuleResult, "capsuleButtons", &hasProperty));
613 if (hasProperty) {
614 napi_get_named_property(env, capsuleResult, "capsuleButtons", &result);
615 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
616 if (valuetype != napi_object) {
617 ANS_LOGE("Wrong argument type. Object expected.");
618 return nullptr;
619 }
620 if (hasProperty && GetNotificationLocalLiveViewCapsuleCardButton(env, result, capsule) == nullptr) {
621 return nullptr;
622 }
623 }
624
625 content->SetCapsule(capsule);
626 content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE);
627
628 return NapiGetNull(env);
629 }
630
GetNotificationLocalLiveViewCardButton(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)631 napi_value Common::GetNotificationLocalLiveViewCardButton(
632 const napi_env &env, const napi_value &contentResult,
633 std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
634 {
635 napi_value buttonResult = nullptr;
636
637 ANS_LOGD("enter");
638 napi_get_named_property(env, contentResult, "cardButtons", &buttonResult);
639
640 // 解析iconbutton数组
641 std::vector<NotificationIconButton> cardButtons;
642 if (GetNotificationIconButton(env, buttonResult, cardButtons, BUTTON_MAX_SIZE) == nullptr) {
643 return nullptr;
644 }
645
646 content->SetCardButton(cardButtons);
647 content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::CARD_BUTTON);
648 return NapiGetNull(env);
649 }
650
GetNotificationLocalLiveViewCapsuleCardButton(const napi_env & env,const napi_value & capsuletResult,OHOS::Notification::NotificationCapsule & capsule)651 napi_value Common::GetNotificationLocalLiveViewCapsuleCardButton(
652 const napi_env &env, const napi_value &capsuletResult,
653 OHOS::Notification::NotificationCapsule &capsule)
654 {
655 ANS_LOGD("enter");
656 std::vector<NotificationIconButton> cardButtons;
657 if (GetNotificationIconButton(env, capsuletResult, cardButtons, CAPSULE_BTN_MAX_SIZE) == nullptr) {
658 return nullptr;
659 }
660
661 capsule.SetCapsuleButton(cardButtons);
662 return NapiGetNull(env);
663 }
664
GetNotificationIconButton(const napi_env & env,const napi_value & buttonResult,std::vector<NotificationIconButton> & cardButtons,const uint32_t maxLen)665 napi_value Common::GetNotificationIconButton(
666 const napi_env &env, const napi_value &buttonResult,
667 std::vector<NotificationIconButton> &cardButtons, const uint32_t maxLen)
668 {
669 // cardButton_item?: NotificationIconButton;
670 napi_value cardButton = nullptr;
671 napi_value result = nullptr;
672 napi_valuetype valuetype = napi_undefined;
673
674 bool isArray = false;
675 bool boolValue = false;
676 uint32_t length = 0;
677
678 size_t strLen = 0;
679 char str[STR_MAX_SIZE] = {0};
680 bool hasProperty = false;
681
682 ANS_LOGD("enter");
683 napi_is_array(env, buttonResult, &isArray);
684 if (!isArray) {
685 ANS_LOGE("Property names is expected to be an array.");
686 return nullptr;
687 }
688 napi_get_array_length(env, buttonResult, &length);
689 if (length > maxLen) {
690 length = maxLen;
691 }
692
693 for (size_t i = 0; i < length; i++) {
694 napi_get_element(env, buttonResult, i, &cardButton);
695
696 NAPI_CALL(env, napi_typeof(env, cardButton, &valuetype));
697 if (valuetype != napi_object) {
698 ANS_LOGE("Wrong argument type. Object expected.");
699 return nullptr;
700 }
701 // 数组item
702 NotificationIconButton button;
703 // name: string
704 NAPI_CALL(env, napi_has_named_property(env, cardButton, "name", &hasProperty));
705 if (!hasProperty) {
706 ANS_LOGE("Property name expected.");
707 return nullptr;
708 } else {
709 napi_get_named_property(env, cardButton, "name", &result);
710 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
711 if (valuetype != napi_string) {
712 ANS_LOGE("Wrong argument type. String expected.");
713 return nullptr;
714 }
715 NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
716 button.SetName(str);
717 }
718
719 // iconResource: Resource
720 NAPI_CALL(env, napi_has_named_property(env, cardButton, "iconResource", &hasProperty));
721 if (!hasProperty) {
722 ANS_LOGE("Property iconResource expected.");
723 return nullptr;
724 } else {
725 // if icon type is Rersouce, get the resource object and return.
726 napi_value iconResource = nullptr;
727 napi_get_named_property(env, cardButton, "iconResource", &iconResource);
728 NAPI_CALL(env, napi_typeof(env, iconResource, &valuetype));
729 if (valuetype != napi_object) {
730 ANS_LOGE("Wrong argument type. iconResource Object expected.");
731 return nullptr;
732 }
733
734 // icon?: Resource
735 auto resource = std::make_shared<ResourceManager::Resource>();
736 if (Common::GetResourceObject(env, resource, iconResource) == nullptr) {
737 ANS_LOGI("Invalid icon resource object or not resource.");
738 } else {
739 button.SetIconResource(resource);
740 }
741
742 // icon?: image.PixelMap
743 auto pixelMap = Media::PixelMapNapi::GetPixelMap(env, iconResource);
744 if (pixelMap == nullptr) {
745 ANS_LOGE("Invalid pixelMap object is null.");
746 } else {
747 button.SetIconImage(pixelMap);
748 }
749 }
750
751 // text?: string
752 NAPI_CALL(env, napi_has_named_property(env, cardButton, "text", &hasProperty));
753 if (hasProperty) {
754 napi_get_named_property(env, cardButton, "text", &result);
755 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
756 if (valuetype != napi_string) {
757 ANS_LOGE("Wrong argument type. String expected.");
758 return nullptr;
759 }
760 NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
761 button.SetText(str);
762 }
763
764 // hidePanel?: boolean;
765 NAPI_CALL(env, napi_has_named_property(env, cardButton, "hidePanel", &hasProperty));
766 if (hasProperty) {
767 napi_get_named_property(env, cardButton, "hidePanel", &result);
768 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
769 if (valuetype != napi_boolean) {
770 ANS_LOGE("Wrong argument type. bool expected.");
771 return nullptr;
772 }
773 napi_get_value_bool(env, result, &boolValue);
774 button.SetHidePanel(boolValue);
775 }
776
777 cardButtons.push_back(button);
778 ANS_LOGD("icon button = %{public}s", button.Dump().c_str());
779 }
780
781 return NapiGetNull(env);
782 }
783
GetResourceObject(napi_env env,std::shared_ptr<ResourceManager::Resource> & resource,napi_value & value)784 napi_value Common::GetResourceObject(napi_env env,
785 std::shared_ptr<ResourceManager::Resource> &resource, napi_value &value)
786 {
787 napi_value name;
788 size_t strLen = 0;
789 char str[STR_MAX_SIZE] = {0};
790 std::vector<std::string> typeName = {"bundleName", "moduleName"};
791 for (const std::string& type: typeName) {
792 napi_status status = napi_get_named_property(env, value, type.c_str(), &name);
793 if (status != napi_ok || name == nullptr) {
794 ANS_LOGE("Failed to get resource name property");
795 return nullptr;
796 }
797 napi_valuetype valueType = napi_valuetype::napi_undefined;
798 NAPI_CALL(env, napi_typeof(env, name, &valueType));
799 if (valueType != napi_string) {
800 ANS_LOGE("Failed to get resource type %{public}d", valueType);
801 return nullptr;
802 }
803 NAPI_CALL(env, napi_get_value_string_utf8(env, name, str, STR_MAX_SIZE - 1, &strLen));
804 if (type == "bundleName") {
805 resource->bundleName = str;
806 } else if (type == "moduleName") {
807 resource->moduleName = str;
808 }
809 }
810
811 napi_value id;
812 napi_status status = napi_get_named_property(env, value, "id", &id);
813 if (status != napi_ok || id == nullptr) {
814 ANS_LOGE("Failed to get resource id property");
815 return nullptr;
816 }
817 napi_valuetype valueType = napi_valuetype::napi_undefined;
818 napi_typeof(env, id, &valueType);
819 if (valueType != napi_number) {
820 ANS_LOGE("Failed to get resource name string");
821 std::string msg = "Incorrect parameter types. The type of id must be number.";
822 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
823 return nullptr;
824 }
825 int32_t resId = 0;
826 status = napi_get_value_int32(env, id, &resId);
827 if (status != napi_ok) {
828 ANS_LOGE("Wrong argument type. Object expected.");
829 return nullptr;
830 }
831 resource->id = resId;
832 ANS_LOGE("Get to get resource bundleName %{public}s moduleName %{public}s id %{public}d",
833 resource->bundleName.c_str(), resource->moduleName.c_str(), resource->id);
834 return NapiGetNull(env);
835 }
836
GetNotificationLocalLiveViewButton(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)837 napi_value Common::GetNotificationLocalLiveViewButton(
838 const napi_env &env, const napi_value &contentResult,
839 std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
840 {
841 napi_value result = nullptr;
842 napi_valuetype valuetype = napi_undefined;
843 bool isArray = false;
844 uint32_t length = 0;
845 napi_value buttonResult = nullptr;
846 bool hasProperty = false;
847 char str[STR_MAX_SIZE] = {0};
848 size_t strLen = 0;
849
850 ANS_LOGD("enter");
851
852 napi_get_named_property(env, contentResult, "button", &buttonResult);
853 NAPI_CALL(env, napi_typeof(env, buttonResult, &valuetype));
854 if (valuetype != napi_object) {
855 ANS_LOGE("Wrong argument type. Object expected.");
856 return nullptr;
857 }
858
859 NotificationLocalLiveViewButton button;
860
861 NAPI_CALL(env, napi_has_named_property(env, buttonResult, "names", &hasProperty));
862 if (hasProperty) {
863 napi_get_named_property(env, buttonResult, "names", &result);
864 napi_is_array(env, result, &isArray);
865 if (!isArray) {
866 ANS_LOGE("Property names is expected to be an array.");
867 return nullptr;
868 }
869 napi_get_array_length(env, result, &length);
870 for (size_t i = 0; i < length; i++) {
871 napi_value buttonName = nullptr;
872 napi_get_element(env, result, i, &buttonName);
873 NAPI_CALL(env, napi_typeof(env, buttonName, &valuetype));
874 if (valuetype != napi_string) {
875 ANS_LOGE("Wrong argument type. String expected.");
876 return nullptr;
877 }
878 NAPI_CALL(env, napi_get_value_string_utf8(env, buttonName, str, STR_MAX_SIZE - 1, &strLen));
879 button.addSingleButtonName(str);
880 ANS_LOGD("button buttonName = %{public}s.", str);
881 }
882 }
883
884 NAPI_CALL(env, napi_has_named_property(env, buttonResult, "icons", &hasProperty));
885 if (hasProperty) {
886 napi_get_named_property(env, buttonResult, "icons", &result);
887 napi_is_array(env, result, &isArray);
888 if (!isArray) {
889 ANS_LOGE("Property icons is expected to be an array.");
890 return nullptr;
891 }
892 napi_get_array_length(env, result, &length);
893 for (size_t i = 0; i < length; i++) {
894 napi_value buttonIcon = nullptr;
895 std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
896 napi_get_element(env, result, i, &buttonIcon);
897 NAPI_CALL(env, napi_typeof(env, buttonIcon, &valuetype));
898 if (valuetype != napi_object) {
899 ANS_LOGE("Wrong argument type. Object expected.");
900 return nullptr;
901 }
902 pixelMap = Media::PixelMapNapi::GetPixelMap(env, buttonIcon);
903 if (pixelMap != nullptr && static_cast<uint32_t>(pixelMap->GetByteCount()) <= MAX_ICON_SIZE) {
904 button.addSingleButtonIcon(pixelMap);
905 } else {
906 ANS_LOGE("Invalid pixelMap object or pixelMap is over size.");
907 return nullptr;
908 }
909 }
910 }
911
912 NAPI_CALL(env, napi_has_named_property(env, buttonResult, "iconsResource", &hasProperty));
913 if (hasProperty) {
914 napi_get_named_property(env, buttonResult, "iconsResource", &result);
915 napi_is_array(env, result, &isArray);
916 if (!isArray) {
917 ANS_LOGE("Property icon resource is expected to be an array.");
918 return nullptr;
919 }
920 napi_get_array_length(env, result, &length);
921 for (size_t i = 0; i < length; i++) {
922 napi_value iconResource = nullptr;
923 auto resource = std::make_shared<ResourceManager::Resource>();
924 napi_get_element(env, result, i, &iconResource);
925 NAPI_CALL(env, napi_typeof(env, iconResource, &valuetype));
926 if (valuetype != napi_object) {
927 ANS_LOGE("Wrong argument type. Object expected.");
928 return nullptr;
929 }
930 if (Common::GetResourceObject(env, resource, iconResource) == nullptr) {
931 ANS_LOGW("Invalid icon resource object.");
932 return nullptr;
933 } else {
934 button.addSingleButtonIconResource(resource);
935 }
936 }
937 }
938
939 ANS_LOGD("button buttonIcon = %{public}s", str);
940 content->SetButton(button);
941 content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON);
942
943 return NapiGetNull(env);
944 }
945
GetNotificationLocalLiveViewProgress(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)946 napi_value Common::GetNotificationLocalLiveViewProgress(const napi_env &env, const napi_value &contentResult,
947 std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
948 {
949 napi_value result = nullptr;
950 napi_valuetype valuetype = napi_undefined;
951 bool hasProperty = false;
952 int32_t intValue = -1;
953 bool boolValue = false;
954 napi_value progressResult = nullptr;
955
956 ANS_LOGD("enter");
957
958 napi_get_named_property(env, contentResult, "progress", &progressResult);
959 NAPI_CALL(env, napi_typeof(env, progressResult, &valuetype));
960 if (valuetype != napi_object) {
961 ANS_LOGE("Wrong argument type. Object expected.");
962 return nullptr;
963 }
964
965 NotificationProgress progress;
966
967 NAPI_CALL(env, napi_has_named_property(env, progressResult, "maxValue", &hasProperty));
968 if (hasProperty) {
969 napi_get_named_property(env, progressResult, "maxValue", &result);
970 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
971 if (valuetype != napi_number) {
972 std::string msg = "Incorrect parameter types. The type of maxValue must be number.";
973 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
974 ANS_LOGE("Wrong argument type. Number expected.");
975 return nullptr;
976 }
977 napi_get_value_int32(env, result, &intValue);
978 progress.SetMaxValue(intValue);
979 ANS_LOGD("progress intValue = %{public}d", intValue);
980 }
981
982 NAPI_CALL(env, napi_has_named_property(env, progressResult, "currentValue", &hasProperty));
983 if (hasProperty) {
984 napi_get_named_property(env, progressResult, "currentValue", &result);
985 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
986 if (valuetype != napi_number) {
987 ANS_LOGE("Wrong argument type. Number expected.");
988 std::string msg = "Incorrect parameter types. The type of currentValue must be number.";
989 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
990 return nullptr;
991 }
992 napi_get_value_int32(env, result, &intValue);
993 progress.SetCurrentValue(intValue);
994 ANS_LOGD("progress currentValue = %{public}d", intValue);
995 }
996
997 NAPI_CALL(env, napi_has_named_property(env, progressResult, "isPercentage", &hasProperty));
998 if (hasProperty) {
999 napi_get_named_property(env, progressResult, "isPercentage", &result);
1000 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1001 if (valuetype != napi_boolean) {
1002 ANS_LOGE("Wrong argument type. bool expected.");
1003 std::string msg = "Incorrect parameter types. The type of isPercentage must be bool.";
1004 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1005 return nullptr;
1006 }
1007 napi_get_value_bool(env, result, &boolValue);
1008 progress.SetIsPercentage(boolValue);
1009 ANS_LOGD("progress isPercentage = %{public}d", boolValue);
1010 }
1011
1012 content->SetProgress(progress);
1013 content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::PROGRESS);
1014
1015 return NapiGetNull(env);
1016 }
1017
GetNotificationLocalLiveViewTime(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)1018 napi_value Common::GetNotificationLocalLiveViewTime(const napi_env &env, const napi_value &contentResult,
1019 std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
1020 {
1021 napi_value result = nullptr;
1022 napi_valuetype valuetype = napi_undefined;
1023 bool hasProperty = false;
1024 int32_t intValue = -1;
1025 bool boolValue = false;
1026 napi_value timeResult = nullptr;
1027
1028 ANS_LOGD("enter");
1029
1030 napi_get_named_property(env, contentResult, "time", &timeResult);
1031 NAPI_CALL(env, napi_typeof(env, timeResult, &valuetype));
1032 if (valuetype != napi_object) {
1033 ANS_LOGE("Wrong argument type. Object expected.");
1034 return nullptr;
1035 }
1036
1037 NotificationTime time;
1038
1039 NAPI_CALL(env, napi_has_named_property(env, timeResult, "initialTime", &hasProperty));
1040 if (hasProperty) {
1041 napi_get_named_property(env, timeResult, "initialTime", &result);
1042 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1043 if (valuetype != napi_number) {
1044 ANS_LOGE("Wrong argument type. Number expected.");
1045 std::string msg = "Incorrect parameter types. The type of initialTime must be number.";
1046 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1047 return nullptr;
1048 }
1049 napi_get_value_int32(env, result, &intValue);
1050 time.SetInitialTime(intValue);
1051 content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::INITIAL_TIME);
1052 ANS_LOGD("time initialTime = %{public}d", intValue);
1053 }
1054
1055 NAPI_CALL(env, napi_has_named_property(env, timeResult, "isCountDown", &hasProperty));
1056 if (hasProperty) {
1057 napi_get_named_property(env, timeResult, "isCountDown", &result);
1058 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1059 if (valuetype != napi_boolean) {
1060 ANS_LOGE("Wrong argument type. bool expected.");
1061 std::string msg = "Incorrect parameter types. The type of isCountDown must be bool.";
1062 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1063 return nullptr;
1064 }
1065 napi_get_value_bool(env, result, &boolValue);
1066 time.SetIsCountDown(boolValue);
1067 ANS_LOGD("time isCountDown = %{public}d", boolValue);
1068 }
1069
1070 NAPI_CALL(env, napi_has_named_property(env, timeResult, "isPaused", &hasProperty));
1071 if (hasProperty) {
1072 napi_get_named_property(env, timeResult, "isPaused", &result);
1073 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1074 if (valuetype != napi_boolean) {
1075 ANS_LOGE("Wrong argument type. bool expected.");
1076 std::string msg = "Incorrect parameter types. The type of isPaused must be bool.";
1077 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1078 return nullptr;
1079 }
1080 napi_get_value_bool(env, result, &boolValue);
1081 time.SetIsPaused(boolValue);
1082 ANS_LOGD("time isPaused = %{public}d", boolValue);
1083 }
1084
1085 NAPI_CALL(env, napi_has_named_property(env, timeResult, "isInTitle", &hasProperty));
1086 if (hasProperty) {
1087 napi_get_named_property(env, timeResult, "isInTitle", &result);
1088 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1089 if (valuetype != napi_boolean) {
1090 ANS_LOGE("Wrong argument type. bool expected.");
1091 std::string msg = "Incorrect parameter types. The type of isInTitle must be bool.";
1092 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1093 return nullptr;
1094 }
1095 napi_get_value_bool(env, result, &boolValue);
1096 time.SetIsInTitle(boolValue);
1097 ANS_LOGD("time isInTitle = %{public}d", boolValue);
1098 }
1099
1100 content->SetTime(time);
1101 content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::TIME);
1102
1103 return NapiGetNull(env);
1104 }
1105
GetNotificationLocalLiveViewContentDetailed(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)1106 napi_value Common::GetNotificationLocalLiveViewContentDetailed(
1107 const napi_env &env, const napi_value &contentResult,
1108 std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
1109 {
1110 bool hasProperty = false;
1111 int32_t type = -1;
1112 napi_value result = nullptr;
1113 napi_valuetype valuetype = napi_undefined;
1114
1115 ANS_LOGD("enter");
1116
1117 //title, text
1118 if (GetNotificationBasicContentDetailed(env, contentResult, content) == nullptr) {
1119 ANS_LOGE("Basic content get fail.");
1120 return nullptr;
1121 }
1122
1123 // typeCode
1124 NAPI_CALL(env, napi_has_named_property(env, contentResult, "typeCode", &hasProperty));
1125 if (!hasProperty) {
1126 ANS_LOGE("Property typeCode expected.");
1127 return nullptr;
1128 }
1129 napi_get_named_property(env, contentResult, "typeCode", &result);
1130 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1131 if (valuetype != napi_number) {
1132 ANS_LOGE("Wrong argument typeCode. Number expected.");
1133 std::string msg = "Incorrect parameter types. The type of typeCode must be number.";
1134 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1135 return nullptr;
1136 }
1137 napi_get_value_int32(env, result, &type);
1138 content->SetType(type);
1139 ANS_LOGD("localLiveView type = %{public}d", type);
1140
1141 //capsule?
1142 NAPI_CALL(env, napi_has_named_property(env, contentResult, "capsule", &hasProperty));
1143 if (hasProperty && GetNotificationLocalLiveViewCapsule(env, contentResult, content) == nullptr) {
1144 return nullptr;
1145 }
1146
1147 //button?
1148 NAPI_CALL(env, napi_has_named_property(env, contentResult, "button", &hasProperty));
1149 if (hasProperty && GetNotificationLocalLiveViewButton(env, contentResult, content) == nullptr) {
1150 return nullptr;
1151 }
1152
1153 //cardButton?
1154 NAPI_CALL(env, napi_has_named_property(env, contentResult, "cardButtons", &hasProperty));
1155 if (hasProperty && GetNotificationLocalLiveViewCardButton(env, contentResult, content) == nullptr) {
1156 return nullptr;
1157 }
1158
1159 // liveViewType?: LiveViewTypes
1160 NAPI_CALL(env, napi_has_named_property(env, contentResult, "liveViewType", &hasProperty));
1161 NotificationLocalLiveViewContent::LiveViewTypes outType = NotificationLocalLiveViewContent::LiveViewTypes::LIVE_VIEW_ACTIVITY;
1162 if (hasProperty && AppExecFwk::UnwrapInt32ByPropertyName(env, contentResult, "liveViewType", type)) {
1163 if (!AnsEnumUtil::LiveViewTypesJSToC(LiveViewTypes(type), outType)) {
1164 ANS_LOGE("The liveview types is not valid.");
1165 return nullptr;
1166 }
1167 }
1168 content->SetLiveViewType(outType);
1169
1170 //progress?
1171 NAPI_CALL(env, napi_has_named_property(env, contentResult, "progress", &hasProperty));
1172 if (hasProperty && GetNotificationLocalLiveViewProgress(env, contentResult, content) == nullptr) {
1173 return nullptr;
1174 }
1175
1176 //time?
1177 NAPI_CALL(env, napi_has_named_property(env, contentResult, "time", &hasProperty));
1178 if (hasProperty && GetNotificationLocalLiveViewTime(env, contentResult, content) == nullptr) {
1179 return nullptr;
1180 }
1181
1182 return NapiGetNull(env);
1183 }
1184
GetNotificationLiveViewContent(const napi_env & env,const napi_value & result,NotificationRequest & request)1185 napi_value Common::GetNotificationLiveViewContent(
1186 const napi_env &env, const napi_value &result, NotificationRequest &request)
1187 {
1188 ANS_LOGD("enter");
1189
1190 napi_value contentResult = AppExecFwk::GetPropertyValueByPropertyName(env, result, "liveView", napi_object);
1191 if (contentResult == nullptr) {
1192 ANS_LOGE("Property liveView expected.");
1193 return nullptr;
1194 }
1195
1196 std::shared_ptr<NotificationLiveViewContent> liveViewContent = std::make_shared<NotificationLiveViewContent>();
1197 if (liveViewContent == nullptr) {
1198 ANS_LOGE("LiveViewContent is null");
1199 return nullptr;
1200 }
1201
1202 if (GetNotificationLiveViewContentDetailed(env, contentResult, liveViewContent) == nullptr) {
1203 return nullptr;
1204 }
1205
1206 request.SetContent(std::make_shared<NotificationContent>(liveViewContent));
1207
1208 return NapiGetNull(env);
1209 }
1210
GetNotificationLiveViewContentDetailed(const napi_env & env,const napi_value & contentResult,std::shared_ptr<NotificationLiveViewContent> & liveViewContent)1211 napi_value Common::GetNotificationLiveViewContentDetailed(
1212 const napi_env &env, const napi_value &contentResult,
1213 std::shared_ptr<NotificationLiveViewContent> &liveViewContent)
1214 {
1215 ANS_LOGD("enter");
1216
1217 // lockScreenPicture?: pixelMap
1218 if (GetLockScreenPicture(env, contentResult, liveViewContent) == nullptr) {
1219 ANS_LOGE("Failed to get lockScreenPicture from liveView content.");
1220 return nullptr;
1221 }
1222
1223 // status: NotificationLiveViewContent::LiveViewStatus
1224 int32_t status = 0;
1225 if (!AppExecFwk::UnwrapInt32ByPropertyName(env, contentResult, "status", status)) {
1226 ANS_LOGE("Failed to get status from liveView content.");
1227 return nullptr;
1228 }
1229 NotificationLiveViewContent::LiveViewStatus outType = NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_BUTT;
1230 if (!AnsEnumUtil::LiveViewStatusJSToC(LiveViewStatus(status), outType)) {
1231 ANS_LOGE("The liveview status is not valid.");
1232 return nullptr;
1233 }
1234 liveViewContent->SetLiveViewStatus(outType);
1235
1236 // version?: uint32_t
1237 napi_value jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult,
1238 "version", napi_number);
1239 if (jsValue != nullptr) {
1240 int32_t version = NotificationLiveViewContent::MAX_VERSION;
1241 NAPI_CALL(env, napi_get_value_int32(env, jsValue, &version));
1242 liveViewContent->SetVersion(version);
1243 }
1244
1245 // extraInfo?: {[key:string] : any}
1246 jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult, "extraInfo", napi_object);
1247 if (jsValue != nullptr) {
1248 std::shared_ptr<AAFwk::WantParams> extras = std::make_shared<AAFwk::WantParams>();
1249 if (!OHOS::AppExecFwk::UnwrapWantParams(env, jsValue, *extras)) {
1250 return nullptr;
1251 }
1252 liveViewContent->SetExtraInfo(extras);
1253 }
1254
1255 //isOnlyLocalUpdate_?: boolean
1256 bool isLocalUpdateOnly = false;
1257 if (AppExecFwk::UnwrapBooleanByPropertyName(env, contentResult, "isLocalUpdateOnly", isLocalUpdateOnly)) {
1258 liveViewContent->SetIsOnlyLocalUpdate(isLocalUpdateOnly);
1259 }
1260
1261 // pictureInfo?: {[key, string]: Array<image.pixelMap>}
1262 jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult, "pictureInfo", napi_object);
1263 if (jsValue == nullptr) {
1264 ANS_LOGI("No picture maps.");
1265 return NapiGetNull(env);
1266 }
1267
1268 std::map<std::string, std::vector<std::shared_ptr<Media::PixelMap>>> pictureMap;
1269 if (GetLiveViewPictureInfo(env, jsValue, pictureMap) == nullptr) {
1270 ANS_LOGE("Failed to get picture map from liveView content.");
1271 return nullptr;
1272 }
1273 liveViewContent->SetPicture(pictureMap);
1274
1275 return NapiGetNull(env);
1276 }
1277
GetLiveViewPictures(const napi_env & env,const napi_value & picturesObj,std::vector<std::shared_ptr<Media::PixelMap>> & pictures)1278 napi_value Common::GetLiveViewPictures(
1279 const napi_env &env, const napi_value &picturesObj,
1280 std::vector<std::shared_ptr<Media::PixelMap>> &pictures)
1281 {
1282 ANS_LOGD("enter");
1283
1284 bool isArray = false;
1285 napi_is_array(env, picturesObj, &isArray);
1286 if (!isArray) {
1287 ANS_LOGE("The picture is not array.");
1288 return nullptr;
1289 }
1290
1291 uint32_t length = 0;
1292 napi_get_array_length(env, picturesObj, &length);
1293 if (length == 0) {
1294 ANS_LOGE("The array is empty.");
1295 return nullptr;
1296 }
1297
1298 for (uint32_t i = 0; i < length; ++i) {
1299 napi_value pictureObj = nullptr;
1300 napi_get_element(env, picturesObj, i, &pictureObj);
1301 if (!AppExecFwk::IsTypeForNapiValue(env, pictureObj, napi_object)) {
1302 ANS_LOGE("Wrong argument type. object expected.");
1303 break;
1304 }
1305
1306 std::shared_ptr<Media::PixelMap> pixelMap = Media::PixelMapNapi::GetPixelMap(env, pictureObj);
1307 if (pixelMap == nullptr) {
1308 ANS_LOGE("Invalid pixelMap.");
1309 break;
1310 }
1311
1312 pictures.emplace_back(pixelMap);
1313 }
1314
1315 return NapiGetNull(env);
1316 }
1317
GetLiveViewPictureInfo(const napi_env & env,const napi_value & pictureMapObj,std::map<std::string,std::vector<std::shared_ptr<Media::PixelMap>>> & pictureMap)1318 napi_value Common::GetLiveViewPictureInfo(
1319 const napi_env &env, const napi_value &pictureMapObj,
1320 std::map<std::string, std::vector<std::shared_ptr<Media::PixelMap>>> &pictureMap)
1321 {
1322 ANS_LOGD("enter");
1323
1324 napi_value pictureNamesObj = nullptr;
1325 uint32_t length = 0;
1326 if (napi_get_property_names(env, pictureMapObj, &pictureNamesObj) != napi_ok) {
1327 ANS_LOGE("Get picture names failed.");
1328 return nullptr;
1329 }
1330 napi_get_array_length(env, pictureNamesObj, &length);
1331 if (length == 0) {
1332 ANS_LOGE("The pictures name is empty.");
1333 return nullptr;
1334 }
1335
1336 napi_value pictureNameObj = nullptr;
1337 napi_value picturesObj = nullptr;
1338 for (uint32_t index = 0; index < length; index++) {
1339 napi_get_element(env, pictureNamesObj, index, &pictureNameObj);
1340 std::string pictureName = AppExecFwk::UnwrapStringFromJS(env, pictureNameObj);
1341 ANS_LOGD("%{public}s called, get pictures of %{public}s.", __func__, pictureName.c_str());
1342 napi_get_named_property(env, pictureMapObj, pictureName.c_str(), &picturesObj);
1343
1344 std::vector<std::shared_ptr<Media::PixelMap>> pictures;
1345 if (!GetLiveViewPictures(env, picturesObj, pictures)) {
1346 ANS_LOGE("Get pictures of %{public}s failed.", pictureName.c_str());
1347 break;
1348 }
1349
1350 pictureMap[pictureName] = pictures;
1351 }
1352
1353 return NapiGetNull(env);
1354 }
1355 }
1356 }
1357