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 {
SetNotificationLocalLiveViewContent(const napi_env & env,NotificationBasicContent * basicContent,napi_value & result)33 napi_value Common::SetNotificationLocalLiveViewContent(
34 const napi_env &env, NotificationBasicContent *basicContent, napi_value &result)
35 {
36 ANS_LOGD("enter");
37 napi_value value = nullptr;
38 if (basicContent == nullptr) {
39 ANS_LOGE("basicContent is null");
40 return NapiGetBoolean(env, false);
41 }
42 OHOS::Notification::NotificationLocalLiveViewContent *localLiveViewContent =
43 static_cast<OHOS::Notification::NotificationLocalLiveViewContent *>(basicContent);
44 if (localLiveViewContent == nullptr) {
45 ANS_LOGE("localLiveViewContent is null");
46 return NapiGetBoolean(env, false);
47 }
48
49 if (!SetNotificationBasicContent(env, localLiveViewContent, result)) {
50 ANS_LOGE("SetNotificationBasicContent call failed");
51 return NapiGetBoolean(env, false);
52 }
53
54 // typeCode: int32_t
55 napi_create_int32(env, localLiveViewContent->GetType(), &value);
56 napi_set_named_property(env, result, "typeCode", value);
57
58 // capsule: NotificationCapsule
59 if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE)) {
60 napi_value capsule = nullptr;
61 napi_create_object(env, &capsule);
62 if (!SetCapsule(env, localLiveViewContent->GetCapsule(), capsule)) {
63 ANS_LOGE("SetCapsule call failed");
64 return NapiGetBoolean(env, false);
65 }
66 napi_set_named_property(env, result, "capsule", capsule);
67 }
68
69 // button: NotificationLocalLiveViewButton
70 if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON)) {
71 napi_value button = nullptr;
72 napi_create_object(env, &button);
73 if (!SetButton(env, localLiveViewContent->GetButton(), button)) {
74 ANS_LOGE("SetButton call failed");
75 return NapiGetBoolean(env, false);
76 }
77 napi_set_named_property(env, result, "button", button);
78 }
79
80 // progress: NotificationProgress
81 if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::PROGRESS)) {
82 napi_value progress = nullptr;
83 napi_create_object(env, &progress);
84 if (!SetProgress(env, localLiveViewContent->GetProgress(), progress)) {
85 ANS_LOGE("SetProgress call failed");
86 return NapiGetBoolean(env, false);
87 }
88 napi_set_named_property(env, result, "progress", progress);
89 }
90
91 // time: NotificationTime
92 if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::TIME)) {
93 napi_value time = nullptr;
94 napi_create_object(env, &time);
95 if (!SetTime(env, localLiveViewContent->GetTime(), time)) {
96 ANS_LOGE("SetMessageUser call failed");
97 return NapiGetBoolean(env, false);
98 }
99 napi_set_named_property(env, result, "time", time);
100 }
101
102 return NapiGetBoolean(env, true);
103 }
104
SetCapsule(const napi_env & env,const NotificationCapsule & capsule,napi_value & result)105 napi_value Common::SetCapsule(const napi_env &env, const NotificationCapsule &capsule, napi_value &result)
106 {
107 ANS_LOGD("enter");
108
109 napi_value value = nullptr;
110 // title: string
111 napi_create_string_utf8(env, capsule.GetTitle().c_str(), NAPI_AUTO_LENGTH, &value);
112 napi_set_named_property(env, result, "title", value);
113
114 // backgroundColor: string
115 napi_create_string_utf8(env, capsule.GetBackgroundColor().c_str(), NAPI_AUTO_LENGTH, &value);
116 napi_set_named_property(env, result, "backgroundColor", value);
117
118 // icon?: image.PixelMap
119 std::shared_ptr<Media::PixelMap> icon = capsule.GetIcon();
120 if (icon) {
121 napi_value iconResult = nullptr;
122 napi_valuetype valuetype = napi_undefined;
123 iconResult = Media::PixelMapNapi::CreatePixelMap(env, icon);
124 NAPI_CALL(env, napi_typeof(env, iconResult, &valuetype));
125 if (valuetype == napi_undefined) {
126 ANS_LOGW("iconResult is undefined");
127 napi_set_named_property(env, result, "icon", NapiGetNull(env));
128 } else {
129 napi_set_named_property(env, result, "icon", iconResult);
130 }
131 }
132 return NapiGetBoolean(env, true);
133 }
134
SetProgress(const napi_env & env,const NotificationProgress & progress,napi_value & result)135 napi_value Common::SetProgress(const napi_env &env, const NotificationProgress &progress, napi_value &result)
136 {
137 ANS_LOGD("enter");
138
139 napi_value value = nullptr;
140 // currentValue: int32_t
141 napi_create_int32(env, progress.GetCurrentValue(), &value);
142 napi_set_named_property(env, result, "currentValue", value);
143
144 // maxValue: int32_t
145 napi_create_int32(env, progress.GetMaxValue(), &value);
146 napi_set_named_property(env, result, "maxValue", value);
147
148 // isPercentage: bool
149 napi_get_boolean(env, progress.GetIsPercentage(), &value);
150 napi_set_named_property(env, result, "isPercentage", value);
151
152 return NapiGetBoolean(env, true);
153 }
154
SetTime(const napi_env & env,const NotificationTime & time,napi_value & result)155 napi_value Common::SetTime(const napi_env &env, const NotificationTime &time, napi_value &result)
156 {
157 ANS_LOGD("enter");
158
159 napi_value value = nullptr;
160 // initialTime: int32_t
161 napi_create_int32(env, time.GetInitialTime(), &value);
162 napi_set_named_property(env, result, "initialTime", value);
163
164 // isCountDown: bool
165 napi_get_boolean(env, time.GetIsCountDown(), &value);
166 napi_set_named_property(env, result, "isCountDown", value);
167
168 // isPaused: bool
169 napi_get_boolean(env, time.GetIsPaused(), &value);
170 napi_set_named_property(env, result, "isPaused", value);
171
172 // isInTitle: bool
173 napi_get_boolean(env, time.GetIsInTitle(), &value);
174 napi_set_named_property(env, result, "isInTitle", value);
175
176 return NapiGetBoolean(env, true);
177 }
178
SetButton(const napi_env & env,const NotificationLocalLiveViewButton & button,napi_value & result)179 napi_value Common::SetButton(const napi_env &env, const NotificationLocalLiveViewButton &button, napi_value &result)
180 {
181 ANS_LOGD("enter");
182
183 napi_value value = nullptr;
184
185 // buttonNames: Array<String>
186 napi_value arr = nullptr;
187 int count = 0;
188 napi_create_array(env, &arr);
189 for (auto vec : button.GetAllButtonNames()) {
190 napi_create_string_utf8(env, vec.c_str(), NAPI_AUTO_LENGTH, &value);
191 napi_set_element(env, arr, count, value);
192 count++;
193 }
194 napi_set_named_property(env, result, "names", arr);
195
196 // buttonIcons: Array<PixelMap>
197 napi_value iconArr = nullptr;
198 int iconCount = 0;
199 napi_create_array(env, &iconArr);
200
201 std::vector<std::shared_ptr<Media::PixelMap>> icons = button.GetAllButtonIcons();
202 for (auto vec : icons) {
203 if (!vec) {
204 continue;
205 }
206 // buttonIcon
207 napi_value iconResult = nullptr;
208 iconResult = Media::PixelMapNapi::CreatePixelMap(env, vec);
209 napi_set_element(env, iconArr, iconCount, iconResult);
210 iconCount++;
211 }
212 napi_set_named_property(env, result, "icons", iconArr);
213
214 return NapiGetBoolean(env, true);
215 }
216
SetNotificationLiveViewContent(const napi_env & env,NotificationBasicContent * basicContent,napi_value & result)217 napi_value Common::SetNotificationLiveViewContent(
218 const napi_env &env, NotificationBasicContent *basicContent, napi_value &result)
219 {
220 ANS_LOGD("enter");
221 napi_value value = nullptr;
222 if (basicContent == nullptr) {
223 ANS_LOGE("BasicContent is null");
224 return NapiGetBoolean(env, false);
225 }
226
227 auto liveViewContent = static_cast<NotificationLiveViewContent *>(basicContent);
228 if (liveViewContent == nullptr) {
229 ANS_LOGE("LiveViewContent is null");
230 return NapiGetBoolean(env, false);
231 }
232
233 // status: LiveViewStatus
234 LiveViewStatus outType = LiveViewStatus::LIVE_VIEW_BUTT;
235 if (!LiveViewStatusCToJS(liveViewContent->GetLiveViewStatus(), outType)) {
236 ANS_LOGE("Liveview status is invalid");
237 return NapiGetBoolean(env, false);
238 }
239 napi_create_int32(env, static_cast<int32_t>(outType), &value);
240 napi_set_named_property(env, result, "status", value);
241
242 // version?: uint32_t
243 napi_create_int32(env, static_cast<int32_t>(liveViewContent->GetVersion()), &value);
244 napi_set_named_property(env, result, "version", value);
245
246 // extraInfo?: {[key:string] : any}
247 std::shared_ptr<AAFwk::WantParams> extraInfoData = liveViewContent->GetExtraInfo();
248 if (extraInfoData != nullptr) {
249 napi_value extraInfo = OHOS::AppExecFwk::WrapWantParams(env, *extraInfoData);
250 napi_set_named_property(env, result, "extraInfo", extraInfo);
251 }
252
253 // pictureInfo?: {[key, string]: Array<image.pixelMap>}
254 if (liveViewContent->GetPicture().empty()) {
255 ANS_LOGI("No pictures in live view.");
256 return NapiGetBoolean(env, true);
257 }
258
259 napi_value pictureMapObj = SetLiveViewPictureInfo(env, liveViewContent->GetPicture());
260 if (pictureMapObj == nullptr) {
261 ANS_LOGE("Set live view picture map failed.");
262 return NapiGetBoolean(env, false);
263 }
264 napi_set_named_property(env, result, "pictureInfo", pictureMapObj);
265
266 return NapiGetBoolean(env, true);
267 }
268
SetLiveViewPictureInfo(const napi_env & env,const std::map<std::string,std::vector<std::shared_ptr<Media::PixelMap>>> & pictureMap)269 napi_value Common::SetLiveViewPictureInfo(
270 const napi_env &env, const std::map<std::string, std::vector<std::shared_ptr<Media::PixelMap>>> &pictureMap)
271 {
272 ANS_LOGD("enter");
273
274 napi_value pictureMapObj = nullptr;
275 NAPI_CALL(env, napi_create_object(env, &pictureMapObj));
276
277 for (auto iter = pictureMap.begin(); iter != pictureMap.end(); iter++) {
278 int count = 0;
279 napi_value picturesObj = nullptr;
280 napi_create_array(env, &picturesObj);
281 for (auto picture : iter->second) {
282 napi_value pictureObj = Media::PixelMapNapi::CreatePixelMap(env, picture);
283 napi_set_element(env, picturesObj, count, pictureObj);
284 count++;
285 }
286
287 if (count > 0) {
288 napi_set_named_property(env, pictureMapObj, iter->first.c_str(), picturesObj);
289 }
290 }
291
292 return pictureMapObj;
293 }
294
GetNotificationLocalLiveViewContent(const napi_env & env,const napi_value & result,NotificationRequest & request)295 napi_value Common::GetNotificationLocalLiveViewContent(
296 const napi_env &env, const napi_value &result, NotificationRequest &request)
297 {
298 ANS_LOGD("enter");
299
300 napi_valuetype valuetype = napi_undefined;
301 napi_value contentResult = nullptr;
302 bool hasProperty = false;
303 NAPI_CALL(env, napi_has_named_property(env, result, "systemLiveView", &hasProperty));
304 if (!hasProperty) {
305 ANS_LOGE("Property localLiveView expected.");
306 return nullptr;
307 }
308 napi_get_named_property(env, result, "systemLiveView", &contentResult);
309 NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
310 if (valuetype != napi_object) {
311 ANS_LOGE("Wrong argument type. Object expected.");
312 return nullptr;
313 }
314
315 std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> localLiveViewContent =
316 std::make_shared<OHOS::Notification::NotificationLocalLiveViewContent>();
317 if (localLiveViewContent == nullptr) {
318 ANS_LOGE("localLiveViewContent is null");
319 return nullptr;
320 }
321
322 if (GetNotificationLocalLiveViewContentDetailed(env, contentResult, localLiveViewContent) == nullptr) {
323 return nullptr;
324 }
325
326 request.SetContent(std::make_shared<NotificationContent>(localLiveViewContent));
327
328 // set isOnGoing of live view true
329 request.SetInProgress(true);
330
331 return NapiGetNull(env);
332 }
333
GetNotificationLocalLiveViewCapsule(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)334 napi_value Common::GetNotificationLocalLiveViewCapsule(
335 const napi_env &env, const napi_value &contentResult,
336 std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
337 {
338 napi_value capsuleResult = nullptr;
339 napi_valuetype valuetype = napi_undefined;
340 bool hasProperty = false;
341 size_t strLen = 0;
342 char str[STR_MAX_SIZE] = {0};
343 std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
344 napi_value result = nullptr;
345
346 ANS_LOGD("enter");
347
348 NAPI_CALL(env, napi_has_named_property(env, contentResult, "capsule", &hasProperty));
349
350 napi_get_named_property(env, contentResult, "capsule", &capsuleResult);
351 NAPI_CALL(env, napi_typeof(env, capsuleResult, &valuetype));
352 if (valuetype != napi_object) {
353 ANS_LOGE("Wrong argument type. Object expected.");
354 return nullptr;
355 }
356
357 NotificationCapsule capsule;
358
359 NAPI_CALL(env, napi_has_named_property(env, capsuleResult, "title", &hasProperty));
360 if (hasProperty) {
361 napi_get_named_property(env, capsuleResult, "title", &result);
362 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
363 if (valuetype != napi_string) {
364 ANS_LOGE("Wrong argument type. String expected.");
365 return nullptr;
366 }
367
368 NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
369 capsule.SetTitle(str);
370 ANS_LOGD("capsule title = %{public}s", str);
371 }
372
373 NAPI_CALL(env, napi_has_named_property(env, capsuleResult, "backgroundColor", &hasProperty));
374 if (hasProperty) {
375 napi_get_named_property(env, capsuleResult, "backgroundColor", &result);
376 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
377 if (valuetype != napi_string) {
378 ANS_LOGE("Wrong argument type. String expected.");
379 return nullptr;
380 }
381 NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
382 capsule.SetBackgroundColor(str);
383 ANS_LOGD("capsule backgroundColor = %{public}s", str);
384 }
385 NAPI_CALL(env, napi_has_named_property(env, capsuleResult, "icon", &hasProperty));
386 if (hasProperty) {
387 napi_get_named_property(env, capsuleResult, "icon", &result);
388 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
389 if (valuetype != napi_object) {
390 ANS_LOGE("Wrong argument type. Object expected.");
391 return nullptr;
392 }
393 pixelMap = Media::PixelMapNapi::GetPixelMap(env, result);
394 if (pixelMap == nullptr) {
395 ANS_LOGE("Invalid object pixelMap");
396 return nullptr;
397 }
398 capsule.SetIcon(pixelMap);
399 ANS_LOGD("capsule icon = %{public}d", pixelMap->GetWidth());
400 }
401
402 content->SetCapsule(capsule);
403 content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE);
404
405 return NapiGetNull(env);
406 }
407
GetNotificationLocalLiveViewButton(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)408 napi_value Common::GetNotificationLocalLiveViewButton(
409 const napi_env &env, const napi_value &contentResult,
410 std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
411 {
412 napi_value result = nullptr;
413 napi_valuetype valuetype = napi_undefined;
414 bool isArray = false;
415 uint32_t length = 0;
416 napi_value buttonResult = nullptr;
417 bool hasProperty = false;
418 char str[STR_MAX_SIZE] = {0};
419 size_t strLen = 0;
420
421 ANS_LOGD("enter");
422
423 napi_get_named_property(env, contentResult, "button", &buttonResult);
424 NAPI_CALL(env, napi_typeof(env, buttonResult, &valuetype));
425 if (valuetype != napi_object) {
426 ANS_LOGE("Wrong argument type. Object expected.");
427 return nullptr;
428 }
429
430 NotificationLocalLiveViewButton button;
431
432 NAPI_CALL(env, napi_has_named_property(env, buttonResult, "names", &hasProperty));
433 if (hasProperty) {
434 napi_get_named_property(env, buttonResult, "names", &result);
435 napi_is_array(env, result, &isArray);
436 if (!isArray) {
437 ANS_LOGE("Property names is expected to be an array.");
438 return nullptr;
439 }
440 napi_get_array_length(env, result, &length);
441 for (size_t i = 0; i < length; i++) {
442 napi_value buttonName = nullptr;
443 napi_get_element(env, result, i, &buttonName);
444 NAPI_CALL(env, napi_typeof(env, buttonName, &valuetype));
445 if (valuetype != napi_string) {
446 ANS_LOGE("Wrong argument type. String expected.");
447 return nullptr;
448 }
449 NAPI_CALL(env, napi_get_value_string_utf8(env, buttonName, str, STR_MAX_SIZE - 1, &strLen));
450 button.addSingleButtonName(str);
451 ANS_LOGD("button buttonName = %{public}s.", str);
452 }
453 }
454
455 NAPI_CALL(env, napi_has_named_property(env, buttonResult, "icons", &hasProperty));
456 if (hasProperty) {
457 napi_get_named_property(env, buttonResult, "icons", &result);
458 napi_is_array(env, result, &isArray);
459 if (!isArray) {
460 ANS_LOGE("Property icons is expected to be an array.");
461 return nullptr;
462 }
463 napi_get_array_length(env, result, &length);
464 for (size_t i = 0; i < length; i++) {
465 napi_value buttonIcon = nullptr;
466 std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
467 napi_get_element(env, result, i, &buttonIcon);
468 NAPI_CALL(env, napi_typeof(env, buttonIcon, &valuetype));
469 if (valuetype != napi_object) {
470 ANS_LOGE("Wrong argument type. Object expected.");
471 return nullptr;
472 }
473 pixelMap = Media::PixelMapNapi::GetPixelMap(env, buttonIcon);
474 if (pixelMap == nullptr) {
475 ANS_LOGE("Invalid object pixelMap");
476 return nullptr;
477 }
478 button.addSingleButtonIcon(pixelMap);
479 }
480 }
481 ANS_LOGD("button buttonIcon = %{public}s", str);
482 content->SetButton(button);
483 content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON);
484
485 return NapiGetNull(env);
486 }
487
GetNotificationLocalLiveViewProgress(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)488 napi_value Common::GetNotificationLocalLiveViewProgress(const napi_env &env, const napi_value &contentResult,
489 std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
490 {
491 napi_value result = nullptr;
492 napi_valuetype valuetype = napi_undefined;
493 bool hasProperty = false;
494 int32_t intValue = -1;
495 bool boolValue = false;
496 napi_value progressResult = nullptr;
497
498 ANS_LOGD("enter");
499
500 napi_get_named_property(env, contentResult, "progress", &progressResult);
501 NAPI_CALL(env, napi_typeof(env, progressResult, &valuetype));
502 if (valuetype != napi_object) {
503 ANS_LOGE("Wrong argument type. Object expected.");
504 return nullptr;
505 }
506
507 NotificationProgress progress;
508
509 NAPI_CALL(env, napi_has_named_property(env, progressResult, "maxValue", &hasProperty));
510 if (hasProperty) {
511 napi_get_named_property(env, progressResult, "maxValue", &result);
512 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
513 if (valuetype != napi_number) {
514 ANS_LOGE("Wrong argument type. Number expected.");
515 return nullptr;
516 }
517 napi_get_value_int32(env, result, &intValue);
518 progress.SetMaxValue(intValue);
519 ANS_LOGD("progress intValue = %{public}d", intValue);
520 }
521
522 NAPI_CALL(env, napi_has_named_property(env, progressResult, "currentValue", &hasProperty));
523 if (hasProperty) {
524 napi_get_named_property(env, progressResult, "currentValue", &result);
525 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
526 if (valuetype != napi_number) {
527 ANS_LOGE("Wrong argument type. Number expected.");
528 return nullptr;
529 }
530 napi_get_value_int32(env, result, &intValue);
531 progress.SetCurrentValue(intValue);
532 ANS_LOGD("progress currentValue = %{public}d", intValue);
533 }
534
535 NAPI_CALL(env, napi_has_named_property(env, progressResult, "isPercentage", &hasProperty));
536 if (hasProperty) {
537 napi_get_named_property(env, progressResult, "isPercentage", &result);
538 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
539 if (valuetype != napi_boolean) {
540 ANS_LOGE("Wrong argument type. bool expected.");
541 return nullptr;
542 }
543 napi_get_value_bool(env, result, &boolValue);
544 progress.SetIsPercentage(boolValue);
545 ANS_LOGD("progress isPercentage = %{public}d", boolValue);
546 }
547
548 content->SetProgress(progress);
549 content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::PROGRESS);
550
551 return NapiGetNull(env);
552 }
553
GetNotificationLocalLiveViewTime(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)554 napi_value Common::GetNotificationLocalLiveViewTime(const napi_env &env, const napi_value &contentResult,
555 std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
556 {
557 napi_value result = nullptr;
558 napi_valuetype valuetype = napi_undefined;
559 bool hasProperty = false;
560 int32_t intValue = -1;
561 bool boolValue = false;
562 napi_value timeResult = nullptr;
563
564 ANS_LOGD("enter");
565
566 napi_get_named_property(env, contentResult, "time", &timeResult);
567 NAPI_CALL(env, napi_typeof(env, timeResult, &valuetype));
568 if (valuetype != napi_object) {
569 ANS_LOGE("Wrong argument type. Object expected.");
570 return nullptr;
571 }
572
573 NotificationTime time;
574
575 NAPI_CALL(env, napi_has_named_property(env, timeResult, "initialTime", &hasProperty));
576 if (hasProperty) {
577 napi_get_named_property(env, timeResult, "initialTime", &result);
578 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
579 if (valuetype != napi_number) {
580 ANS_LOGE("Wrong argument type. Number expected.");
581 return nullptr;
582 }
583 napi_get_value_int32(env, result, &intValue);
584 time.SetInitialTime(intValue);
585 ANS_LOGD("time initialTime = %{public}d", intValue);
586 }
587
588 NAPI_CALL(env, napi_has_named_property(env, timeResult, "isCountDown", &hasProperty));
589 if (hasProperty) {
590 napi_get_named_property(env, timeResult, "isCountDown", &result);
591 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
592 if (valuetype != napi_boolean) {
593 ANS_LOGE("Wrong argument type. bool expected.");
594 return nullptr;
595 }
596 napi_get_value_bool(env, result, &boolValue);
597 time.SetIsCountDown(boolValue);
598 ANS_LOGD("time isCountDown = %{public}d", boolValue);
599 }
600
601 NAPI_CALL(env, napi_has_named_property(env, timeResult, "isPaused", &hasProperty));
602 if (hasProperty) {
603 napi_get_named_property(env, timeResult, "isPaused", &result);
604 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
605 if (valuetype != napi_boolean) {
606 ANS_LOGE("Wrong argument type. bool expected.");
607 return nullptr;
608 }
609 napi_get_value_bool(env, result, &boolValue);
610 time.SetIsPaused(boolValue);
611 ANS_LOGD("time isPaused = %{public}d", boolValue);
612 }
613
614 NAPI_CALL(env, napi_has_named_property(env, timeResult, "isInTitle", &hasProperty));
615 if (hasProperty) {
616 napi_get_named_property(env, timeResult, "isInTitle", &result);
617 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
618 if (valuetype != napi_boolean) {
619 ANS_LOGE("Wrong argument type. bool expected.");
620 return nullptr;
621 }
622 napi_get_value_bool(env, result, &boolValue);
623 time.SetIsInTitle(boolValue);
624 ANS_LOGD("time isInTitle = %{public}d", boolValue);
625 }
626
627 content->SetTime(time);
628 content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::TIME);
629
630 return NapiGetNull(env);
631 }
632
GetNotificationLocalLiveViewContentDetailed(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)633 napi_value Common::GetNotificationLocalLiveViewContentDetailed(
634 const napi_env &env, const napi_value &contentResult,
635 std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
636 {
637 bool hasProperty = false;
638 int32_t type = -1;
639 napi_value result = nullptr;
640 napi_valuetype valuetype = napi_undefined;
641
642 ANS_LOGD("enter");
643
644 //title, text
645 if (GetNotificationBasicContentDetailed(env, contentResult, content) == nullptr) {
646 ANS_LOGE("Basic content get fail.");
647 return nullptr;
648 }
649
650 // typeCode
651 NAPI_CALL(env, napi_has_named_property(env, contentResult, "typeCode", &hasProperty));
652 if (!hasProperty) {
653 ANS_LOGE("Property typeCode expected.");
654 return nullptr;
655 }
656 napi_get_named_property(env, contentResult, "typeCode", &result);
657 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
658 if (valuetype != napi_number) {
659 ANS_LOGE("Wrong argument typeCode. Number expected.");
660 return nullptr;
661 }
662 napi_get_value_int32(env, result, &type);
663 content->SetType(type);
664 ANS_LOGD("localLiveView type = %{public}d", type);
665
666 //capsule?
667 NAPI_CALL(env, napi_has_named_property(env, contentResult, "capsule", &hasProperty));
668 if (hasProperty && GetNotificationLocalLiveViewCapsule(env, contentResult, content) == nullptr) {
669 return nullptr;
670 }
671
672 //button?
673 NAPI_CALL(env, napi_has_named_property(env, contentResult, "button", &hasProperty));
674 if (hasProperty && GetNotificationLocalLiveViewButton(env, contentResult, content) == nullptr) {
675 return nullptr;
676 }
677
678 //progress?
679 NAPI_CALL(env, napi_has_named_property(env, contentResult, "progress", &hasProperty));
680 if (hasProperty && GetNotificationLocalLiveViewProgress(env, contentResult, content) == nullptr) {
681 return nullptr;
682 }
683
684 //time?
685 NAPI_CALL(env, napi_has_named_property(env, contentResult, "time", &hasProperty));
686 if (hasProperty && GetNotificationLocalLiveViewTime(env, contentResult, content) == nullptr) {
687 return nullptr;
688 }
689
690 return NapiGetNull(env);
691 }
692
GetNotificationLiveViewContent(const napi_env & env,const napi_value & result,NotificationRequest & request)693 napi_value Common::GetNotificationLiveViewContent(
694 const napi_env &env, const napi_value &result, NotificationRequest &request)
695 {
696 ANS_LOGD("enter");
697
698 napi_value contentResult = AppExecFwk::GetPropertyValueByPropertyName(env, result, "liveView", napi_object);
699 if (contentResult == nullptr) {
700 ANS_LOGE("Property liveView expected.");
701 return nullptr;
702 }
703
704 std::shared_ptr<NotificationLiveViewContent> liveViewContent = std::make_shared<NotificationLiveViewContent>();
705 if (liveViewContent == nullptr) {
706 ANS_LOGE("LiveViewContent is null");
707 return nullptr;
708 }
709
710 if (GetNotificationLiveViewContentDetailed(env, contentResult, liveViewContent) == nullptr) {
711 return nullptr;
712 }
713
714 request.SetContent(std::make_shared<NotificationContent>(liveViewContent));
715
716 return NapiGetNull(env);
717 }
718
GetNotificationLiveViewContentDetailed(const napi_env & env,const napi_value & contentResult,std::shared_ptr<NotificationLiveViewContent> & liveViewContent)719 napi_value Common::GetNotificationLiveViewContentDetailed(
720 const napi_env &env, const napi_value &contentResult,
721 std::shared_ptr<NotificationLiveViewContent> &liveViewContent)
722 {
723 ANS_LOGD("enter");
724
725 // status: NotificationLiveViewContent::LiveViewStatus
726 int32_t status = 0;
727 if (!AppExecFwk::UnwrapInt32ByPropertyName(env, contentResult, "status", status)) {
728 ANS_LOGE("Failed to get status from liveView content.");
729 return nullptr;
730 }
731 NotificationLiveViewContent::LiveViewStatus outType = NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_BUTT;
732 if (!LiveViewStatusJSToC(LiveViewStatus(status), outType)) {
733 ANS_LOGE("The liveview status is not valid.");
734 return nullptr;
735 }
736 liveViewContent->SetLiveViewStatus(outType);
737
738 // version?: uint32_t
739 napi_value jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult,
740 "version", napi_number);
741 if (jsValue != nullptr) {
742 int32_t version = NotificationLiveViewContent::MAX_VERSION;
743 NAPI_CALL(env, napi_get_value_int32(env, jsValue, &version));
744 liveViewContent->SetVersion(version);
745 }
746
747 // extraInfo?: {[key:string] : any}
748 jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult, "extraInfo", napi_object);
749 if (jsValue != nullptr) {
750 std::shared_ptr<AAFwk::WantParams> extras = std::make_shared<AAFwk::WantParams>();
751 if (!OHOS::AppExecFwk::UnwrapWantParams(env, jsValue, *extras)) {
752 return nullptr;
753 }
754 liveViewContent->SetExtraInfo(extras);
755 }
756
757 // pictureInfo?: {[key, string]: Array<image.pixelMap>}
758 jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult, "pictureInfo", napi_object);
759 if (jsValue == nullptr) {
760 ANS_LOGI("No picture maps.");
761 return NapiGetNull(env);
762 }
763
764 std::map<std::string, std::vector<std::shared_ptr<Media::PixelMap>>> pictureMap;
765 if (GetLiveViewPictureInfo(env, jsValue, pictureMap) == nullptr) {
766 ANS_LOGE("Failed to get picture map from liveView content.");
767 return nullptr;
768 }
769 liveViewContent->SetPicture(pictureMap);
770
771 return NapiGetNull(env);
772 }
773
GetLiveViewPictures(const napi_env & env,const napi_value & picturesObj,std::vector<std::shared_ptr<Media::PixelMap>> & pictures)774 napi_value Common::GetLiveViewPictures(
775 const napi_env &env, const napi_value &picturesObj,
776 std::vector<std::shared_ptr<Media::PixelMap>> &pictures)
777 {
778 ANS_LOGD("enter");
779
780 bool isArray = false;
781 napi_is_array(env, picturesObj, &isArray);
782 if (!isArray) {
783 ANS_LOGE("The picture is not array.");
784 return nullptr;
785 }
786
787 uint32_t length = 0;
788 napi_get_array_length(env, picturesObj, &length);
789 if (length == 0) {
790 ANS_LOGE("The array is empty.");
791 return nullptr;
792 }
793
794 for (uint32_t i = 0; i < length; ++i) {
795 napi_value pictureObj = nullptr;
796 napi_get_element(env, picturesObj, i, &pictureObj);
797 if (!AppExecFwk::IsTypeForNapiValue(env, pictureObj, napi_object)) {
798 ANS_LOGE("Wrong argument type. object expected.");
799 break;
800 }
801
802 std::shared_ptr<Media::PixelMap> pixelMap = Media::PixelMapNapi::GetPixelMap(env, pictureObj);
803 if (pixelMap == nullptr) {
804 ANS_LOGE("Invalid pixelMap.");
805 break;
806 }
807
808 pictures.emplace_back(pixelMap);
809 }
810
811 return NapiGetNull(env);
812 }
813
GetLiveViewPictureInfo(const napi_env & env,const napi_value & pictureMapObj,std::map<std::string,std::vector<std::shared_ptr<Media::PixelMap>>> & pictureMap)814 napi_value Common::GetLiveViewPictureInfo(
815 const napi_env &env, const napi_value &pictureMapObj,
816 std::map<std::string, std::vector<std::shared_ptr<Media::PixelMap>>> &pictureMap)
817 {
818 ANS_LOGD("enter");
819
820 napi_value pictureNamesObj = nullptr;
821 uint32_t length = 0;
822 if (napi_get_property_names(env, pictureMapObj, &pictureNamesObj) != napi_ok) {
823 ANS_LOGE("Get picture names failed.");
824 return nullptr;
825 }
826 napi_get_array_length(env, pictureNamesObj, &length);
827 if (length == 0) {
828 ANS_LOGE("The pictures name is empty.");
829 return nullptr;
830 }
831
832 napi_value pictureNameObj = nullptr;
833 napi_value picturesObj = nullptr;
834 for (uint32_t index = 0; index < length; index++) {
835 napi_get_element(env, pictureNamesObj, index, &pictureNameObj);
836 std::string pictureName = AppExecFwk::UnwrapStringFromJS(env, pictureNameObj);
837 ANS_LOGD("%{public}s called, get pictures of %{public}s.", __func__, pictureName.c_str());
838 napi_get_named_property(env, pictureMapObj, pictureName.c_str(), &picturesObj);
839
840 std::vector<std::shared_ptr<Media::PixelMap>> pictures;
841 if (!GetLiveViewPictures(env, picturesObj, pictures)) {
842 ANS_LOGE("Get pictures of %{public}s failed.", pictureName.c_str());
843 break;
844 }
845
846 pictureMap[pictureName] = pictures;
847 }
848
849 return NapiGetNull(env);
850 }
851 }
852 }
853