• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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("called");
39     napi_value value = nullptr;
40     if (basicContent == nullptr) {
41         ANS_LOGE("null basicContent");
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("null localLiveViewContent");
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("SetTime 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("called");
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_LOGE("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("called");
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("called");
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("called");
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("called");
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("called");
389     napi_value value = nullptr;
390     if (basicContent == nullptr) {
391         ANS_LOGE("null basicContent");
392         return NapiGetBoolean(env, false);
393     }
394 
395     // lockScreenPicture?: pixelMap
396     if (!SetLockScreenPicture(env, basicContent, result)) {
397         ANS_LOGE("null lockScreenPicture");
398         return NapiGetBoolean(env, false);
399     }
400 
401     auto liveViewContent = static_cast<NotificationLiveViewContent *>(basicContent);
402     if (liveViewContent == nullptr) {
403         ANS_LOGE("null liveViewContent");
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     } else {
431         napi_value pictureMapObj = SetLiveViewPictureInfo(env, liveViewContent->GetPicture());
432         if (pictureMapObj == nullptr) {
433             ANS_LOGE("null pictureMapObj");
434             return NapiGetBoolean(env, false);
435         }
436         napi_set_named_property(env, result, "pictureInfo", pictureMapObj);
437     }
438 
439     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> agent = liveViewContent->GetExtensionWantAgent();
440     if (agent) {
441         napi_value wantAgent = nullptr;
442         wantAgent = CreateWantAgentByJS(env, agent);
443         napi_set_named_property(env, result, "extensionWantAgent", wantAgent);
444     } else {
445         napi_set_named_property(env, result, "extensionWantAgent", NapiGetNull(env));
446     }
447     return NapiGetBoolean(env, true);
448 }
449 
SetLiveViewPictureInfo(const napi_env & env,const std::map<std::string,std::vector<std::shared_ptr<Media::PixelMap>>> & pictureMap)450 napi_value Common::SetLiveViewPictureInfo(
451     const napi_env &env, const std::map<std::string, std::vector<std::shared_ptr<Media::PixelMap>>> &pictureMap)
452 {
453     ANS_LOGD("called");
454 
455     napi_value pictureMapObj = nullptr;
456     NAPI_CALL(env, napi_create_object(env, &pictureMapObj));
457 
458     for (auto iter = pictureMap.begin(); iter != pictureMap.end(); iter++) {
459         int count = 0;
460         napi_value picturesObj = nullptr;
461         napi_create_array(env, &picturesObj);
462         for (auto picture : iter->second) {
463             napi_value pictureObj = Media::PixelMapNapi::CreatePixelMap(env, picture);
464             napi_set_element(env, picturesObj, count, pictureObj);
465             count++;
466         }
467 
468         if (count > 0) {
469             napi_set_named_property(env, pictureMapObj, iter->first.c_str(), picturesObj);
470         }
471     }
472 
473     return pictureMapObj;
474 }
475 
GetNotificationLocalLiveViewContent(const napi_env & env,const napi_value & result,NotificationRequest & request)476 napi_value Common::GetNotificationLocalLiveViewContent(
477     const napi_env &env, const napi_value &result, NotificationRequest &request)
478 {
479     ANS_LOGD("called");
480 
481     napi_valuetype valuetype = napi_undefined;
482     napi_value contentResult = nullptr;
483     bool hasProperty = false;
484     NAPI_CALL(env, napi_has_named_property(env, result, "systemLiveView", &hasProperty));
485     if (!hasProperty) {
486         ANS_LOGE("Property localLiveView expected.");
487         return nullptr;
488     }
489     napi_get_named_property(env, result, "systemLiveView", &contentResult);
490     NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
491     if (valuetype != napi_object) {
492         ANS_LOGE("Wrong argument type. Object expected.");
493         return nullptr;
494     }
495 
496     std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> localLiveViewContent =
497         std::make_shared<OHOS::Notification::NotificationLocalLiveViewContent>();
498     if (localLiveViewContent == nullptr) {
499         ANS_LOGE("null localLiveViewContent");
500         return nullptr;
501     }
502 
503     if (GetNotificationLocalLiveViewContentDetailed(env, contentResult, localLiveViewContent) == nullptr) {
504         return nullptr;
505     }
506 
507     request.SetContent(std::make_shared<NotificationContent>(localLiveViewContent));
508 
509     // set isOnGoing of live view true
510     request.SetInProgress(true);
511 
512     return NapiGetNull(env);
513 }
514 
GetNotificationLocalLiveViewCapsule(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)515 napi_value Common::GetNotificationLocalLiveViewCapsule(
516     const napi_env &env, const napi_value &contentResult,
517     std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
518 {
519     napi_value capsuleResult = nullptr;
520     napi_valuetype valuetype = napi_undefined;
521     bool hasProperty = false;
522     size_t strLen = 0;
523     char str[STR_MAX_SIZE] = {0};
524     std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
525     napi_value result = nullptr;
526     int32_t intValue;
527 
528     ANS_LOGD("called");
529 
530     NAPI_CALL(env, napi_has_named_property(env, contentResult, "capsule", &hasProperty));
531 
532     napi_get_named_property(env, contentResult, "capsule", &capsuleResult);
533     NAPI_CALL(env, napi_typeof(env, capsuleResult, &valuetype));
534     if (valuetype != napi_object) {
535         ANS_LOGE("Wrong argument type. Object expected.");
536         return nullptr;
537     }
538 
539     NotificationCapsule capsule;
540 
541     NAPI_CALL(env, napi_has_named_property(env, capsuleResult, "title", &hasProperty));
542     if (hasProperty) {
543         napi_get_named_property(env, capsuleResult, "title", &result);
544         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
545         if (valuetype != napi_string) {
546             ANS_LOGE("Wrong argument type. String expected.");
547             std::string msg = "Incorrect parameter types. The type of title must be string.";
548             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
549             return nullptr;
550         }
551 
552         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
553         capsule.SetTitle(str);
554     }
555 
556     NAPI_CALL(env, napi_has_named_property(env, capsuleResult, "backgroundColor", &hasProperty));
557     if (hasProperty) {
558         napi_get_named_property(env, capsuleResult, "backgroundColor", &result);
559         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
560         if (valuetype != napi_string) {
561             ANS_LOGE("Wrong argument type. String expected.");
562             std::string msg = "Incorrect parameter types. The type of backgroundColor must be string.";
563             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
564             return nullptr;
565         }
566         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
567         capsule.SetBackgroundColor(str);
568         ANS_LOGD("capsule backgroundColor = %{public}s", str);
569     }
570 
571     NAPI_CALL(env, napi_has_named_property(env, capsuleResult, "content", &hasProperty));
572     if (hasProperty) {
573         napi_get_named_property(env, capsuleResult, "content", &result);
574         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
575         if (valuetype != napi_string) {
576             ANS_LOGE("Wrong argument type. String expected.");
577             std::string msg = "Incorrect parameter types. The type of content must be string.";
578             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
579             return nullptr;
580         }
581         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
582         capsule.SetContent(str);
583         ANS_LOGD("capsule content = %{public}s", str);
584     }
585 
586     NAPI_CALL(env, napi_has_named_property(env, capsuleResult, "icon", &hasProperty));
587     if (hasProperty) {
588         napi_get_named_property(env, capsuleResult, "icon", &result);
589         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
590         if (valuetype != napi_object) {
591             ANS_LOGE("Wrong argument type. Object expected.");
592             std::string msg = "Incorrect parameter types. The type of icon must be object.";
593             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
594             return nullptr;
595         }
596         pixelMap = Media::PixelMapNapi::GetPixelMap(env, result);
597         if (pixelMap == nullptr) {
598             ANS_LOGE("null pixelMap");
599             return nullptr;
600         }
601         capsule.SetIcon(pixelMap);
602         ANS_LOGD("capsule icon = %{public}d", pixelMap->GetWidth());
603     }
604 
605     //time?: number
606     NAPI_CALL(env, napi_has_named_property(env, capsuleResult, "time", &hasProperty));
607     if (hasProperty) {
608         napi_get_named_property(env, capsuleResult, "time", &result);
609         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
610         if (valuetype != napi_number) {
611             ANS_LOGE("Wrong argument type. Number expected.");
612             return nullptr;
613         }
614         napi_get_value_int32(env, result, &intValue);
615         capsule.SetTime(intValue);
616     }
617 
618     NAPI_CALL(env, napi_has_named_property(env, capsuleResult, "capsuleButtons", &hasProperty));
619     if (hasProperty) {
620         napi_get_named_property(env, capsuleResult, "capsuleButtons", &result);
621         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
622         if (valuetype != napi_object) {
623             ANS_LOGE("Wrong argument type. Object expected.");
624             return nullptr;
625         }
626         if (hasProperty && GetNotificationLocalLiveViewCapsuleCardButton(env, result, capsule) == nullptr) {
627             return nullptr;
628         }
629     }
630 
631     content->SetCapsule(capsule);
632     content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE);
633 
634     return NapiGetNull(env);
635 }
636 
GetNotificationLocalLiveViewCardButton(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)637 napi_value Common::GetNotificationLocalLiveViewCardButton(
638     const napi_env &env, const napi_value &contentResult,
639     std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
640 {
641     napi_value buttonResult = nullptr;
642 
643     ANS_LOGD("called");
644     napi_get_named_property(env, contentResult, "cardButtons", &buttonResult);
645 
646     // 解析iconbutton数组
647     std::vector<NotificationIconButton> cardButtons;
648     if (GetNotificationIconButton(env, buttonResult, cardButtons, BUTTON_MAX_SIZE) == nullptr) {
649         return nullptr;
650     }
651 
652     content->SetCardButton(cardButtons);
653     content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::CARD_BUTTON);
654     return NapiGetNull(env);
655 }
656 
GetNotificationLocalLiveViewCapsuleCardButton(const napi_env & env,const napi_value & capsuletResult,OHOS::Notification::NotificationCapsule & capsule)657 napi_value Common::GetNotificationLocalLiveViewCapsuleCardButton(
658     const napi_env &env, const napi_value &capsuletResult,
659     OHOS::Notification::NotificationCapsule &capsule)
660 {
661     ANS_LOGD("called");
662     std::vector<NotificationIconButton> cardButtons;
663     if (GetNotificationIconButton(env, capsuletResult, cardButtons, CAPSULE_BTN_MAX_SIZE) == nullptr) {
664         return nullptr;
665     }
666 
667     capsule.SetCapsuleButton(cardButtons);
668     return NapiGetNull(env);
669 }
670 
GetNotificationIconButton(const napi_env & env,const napi_value & buttonResult,std::vector<NotificationIconButton> & cardButtons,const uint32_t maxLen)671 napi_value Common::GetNotificationIconButton(
672     const napi_env &env, const napi_value &buttonResult,
673     std::vector<NotificationIconButton> &cardButtons, const uint32_t maxLen)
674 {
675     // cardButton_item?: NotificationIconButton;
676     napi_value cardButton = nullptr;
677     napi_value result = nullptr;
678     napi_valuetype valuetype = napi_undefined;
679 
680     bool isArray = false;
681     bool boolValue = false;
682     uint32_t length = 0;
683 
684     size_t strLen = 0;
685     char str[STR_MAX_SIZE] = {0};
686     bool hasProperty = false;
687 
688     ANS_LOGD("called");
689     napi_is_array(env, buttonResult, &isArray);
690     if (!isArray) {
691         ANS_LOGE("Property names is expected to be an array.");
692         return nullptr;
693     }
694     napi_get_array_length(env, buttonResult, &length);
695     if (length > maxLen) {
696         length = maxLen;
697     }
698 
699     for (size_t i = 0; i < length; i++) {
700         napi_get_element(env, buttonResult, i, &cardButton);
701 
702         NAPI_CALL(env, napi_typeof(env, cardButton, &valuetype));
703         if (valuetype != napi_object) {
704             ANS_LOGE("Wrong argument type. Object expected.");
705             return nullptr;
706         }
707         // 数组item
708         NotificationIconButton button;
709         // name: string
710         NAPI_CALL(env, napi_has_named_property(env, cardButton, "name", &hasProperty));
711         if (!hasProperty) {
712             ANS_LOGE("Property name expected.");
713             return nullptr;
714         } else {
715             napi_get_named_property(env, cardButton, "name", &result);
716             NAPI_CALL(env, napi_typeof(env, result, &valuetype));
717             if (valuetype != napi_string) {
718                 ANS_LOGE("Wrong argument type. String expected.");
719                 return nullptr;
720             }
721             NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
722             button.SetName(str);
723         }
724 
725         // iconResource: Resource
726         NAPI_CALL(env, napi_has_named_property(env, cardButton, "iconResource", &hasProperty));
727         if (!hasProperty) {
728             ANS_LOGE("Property iconResource expected.");
729             return nullptr;
730         } else {
731             // if icon type is Rersouce, get the resource object and return.
732             napi_value iconResource = nullptr;
733             napi_get_named_property(env, cardButton, "iconResource", &iconResource);
734             NAPI_CALL(env, napi_typeof(env, iconResource, &valuetype));
735             if (valuetype != napi_object) {
736                 ANS_LOGE("Wrong argument type. iconResource Object expected.");
737                 return nullptr;
738             }
739 
740             // icon?: Resource
741             auto resource = std::make_shared<ResourceManager::Resource>();
742             if (Common::GetResourceObject(env, resource, iconResource) == nullptr) {
743                 ANS_LOGE("Invalid icon resource object or not resource.");
744             } else {
745                 button.SetIconResource(resource);
746             }
747 
748             // icon?: image.PixelMap
749             auto pixelMap = Media::PixelMapNapi::GetPixelMap(env, iconResource);
750             if (pixelMap == nullptr) {
751                 ANS_LOGE("null pixelMap");
752             } else {
753                 button.SetIconImage(pixelMap);
754             }
755         }
756 
757         // text?: string
758         NAPI_CALL(env, napi_has_named_property(env, cardButton, "text", &hasProperty));
759         if (hasProperty) {
760             napi_get_named_property(env, cardButton, "text", &result);
761             NAPI_CALL(env, napi_typeof(env, result, &valuetype));
762             if (valuetype != napi_string) {
763                 ANS_LOGE("Wrong argument type. String expected.");
764                 return nullptr;
765             }
766             NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
767             button.SetText(str);
768         }
769 
770         // hidePanel?: boolean;
771         NAPI_CALL(env, napi_has_named_property(env, cardButton, "hidePanel", &hasProperty));
772         if (hasProperty) {
773             napi_get_named_property(env, cardButton, "hidePanel", &result);
774             NAPI_CALL(env, napi_typeof(env, result, &valuetype));
775             if (valuetype != napi_boolean) {
776                 ANS_LOGE("Wrong argument type. bool expected.");
777                 return nullptr;
778             }
779             napi_get_value_bool(env, result, &boolValue);
780             button.SetHidePanel(boolValue);
781         }
782 
783         cardButtons.push_back(button);
784         ANS_LOGD("icon button = %{public}s", button.Dump().c_str());
785     }
786 
787     return NapiGetNull(env);
788 }
789 
GetResourceObject(napi_env env,std::shared_ptr<ResourceManager::Resource> & resource,napi_value & value)790 napi_value Common::GetResourceObject(napi_env env,
791     std::shared_ptr<ResourceManager::Resource> &resource, napi_value &value)
792 {
793     napi_value name;
794     size_t strLen = 0;
795     char str[STR_MAX_SIZE] = {0};
796     std::vector<std::string> typeName = {"bundleName", "moduleName"};
797     for (const std::string& type: typeName) {
798         napi_status status = napi_get_named_property(env, value, type.c_str(), &name);
799         if (status != napi_ok || name == nullptr) {
800             ANS_LOGE("Failed to get resource name property");
801             return nullptr;
802         }
803         napi_valuetype valueType = napi_valuetype::napi_undefined;
804         NAPI_CALL(env, napi_typeof(env, name, &valueType));
805         if (valueType != napi_string) {
806             ANS_LOGE("Failed to get resource type %{public}d", valueType);
807             return nullptr;
808         }
809         NAPI_CALL(env, napi_get_value_string_utf8(env, name, str, STR_MAX_SIZE - 1, &strLen));
810         if (type == "bundleName") {
811             resource->bundleName = str;
812         } else if (type == "moduleName") {
813             resource->moduleName = str;
814         }
815     }
816 
817     napi_value id;
818     napi_status status = napi_get_named_property(env, value, "id", &id);
819     if (status != napi_ok || id == nullptr) {
820         ANS_LOGE("Failed to get resource id property");
821         return nullptr;
822     }
823     napi_valuetype valueType = napi_valuetype::napi_undefined;
824     napi_typeof(env, id, &valueType);
825     if (valueType != napi_number) {
826         ANS_LOGE("Failed to get resource name string");
827         std::string msg = "Incorrect parameter types. The type of id must be number.";
828         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
829         return nullptr;
830     }
831     int32_t resId = 0;
832     status = napi_get_value_int32(env, id, &resId);
833     if (status != napi_ok) {
834         ANS_LOGE("Wrong argument type. Object expected.");
835         return nullptr;
836     }
837     resource->id = resId;
838     ANS_LOGE("Get to get resource bundleName %{public}s moduleName %{public}s id %{public}d",
839         resource->bundleName.c_str(), resource->moduleName.c_str(), resource->id);
840     return NapiGetNull(env);
841 }
842 
GetNotificationLocalLiveViewButton(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)843 napi_value Common::GetNotificationLocalLiveViewButton(
844     const napi_env &env, const napi_value &contentResult,
845     std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
846 {
847     napi_value result = nullptr;
848     napi_valuetype valuetype = napi_undefined;
849     bool isArray = false;
850     uint32_t length = 0;
851     napi_value buttonResult = nullptr;
852     bool hasProperty = false;
853     char str[STR_MAX_SIZE] = {0};
854     size_t strLen = 0;
855 
856     ANS_LOGD("called");
857 
858     napi_get_named_property(env, contentResult, "button", &buttonResult);
859     NAPI_CALL(env, napi_typeof(env, buttonResult, &valuetype));
860     if (valuetype != napi_object) {
861         ANS_LOGE("Wrong argument type. Object expected.");
862         return nullptr;
863     }
864 
865     NotificationLocalLiveViewButton button;
866 
867     NAPI_CALL(env, napi_has_named_property(env, buttonResult, "names", &hasProperty));
868     if (hasProperty) {
869         napi_get_named_property(env, buttonResult, "names", &result);
870         napi_is_array(env, result, &isArray);
871         if (!isArray) {
872             ANS_LOGE("Property names is expected to be an array.");
873             return nullptr;
874         }
875         napi_get_array_length(env, result, &length);
876         for (size_t i = 0; i < length; i++) {
877             napi_value buttonName = nullptr;
878             napi_get_element(env, result, i, &buttonName);
879             NAPI_CALL(env, napi_typeof(env, buttonName, &valuetype));
880             if (valuetype != napi_string) {
881                 ANS_LOGE("Wrong argument type. String expected.");
882                 return nullptr;
883             }
884             NAPI_CALL(env, napi_get_value_string_utf8(env, buttonName, str, STR_MAX_SIZE - 1, &strLen));
885             button.addSingleButtonName(str);
886             ANS_LOGD("button buttonName = %{public}s.", str);
887         }
888     }
889 
890     NAPI_CALL(env, napi_has_named_property(env, buttonResult, "icons", &hasProperty));
891     if (hasProperty) {
892         napi_get_named_property(env, buttonResult, "icons", &result);
893         napi_is_array(env, result, &isArray);
894         if (!isArray) {
895             ANS_LOGE("Property icons is expected to be an array.");
896             return nullptr;
897         }
898         napi_get_array_length(env, result, &length);
899         for (size_t i = 0; i < length; i++) {
900             napi_value buttonIcon = nullptr;
901             std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
902             napi_get_element(env, result, i, &buttonIcon);
903             NAPI_CALL(env, napi_typeof(env, buttonIcon, &valuetype));
904             if (valuetype != napi_object) {
905                 ANS_LOGE("Wrong argument type. Object expected.");
906                 return nullptr;
907             }
908             pixelMap = Media::PixelMapNapi::GetPixelMap(env, buttonIcon);
909             if (pixelMap != nullptr && static_cast<uint32_t>(pixelMap->GetByteCount()) <= MAX_ICON_SIZE) {
910                 button.addSingleButtonIcon(pixelMap);
911             } else {
912                 ANS_LOGE("Invalid pixelMap object or pixelMap is over size.");
913                 return nullptr;
914             }
915         }
916     }
917 
918     NAPI_CALL(env, napi_has_named_property(env, buttonResult, "iconsResource", &hasProperty));
919     if (hasProperty) {
920         napi_get_named_property(env, buttonResult, "iconsResource", &result);
921         napi_is_array(env, result, &isArray);
922         if (!isArray) {
923             ANS_LOGE("Property icon resource is expected to be an array.");
924             return nullptr;
925         }
926         napi_get_array_length(env, result, &length);
927         for (size_t i = 0; i < length; i++) {
928             napi_value iconResource = nullptr;
929             auto resource = std::make_shared<ResourceManager::Resource>();
930             napi_get_element(env, result, i, &iconResource);
931             NAPI_CALL(env, napi_typeof(env, iconResource, &valuetype));
932             if (valuetype != napi_object) {
933                 ANS_LOGE("Wrong argument type. Object expected.");
934                 return nullptr;
935             }
936             if (Common::GetResourceObject(env, resource, iconResource) == nullptr) {
937                 ANS_LOGW("Invalid icon resource object.");
938                 return nullptr;
939             } else {
940                 button.addSingleButtonIconResource(resource);
941             }
942         }
943     }
944 
945     ANS_LOGD("button buttonIcon = %{public}s", str);
946     content->SetButton(button);
947     content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON);
948 
949     return NapiGetNull(env);
950 }
951 
GetNotificationLocalLiveViewProgress(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)952 napi_value Common::GetNotificationLocalLiveViewProgress(const napi_env &env, const napi_value &contentResult,
953     std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
954 {
955     napi_value result = nullptr;
956     napi_valuetype valuetype = napi_undefined;
957     bool hasProperty = false;
958     int32_t intValue = -1;
959     bool boolValue = false;
960     napi_value progressResult = nullptr;
961 
962     ANS_LOGD("called");
963 
964     napi_get_named_property(env, contentResult, "progress", &progressResult);
965     NAPI_CALL(env, napi_typeof(env, progressResult, &valuetype));
966     if (valuetype != napi_object) {
967         ANS_LOGE("Wrong argument type. Object expected.");
968         return nullptr;
969     }
970 
971     NotificationProgress progress;
972 
973     NAPI_CALL(env, napi_has_named_property(env, progressResult, "maxValue", &hasProperty));
974     if (hasProperty) {
975         napi_get_named_property(env, progressResult, "maxValue", &result);
976         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
977         if (valuetype != napi_number) {
978             std::string msg = "Incorrect parameter types. The type of maxValue must be number.";
979             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
980             ANS_LOGE("Wrong argument type. Number expected.");
981             return nullptr;
982         }
983         napi_get_value_int32(env, result, &intValue);
984         progress.SetMaxValue(intValue);
985         ANS_LOGD("progress intValue = %{public}d", intValue);
986     }
987 
988     NAPI_CALL(env, napi_has_named_property(env, progressResult, "currentValue", &hasProperty));
989     if (hasProperty) {
990         napi_get_named_property(env, progressResult, "currentValue", &result);
991         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
992         if (valuetype != napi_number) {
993             ANS_LOGE("Wrong argument type. Number expected.");
994             std::string msg = "Incorrect parameter types. The type of currentValue must be number.";
995             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
996             return nullptr;
997         }
998         napi_get_value_int32(env, result, &intValue);
999         progress.SetCurrentValue(intValue);
1000         ANS_LOGD("progress currentValue = %{public}d", intValue);
1001     }
1002 
1003     NAPI_CALL(env, napi_has_named_property(env, progressResult, "isPercentage", &hasProperty));
1004     if (hasProperty) {
1005         napi_get_named_property(env, progressResult, "isPercentage", &result);
1006         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1007         if (valuetype != napi_boolean) {
1008             ANS_LOGE("Wrong argument type. bool expected.");
1009             std::string msg = "Incorrect parameter types. The type of isPercentage must be bool.";
1010             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1011             return nullptr;
1012         }
1013         napi_get_value_bool(env, result, &boolValue);
1014         progress.SetIsPercentage(boolValue);
1015         ANS_LOGD("progress isPercentage = %{public}d", boolValue);
1016     }
1017 
1018     content->SetProgress(progress);
1019     content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::PROGRESS);
1020 
1021     return NapiGetNull(env);
1022 }
1023 
GetNotificationLocalLiveViewTime(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)1024 napi_value Common::GetNotificationLocalLiveViewTime(const napi_env &env, const napi_value &contentResult,
1025     std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
1026 {
1027     napi_value result = nullptr;
1028     napi_valuetype valuetype = napi_undefined;
1029     bool hasProperty = false;
1030     int32_t intValue = -1;
1031     bool boolValue = false;
1032     napi_value timeResult = nullptr;
1033 
1034     ANS_LOGD("called");
1035 
1036     napi_get_named_property(env, contentResult, "time", &timeResult);
1037     NAPI_CALL(env, napi_typeof(env, timeResult, &valuetype));
1038     if (valuetype != napi_object) {
1039         ANS_LOGE("Wrong argument type. Object expected.");
1040         return nullptr;
1041     }
1042 
1043     NotificationTime time;
1044 
1045     NAPI_CALL(env, napi_has_named_property(env, timeResult, "initialTime", &hasProperty));
1046     if (hasProperty) {
1047         napi_get_named_property(env, timeResult, "initialTime", &result);
1048         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1049         if (valuetype != napi_number) {
1050             ANS_LOGE("Wrong argument type. Number expected.");
1051             std::string msg = "Incorrect parameter types. The type of initialTime must be number.";
1052             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1053             return nullptr;
1054         }
1055         napi_get_value_int32(env, result, &intValue);
1056         time.SetInitialTime(intValue);
1057         content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::INITIAL_TIME);
1058         ANS_LOGD("time initialTime = %{public}d", intValue);
1059     }
1060 
1061     NAPI_CALL(env, napi_has_named_property(env, timeResult, "isCountDown", &hasProperty));
1062     if (hasProperty) {
1063         napi_get_named_property(env, timeResult, "isCountDown", &result);
1064         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1065         if (valuetype != napi_boolean) {
1066             ANS_LOGE("Wrong argument type. bool expected.");
1067             std::string msg = "Incorrect parameter types. The type of isCountDown must be bool.";
1068             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1069             return nullptr;
1070         }
1071         napi_get_value_bool(env, result, &boolValue);
1072         time.SetIsCountDown(boolValue);
1073         ANS_LOGD("time isCountDown = %{public}d", boolValue);
1074     }
1075 
1076     NAPI_CALL(env, napi_has_named_property(env, timeResult, "isPaused", &hasProperty));
1077     if (hasProperty) {
1078         napi_get_named_property(env, timeResult, "isPaused", &result);
1079         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1080         if (valuetype != napi_boolean) {
1081             ANS_LOGE("Wrong argument type. bool expected.");
1082             std::string msg = "Incorrect parameter types. The type of isPaused must be bool.";
1083             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1084             return nullptr;
1085         }
1086         napi_get_value_bool(env, result, &boolValue);
1087         time.SetIsPaused(boolValue);
1088         ANS_LOGD("time isPaused = %{public}d", boolValue);
1089     }
1090 
1091     NAPI_CALL(env, napi_has_named_property(env, timeResult, "isInTitle", &hasProperty));
1092     if (hasProperty) {
1093         napi_get_named_property(env, timeResult, "isInTitle", &result);
1094         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1095         if (valuetype != napi_boolean) {
1096             ANS_LOGE("Wrong argument type. bool expected.");
1097             std::string msg = "Incorrect parameter types. The type of isInTitle must be bool.";
1098             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1099             return nullptr;
1100         }
1101         napi_get_value_bool(env, result, &boolValue);
1102         time.SetIsInTitle(boolValue);
1103         ANS_LOGD("time isInTitle = %{public}d", boolValue);
1104     }
1105 
1106     content->SetTime(time);
1107     content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::TIME);
1108 
1109     return NapiGetNull(env);
1110 }
1111 
GetNotificationLocalLiveViewContentDetailed(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)1112 napi_value Common::GetNotificationLocalLiveViewContentDetailed(
1113     const napi_env &env, const napi_value &contentResult,
1114     std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> content)
1115 {
1116     bool hasProperty = false;
1117     int32_t type = -1;
1118     napi_value result = nullptr;
1119     napi_valuetype valuetype = napi_undefined;
1120 
1121     ANS_LOGD("called");
1122 
1123     //title, text
1124     if (GetNotificationBasicContentDetailed(env, contentResult, content) == nullptr) {
1125         ANS_LOGE("Basic content get fail.");
1126         return nullptr;
1127     }
1128 
1129     // typeCode
1130     NAPI_CALL(env, napi_has_named_property(env, contentResult, "typeCode", &hasProperty));
1131     if (!hasProperty) {
1132         ANS_LOGE("Property typeCode expected.");
1133         return nullptr;
1134     }
1135     napi_get_named_property(env, contentResult, "typeCode", &result);
1136     NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1137     if (valuetype != napi_number) {
1138         ANS_LOGE("Wrong argument typeCode. Number expected.");
1139         std::string msg = "Incorrect parameter types. The type of typeCode must be number.";
1140         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1141         return nullptr;
1142     }
1143     napi_get_value_int32(env, result, &type);
1144     content->SetType(type);
1145     ANS_LOGD("localLiveView type = %{public}d", type);
1146 
1147     //capsule?
1148     NAPI_CALL(env, napi_has_named_property(env, contentResult, "capsule", &hasProperty));
1149     if (hasProperty && GetNotificationLocalLiveViewCapsule(env, contentResult, content) == nullptr) {
1150         return nullptr;
1151     }
1152 
1153     //button?
1154     NAPI_CALL(env, napi_has_named_property(env, contentResult, "button", &hasProperty));
1155     if (hasProperty && GetNotificationLocalLiveViewButton(env, contentResult, content) == nullptr) {
1156         return nullptr;
1157     }
1158 
1159     //cardButton?
1160     NAPI_CALL(env, napi_has_named_property(env, contentResult, "cardButtons", &hasProperty));
1161     if (hasProperty && GetNotificationLocalLiveViewCardButton(env, contentResult, content) == nullptr) {
1162         return nullptr;
1163     }
1164 
1165     // liveViewType?: LiveViewTypes
1166     NAPI_CALL(env, napi_has_named_property(env, contentResult, "liveViewType", &hasProperty));
1167     NotificationLocalLiveViewContent::LiveViewTypes outType = NotificationLocalLiveViewContent::LiveViewTypes::LIVE_VIEW_ACTIVITY;
1168     if (hasProperty && AppExecFwk::UnwrapInt32ByPropertyName(env, contentResult, "liveViewType", type)) {
1169         if (!AnsEnumUtil::LiveViewTypesJSToC(LiveViewTypes(type), outType)) {
1170             ANS_LOGE("The liveview types is not valid.");
1171             return nullptr;
1172         }
1173     }
1174     content->SetLiveViewType(outType);
1175 
1176     //progress?
1177     NAPI_CALL(env, napi_has_named_property(env, contentResult, "progress", &hasProperty));
1178     if (hasProperty && GetNotificationLocalLiveViewProgress(env, contentResult, content) == nullptr) {
1179         return nullptr;
1180     }
1181 
1182     //time?
1183     NAPI_CALL(env, napi_has_named_property(env, contentResult, "time", &hasProperty));
1184     if (hasProperty && GetNotificationLocalLiveViewTime(env, contentResult, content) == nullptr) {
1185         return nullptr;
1186     }
1187 
1188     return NapiGetNull(env);
1189 }
1190 
GetNotificationLiveViewContent(const napi_env & env,const napi_value & result,NotificationRequest & request)1191 napi_value Common::GetNotificationLiveViewContent(
1192     const napi_env &env, const napi_value &result, NotificationRequest &request)
1193 {
1194     ANS_LOGD("called");
1195 
1196     napi_value contentResult = AppExecFwk::GetPropertyValueByPropertyName(env, result, "liveView", napi_object);
1197     if (contentResult == nullptr) {
1198         ANS_LOGE("null contentResult");
1199         return nullptr;
1200     }
1201 
1202     std::shared_ptr<NotificationLiveViewContent> liveViewContent = std::make_shared<NotificationLiveViewContent>();
1203     if (liveViewContent == nullptr) {
1204         ANS_LOGE("null liveViewContent");
1205         return nullptr;
1206     }
1207 
1208     if (GetNotificationLiveViewContentDetailed(env, contentResult, liveViewContent) == nullptr) {
1209         return nullptr;
1210     }
1211 
1212     request.SetContent(std::make_shared<NotificationContent>(liveViewContent));
1213 
1214     return NapiGetNull(env);
1215 }
1216 
GetNotificationLiveViewContentDetailed(const napi_env & env,const napi_value & contentResult,std::shared_ptr<NotificationLiveViewContent> & liveViewContent)1217 napi_value Common::GetNotificationLiveViewContentDetailed(
1218     const napi_env &env, const napi_value &contentResult,
1219     std::shared_ptr<NotificationLiveViewContent> &liveViewContent)
1220 {
1221     ANS_LOGD("called");
1222 
1223     // lockScreenPicture?: pixelMap
1224     if (GetLockScreenPicture(env, contentResult, liveViewContent) == nullptr) {
1225         ANS_LOGE("null lockScreenPicture");
1226         return nullptr;
1227     }
1228 
1229     // status: NotificationLiveViewContent::LiveViewStatus
1230     int32_t status = 0;
1231     if (!AppExecFwk::UnwrapInt32ByPropertyName(env, contentResult, "status", status)) {
1232         ANS_LOGE("Failed to get status from liveView content.");
1233         return nullptr;
1234     }
1235     NotificationLiveViewContent::LiveViewStatus outType = NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_BUTT;
1236     if (!AnsEnumUtil::LiveViewStatusJSToC(LiveViewStatus(status), outType)) {
1237         ANS_LOGE("The liveview status is not valid.");
1238         return nullptr;
1239     }
1240     liveViewContent->SetLiveViewStatus(outType);
1241 
1242     // version?: uint32_t
1243     napi_value jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult,
1244         "version", napi_number);
1245     if (jsValue != nullptr) {
1246         int32_t version = NotificationLiveViewContent::MAX_VERSION;
1247         NAPI_CALL(env, napi_get_value_int32(env, jsValue, &version));
1248         liveViewContent->SetVersion(version);
1249     }
1250 
1251     // extraInfo?: {[key:string] : any}
1252     jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult, "extraInfo", napi_object);
1253     if (jsValue != nullptr) {
1254         std::shared_ptr<AAFwk::WantParams> extras = std::make_shared<AAFwk::WantParams>();
1255         if (!OHOS::AppExecFwk::UnwrapWantParams(env, jsValue, *extras)) {
1256             return nullptr;
1257         }
1258         liveViewContent->SetExtraInfo(extras);
1259     }
1260 
1261     //isOnlyLocalUpdate_?: boolean
1262     bool isLocalUpdateOnly = false;
1263     if (AppExecFwk::UnwrapBooleanByPropertyName(env, contentResult, "isLocalUpdateOnly", isLocalUpdateOnly)) {
1264         liveViewContent->SetIsOnlyLocalUpdate(isLocalUpdateOnly);
1265     }
1266 
1267     // pictureInfo?: {[key, string]: Array<image.pixelMap>}
1268     jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult, "pictureInfo", napi_object);
1269     if (jsValue == nullptr) {
1270         ANS_LOGE("null jsValue");
1271         } else {
1272         std::map<std::string, std::vector<std::shared_ptr<Media::PixelMap>>> pictureMap;
1273         if (GetLiveViewPictureInfo(env, jsValue, pictureMap) == nullptr) {
1274             ANS_LOGE("null LiveViewPictureInfo");
1275             return nullptr;
1276         }
1277         liveViewContent->SetPicture(pictureMap);
1278     }
1279     if (GetLiveViewWantAgent(env, contentResult, liveViewContent) == nullptr) {
1280         ANS_LOGD("no live view wantAgent");
1281         return nullptr;
1282     }
1283 
1284     return NapiGetNull(env);
1285 }
1286 
GetLiveViewWantAgent(const napi_env & env,const napi_value & value,std::shared_ptr<NotificationLiveViewContent> & liveViewContent)1287 napi_value Common::GetLiveViewWantAgent(const napi_env &env, const napi_value &value,
1288     std::shared_ptr<NotificationLiveViewContent> &liveViewContent)
1289 {
1290     if (liveViewContent == nullptr) {
1291         ANS_LOGE("null liveViewContent");
1292         return nullptr;
1293     }
1294 
1295     bool hasProperty = false;
1296     AbilityRuntime::WantAgent::WantAgent *wantAgent = nullptr;
1297     napi_value result = nullptr;
1298     napi_valuetype valuetype = napi_undefined;
1299 
1300     NAPI_CALL(env, napi_has_named_property(env, value, "extensionWantAgent", &hasProperty));
1301     if (hasProperty) {
1302         napi_get_named_property(env, value, "extensionWantAgent", &result);
1303         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1304         if (valuetype != napi_object) {
1305             ANS_LOGE("Wrong argument type. Object expected.");
1306             std::string msg = "Incorrect parameter types. The type of wantAgent must be object.";
1307             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1308             return nullptr;
1309         }
1310         napi_unwrap(env, result, (void **)&wantAgent);
1311         if (wantAgent == nullptr) {
1312             ANS_LOGE("null wantAgent");
1313             return nullptr;
1314         }
1315         std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> sWantAgent =
1316             std::make_shared<AbilityRuntime::WantAgent::WantAgent>(*wantAgent);
1317         liveViewContent->SetExtensionWantAgent(sWantAgent);
1318     }
1319     return NapiGetNull(env);
1320 }
1321 
GetLiveViewPictures(const napi_env & env,const napi_value & picturesObj,std::vector<std::shared_ptr<Media::PixelMap>> & pictures)1322 napi_value Common::GetLiveViewPictures(
1323     const napi_env &env, const napi_value &picturesObj,
1324     std::vector<std::shared_ptr<Media::PixelMap>> &pictures)
1325 {
1326     ANS_LOGD("called");
1327 
1328     bool isArray = false;
1329     napi_is_array(env, picturesObj, &isArray);
1330     if (!isArray) {
1331         ANS_LOGE("The picture is not array.");
1332         return nullptr;
1333     }
1334 
1335     uint32_t length = 0;
1336     napi_get_array_length(env, picturesObj, &length);
1337     if (length == 0) {
1338         ANS_LOGE("The array is empty.");
1339         return nullptr;
1340     }
1341 
1342     for (uint32_t i = 0; i < length; ++i) {
1343         napi_value pictureObj = nullptr;
1344         napi_get_element(env, picturesObj, i, &pictureObj);
1345         if (!AppExecFwk::IsTypeForNapiValue(env, pictureObj, napi_object)) {
1346             ANS_LOGE("Wrong argument type. object expected.");
1347             break;
1348         }
1349 
1350         std::shared_ptr<Media::PixelMap> pixelMap = Media::PixelMapNapi::GetPixelMap(env, pictureObj);
1351         if (pixelMap == nullptr) {
1352             ANS_LOGE("null pixelMap");
1353             break;
1354         }
1355 
1356         pictures.emplace_back(pixelMap);
1357     }
1358 
1359     return NapiGetNull(env);
1360 }
1361 
GetLiveViewPictureInfo(const napi_env & env,const napi_value & pictureMapObj,std::map<std::string,std::vector<std::shared_ptr<Media::PixelMap>>> & pictureMap)1362 napi_value Common::GetLiveViewPictureInfo(
1363     const napi_env &env, const napi_value &pictureMapObj,
1364     std::map<std::string, std::vector<std::shared_ptr<Media::PixelMap>>> &pictureMap)
1365 {
1366     ANS_LOGD("called");
1367 
1368     napi_value pictureNamesObj = nullptr;
1369     uint32_t length = 0;
1370     if (napi_get_property_names(env, pictureMapObj, &pictureNamesObj) != napi_ok) {
1371         ANS_LOGE("Get picture names failed.");
1372         return nullptr;
1373     }
1374     napi_get_array_length(env, pictureNamesObj, &length);
1375     if (length == 0) {
1376         ANS_LOGE("The pictures name is empty.");
1377         return nullptr;
1378     }
1379 
1380     napi_value pictureNameObj = nullptr;
1381     napi_value picturesObj = nullptr;
1382     for (uint32_t index = 0; index < length; index++) {
1383         napi_get_element(env, pictureNamesObj, index, &pictureNameObj);
1384         std::string pictureName = AppExecFwk::UnwrapStringFromJS(env, pictureNameObj);
1385         ANS_LOGD("%{public}s called, get pictures of %{public}s.", __func__, pictureName.c_str());
1386         napi_get_named_property(env, pictureMapObj, pictureName.c_str(), &picturesObj);
1387 
1388         std::vector<std::shared_ptr<Media::PixelMap>> pictures;
1389         if (!GetLiveViewPictures(env, picturesObj, pictures)) {
1390             ANS_LOGE("Get pictures of %{public}s failed.", pictureName.c_str());
1391             break;
1392         }
1393 
1394         pictureMap[pictureName] = pictures;
1395     }
1396 
1397     return NapiGetNull(env);
1398 }
1399 }
1400 }
1401