1 /*
2 * Copyright (c) 2024 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 "notification_napi.h"
17 #include "ans_inner_errors.h"
18 #include "location_log.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 Location {
GetNotificationLocalLiveViewContent(const napi_env & env,const napi_value & result,NotificationRequest & request)33 napi_value NotificationNapi::GetNotificationLocalLiveViewContent(
34 const napi_env &env, const napi_value &result, NotificationRequest &request)
35 {
36 LBSLOGD(NAPI_UTILS, "enter");
37
38 napi_valuetype valuetype = napi_undefined;
39 napi_value contentResult = nullptr;
40 bool hasProperty = false;
41 NAPI_CALL(env, napi_has_named_property(env, result, "systemLiveView", &hasProperty));
42 if (!hasProperty) {
43 LBSLOGE(NAPI_UTILS, "Property localLiveView expected.");
44 return nullptr;
45 }
46 napi_get_named_property(env, result, "systemLiveView", &contentResult);
47 NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
48 if (valuetype != napi_object) {
49 LBSLOGE(NAPI_UTILS, "Wrong argument type. Object expected.");
50 return nullptr;
51 }
52
53 std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> localLiveViewContent =
54 std::make_shared<OHOS::Notification::NotificationLocalLiveViewContent>();
55 if (GetNotificationLocalLiveViewContentDetailed(env, contentResult, localLiveViewContent) == nullptr) {
56 return nullptr;
57 }
58
59 request.SetContent(std::make_shared<NotificationContent>(localLiveViewContent));
60
61 // set isOnGoing of live view true
62 request.SetInProgress(true);
63
64 return NapiGetNull(env);
65 }
66
GetNotificationLocalLiveViewCapsule(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)67 napi_value NotificationNapi::GetNotificationLocalLiveViewCapsule(
68 const napi_env &env, const napi_value &contentResult,
69 std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
70 {
71 napi_value capsuleResult = nullptr;
72 napi_valuetype valuetype = napi_undefined;
73 bool hasProperty = false;
74 size_t strLen = 0;
75 char str[STR_MAX_SIZE] = {0};
76 std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
77 napi_value result = nullptr;
78
79 LBSLOGD(NAPI_UTILS, "enter");
80
81 NAPI_CALL(env, napi_has_named_property(env, contentResult, "capsule", &hasProperty));
82
83 napi_get_named_property(env, contentResult, "capsule", &capsuleResult);
84 NAPI_CALL(env, napi_typeof(env, capsuleResult, &valuetype));
85 if (valuetype != napi_object) {
86 LBSLOGE(NAPI_UTILS, "Wrong argument type. Object expected.");
87 return nullptr;
88 }
89
90 NotificationCapsule capsule;
91
92 NAPI_CALL(env, napi_has_named_property(env, capsuleResult, "title", &hasProperty));
93 if (hasProperty) {
94 napi_get_named_property(env, capsuleResult, "title", &result);
95 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
96 if (valuetype != napi_string) {
97 LBSLOGE(NAPI_UTILS, "Wrong argument type. String expected.");
98 return nullptr;
99 }
100
101 NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
102 capsule.SetTitle(str);
103 LBSLOGD(NAPI_UTILS, "capsule title = %{public}s", str);
104 }
105
106 NAPI_CALL(env, napi_has_named_property(env, capsuleResult, "backgroundColor", &hasProperty));
107 if (hasProperty) {
108 napi_get_named_property(env, capsuleResult, "backgroundColor", &result);
109 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
110 if (valuetype != napi_string) {
111 LBSLOGE(NAPI_UTILS, "Wrong argument type. String expected.");
112 return nullptr;
113 }
114 NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
115 capsule.SetBackgroundColor(str);
116 LBSLOGD(NAPI_UTILS, "capsule backgroundColor = %{public}s", str);
117 }
118 NAPI_CALL(env, napi_has_named_property(env, capsuleResult, "icon", &hasProperty));
119 if (hasProperty) {
120 napi_get_named_property(env, capsuleResult, "icon", &result);
121 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
122 if (valuetype != napi_object) {
123 LBSLOGE(NAPI_UTILS, "Wrong argument type. Object expected.");
124 return nullptr;
125 }
126 pixelMap = Media::PixelMapNapi::GetPixelMap(env, result);
127 if (pixelMap == nullptr) {
128 LBSLOGE(NAPI_UTILS, "Invalid object pixelMap");
129 return nullptr;
130 }
131 capsule.SetIcon(pixelMap);
132 LBSLOGD(NAPI_UTILS, "capsule icon = %{public}d", pixelMap->GetWidth());
133 }
134
135 content->SetCapsule(capsule);
136 content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE);
137
138 return NapiGetNull(env);
139 }
140
GetNotificationLocalLiveViewButton(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)141 napi_value NotificationNapi::GetNotificationLocalLiveViewButton(
142 const napi_env &env, const napi_value &contentResult,
143 std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
144 {
145 napi_value result = nullptr;
146 napi_valuetype valuetype = napi_undefined;
147 bool isArray = false;
148 uint32_t length = 0;
149 napi_value buttonResult = nullptr;
150 bool hasProperty = false;
151 char str[STR_MAX_SIZE] = {0};
152 size_t strLen = 0;
153
154 LBSLOGD(NAPI_UTILS, "enter");
155
156 napi_get_named_property(env, contentResult, "button", &buttonResult);
157 NAPI_CALL(env, napi_typeof(env, buttonResult, &valuetype));
158 if (valuetype != napi_object) {
159 LBSLOGE(NAPI_UTILS, "Wrong argument type. Object expected.");
160 return nullptr;
161 }
162
163 NotificationLocalLiveViewButton button;
164
165 NAPI_CALL(env, napi_has_named_property(env, buttonResult, "names", &hasProperty));
166 if (hasProperty) {
167 napi_get_named_property(env, buttonResult, "names", &result);
168 napi_is_array(env, result, &isArray);
169 if (!isArray) {
170 LBSLOGE(NAPI_UTILS, "Property names is expected to be an array.");
171 return nullptr;
172 }
173 napi_get_array_length(env, result, &length);
174 for (size_t i = 0; i < length; i++) {
175 napi_value buttonName = nullptr;
176 napi_get_element(env, result, i, &buttonName);
177 NAPI_CALL(env, napi_typeof(env, buttonName, &valuetype));
178 if (valuetype != napi_string) {
179 LBSLOGE(NAPI_UTILS, "Wrong argument type. String expected.");
180 return nullptr;
181 }
182 NAPI_CALL(env, napi_get_value_string_utf8(env, buttonName, str, STR_MAX_SIZE - 1, &strLen));
183 button.addSingleButtonName(str);
184 LBSLOGD(NAPI_UTILS, "button buttonName = %{public}s.", str);
185 }
186 }
187
188 NAPI_CALL(env, napi_has_named_property(env, buttonResult, "icons", &hasProperty));
189 if (hasProperty) {
190 napi_get_named_property(env, buttonResult, "icons", &result);
191 napi_is_array(env, result, &isArray);
192 if (!isArray) {
193 LBSLOGE(NAPI_UTILS, "Property icons is expected to be an array.");
194 return nullptr;
195 }
196 napi_get_array_length(env, result, &length);
197 for (size_t i = 0; i < length; i++) {
198 napi_value buttonIcon = nullptr;
199 std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
200 napi_get_element(env, result, i, &buttonIcon);
201 NAPI_CALL(env, napi_typeof(env, buttonIcon, &valuetype));
202 if (valuetype != napi_object) {
203 LBSLOGE(NAPI_UTILS, "Wrong argument type. Object expected.");
204 return nullptr;
205 }
206 pixelMap = Media::PixelMapNapi::GetPixelMap(env, buttonIcon);
207 if (pixelMap != nullptr && static_cast<uint32_t>(pixelMap->GetByteCount()) <= MAX_ICON_SIZE) {
208 button.addSingleButtonIcon(pixelMap);
209 } else {
210 LBSLOGE(NAPI_UTILS, "Invalid pixelMap object or pixelMap is over size.");
211 return nullptr;
212 }
213 }
214 }
215 LBSLOGD(NAPI_UTILS, "button buttonIcon = %{public}s", str);
216 content->SetButton(button);
217 content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON);
218
219 return NapiGetNull(env);
220 }
221
GetNotificationLocalLiveViewProgress(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)222 napi_value NotificationNapi::GetNotificationLocalLiveViewProgress(const napi_env &env, const napi_value &contentResult,
223 std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
224 {
225 napi_value result = nullptr;
226 napi_valuetype valuetype = napi_undefined;
227 bool hasProperty = false;
228 int32_t intValue = -1;
229 bool boolValue = false;
230 napi_value progressResult = nullptr;
231
232 LBSLOGD(NAPI_UTILS, "enter");
233
234 napi_get_named_property(env, contentResult, "progress", &progressResult);
235 NAPI_CALL(env, napi_typeof(env, progressResult, &valuetype));
236 if (valuetype != napi_object) {
237 LBSLOGE(NAPI_UTILS, "Wrong argument type. Object expected.");
238 return nullptr;
239 }
240
241 NotificationProgress progress;
242
243 NAPI_CALL(env, napi_has_named_property(env, progressResult, "maxValue", &hasProperty));
244 if (hasProperty) {
245 napi_get_named_property(env, progressResult, "maxValue", &result);
246 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
247 if (valuetype != napi_number) {
248 LBSLOGE(NAPI_UTILS, "Wrong argument type. Number expected.");
249 return nullptr;
250 }
251 napi_get_value_int32(env, result, &intValue);
252 progress.SetMaxValue(intValue);
253 LBSLOGD(NAPI_UTILS, "progress intValue = %{public}d", intValue);
254 }
255
256 NAPI_CALL(env, napi_has_named_property(env, progressResult, "currentValue", &hasProperty));
257 if (hasProperty) {
258 napi_get_named_property(env, progressResult, "currentValue", &result);
259 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
260 if (valuetype != napi_number) {
261 LBSLOGE(NAPI_UTILS, "Wrong argument type. Number expected.");
262 return nullptr;
263 }
264 napi_get_value_int32(env, result, &intValue);
265 progress.SetCurrentValue(intValue);
266 LBSLOGD(NAPI_UTILS, "progress currentValue = %{public}d", intValue);
267 }
268
269 NAPI_CALL(env, napi_has_named_property(env, progressResult, "isPercentage", &hasProperty));
270 if (hasProperty) {
271 napi_get_named_property(env, progressResult, "isPercentage", &result);
272 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
273 if (valuetype != napi_boolean) {
274 LBSLOGE(NAPI_UTILS, "Wrong argument type. bool expected.");
275 return nullptr;
276 }
277 napi_get_value_bool(env, result, &boolValue);
278 progress.SetIsPercentage(boolValue);
279 LBSLOGD(NAPI_UTILS, "progress isPercentage = %{public}d", boolValue);
280 }
281
282 content->SetProgress(progress);
283 content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::PROGRESS);
284
285 return NapiGetNull(env);
286 }
287
GetNotificationLocalLiveViewTime(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)288 napi_value NotificationNapi::GetNotificationLocalLiveViewTime(const napi_env &env, const napi_value &contentResult,
289 std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
290 {
291 napi_value result = nullptr;
292 napi_valuetype valuetype = napi_undefined;
293 bool hasProperty = false;
294 int32_t intValue = -1;
295 bool boolValue = false;
296 napi_value timeResult = nullptr;
297
298 LBSLOGD(NAPI_UTILS, "enter");
299
300 napi_get_named_property(env, contentResult, "time", &timeResult);
301 NAPI_CALL(env, napi_typeof(env, timeResult, &valuetype));
302 if (valuetype != napi_object) {
303 LBSLOGE(NAPI_UTILS, "Wrong argument type. Object expected.");
304 return nullptr;
305 }
306
307 NotificationTime time;
308
309 NAPI_CALL(env, napi_has_named_property(env, timeResult, "initialTime", &hasProperty));
310 if (hasProperty) {
311 napi_get_named_property(env, timeResult, "initialTime", &result);
312 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
313 if (valuetype != napi_number) {
314 LBSLOGE(NAPI_UTILS, "Wrong argument type. Number expected.");
315 return nullptr;
316 }
317 napi_get_value_int32(env, result, &intValue);
318 time.SetInitialTime(intValue);
319 content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::INITIAL_TIME);
320 LBSLOGD(NAPI_UTILS, "time initialTime = %{public}d", intValue);
321 }
322
323 NAPI_CALL(env, napi_has_named_property(env, timeResult, "isCountDown", &hasProperty));
324 if (hasProperty) {
325 napi_get_named_property(env, timeResult, "isCountDown", &result);
326 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
327 if (valuetype != napi_boolean) {
328 LBSLOGE(NAPI_UTILS, "Wrong argument type. bool expected.");
329 return nullptr;
330 }
331 napi_get_value_bool(env, result, &boolValue);
332 time.SetIsCountDown(boolValue);
333 LBSLOGD(NAPI_UTILS, "time isCountDown = %{public}d", boolValue);
334 }
335
336 NAPI_CALL(env, napi_has_named_property(env, timeResult, "isPaused", &hasProperty));
337 if (hasProperty) {
338 napi_get_named_property(env, timeResult, "isPaused", &result);
339 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
340 if (valuetype != napi_boolean) {
341 LBSLOGE(NAPI_UTILS, "Wrong argument type. bool expected.");
342 return nullptr;
343 }
344 napi_get_value_bool(env, result, &boolValue);
345 time.SetIsPaused(boolValue);
346 LBSLOGD(NAPI_UTILS, "time isPaused = %{public}d", boolValue);
347 }
348
349 NAPI_CALL(env, napi_has_named_property(env, timeResult, "isInTitle", &hasProperty));
350 if (hasProperty) {
351 napi_get_named_property(env, timeResult, "isInTitle", &result);
352 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
353 if (valuetype != napi_boolean) {
354 LBSLOGE(NAPI_UTILS, "Wrong argument type. bool expected.");
355 return nullptr;
356 }
357 napi_get_value_bool(env, result, &boolValue);
358 time.SetIsInTitle(boolValue);
359 LBSLOGD(NAPI_UTILS, "time isInTitle = %{public}d", boolValue);
360 }
361
362 content->SetTime(time);
363 content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::TIME);
364
365 return NapiGetNull(env);
366 }
367
GetNotificationLocalLiveViewContentDetailed(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)368 napi_value NotificationNapi::GetNotificationLocalLiveViewContentDetailed(
369 const napi_env &env, const napi_value &contentResult,
370 std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
371 {
372 bool hasProperty = false;
373 int32_t type = -1;
374 napi_value result = nullptr;
375 napi_valuetype valuetype = napi_undefined;
376
377 LBSLOGD(NAPI_UTILS, "enter");
378
379 //title, text
380 if (GetNotificationBasicContentDetailed(env, contentResult, content) == nullptr) {
381 LBSLOGE(NAPI_UTILS, "Basic content get fail.");
382 return nullptr;
383 }
384
385 // typeCode
386 NAPI_CALL(env, napi_has_named_property(env, contentResult, "typeCode", &hasProperty));
387 if (!hasProperty) {
388 LBSLOGE(NAPI_UTILS, "Property typeCode expected.");
389 return nullptr;
390 }
391 napi_get_named_property(env, contentResult, "typeCode", &result);
392 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
393 if (valuetype != napi_number) {
394 LBSLOGE(NAPI_UTILS, "Wrong argument typeCode. Number expected.");
395 return nullptr;
396 }
397 napi_get_value_int32(env, result, &type);
398 content->SetType(type);
399 LBSLOGD(NAPI_UTILS, "localLiveView type = %{public}d", type);
400
401 //capsule?
402 NAPI_CALL(env, napi_has_named_property(env, contentResult, "capsule", &hasProperty));
403 if (hasProperty && GetNotificationLocalLiveViewCapsule(env, contentResult, content) == nullptr) {
404 return nullptr;
405 }
406
407 //button?
408 NAPI_CALL(env, napi_has_named_property(env, contentResult, "button", &hasProperty));
409 if (hasProperty && GetNotificationLocalLiveViewButton(env, contentResult, content) == nullptr) {
410 return nullptr;
411 }
412
413 //progress?
414 NAPI_CALL(env, napi_has_named_property(env, contentResult, "progress", &hasProperty));
415 if (hasProperty && GetNotificationLocalLiveViewProgress(env, contentResult, content) == nullptr) {
416 return nullptr;
417 }
418
419 //time?
420 NAPI_CALL(env, napi_has_named_property(env, contentResult, "time", &hasProperty));
421 if (hasProperty && GetNotificationLocalLiveViewTime(env, contentResult, content) == nullptr) {
422 return nullptr;
423 }
424
425 return NapiGetNull(env);
426 }
427
GetNotificationLiveViewContent(const napi_env & env,const napi_value & result,NotificationRequest & request)428 napi_value NotificationNapi::GetNotificationLiveViewContent(
429 const napi_env &env, const napi_value &result, NotificationRequest &request)
430 {
431 LBSLOGD(NAPI_UTILS, "enter");
432
433 napi_value contentResult = AppExecFwk::GetPropertyValueByPropertyName(env, result, "liveView", napi_object);
434 if (contentResult == nullptr) {
435 LBSLOGE(NAPI_UTILS, "Property liveView expected.");
436 return nullptr;
437 }
438
439 std::shared_ptr<NotificationLiveViewContent> liveViewContent = std::make_shared<NotificationLiveViewContent>();
440 if (GetNotificationLiveViewContentDetailed(env, contentResult, liveViewContent) == nullptr) {
441 return nullptr;
442 }
443
444 request.SetContent(std::make_shared<NotificationContent>(liveViewContent));
445
446 return NapiGetNull(env);
447 }
448
GetNotificationLiveViewContentDetailed(const napi_env & env,const napi_value & contentResult,std::shared_ptr<NotificationLiveViewContent> & liveViewContent)449 napi_value NotificationNapi::GetNotificationLiveViewContentDetailed(
450 const napi_env &env, const napi_value &contentResult,
451 std::shared_ptr<NotificationLiveViewContent> &liveViewContent)
452 {
453 LBSLOGD(NAPI_UTILS, "enter");
454
455 // lockScreenPicture?: pixelMap
456 if (GetLockScreenPicture(env, contentResult, liveViewContent) == nullptr) {
457 LBSLOGE(NAPI_UTILS, "Failed to get lockScreenPicture from liveView content.");
458 return nullptr;
459 }
460
461 // status: NotificationLiveViewContent::LiveViewStatus
462 int32_t status = 0;
463 if (!AppExecFwk::UnwrapInt32ByPropertyName(env, contentResult, "status", status)) {
464 LBSLOGE(NAPI_UTILS, "Failed to get status from liveView content.");
465 return nullptr;
466 }
467 NotificationLiveViewContent::LiveViewStatus outType = NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_BUTT;
468 if (!AnsEnumUtil::LiveViewStatusJSToC(LiveViewStatus(status), outType)) {
469 LBSLOGE(NAPI_UTILS, "The liveview status is not valid.");
470 return nullptr;
471 }
472 liveViewContent->SetLiveViewStatus(outType);
473
474 // version?: uint32_t
475 napi_value jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult,
476 "version", napi_number);
477 if (jsValue != nullptr) {
478 int32_t version = NotificationLiveViewContent::MAX_VERSION;
479 NAPI_CALL(env, napi_get_value_int32(env, jsValue, &version));
480 liveViewContent->SetVersion(version);
481 }
482
483 // extraInfo?: {[key:string] : any}
484 jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult, "extraInfo", napi_object);
485 if (jsValue != nullptr) {
486 std::shared_ptr<AAFwk::WantParams> extras = std::make_shared<AAFwk::WantParams>();
487 if (!OHOS::AppExecFwk::UnwrapWantParams(env, jsValue, *extras)) {
488 return nullptr;
489 }
490 liveViewContent->SetExtraInfo(extras);
491 }
492
493 //isOnlyLocalUpdate_?: boolean
494 bool isLocalUpdateOnly = false;
495 if (AppExecFwk::UnwrapBooleanByPropertyName(env, contentResult, "isLocalUpdateOnly", isLocalUpdateOnly)) {
496 liveViewContent->SetIsOnlyLocalUpdate(isLocalUpdateOnly);
497 }
498
499 // pictureInfo?: {[key, string]: Array<image.pixelMap>}
500 jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult, "pictureInfo", napi_object);
501 if (jsValue == nullptr) {
502 LBSLOGI(NAPI_UTILS, "No picture maps.");
503 return NapiGetNull(env);
504 }
505
506 std::map<std::string, std::vector<std::shared_ptr<Media::PixelMap>>> pictureMap;
507 if (GetLiveViewPictureInfo(env, jsValue, pictureMap) == nullptr) {
508 LBSLOGE(NAPI_UTILS, "Failed to get picture map from liveView content.");
509 return nullptr;
510 }
511 liveViewContent->SetPicture(pictureMap);
512
513 return NapiGetNull(env);
514 }
515
GetLiveViewPictures(const napi_env & env,const napi_value & picturesObj,std::vector<std::shared_ptr<Media::PixelMap>> & pictures)516 napi_value NotificationNapi::GetLiveViewPictures(
517 const napi_env &env, const napi_value &picturesObj,
518 std::vector<std::shared_ptr<Media::PixelMap>> &pictures)
519 {
520 LBSLOGD(NAPI_UTILS, "enter");
521
522 bool isArray = false;
523 napi_is_array(env, picturesObj, &isArray);
524 if (!isArray) {
525 LBSLOGE(NAPI_UTILS, "The picture is not array.");
526 return nullptr;
527 }
528
529 uint32_t length = 0;
530 napi_get_array_length(env, picturesObj, &length);
531 if (length == 0) {
532 LBSLOGE(NAPI_UTILS, "The array is empty.");
533 return nullptr;
534 }
535
536 for (uint32_t i = 0; i < length; ++i) {
537 napi_value pictureObj = nullptr;
538 napi_get_element(env, picturesObj, i, &pictureObj);
539 if (!AppExecFwk::IsTypeForNapiValue(env, pictureObj, napi_object)) {
540 LBSLOGE(NAPI_UTILS, "Wrong argument type. object expected.");
541 break;
542 }
543
544 std::shared_ptr<Media::PixelMap> pixelMap = Media::PixelMapNapi::GetPixelMap(env, pictureObj);
545 if (pixelMap == nullptr) {
546 LBSLOGE(NAPI_UTILS, "Invalid pixelMap.");
547 break;
548 }
549
550 pictures.emplace_back(pixelMap);
551 }
552
553 return NapiGetNull(env);
554 }
555
GetLiveViewPictureInfo(const napi_env & env,const napi_value & pictureMapObj,std::map<std::string,std::vector<std::shared_ptr<Media::PixelMap>>> & pictureMap)556 napi_value NotificationNapi::GetLiveViewPictureInfo(
557 const napi_env &env, const napi_value &pictureMapObj,
558 std::map<std::string, std::vector<std::shared_ptr<Media::PixelMap>>> &pictureMap)
559 {
560 LBSLOGD(NAPI_UTILS, "enter");
561
562 napi_value pictureNamesObj = nullptr;
563 uint32_t length = 0;
564 if (napi_get_property_names(env, pictureMapObj, &pictureNamesObj) != napi_ok) {
565 LBSLOGE(NAPI_UTILS, "Get picture names failed.");
566 return nullptr;
567 }
568 napi_get_array_length(env, pictureNamesObj, &length);
569 if (length == 0) {
570 LBSLOGE(NAPI_UTILS, "The pictures name is empty.");
571 return nullptr;
572 }
573
574 napi_value pictureNameObj = nullptr;
575 napi_value picturesObj = nullptr;
576 for (uint32_t index = 0; index < length; index++) {
577 napi_get_element(env, pictureNamesObj, index, &pictureNameObj);
578 std::string pictureName = AppExecFwk::UnwrapStringFromJS(env, pictureNameObj);
579 LBSLOGD(NAPI_UTILS, "%{public}s called, get pictures of %{public}s.", __func__, pictureName.c_str());
580 napi_get_named_property(env, pictureMapObj, pictureName.c_str(), &picturesObj);
581
582 std::vector<std::shared_ptr<Media::PixelMap>> pictures;
583 if (!GetLiveViewPictures(env, picturesObj, pictures)) {
584 LBSLOGE(NAPI_UTILS, "Get pictures of %{public}s failed.", pictureName.c_str());
585 break;
586 }
587
588 pictureMap[pictureName] = pictures;
589 }
590
591 return NapiGetNull(env);
592 }
593 }
594 }
595