1 /*
2 * Copyright (c) 2025 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 #include "sts_notification_content.h"
16
17 #include "sts_common.h"
18 #include "sts_convert_other.h"
19 #include "want_params.h"
20 #include "ani_common_want.h"
21 #include "sts_notification_manager.h"
22
23 namespace OHOS {
24 namespace NotificationSts {
25 using namespace OHOS::AAFwk;
26 using namespace OHOS::AppExecFwk;
27 using NotificationBasicContent = OHOS::Notification::NotificationBasicContent;
28
StsToC(const STSLiveViewStatus inType,LiveViewStatus & outType)29 bool StsLiveViewStatusUtils::StsToC(const STSLiveViewStatus inType, LiveViewStatus &outType)
30 {
31 ANS_LOGD("StsLiveViewStatusUtils::StsToC inType = %{public}d", static_cast<int>(inType));
32 switch (inType) {
33 case STSLiveViewStatus::LIVE_VIEW_CREATE:
34 outType = LiveViewStatus::LIVE_VIEW_CREATE;
35 break;
36 case STSLiveViewStatus::LIVE_VIEW_INCREMENTAL_UPDATE:
37 outType = LiveViewStatus::LIVE_VIEW_INCREMENTAL_UPDATE;
38 break;
39 case STSLiveViewStatus::LIVE_VIEW_END:
40 outType = LiveViewStatus::LIVE_VIEW_END;
41 break;
42 case STSLiveViewStatus::LIVE_VIEW_FULL_UPDATE:
43 outType = LiveViewStatus::LIVE_VIEW_FULL_UPDATE;
44 break;
45 default:
46 ANS_LOGE("LiveViewStatus %{public}d is an invalid value", inType);
47 return false;
48 }
49 return true;
50 }
51
CToSts(const LiveViewStatus inType,STSLiveViewStatus & outType)52 bool StsLiveViewStatusUtils::CToSts(const LiveViewStatus inType, STSLiveViewStatus &outType)
53 {
54 ANS_LOGD("StsLiveViewStatusUtils::CToSts:inType = %{public}d", static_cast<int>(inType));
55 switch (inType) {
56 case LiveViewStatus::LIVE_VIEW_CREATE:
57 outType = STSLiveViewStatus::LIVE_VIEW_CREATE;
58 break;
59 case LiveViewStatus::LIVE_VIEW_INCREMENTAL_UPDATE:
60 outType = STSLiveViewStatus::LIVE_VIEW_INCREMENTAL_UPDATE;
61 break;
62 case LiveViewStatus::LIVE_VIEW_END:
63 outType = STSLiveViewStatus::LIVE_VIEW_END;
64 break;
65 case LiveViewStatus::LIVE_VIEW_FULL_UPDATE:
66 outType = STSLiveViewStatus::LIVE_VIEW_FULL_UPDATE;
67 break;
68 default:
69 ANS_LOGE("LiveViewStatus %{public}d is an invalid value", inType);
70 return false;
71 }
72 return true;
73 }
74
LiveViewStatusEtsToC(ani_env * env,ani_enum_item enumItem,LiveViewStatus & liveViewStatus)75 bool LiveViewStatusEtsToC(ani_env *env, ani_enum_item enumItem, LiveViewStatus &liveViewStatus)
76 {
77 ANS_LOGD("LiveViewStatusEtsToC call");
78 if (env == nullptr) {
79 ANS_LOGE("LiveViewStatusEtsToC failed, env is nullptr");
80 return false;
81 }
82 STSLiveViewStatus stsLiveViewStatus = STSLiveViewStatus::LIVE_VIEW_CREATE;
83 if (!EnumConvertAniToNative(env, enumItem, stsLiveViewStatus)
84 || !StsLiveViewStatusUtils::StsToC(stsLiveViewStatus, liveViewStatus)) {
85 ANS_LOGE("LiveViewStatusEtsToC failed");
86 return false;
87 }
88 ANS_LOGD("LiveViewStatusEtsToC end");
89 return true;
90 }
91
LiveViewStatusCToEts(ani_env * env,LiveViewStatus liveViewStatus,ani_enum_item & enumItem)92 bool LiveViewStatusCToEts(ani_env *env, LiveViewStatus liveViewStatus, ani_enum_item &enumItem)
93 {
94 ANS_LOGD("LiveViewStatusCToEts call");
95 if (env == nullptr) {
96 ANS_LOGE("LiveViewStatusCToEts failed, env is nullptr");
97 return false;
98 }
99 STSLiveViewStatus stsLiveViewStatus = STSLiveViewStatus::LIVE_VIEW_CREATE;
100 if (!StsLiveViewStatusUtils::CToSts(liveViewStatus, stsLiveViewStatus)
101 || !EnumConvertNativeToAni(env,
102 "Lnotification/notificationContent/#LiveViewStatus", stsLiveViewStatus, enumItem)) {
103 ANS_LOGE("LiveViewStatusCToEts failed");
104 return false;
105 }
106 ANS_LOGD("LiveViewStatusCToEts end");
107 return true;
108 }
109
LiveViewTypesEtsToC(ani_env * env,ani_enum_item enumItem,LiveViewTypes & liveViewTypes)110 bool LiveViewTypesEtsToC(ani_env *env, ani_enum_item enumItem, LiveViewTypes &liveViewTypes)
111 {
112 ANS_LOGD("LiveViewTypesEtsToC call");
113 return EnumConvertAniToNative(env, enumItem, liveViewTypes);
114 }
115
LiveViewTypesCToEts(ani_env * env,LiveViewTypes liveViewTypes,ani_enum_item & enumItem)116 bool LiveViewTypesCToEts(ani_env *env, LiveViewTypes liveViewTypes, ani_enum_item &enumItem)
117 {
118 ANS_LOGD("LiveViewTypesCToEts call");
119 return EnumConvertNativeToAni(env,
120 "Lnotification/notificationContent/#LiveViewTypes", liveViewTypes, enumItem);
121 }
122
UnWarpNotificationProgress(ani_env * env,ani_object obj,NotificationProgress & notificationProgress)123 void UnWarpNotificationProgress(ani_env *env, ani_object obj, NotificationProgress ¬ificationProgress)
124 {
125 ANS_LOGD("UnWarpNotificationProgress call");
126 if (env == nullptr || obj == nullptr) {
127 ANS_LOGE("UnWarpNotificationProgress failed, has nullptr");
128 return;
129 }
130 ani_double maxValueAni = 0.0;
131 ani_boolean isUndefined = ANI_TRUE;
132 if (GetPropertyDouble(env, obj, "maxValue", isUndefined, maxValueAni) == ANI_OK
133 && isUndefined == ANI_FALSE) {
134 notificationProgress.SetMaxValue(static_cast<int32_t>(maxValueAni));
135 } else {
136 ANS_LOGD("UnWarpNotificationProgress: get maxValue failed");
137 }
138 ani_double currentValueAni = 0.0;
139 if (GetPropertyDouble(env, obj, "currentValue", isUndefined, currentValueAni) == ANI_OK
140 && isUndefined == ANI_FALSE) {
141 notificationProgress.SetCurrentValue(static_cast<int32_t>(currentValueAni));
142 } else {
143 ANS_LOGD("UnWarpNotificationProgress: get currentValue failed");
144 }
145 bool isPercentage = true;
146 if (ANI_OK == GetPropertyBool(env, obj, "isPercentage", isUndefined, isPercentage)
147 && isUndefined == ANI_FALSE) {
148 notificationProgress.SetIsPercentage(isPercentage);
149 } else {
150 ANS_LOGD("UnWarpNotificationProgress: get isPercentage failed");
151 }
152 ANS_LOGD("UnWarpNotificationProgress end");
153 }
154
WarpNotificationProgress(ani_env * env,const NotificationProgress & progress,ani_object & progressObject)155 bool WarpNotificationProgress(ani_env *env, const NotificationProgress &progress, ani_object &progressObject)
156 {
157 ANS_LOGD("WarpNotificationProgress call");
158 if (env == nullptr) {
159 ANS_LOGE("WarpNotificationProgress failed, env is nullptr");
160 return false;
161 }
162 ani_class progressClass = nullptr;
163 if (!CreateClassObjByClassName(env,
164 "Lnotification/notificationContent/NotificationProgressInner;", progressClass, progressObject)
165 || progressObject == nullptr) {
166 ANS_LOGE("WarpNotificationProgress: create class failed");
167 return false;
168 }
169 // maxValue?: number;
170 if (!SetPropertyOptionalByDouble(env, progressObject, "maxValue", progress.GetMaxValue())) {
171 ANS_LOGD("WarpNotificationProgress: set maxValue failed");
172 }
173 // currentValue?: number;
174 if (!SetPropertyOptionalByDouble(env, progressObject, "currentValue", progress.GetCurrentValue())) {
175 ANS_LOGD("WarpNotificationProgress: set currentValue failed");
176 }
177 // isPercentage?: boolean;
178 if (!SetPropertyOptionalByBoolean(env, progressObject, "isPercentage", progress.GetIsPercentage())) {
179 ANS_LOGD("WarpNotificationProgress: set currentValue failed");
180 }
181 ANS_LOGD("WarpNotificationProgress end");
182 return true;
183 }
184
UnWarpNotificationTime(ani_env * env,ani_object obj,NotificationTime & notificationTime)185 void UnWarpNotificationTime(ani_env *env, ani_object obj,
186 NotificationTime ¬ificationTime)
187 {
188 ANS_LOGD("UnWarpNotificationTime call");
189 if (env == nullptr || obj == nullptr) {
190 ANS_LOGE("UnWarpNotificationTime failed, has nullptr");
191 return;
192 }
193 ani_boolean isUndefined = ANI_TRUE;
194 ani_double initialTime = 0.0;
195 if (GetPropertyDouble(env, obj, "version", isUndefined, initialTime) == ANI_OK
196 && isUndefined == ANI_FALSE) {
197 notificationTime.SetInitialTime(static_cast<int32_t>(initialTime));
198 } else {
199 ANS_LOGD("UnWarpNotificationTime: get version failed");
200 }
201 bool isCountDown = true;
202 if (ANI_OK == GetPropertyBool(env, obj, "isCountDown", isUndefined, isCountDown)
203 && isUndefined == ANI_FALSE) {
204 notificationTime.SetIsCountDown(isCountDown);
205 } else {
206 ANS_LOGD("UnWarpNotificationTime: get isCountDown failed");
207 }
208 bool isPaused = true;
209 if (ANI_OK == GetPropertyBool(env, obj, "isPaused", isUndefined, isPaused)
210 && isUndefined == ANI_FALSE) {
211 notificationTime.SetIsPaused(isPaused);
212 } else {
213 ANS_LOGD("UnWarpNotificationTime: get isPaused failed");
214 }
215 bool isInTitle = true;
216 isUndefined = ANI_TRUE;
217 if (ANI_OK == GetPropertyBool(env, obj, "isInTitle", isUndefined, isInTitle)
218 && isUndefined == ANI_FALSE) {
219 notificationTime.SetIsInTitle(isInTitle);
220 } else {
221 ANS_LOGD("UnWarpNotificationTime: get isInTitle failed");
222 }
223 ANS_LOGD("UnWarpNotificationTime end");
224 }
225
WarpNotificationTime(ani_env * env,const NotificationTime & time,bool isInitialTimeExist,ani_object & timeObject)226 bool WarpNotificationTime(ani_env *env, const NotificationTime &time, bool isInitialTimeExist, ani_object &timeObject)
227 {
228 ANS_LOGD("WarpNotificationTime call");
229 if (env == nullptr) {
230 ANS_LOGE("WarpNotificationTime failed, env is nullptr");
231 return false;
232 }
233 ani_class timeClass = nullptr;
234 if (!CreateClassObjByClassName(env,
235 "Lnotification/notificationContent/NotificationTimeInner;", timeClass, timeObject)
236 || timeObject == nullptr) {
237 ANS_LOGE("WarpNotificationTime: create class failed");
238 return false;
239 }
240 // initialTime?: number;
241 if (isInitialTimeExist) {
242 if (!SetPropertyOptionalByDouble(env, timeObject, "initialTime", time.GetInitialTime())) {
243 ANS_LOGD("WarpNotificationTime: set initialTime failed");
244 }
245 }
246 // isCountDown?: boolean;
247 if (!SetPropertyOptionalByBoolean(env, timeObject, "isCountDown", time.GetIsCountDown())) {
248 ANS_LOGD("WarpNotificationTime: set isCountDown failed");
249 }
250 // isPaused?: boolean;
251 if (!SetPropertyOptionalByBoolean(env, timeObject, "isPaused", time.GetIsPaused())) {
252 ANS_LOGD("WarpNotificationTime: set isPaused failed");
253 }
254 // isInTitle?: boolean;
255 if (!SetPropertyOptionalByBoolean(env, timeObject, "isInTitle", time.GetIsInTitle())) {
256 ANS_LOGD("WarpNotificationTime: set isInTitle failed");
257 }
258 ANS_LOGD("WarpNotificationTime end");
259 return true;
260 }
261
UnWarpNotificationIconButton(ani_env * env,ani_object obj,NotificationIconButton & iconButton)262 ani_status UnWarpNotificationIconButton(ani_env *env, ani_object obj, NotificationIconButton &iconButton)
263 {
264 ANS_LOGD("UnWarpNotificationIconButton call");
265 if (env == nullptr || obj == nullptr) {
266 ANS_LOGE("UnWarpNotificationIconButton failed, env is nullptr");
267 return ANI_ERROR;
268 }
269 ani_status status = ANI_ERROR;
270 ani_boolean isUndefined = ANI_TRUE;
271 std::string tempStr = "";
272 if ((status = GetPropertyString(env, obj, "name", isUndefined, tempStr)) != ANI_OK || isUndefined == ANI_TRUE) {
273 ANS_LOGE("UnWarpNotificationIconButton: get name failed, status = %{public}d", status);
274 return ANI_INVALID_ARGS;
275 }
276 std::string name = GetResizeStr(tempStr, STR_MAX_SIZE);
277 iconButton.SetName(name);
278 ani_ref iconRef = {};
279 if ((status = GetPropertyRef(env, obj, "iconResource", isUndefined, iconRef)) != ANI_OK
280 || isUndefined == ANI_TRUE || iconRef == nullptr) {
281 ANS_LOGE("UnWarpNotificationIconButton: get iconResource failed, status = %{public}d", status);
282 return ANI_INVALID_ARGS;
283 }
284 ResourceManager::Resource resource;
285 if (ANI_OK == UnwrapResource(env, static_cast<ani_object>(iconRef), resource)) {
286 iconButton.SetIconResource(std::make_shared<ResourceManager::Resource>(resource));
287 } else {
288 std::shared_ptr<PixelMap> pixelMap = GetPixelMapFromAni(env, static_cast<ani_object>(iconRef));
289 if (pixelMap == nullptr) {
290 ANS_LOGE("UnWarpNotificationIconButton: get iconResource failed");
291 return ANI_INVALID_ARGS;
292 }
293 iconButton.SetIconImage(pixelMap);
294 }
295 if (GetPropertyString(env, obj, "text", isUndefined, tempStr) == ANI_OK && isUndefined == ANI_FALSE) {
296 std::string text = GetResizeStr(tempStr, STR_MAX_SIZE);
297 iconButton.SetName(text);
298 } else {
299 ANS_LOGD("UnWarpNotificationIconButton: get text failed");
300 }
301 bool hidePanel = true;
302 if (ANI_OK == GetPropertyBool(env, obj, "hidePanel", isUndefined, hidePanel)
303 && isUndefined == ANI_FALSE) {
304 iconButton.SetHidePanel(hidePanel);
305 } else {
306 ANS_LOGD("UnWarpNotificationIconButton: get hidePanel failed");
307 }
308 ANS_LOGD("UnWarpNotificationIconButton end");
309 return status;
310 }
311
GetIconButtonArray(ani_env * env,ani_object param,const char * name,std::vector<NotificationIconButton> & res)312 ani_status GetIconButtonArray(ani_env *env,
313 ani_object param, const char *name, std::vector<NotificationIconButton> &res)
314 {
315 ANS_LOGD("GetIconButtonArray call");
316 if (env == nullptr || param == nullptr || name == nullptr) {
317 ANS_LOGE("GetIconButtonArray failed, has nullptr");
318 return ANI_ERROR;
319 }
320 ani_ref arrayObj = nullptr;
321 ani_boolean isUndefined = true;
322 ani_status status = ANI_ERROR;
323 ani_double length;
324 if (((status = GetPropertyRef(env, param, name, isUndefined, arrayObj)) != ANI_OK) || isUndefined == ANI_TRUE) {
325 ANS_LOGI("get param failed, may be %{public}s : undefined", name);
326 return ANI_INVALID_ARGS;
327 }
328 status = env->Object_GetPropertyByName_Double(static_cast<ani_object>(arrayObj), "length", &length);
329 if (status != ANI_OK) {
330 ANS_LOGI("status : %{public}d", status);
331 return status;
332 }
333 for (int i = 0; i < static_cast<int>(length); i++) {
334 ani_ref buttonRef;
335 status = env->Object_CallMethodByName_Ref(static_cast<ani_object>(arrayObj),
336 "$_get", "I:Lstd/core/Object;", &buttonRef, (ani_int)i);
337 if (status != ANI_OK) {
338 ANS_LOGI("status : %{public}d, index: %{public}d", status, i);
339 return status;
340 }
341 NotificationIconButton button;
342 if (UnWarpNotificationIconButton(env, static_cast<ani_object>(buttonRef), button) == ANI_OK) {
343 res.push_back(button);
344 } else {
345 ANS_LOGE("GetIconButtonArray: UnWarpNotificationIconButton failed");
346 return ANI_INVALID_ARGS;
347 }
348 }
349 ANS_LOGD("GetIconButtonArray end");
350 return status;
351 }
352
UnWarpNotificationLocalLiveViewButton(ani_env * env,ani_object obj,NotificationLocalLiveViewButton & button)353 void UnWarpNotificationLocalLiveViewButton(ani_env *env, ani_object obj,
354 NotificationLocalLiveViewButton &button)
355 {
356 ANS_LOGD("UnWarpNotificationLocalLiveViewButton call");
357 if (env == nullptr || obj == nullptr) {
358 ANS_LOGE("UnWarpNotificationLocalLiveViewButton failed, has nullptr");
359 return;
360 }
361 std::vector<std::string> names = {};
362 ani_boolean isUndefined = ANI_TRUE;
363 // names?: Array<string>
364 if (GetPropertyStringArray(env, obj, "names", isUndefined, names) == ANI_OK && isUndefined == ANI_FALSE) {
365 for (auto name: names) {
366 button.addSingleButtonName(GetResizeStr(name, STR_MAX_SIZE));
367 }
368 } else {
369 ANS_LOGD("UnWarpNotificationLocalLiveViewButton get names failed.");
370 }
371 // icons?: Array<image.PixelMap>
372 std::vector<std::shared_ptr<PixelMap>> icons = {};
373 if (ANI_OK == GetPixelMapArray(env, obj, "icons", icons)) {
374 for (auto icon : icons) {
375 button.addSingleButtonIcon(icon);
376 }
377 } else {
378 ANS_LOGD("UnWarpNotificationLocalLiveViewButton get icons failed.");
379 }
380 // iconsResource?: Array<Resource>
381 std::vector<ResourceManager::Resource> resources = {};
382 if (ANI_OK == GetResourceArray(env, obj, "iconsResource", resources)) {
383 for (auto res : resources) {
384 std::shared_ptr<ResourceManager::Resource> pRes = std::make_shared<ResourceManager::Resource>(res);
385 button.addSingleButtonIconResource(pRes);
386 }
387 } else {
388 ANS_LOGD("UnWarpNotificationLocalLiveViewButton get iconsResource failed.");
389 }
390 ANS_LOGD("UnWarpNotificationLocalLiveViewButton end");
391 }
392
WarpNotificationLocalLiveViewButton(ani_env * env,const NotificationLocalLiveViewButton & button,ani_object & buttonObject)393 bool WarpNotificationLocalLiveViewButton(
394 ani_env *env, const NotificationLocalLiveViewButton &button, ani_object &buttonObject)
395 {
396 ANS_LOGD("WarpNotificationLocalLiveViewButton call");
397 if (env == nullptr) {
398 ANS_LOGE("WarpNotificationLocalLiveViewButton failed, env is nullptr");
399 return false;
400 }
401 ani_class buttonClass = nullptr;
402 if (!CreateClassObjByClassName(env,
403 "Lnotification/notificationContent/NotificationButtonInner;", buttonClass, buttonObject)
404 || buttonObject == nullptr) {
405 ANS_LOGE("WarpNotificationLocalLiveViewButton: create class failed");
406 return false;
407 }
408 // names?: Array<string>;
409 std::vector<std::string> names = button.GetAllButtonNames();
410 ani_object namesObjectArray = GetAniStringArrayByVectorString(env, names);
411 if (namesObjectArray == nullptr) {
412 ANS_LOGE("namesObjectArray is nullptr");
413 return false;
414 }
415 if (!SetPropertyByRef(env, buttonObject, "names", namesObjectArray)) {
416 ANS_LOGE("Set names failed");
417 return false;
418 }
419 // icons?: Array<image.PixelMap>;
420 std::vector<std::shared_ptr<Media::PixelMap>> icons = button.GetAllButtonIcons();
421 ani_object iconsObjectArray = GetAniArrayPixelMap(env, icons);
422 if (iconsObjectArray == nullptr) {
423 ANS_LOGE("iconsObjectArray is nullptr");
424 return false;
425 }
426 if (!SetPropertyByRef(env, buttonObject, "icons", iconsObjectArray)) {
427 ANS_LOGE("Set icons failed");
428 return false;
429 }
430 // iconsResource?: Array<Resource>;
431 std::vector<std::shared_ptr<ResourceManager::Resource>> iconsResource = button.GetAllButtonIconResource();
432 ani_object resourceObjectArray = GetAniArrayResource(env, iconsResource);
433 if (resourceObjectArray == nullptr) {
434 ANS_LOGE("resourceObjectArray is nullptr");
435 return false;
436 }
437 if (!SetPropertyByRef(env, buttonObject, "iconsResource", resourceObjectArray)) {
438 ANS_LOGE("Set iconsResource failed");
439 return false;
440 }
441 ANS_LOGD("WarpNotificationLocalLiveViewButton end");
442 return true;
443 }
444
getCapsuleByIcon(ani_env * env,ani_object obj,std::shared_ptr<PixelMap> & pixelMap)445 bool getCapsuleByIcon(ani_env *env, ani_object obj, std::shared_ptr<PixelMap> &pixelMap)
446 {
447 ani_boolean isUndefined = ANI_TRUE;
448 pixelMap = nullptr;
449 ani_ref tempRef = nullptr;
450 GetPropertyRefValue(env, obj, "icon", isUndefined, tempRef);
451 if (tempRef != nullptr) {
452 if (isUndefined == ANI_TRUE) {
453 ANS_LOGE("icon of Capsule is undefined");
454 return false;
455 }
456 std::shared_ptr<PixelMap> pixelMap = GetPixelMapFromAni(env, static_cast<ani_object>(tempRef));
457 }
458 return true;
459 }
460
getCapsuleByButtons(ani_env * env,ani_object obj,std::vector<NotificationIconButton> & iconButtons)461 bool getCapsuleByButtons(ani_env *env, ani_object obj, std::vector<NotificationIconButton> &iconButtons)
462 {
463 ani_boolean isUndefined = ANI_TRUE;
464 iconButtons = {};
465 ani_ref tempRef = nullptr;
466 GetPropertyRefValue(env, obj, "capsuleButtons", isUndefined, tempRef);
467 if (tempRef != nullptr) {
468 if (isUndefined == ANI_TRUE) {
469 ANS_LOGE("capsuleButtons of Capsule is undefined");
470 return false;
471 }
472 if (GetIconButtonArray(env, obj, "capsuleButtons", iconButtons) != ANI_OK || iconButtons.empty()) {
473 ANS_LOGE("get capsuleButtons failed");
474 return false;
475 }
476 }
477 return true;
478 }
479
getCapsuleByString(ani_env * env,ani_object obj,const char * name,std::string & out)480 bool getCapsuleByString(ani_env *env, ani_object obj, const char *name, std::string &out)
481 {
482 ani_boolean isUndefined = ANI_TRUE;
483 out = "";
484 ani_ref tempRef = nullptr;
485 GetPropertyRefValue(env, obj, name, isUndefined, tempRef);
486 if (tempRef != nullptr) {
487 if (isUndefined == ANI_TRUE) {
488 ANS_LOGE("%{public}s of Capsule is undefined", name);
489 return false;
490 }
491 if (GetStringByAniString(env, reinterpret_cast<ani_string>(tempRef), out) != ANI_OK) {
492 ANS_LOGE("get string of %{public}s failed", name);
493 return false;
494 }
495 }
496 return true;
497 }
498
getCapsuleByDouble(ani_env * env,ani_object obj,const char * name,double & out)499 bool getCapsuleByDouble(ani_env *env, ani_object obj, const char *name, double &out)
500 {
501 ani_boolean isUndefined = ANI_TRUE;
502 out = ERR_OK;
503 ani_ref tempRef = nullptr;
504 GetPropertyRefValue(env, obj, name, isUndefined, tempRef);
505 if (tempRef != nullptr) {
506 if (isUndefined == ANI_TRUE) {
507 ANS_LOGE("%{public}s of Capsule is undefined", name);
508 return false;
509 }
510 if ((env->Object_CallMethodByName_Double(static_cast<ani_object>(tempRef),
511 "unboxed", ":D", &out)) != ANI_OK) {
512 ANS_LOGE("get double of %{public}s failed", name);
513 return false;
514 }
515 }
516 return true;
517 }
518
UnWarpNotificationCapsule(ani_env * env,ani_object obj,NotificationCapsule & capsule)519 bool UnWarpNotificationCapsule(ani_env *env, ani_object obj, NotificationCapsule &capsule)
520 {
521 ANS_LOGD("UnWarpNotificationCapsule call");
522 if (env == nullptr || obj == nullptr) {
523 ANS_LOGE("UnWarpNotificationCapsule failed, has nullptr");
524 return false;
525 }
526 std::string tempStr = "";
527 if (!getCapsuleByString(env, obj, "title", tempStr)) {
528 ANS_LOGE("get title failed");
529 return false;
530 }
531 capsule.SetTitle(GetResizeStr(tempStr, STR_MAX_SIZE));
532
533 if (!getCapsuleByString(env, obj, "backgroundColor", tempStr)) {
534 ANS_LOGE("get backgroundColor failed");
535 return false;
536 }
537 capsule.SetBackgroundColor(GetResizeStr(tempStr, STR_MAX_SIZE));
538
539 if (!getCapsuleByString(env, obj, "content", tempStr)) {
540 ANS_LOGE("get content failed");
541 return false;
542 }
543 capsule.SetContent(GetResizeStr(tempStr, STR_MAX_SIZE));
544
545 ani_double time = 0.0;
546 if (!getCapsuleByDouble(env, obj, "time", time)) {
547 ANS_LOGE("get content failed");
548 return false;
549 }
550 capsule.SetTime(static_cast<int32_t>(time));
551 std::shared_ptr<PixelMap> pixelMap = nullptr;
552 if (!getCapsuleByIcon(env, obj, pixelMap) || pixelMap == nullptr) {
553 ANS_LOGE("get icon failed");
554 return false;
555 }
556 capsule.SetIcon(pixelMap);
557 std::vector<NotificationIconButton> iconButtons = {};
558 if (!getCapsuleByButtons(env, obj, iconButtons)) {
559 ANS_LOGE("get icon failed");
560 return false;
561 }
562 capsule.SetCapsuleButton(iconButtons);
563 return true;
564 }
565
WarpNotificationIconButton(ani_env * env,const NotificationIconButton & button)566 ani_object WarpNotificationIconButton(ani_env *env, const NotificationIconButton &button)
567 {
568 ANS_LOGD("WarpNotificationIconButton call");
569 if (env == nullptr) {
570 ANS_LOGE("WarpNotificationIconButton failed, env is nullptr");
571 return nullptr;
572 }
573 ani_class iconButtonCls = nullptr;
574 ani_object iconButtonObject = nullptr;
575 if (!CreateClassObjByClassName(env,
576 "Lnotification/notificationContent/NotificationIconButtonInner;", iconButtonCls, iconButtonObject)
577 || iconButtonObject == nullptr) {
578 ANS_LOGE("WarpNotificationIconButton: create class failed");
579 return nullptr;
580 }
581 // name: string
582 if (!SetPropertyOptionalByString(env, iconButtonObject, "name", button.GetName())) {
583 ANS_LOGE("WarpNotificationIconButton: set name failed");
584 return nullptr;
585 }
586 // iconResource: IconType; type IconType = Resource | image.PixelMap;
587 std::shared_ptr<Media::PixelMap> icon = button.GetIconImage();
588 if (icon) {
589 ani_object pixelMapObject = CreateAniPixelMap(env, icon);
590 if (pixelMapObject == nullptr) {
591 ANS_LOGE("WarpNotificationIconButton: pixelMapObject is nullptr");
592 return nullptr;
593 }
594 if (!SetPropertyByRef(env, iconButtonObject, "iconResource", pixelMapObject)) {
595 ANS_LOGE("WarpNotificationIconButton: set iconResource failed");
596 return nullptr;
597 }
598 } else {
599 ani_object resourceObject = GetAniResource(env, button.GetIconResource());
600 if (resourceObject == nullptr) {
601 ANS_LOGE("WarpNotificationIconButton: resourceObject is nullptr");
602 return nullptr;
603 }
604 if (!SetPropertyByRef(env, iconButtonObject, "iconResource", resourceObject)) {
605 ANS_LOGE("WarpNotificationIconButton: set iconResource failed");
606 return nullptr;
607 }
608 }
609 // text?: string;
610 SetPropertyOptionalByString(env, iconButtonObject, "text", button.GetText());
611 // hidePanel?: boolean;
612 SetPropertyOptionalByBoolean(env, iconButtonObject, "hidePanel", button.GetHidePanel());
613 ANS_LOGD("WarpNotificationIconButton end");
614 return iconButtonObject;
615 }
616
GetAniIconButtonArray(ani_env * env,const std::vector<NotificationIconButton> buttons)617 ani_object GetAniIconButtonArray(ani_env *env, const std::vector<NotificationIconButton> buttons)
618 {
619 ANS_LOGD("GetAniIconButtonArray start");
620 if (env == nullptr || buttons.empty()) {
621 ANS_LOGE("GetAniIconButtonArray failed, env is nullptr or buttons is empty");
622 return nullptr;
623 }
624 ani_object arrayObj = newArrayClass(env, buttons.size());
625 if (arrayObj == nullptr) {
626 ANS_LOGE("GetAniIconButtonArray failed, arrayObj is nullptr");
627 return nullptr;
628 }
629 ani_size index = 0;
630 for (auto &button : buttons) {
631 ani_object item = WarpNotificationIconButton(env, button);
632 if (item == nullptr) {
633 ANS_LOGE("GetAniIconButtonArray: item is nullptr");
634 return nullptr;
635 }
636 if (ANI_OK != env->Object_CallMethodByName_Void(arrayObj, "$_set", "ILstd/core/Object;:V", index, item)) {
637 ANS_LOGE("GetAniIconButtonArray: add item failed");
638 return nullptr;
639 }
640 index ++;
641 }
642 ANS_LOGE("GetAniIconButtonArray end");
643 return arrayObj;
644 }
645
WarpNotificationCapsule(ani_env * env,const NotificationCapsule & capsule,ani_object & capsuleObject)646 bool WarpNotificationCapsule(ani_env *env, const NotificationCapsule &capsule, ani_object &capsuleObject)
647 {
648 ANS_LOGD("WarpNotificationCapsule start");
649 if (env == nullptr) {
650 ANS_LOGE("GetAniIconButtonArray failed, env is nullptr");
651 return false;
652 }
653 ani_class capsuleClass = nullptr;
654 if (!CreateClassObjByClassName(env,
655 "Lnotification/notificationContent/NotificationCapsuleInner;", capsuleClass, capsuleObject)
656 || capsuleObject == nullptr) {
657 ANS_LOGE("GetAniIconButtonArray: create class failed");
658 return false;
659 }
660 // title?: string;
661 SetPropertyOptionalByString(env, capsuleObject, "title", capsule.GetTitle());
662 // icon?: image.PixelMap;
663 std::shared_ptr<Media::PixelMap> icon = capsule.GetIcon();
664 if (icon) {
665 ani_object pixelMapObject = CreateAniPixelMap(env, icon);
666 if (pixelMapObject == nullptr) {
667 ANS_LOGE("CreatePixelMap failed, pixelMapObject is nullptr");
668 } else {
669 SetPropertyByRef(env, capsuleObject, "icon", pixelMapObject);
670 }
671 }
672 // backgroundColor?: string;
673 if (!SetPropertyOptionalByString(env, capsuleObject, "backgroundColor", capsule.GetBackgroundColor())) {
674 ANS_LOGD("WarpNotificationCapsule: set backgroundColor failed");
675 }
676 //content?: string;
677 if (!SetPropertyOptionalByString(env, capsuleObject, "content", capsule.GetContent())) {
678 ANS_LOGD("WarpNotificationCapsule: set content failed");
679 }
680 // time?: number;
681 if (!SetPropertyOptionalByDouble(env, capsuleObject, "time", capsule.GetTime())) {
682 ANS_LOGD("WarpNotificationCapsule: set time failed");
683 }
684 // capsuleButtons?: Array<NotificationIconButton>;
685 std::vector<NotificationIconButton> buttons = capsule.GetCapsuleButton();
686 ani_object buttonsObjectArray = GetAniIconButtonArray(env, buttons);
687 if (buttonsObjectArray == nullptr
688 || SetPropertyByRef(env, capsuleObject, "capsuleButtons", buttonsObjectArray)) {
689 ANS_LOGD("WarpNotificationCapsule: set capsuleButtons failed");
690 }
691 ANS_LOGD("WarpNotificationCapsule end");
692 return true;
693 }
694
UnWarpNotificationBasicContent(ani_env * env,ani_object obj,std::shared_ptr<NotificationBasicContent> basicContent)695 ani_status UnWarpNotificationBasicContent(ani_env *env, ani_object obj,
696 std::shared_ptr<NotificationBasicContent> basicContent)
697 {
698 ANS_LOGD("UnWarpNotificationBasicContent call");
699 if (env == nullptr || obj == nullptr || basicContent == nullptr) {
700 ANS_LOGE("UnWarpNotificationBasicContent failed, has nullptr");
701 return ANI_ERROR;
702 }
703 ani_status status = ANI_ERROR;
704 ani_boolean isUndefined = ANI_TRUE;
705 std::string title;
706 if ((status = GetPropertyString(env, obj, "title", isUndefined, title)) != ANI_OK || isUndefined == ANI_TRUE) {
707 ANS_LOGE("UnWarpNotificationBasicContent: get title failed, status = %{public}d", status);
708 return ANI_INVALID_ARGS;
709 }
710 basicContent->SetTitle(GetResizeStr(title, SHORT_TEXT_SIZE));
711 std::string text;
712 if ((status = GetPropertyString(env, obj, "text", isUndefined, text)) != ANI_OK || isUndefined == ANI_TRUE) {
713 ANS_LOGE("UnWarpNotificationBasicContent: get text failed, status = %{public}d", status);
714 return ANI_INVALID_ARGS;
715 }
716 basicContent->SetText(GetResizeStr(text, COMMON_TEXT_SIZE));
717 std::string additionalText;
718 if (GetPropertyString(env, obj, "additionalText", isUndefined, additionalText) == ANI_OK
719 && isUndefined == ANI_FALSE) {
720 basicContent->SetAdditionalText(GetResizeStr(additionalText, COMMON_TEXT_SIZE));
721 } else {
722 ANS_LOGD("UnWarpNotificationBasicContent: get additionalText failed");
723 }
724 ani_ref lockscreenPictureRef = {};
725 if (env->Object_GetPropertyByName_Ref(obj, "lockscreenPicture", &lockscreenPictureRef) != ANI_OK
726 || lockscreenPictureRef == nullptr) {
727 ANS_LOGD("UnWarpNotificationBasicContent: get lockscreenPicture failed");
728 } else {
729 std::shared_ptr<PixelMap> pixelMap = GetPixelMapFromAni(env, static_cast<ani_object>(lockscreenPictureRef));
730 if (pixelMap != nullptr) {
731 basicContent->SetLockScreenPicture(pixelMap);
732 } else {
733 ANS_LOGD("UnWarpNotificationBasicContent: get lockscreenPicture by pixelMap failed");
734 }
735 }
736 ANS_LOGD("UnWarpNotificationBasicContent end");
737 return status;
738 }
739
UnWarpNotificationNormalContent(ani_env * env,ani_object obj,std::shared_ptr<NotificationNormalContent> & normalContent)740 ani_status UnWarpNotificationNormalContent(ani_env *env, ani_object obj,
741 std::shared_ptr<NotificationNormalContent> &normalContent)
742 {
743 ANS_LOGD("UnWarpNotificationNormalContent call");
744 if (env == nullptr || obj == nullptr || normalContent == nullptr) {
745 ANS_LOGE("UnWarpNotificationNormalContent failed, has nullptr");
746 return ANI_ERROR;
747 }
748 ani_status status = ANI_ERROR;
749 if ((status = UnWarpNotificationBasicContent(env, obj, normalContent)) != ANI_OK) {
750 ANS_LOGE("UnWarpNotificationNormalContent failed");
751 return status;
752 }
753 ANS_LOGE("UnWarpNotificationNormalContent end");
754 return status;
755 }
756
UnWarpNotificationLongTextContent(ani_env * env,ani_object obj,std::shared_ptr<NotificationLongTextContent> & longTextContent)757 ani_status UnWarpNotificationLongTextContent(ani_env *env, ani_object obj,
758 std::shared_ptr<NotificationLongTextContent> &longTextContent)
759 {
760 ANS_LOGD("UnWarpNotificationLongTextContent call");
761 if (env == nullptr || obj == nullptr || longTextContent == nullptr) {
762 ANS_LOGE("UnWarpNotificationLongTextContent failed, has nullptr");
763 return ANI_ERROR;
764 }
765 ani_status status = ANI_ERROR;
766 if ((status = UnWarpNotificationBasicContent(env, obj, longTextContent)) != ANI_OK) {
767 ANS_LOGE("UnWarpNotificationLongTextContent:get BasicContent failed");
768 return status;
769 }
770 ani_boolean isUndefined = ANI_TRUE;
771 std::string longText;
772 if ((status = GetPropertyString(env, obj, "longText", isUndefined, longText)) != ANI_OK
773 || isUndefined == ANI_TRUE) {
774 ANS_LOGE("UnWarpNotificationLongTextContent:get longText failed");
775 return ANI_INVALID_ARGS;
776 }
777 longTextContent->SetLongText(GetResizeStr(longText, COMMON_TEXT_SIZE));
778 std::string briefText;
779 if ((status = GetPropertyString(env, obj, "briefText", isUndefined, briefText)) != ANI_OK
780 || isUndefined == ANI_TRUE) {
781 ANS_LOGE("UnWarpNotificationLongTextContent:get briefText failed");
782 return ANI_INVALID_ARGS;
783 }
784 longTextContent->SetBriefText(GetResizeStr(briefText, SHORT_TEXT_SIZE));
785 std::string expandedTitle;
786 if ((status = GetPropertyString(env, obj, "expandedTitle", isUndefined, expandedTitle)) != ANI_OK
787 || isUndefined == ANI_TRUE) {
788 ANS_LOGE("UnWarpNotificationLongTextContent:get expandedTitle failed");
789 return ANI_INVALID_ARGS;
790 }
791 longTextContent->SetExpandedTitle(GetResizeStr(expandedTitle, SHORT_TEXT_SIZE));
792 ANS_LOGD("UnWarpNotificationLongTextContent end");
793 return status;
794 }
795
UnWarpNotificationMultiLineContent(ani_env * env,ani_object obj,std::shared_ptr<NotificationMultiLineContent> & multiLineContent)796 ani_status UnWarpNotificationMultiLineContent(ani_env *env, ani_object obj,
797 std::shared_ptr<NotificationMultiLineContent> &multiLineContent)
798 {
799 ANS_LOGD("UnWarpNotificationMultiLineContent call");
800 if (env == nullptr || obj == nullptr || multiLineContent == nullptr) {
801 ANS_LOGE("UnWarpNotificationMultiLineContent failed, has nullptr");
802 return ANI_ERROR;
803 }
804 ani_status status = ANI_ERROR;
805 if ((status = UnWarpNotificationBasicContent(env, obj, multiLineContent)) != ANI_OK) {
806 ANS_LOGE("UnWarpNotificationMultiLineContent: get BasicContent failed");
807 return status;
808 }
809 ani_boolean isUndefined = ANI_TRUE;
810 std::string longTitle;
811 if ((status = GetPropertyString(env, obj, "longTitle", isUndefined, longTitle)) != ANI_OK
812 || isUndefined == ANI_TRUE) {
813 ANS_LOGE("UnWarpNotificationMultiLineContent: get longTitle failed");
814 return ANI_INVALID_ARGS;
815 }
816 multiLineContent->SetExpandedTitle(GetResizeStr(longTitle, SHORT_TEXT_SIZE));
817
818 std::string briefText;
819 isUndefined = ANI_TRUE;
820 if ((status = GetPropertyString(env, obj, "briefText", isUndefined, briefText)) != ANI_OK
821 || isUndefined == ANI_TRUE) {
822 ANS_LOGE("UnWarpNotificationMultiLineContent: get briefText failed");
823 return ANI_INVALID_ARGS;
824 }
825 multiLineContent->SetBriefText(GetResizeStr(briefText, SHORT_TEXT_SIZE));
826
827 std::vector<std::string> lines = {};
828 isUndefined = ANI_TRUE;
829 if ((status = GetPropertyStringArray(env, obj, "lines", isUndefined, lines)) != ANI_OK
830 || isUndefined == ANI_TRUE) {
831 ANS_LOGE("UnWarpNotificationMultiLineContent: get lines failed");
832 return ANI_INVALID_ARGS;
833 }
834 for (auto line : lines) {
835 multiLineContent->AddSingleLine(GetResizeStr(line, SHORT_TEXT_SIZE));
836 }
837 ANS_LOGD("UnWarpNotificationMultiLineContent end");
838 return status;
839 }
840
UnWarpNotificationPictureContent(ani_env * env,ani_object obj,std::shared_ptr<NotificationPictureContent> & pictureContent)841 ani_status UnWarpNotificationPictureContent(ani_env *env, ani_object obj,
842 std::shared_ptr<NotificationPictureContent> &pictureContent)
843 {
844 ANS_LOGD("UnWarpNotificationPictureContent call");
845 if (env == nullptr || obj == nullptr || pictureContent == nullptr) {
846 ANS_LOGE("UnWarpNotificationPictureContent failed, has nullptr");
847 return ANI_ERROR;
848 }
849 ani_status status = ANI_ERROR;
850 if ((status = UnWarpNotificationBasicContent(env, obj, pictureContent)) != ANI_OK) {
851 ANS_LOGE("UnWarpNotificationPictureContent: get BasicContent failed");
852 return status;
853 }
854 std::string expandedTitle;
855 ani_boolean isUndefined = ANI_TRUE;
856 if ((status = GetPropertyString(env, obj, "expandedTitle", isUndefined, expandedTitle)) != ANI_OK
857 || isUndefined == ANI_TRUE) {
858 ANS_LOGE("UnWarpNotificationPictureContent: get expandedTitle failed");
859 return ANI_INVALID_ARGS;
860 }
861 pictureContent->SetExpandedTitle(GetResizeStr(expandedTitle, SHORT_TEXT_SIZE));
862
863 std::string briefText;
864 if ((status = GetPropertyString(env, obj, "briefText", isUndefined, briefText)) != ANI_OK
865 || isUndefined == ANI_TRUE) {
866 ANS_LOGE("UnWarpNotificationPictureContent: get briefText failed");
867 return ANI_INVALID_ARGS;
868 }
869 pictureContent->SetBriefText(GetResizeStr(briefText, SHORT_TEXT_SIZE));
870 ani_ref pictureRef = {};
871 if ((status = GetPropertyRef(env, obj, "picture", isUndefined, pictureRef)) != ANI_OK
872 || isUndefined == ANI_TRUE || pictureRef == nullptr) {
873 ANS_LOGE("UnWarpNotificationPictureContent: get briefText failed");
874 return ANI_INVALID_ARGS;
875 }
876 std::shared_ptr<PixelMap> pixelMap = GetPixelMapFromAni(env, static_cast<ani_object>(pictureRef));
877 if (pixelMap == nullptr) {
878 ANS_LOGE("UnWarpNotificationPictureContent: get briefText by pixelMap failed");
879 return ANI_INVALID_ARGS;
880 }
881 pictureContent->SetBigPicture(pixelMap);
882 ANS_LOGD("UnWarpNotificationPictureContent end");
883 return status;
884 }
885
CheckAniLiveViewContentParam(ani_env * env,ani_object obj,std::shared_ptr<NotificationLiveViewContent> & liveViewContent)886 bool CheckAniLiveViewContentParam(
887 ani_env *env, ani_object obj, std::shared_ptr<NotificationLiveViewContent> &liveViewContent)
888 {
889 if (env == nullptr) {
890 ANS_LOGE("env is null");
891 return false;
892 }
893 if (obj == nullptr) {
894 ANS_LOGE("obj is null");
895 return false;
896 }
897 if (liveViewContent == nullptr) {
898 ANS_LOGE("liveViewContent is null");
899 return false;
900 }
901 return true;
902 }
903
GetAniLiveViewContentVersion(ani_env * env,ani_object obj,std::shared_ptr<NotificationLiveViewContent> & liveViewContent)904 void GetAniLiveViewContentVersion(
905 ani_env *env, ani_object obj, std::shared_ptr<NotificationLiveViewContent> &liveViewContent)
906 {
907 if (!CheckAniLiveViewContentParam(env, obj, liveViewContent)) {
908 ANS_LOGD("CheckAniLiveViewContentParam faild");
909 return;
910 }
911 ani_double versionAni = 0.0;
912 ani_boolean isUndefined = ANI_TRUE;
913 if (GetPropertyDouble(env, obj, "version", isUndefined, versionAni) != ANI_OK
914 || isUndefined == ANI_TRUE) {
915 ANS_LOGD("UnWarpNotificationLiveViewContent: get version failed");
916 return;
917 }
918 liveViewContent->SetVersion(static_cast<int32_t>(versionAni));
919 }
920
GetAniLiveViewContentExtraInfo(ani_env * env,ani_object obj,std::shared_ptr<NotificationLiveViewContent> & liveViewContent)921 void GetAniLiveViewContentExtraInfo(
922 ani_env *env, ani_object obj, std::shared_ptr<NotificationLiveViewContent> &liveViewContent)
923 {
924 if (!CheckAniLiveViewContentParam(env, obj, liveViewContent)) {
925 ANS_LOGD("CheckAniLiveViewContentParam faild");
926 return;
927 }
928 ani_status status = ANI_OK;
929 ani_ref extraInfoRef;
930 ani_boolean isUndefined = ANI_TRUE;
931 if (ANI_OK != (status = GetPropertyRef(env, obj, "extraInfo", isUndefined, extraInfoRef))
932 || isUndefined == ANI_TRUE || extraInfoRef == nullptr) {
933 ANS_LOGD("UnWarpNotificationLiveViewContent: get extraInfo failed. status %{public}d", status);
934 return;
935 }
936 AAFwk::WantParams wantParams = {};
937 if (!UnwrapWantParams(env, extraInfoRef, wantParams)) {
938 ANS_LOGD("UnWarpNotificationLiveViewContent: get extraInfo by ref failed");
939 return;
940 }
941 std::shared_ptr<AAFwk::WantParams> extraInfo = std::make_shared<WantParams>(wantParams);
942 liveViewContent->SetExtraInfo(extraInfo);
943 }
944
GetAniLiveViewContentPictureInfo(ani_env * env,ani_object obj,std::shared_ptr<NotificationLiveViewContent> & liveViewContent)945 void GetAniLiveViewContentPictureInfo(
946 ani_env *env, ani_object obj, std::shared_ptr<NotificationLiveViewContent> &liveViewContent)
947 {
948 if (!CheckAniLiveViewContentParam(env, obj, liveViewContent)) {
949 ANS_LOGD("CheckAniLiveViewContentParam faild");
950 return;
951 }
952 ani_ref pictureInfoRef;
953 ani_boolean isUndefined = ANI_TRUE;
954 if (ANI_OK != GetPropertyRef(env, obj, "pictureInfo", isUndefined, pictureInfoRef)
955 || isUndefined == ANI_TRUE || pictureInfoRef == nullptr) {
956 ANS_LOGD("UnWarpNotificationLiveViewContent: get pictureInfo failed");
957 return;
958 }
959 std::map<std::string, std::vector<std::shared_ptr<Media::PixelMap>>> pictureMap;
960 if (GetMapOfPictureInfo(env, static_cast<ani_object>(pictureInfoRef), pictureMap) != ANI_OK) {
961 ANS_LOGD("UnWarpNotificationLiveViewContent: get pictureInfo by ref failed");
962 return;
963 }
964 liveViewContent->SetPicture(pictureMap);
965 }
966
GetAniLiveViewContentIsLocalUpdateOnly(ani_env * env,ani_object obj,std::shared_ptr<NotificationLiveViewContent> & liveViewContent)967 void GetAniLiveViewContentIsLocalUpdateOnly(
968 ani_env *env, ani_object obj, std::shared_ptr<NotificationLiveViewContent> &liveViewContent)
969 {
970 if (!CheckAniLiveViewContentParam(env, obj, liveViewContent)) {
971 ANS_LOGD("CheckAniLiveViewContentParam faild");
972 return;
973 }
974 ani_status status = ANI_OK;
975 bool isLocalUpdateOnly = true;
976 ani_boolean isUndefined = ANI_TRUE;
977 if (ANI_OK != (status = GetPropertyBool(env, obj, "isLocalUpdateOnly", isUndefined, isLocalUpdateOnly))) {
978 ANS_LOGD("get 'isLocalUpdateOnly' faild. status %{public}d", status);
979 return;
980 }
981 if (isUndefined == ANI_TRUE) {
982 ANS_LOGD("'isLocalUpdateOnly' is Undefined");
983 return;
984 }
985 liveViewContent->SetIsOnlyLocalUpdate(isLocalUpdateOnly);
986 }
987
UnWarpNotificationLiveViewContentByOther(ani_env * env,ani_object obj,std::shared_ptr<NotificationLiveViewContent> & liveViewContent)988 void UnWarpNotificationLiveViewContentByOther(ani_env *env, ani_object obj,
989 std::shared_ptr<NotificationLiveViewContent> &liveViewContent)
990 {
991 ANS_LOGD("UnWarpNotificationLiveViewContentByOther call");
992 if (!CheckAniLiveViewContentParam(env, obj, liveViewContent)) {
993 ANS_LOGD("CheckAniLiveViewContentParam faild");
994 return;
995 }
996 GetAniLiveViewContentVersion(env, obj, liveViewContent);
997 GetAniLiveViewContentExtraInfo(env, obj, liveViewContent);
998 GetAniLiveViewContentPictureInfo(env, obj, liveViewContent);
999 GetAniLiveViewContentIsLocalUpdateOnly(env, obj, liveViewContent);
1000 ANS_LOGD("UnWarpNotificationLiveViewContentByOther end");
1001 }
1002
UnWarpNotificationLiveViewContent(ani_env * env,ani_object obj,std::shared_ptr<NotificationLiveViewContent> & liveViewContent)1003 ani_status UnWarpNotificationLiveViewContent(ani_env *env, ani_object obj,
1004 std::shared_ptr<NotificationLiveViewContent> &liveViewContent)
1005 {
1006 ANS_LOGD("UnWarpNotificationLiveViewContent call");
1007 if (env == nullptr || obj == nullptr || liveViewContent == nullptr) {
1008 ANS_LOGE("UnWarpNotificationLiveViewContent failed, has nullptr");
1009 return ANI_ERROR;
1010 }
1011 ani_status status = ANI_ERROR;
1012 if ((status = UnWarpNotificationBasicContent(env, obj, liveViewContent)) != ANI_OK) {
1013 ANS_LOGE("UnWarpNotificationLiveViewContent: get BasicContent failed");
1014 return status;
1015 }
1016 ani_boolean isUndefined = ANI_TRUE;
1017 ani_ref statusRef;
1018 if ((status = GetPropertyRef(env, obj, "status", isUndefined, statusRef)) != ANI_OK
1019 || isUndefined == ANI_TRUE || statusRef == nullptr) {
1020 ANS_LOGE("UnWarpNotificationLiveViewContent: get status failed");
1021 return ANI_INVALID_ARGS;
1022 }
1023 LiveViewStatus liveViewStatus = LiveViewStatus::LIVE_VIEW_CREATE;
1024 if (!LiveViewStatusEtsToC(env, static_cast<ani_enum_item>(statusRef), liveViewStatus)) {
1025 ANS_LOGE("UnWarpNotificationLiveViewContent: get status by ref failed");
1026 return ANI_INVALID_ARGS;
1027 }
1028 liveViewContent->SetLiveViewStatus(liveViewStatus);
1029 UnWarpNotificationLiveViewContentByOther(env, obj, liveViewContent);
1030 ANS_LOGD("UnWarpNotificationLiveViewContent end");
1031 return status;
1032 }
1033
GetLocalLiveViewContentByOne(ani_env * env,ani_object obj,std::shared_ptr<NotificationLocalLiveViewContent> & localLiveViewContent)1034 bool GetLocalLiveViewContentByOne(ani_env *env, ani_object obj,
1035 std::shared_ptr<NotificationLocalLiveViewContent> &localLiveViewContent)
1036 {
1037 ANS_LOGD("GetLocalLiveViewContentByOne call");
1038 if (env == nullptr || obj == nullptr || localLiveViewContent == nullptr) {
1039 ANS_LOGE("GetLocalLiveViewContentByOne failed, has nullptr");
1040 return false;
1041 }
1042 ani_boolean isUndefined = ANI_TRUE;
1043 ani_ref capsuleRef = {};
1044 if (GetPropertyRef(env, obj, "capsule", isUndefined, capsuleRef) == ANI_OK
1045 && isUndefined == ANI_FALSE && capsuleRef != nullptr) {
1046 NotificationCapsule capsule;
1047 if (!UnWarpNotificationCapsule(env, static_cast<ani_object>(capsuleRef), capsule)) {
1048 return false;
1049 }
1050 localLiveViewContent->SetCapsule(capsule);
1051 localLiveViewContent->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE);
1052 }
1053 ani_ref buttonRef = {};
1054 if (GetPropertyRef(env, obj, "button", isUndefined, buttonRef) == ANI_OK
1055 && isUndefined == ANI_FALSE && buttonRef != nullptr) {
1056 NotificationLocalLiveViewButton button;
1057 UnWarpNotificationLocalLiveViewButton(env, static_cast<ani_object>(buttonRef), button);
1058 localLiveViewContent->SetButton(button);
1059 localLiveViewContent->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON);
1060 } else {
1061 ANS_LOGD("GetLocalLiveViewContentByOne: get button failed");
1062 }
1063 std::vector<NotificationIconButton> buttons = {};
1064 if (GetIconButtonArray(env, obj, "cardButtons", buttons) == ANI_OK) {
1065 localLiveViewContent->SetCardButton(buttons);
1066 localLiveViewContent->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::CARD_BUTTON);
1067 } else {
1068 ANS_LOGD("GetLocalLiveViewContentByOne: get cardButtons failed");
1069 }
1070 ANS_LOGD("GetLocalLiveViewContentByOne end");
1071 return true;
1072 }
1073
GetLocalLiveViewContentByTwo(ani_env * env,ani_object obj,std::shared_ptr<NotificationLocalLiveViewContent> & localLiveViewContent)1074 void GetLocalLiveViewContentByTwo(ani_env *env, ani_object obj,
1075 std::shared_ptr<NotificationLocalLiveViewContent> &localLiveViewContent)
1076 {
1077 ANS_LOGD("GetLocalLiveViewContentByTwo call");
1078 if (env == nullptr || obj == nullptr || localLiveViewContent == nullptr) {
1079 ANS_LOGE("GetLocalLiveViewContentByTwo failed, has nullptr");
1080 return;
1081 }
1082 ani_boolean isUndefined = ANI_TRUE;
1083 ani_ref timeRef = {};
1084 if (GetPropertyRef(env, obj, "time", isUndefined, timeRef) == ANI_OK
1085 && isUndefined == ANI_FALSE && timeRef != nullptr) {
1086 NotificationTime notificationTime;
1087 UnWarpNotificationTime(env, static_cast<ani_object>(timeRef), notificationTime);
1088 localLiveViewContent->SetTime(notificationTime);
1089 localLiveViewContent->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::TIME);
1090 } else {
1091 ANS_LOGD("GetLocalLiveViewContentByTwo: get time failed");
1092 }
1093 ani_ref progressRef = {};
1094 if (GetPropertyRef(env, obj, "progress", isUndefined, progressRef) == ANI_OK
1095 && isUndefined == ANI_FALSE && progressRef != nullptr) {
1096 NotificationProgress notificationProgress;
1097 UnWarpNotificationProgress(env, static_cast<ani_object>(progressRef), notificationProgress);
1098 localLiveViewContent->SetProgress(notificationProgress);
1099 localLiveViewContent->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::PROGRESS);
1100 } else {
1101 ANS_LOGD("GetLocalLiveViewContentByTwo: get progress failed");
1102 }
1103 ani_ref liveViewTypeRef = {};
1104 if (GetPropertyRef(env, obj, "liveViewType", isUndefined, liveViewTypeRef) == ANI_OK
1105 && isUndefined == ANI_FALSE && liveViewTypeRef != nullptr) {
1106 LiveViewTypes types = LiveViewTypes::LIVE_VIEW_ACTIVITY;
1107 if (LiveViewTypesEtsToC(env, static_cast<ani_enum_item>(liveViewTypeRef), types)) {
1108 localLiveViewContent->SetLiveViewType(types);
1109 } else {
1110 ANS_LOGD("GetLocalLiveViewContentByTwo: get liveViewType by ref failed");
1111 }
1112 } else {
1113 ANS_LOGD("GetLocalLiveViewContentByTwo: get liveViewType failed");
1114 }
1115 ANS_LOGD("GetLocalLiveViewContentByTwo end");
1116 }
1117
UnWarpNotificationLocalLiveViewContent(ani_env * env,ani_object obj,std::shared_ptr<NotificationLocalLiveViewContent> & localLiveViewContent)1118 ani_status UnWarpNotificationLocalLiveViewContent(ani_env *env, ani_object obj,
1119 std::shared_ptr<NotificationLocalLiveViewContent> &localLiveViewContent)
1120 {
1121 ANS_LOGD("UnWarpNotificationLocalLiveViewContent call");
1122 if (env == nullptr || obj == nullptr || localLiveViewContent == nullptr) {
1123 ANS_LOGE("UnWarpNotificationLocalLiveViewContent failed, has nullptr");
1124 return ANI_ERROR;
1125 }
1126 ani_status status = ANI_ERROR;
1127 if ((status = UnWarpNotificationBasicContent(env, obj, localLiveViewContent)) != ANI_OK) {
1128 ANS_LOGE("UnWarpNotificationLocalLiveViewContent: get BasicContent failed");
1129 return status;
1130 }
1131 ani_double typeCode = 0.0;
1132 ani_boolean isUndefined = ANI_TRUE;
1133 if ((status = GetPropertyDouble(env, obj, "typeCode", isUndefined, typeCode)) != ANI_OK
1134 || isUndefined == ANI_TRUE) {
1135 ANS_LOGE("UnWarpNotificationLocalLiveViewContent: get typeCode failed");
1136 return ANI_INVALID_ARGS;
1137 }
1138 localLiveViewContent->SetType(static_cast<int32_t>(typeCode));
1139 if (!GetLocalLiveViewContentByOne(env, obj, localLiveViewContent)) {
1140 return ANI_INVALID_ARGS;
1141 }
1142 GetLocalLiveViewContentByTwo(env, obj, localLiveViewContent);
1143 ANS_LOGD("UnWarpNotificationLocalLiveViewContent end");
1144 return status;
1145 }
1146
SetNotificationBasicContent(ani_env * env,const NotificationBasicContent * basicContent,ani_object & object)1147 bool SetNotificationBasicContent(
1148 ani_env* env, const NotificationBasicContent *basicContent, ani_object &object)
1149 {
1150 ANS_LOGD("SetNotificationBasicContent call");
1151 if (env == nullptr || basicContent == nullptr || object == nullptr) {
1152 ANS_LOGE("SetNotificationBasicContent failed, has nullptr");
1153 return false;
1154 }
1155 if (!SetPropertyOptionalByString(env, object, "title", basicContent->GetTitle())) {
1156 ANS_LOGE("SetNotificationBasicContent: set title failed");
1157 return false;
1158 }
1159 if (!SetPropertyOptionalByString(env, object, "text", basicContent->GetText())) {
1160 ANS_LOGE("SetNotificationBasicContent: set text failed");
1161 return false;
1162 }
1163 if (!SetPropertyOptionalByString(env, object, "additionalText", basicContent->GetAdditionalText())) {
1164 ANS_LOGD("SetNotificationBasicContent: set additionalText failed");
1165 }
1166 ani_ref lockScreenPicObj = CreateAniPixelMap(env, basicContent->GetLockScreenPicture());
1167 if (lockScreenPicObj == nullptr || !SetPropertyByRef(env, object, "lockScreenPicture", lockScreenPicObj)) {
1168 ANS_LOGD("SetNotificationBasicContent: set lockScreenPicture failed");
1169 }
1170 ANS_LOGD("SetNotificationBasicContent end");
1171 return true;
1172 }
1173
SetNotificationNormalContent(ani_env * env,std::shared_ptr<NotificationContent> nContent,ani_object & ncObj)1174 bool SetNotificationNormalContent(
1175 ani_env* env, std::shared_ptr<NotificationContent> nContent, ani_object &ncObj)
1176 {
1177 ANS_LOGD("SetNotificationNormalContent call");
1178 if (env == nullptr || nContent == nullptr || ncObj == nullptr) {
1179 ANS_LOGE("SetNotificationNormalContent failed, has nullptr");
1180 return false;
1181 }
1182 ani_class contentCls;
1183 ani_object contentObj;
1184 if (!CreateClassObjByClassName(env,
1185 "Lnotification/notificationContent/NotificationBasicContentInner;", contentCls, contentObj)
1186 || contentCls == nullptr || contentObj == nullptr) {
1187 ANS_LOGE("SetNotificationNormalContent: create class failed");
1188 return false;
1189 }
1190 std::shared_ptr<NotificationBasicContent> basicContent = nContent->GetNotificationContent();
1191 if (basicContent == nullptr) {
1192 ANS_LOGE("SetNotificationNormalContent: get basicContent failed");
1193 return false;
1194 }
1195 if (!SetNotificationBasicContent(env, basicContent.get(), contentObj)) {
1196 ANS_LOGE("SetNotificationNormalContent: set basicContent failed");
1197 return false;
1198 }
1199 if (!SetPropertyByRef(env, ncObj, "normal", contentObj)) {
1200 ANS_LOGE("SetNotificationNormalContent: set normal to ncObj failed");
1201 return false;
1202 }
1203 return true;
1204 }
1205
SetNotificationLongTextContent(ani_env * env,std::shared_ptr<NotificationContent> nContent,ani_object & ncObj)1206 bool SetNotificationLongTextContent(
1207 ani_env* env, std::shared_ptr<NotificationContent> nContent, ani_object &ncObj)
1208 {
1209 ANS_LOGD("SetNotificationLongTextContent call");
1210 if (env == nullptr || nContent == nullptr || ncObj == nullptr) {
1211 ANS_LOGE("SetNotificationLongTextContent failed, has nullptr");
1212 return false;
1213 }
1214 ani_class contentCls;
1215 ani_object contentObj;
1216 if (!CreateClassObjByClassName(env,
1217 "Lnotification/notificationContent/NotificationLongTextContentInner;", contentCls, contentObj)
1218 || contentObj == nullptr) {
1219 ANS_LOGE("SetNotificationLongTextContent: create class failed");
1220 return false;
1221 }
1222 auto content = std::reinterpret_pointer_cast<NotificationLongTextContent>(nContent->GetNotificationContent());
1223 if (content == nullptr) {
1224 ANS_LOGE("SetNotificationLongTextContent: get LongTextContent failed");
1225 return false;
1226 }
1227 if (!SetNotificationBasicContent(env, content.get(), contentObj)) {
1228 ANS_LOGE("SetNotificationLongTextContent: set BasicContent failed");
1229 return false;
1230 }
1231 if (!SetPropertyOptionalByString(env, contentObj, "longText", content->GetLongText())) {
1232 ANS_LOGE("SetNotificationLongTextContent: set longText failed");
1233 return false;
1234 }
1235 if (!SetPropertyOptionalByString(env, contentObj, "briefText", content->GetBriefText())) {
1236 ANS_LOGE("SetNotificationLongTextContent: set briefText failed");
1237 return false;
1238 }
1239 if (!SetPropertyOptionalByString(env, contentObj, "expandedTitle", content->GetExpandedTitle())) {
1240 ANS_LOGE("SetNotificationLongTextContent: set expandedTitle failed");
1241 return false;
1242 }
1243 return SetPropertyByRef(env, ncObj, "longText", contentObj);
1244 }
1245
SetNotificationPictureContent(ani_env * env,std::shared_ptr<NotificationContent> nContent,ani_object & ncObj)1246 bool SetNotificationPictureContent(
1247 ani_env* env, std::shared_ptr<NotificationContent> nContent, ani_object &ncObj)
1248 {
1249 ANS_LOGD("SetNotificationPictureContent call");
1250 if (env == nullptr || nContent == nullptr || ncObj == nullptr) {
1251 ANS_LOGE("SetNotificationPictureContent failed, has nullptr");
1252 return false;
1253 }
1254 ani_class contentCls;
1255 ani_object contentObj;
1256 if (!CreateClassObjByClassName(env,
1257 "Lnotification/notificationContent/NotificationPictureContentInner;", contentCls, contentObj)
1258 || contentObj == nullptr) {
1259 ANS_LOGE("SetNotificationPictureContent: create class failed");
1260 return false;
1261 }
1262 auto content = std::reinterpret_pointer_cast<NotificationPictureContent>(nContent->GetNotificationContent());
1263 if (content == nullptr) {
1264 ANS_LOGE("SetNotificationPictureContent: get PictureContent failed");
1265 return false;
1266 }
1267 if (!SetNotificationBasicContent(env, content.get(), contentObj)) {
1268 ANS_LOGE("SetNotificationPictureContent: set BasicContent failed");
1269 return false;
1270 }
1271 if (!SetPropertyOptionalByString(env, contentObj, "briefText", content->GetBriefText())) {
1272 ANS_LOGD("SetNotificationPictureContent: set briefText failed");
1273 }
1274 if (!SetPropertyOptionalByString(env, contentObj, "expandedTitle", content->GetExpandedTitle())) {
1275 ANS_LOGD("SetNotificationPictureContent: set expandedTitle failed");
1276 }
1277 ani_object pictureObj = CreateAniPixelMap(env, content->GetBigPicture());
1278 if (pictureObj == nullptr || !SetPropertyByRef(env, contentObj, "picture", pictureObj)) {
1279 ANS_LOGD("SetNotificationPictureContent: set picture failed");
1280 }
1281 return SetPropertyByRef(env, ncObj, "picture", contentObj);
1282 }
1283
SetNotificationMultiLineContent(ani_env * env,std::shared_ptr<NotificationContent> nContent,ani_object & ncObj)1284 bool SetNotificationMultiLineContent(
1285 ani_env* env, std::shared_ptr<NotificationContent> nContent, ani_object &ncObj)
1286 {
1287 ANS_LOGD("SetNotificationMultiLineContent call");
1288 if (env == nullptr || nContent == nullptr || ncObj == nullptr) {
1289 ANS_LOGE("SetNotificationMultiLineContent failed, has nullptr");
1290 return false;
1291 }
1292 ani_class contentCls;
1293 ani_object contentObj;
1294 if (!CreateClassObjByClassName(env,
1295 "Lnotification/notificationContent/NotificationMultiLineContentInner;", contentCls, contentObj)
1296 || contentObj == nullptr) {
1297 ANS_LOGE("SetNotificationMultiLineContent: create class failed");
1298 return false;
1299 }
1300 auto content = std::reinterpret_pointer_cast<NotificationMultiLineContent>(nContent->GetNotificationContent());
1301 if (content == nullptr) {
1302 ANS_LOGE("SetNotificationMultiLineContent: get MultiLineContent failed");
1303 return false;
1304 }
1305 if (!SetNotificationBasicContent(env, content.get(), contentObj)) {
1306 ANS_LOGE("SetNotificationMultiLineContent: set BasicContent failed");
1307 return false;
1308 }
1309 if (!SetPropertyOptionalByString(env, contentObj, "briefText", content->GetBriefText())) {
1310 ANS_LOGD("SetNotificationMultiLineContent: set briefText failed");
1311 }
1312 if (!SetPropertyOptionalByString(env, contentObj, "longTitle", content->GetExpandedTitle())) {
1313 ANS_LOGD("SetNotificationMultiLineContent: set briefText failed");
1314 }
1315 std::vector<std::string> allLines = content->GetAllLines();
1316 ani_object allLinesObject = GetAniStringArrayByVectorString(env, allLines);
1317 if (allLinesObject == nullptr || !SetPropertyByRef(env, contentObj, "lines", allLinesObject)) {
1318 ANS_LOGD("SetNotificationMultiLineContent: set lines failed");
1319 }
1320 return SetPropertyByRef(env, ncObj, "multiLine", contentObj);
1321 }
1322
SetCapsule(ani_env * env,std::shared_ptr<NotificationLocalLiveViewContent> & content,ani_object & contentObj)1323 void SetCapsule(ani_env *env, std::shared_ptr<NotificationLocalLiveViewContent> &content, ani_object &contentObj)
1324 {
1325 if (content->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE)) {
1326 ani_object capsuleObject = nullptr;
1327 if (!WarpNotificationCapsule(env, content->GetCapsule(), capsuleObject)
1328 || capsuleObject == nullptr || !SetPropertyByRef(env, contentObj, "capsule", capsuleObject)) {
1329 ANS_LOGD("SetNotificationMultiLineContent: set capsule failed");
1330 }
1331 }
1332 }
1333
SetButton(ani_env * env,std::shared_ptr<NotificationLocalLiveViewContent> & content,ani_object & contentObj)1334 void SetButton(ani_env *env, std::shared_ptr<NotificationLocalLiveViewContent> &content, ani_object &contentObj)
1335 {
1336 if (content->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON)) {
1337 ani_object buttonObject = nullptr;
1338 if (!WarpNotificationLocalLiveViewButton(env, content->GetButton(), buttonObject)
1339 || buttonObject == nullptr || !SetPropertyByRef(env, contentObj, "button", buttonObject)) {
1340 ANS_LOGD("SetNotificationMultiLineContent: set button failed");
1341 }
1342 }
1343 }
1344
SetCardButtons(ani_env * env,std::shared_ptr<NotificationLocalLiveViewContent> & content,ani_object & contentObj)1345 void SetCardButtons(ani_env *env, std::shared_ptr<NotificationLocalLiveViewContent> &content, ani_object &contentObj)
1346 {
1347 if (content->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::CARD_BUTTON)) {
1348 std::vector<NotificationIconButton> buttons = content->GetCardButton();
1349 ani_object buttonsObjectArray = GetAniIconButtonArray(env, buttons);
1350 if (buttonsObjectArray == nullptr || !SetPropertyByRef(env, contentObj, "cardButtons", buttonsObjectArray)) {
1351 ANS_LOGD("SetNotificationMultiLineContent: set cardButtons failed");
1352 }
1353 }
1354 }
1355
SetProgress(ani_env * env,std::shared_ptr<NotificationLocalLiveViewContent> & content,ani_object & contentObj)1356 void SetProgress(ani_env *env, std::shared_ptr<NotificationLocalLiveViewContent> &content, ani_object &contentObj)
1357 {
1358 if (content->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::PROGRESS)) {
1359 ani_object progressObject = nullptr;
1360 if (!WarpNotificationProgress(env, content->GetProgress(), progressObject)
1361 || progressObject == nullptr || !SetPropertyByRef(env, contentObj, "progress", progressObject)) {
1362 ANS_LOGD("SetNotificationMultiLineContent: set progress failed");
1363 }
1364 }
1365 }
1366
SetTime(ani_env * env,std::shared_ptr<NotificationLocalLiveViewContent> & content,ani_object & contentObj)1367 void SetTime(ani_env *env, std::shared_ptr<NotificationLocalLiveViewContent> &content, ani_object &contentObj)
1368 {
1369 if (content->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::TIME)) {
1370 bool flag = content->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::INITIAL_TIME);
1371 ani_object timeObject = nullptr;
1372 if (!WarpNotificationTime(env, content->GetTime(), flag, timeObject)
1373 || timeObject == nullptr || !SetPropertyByRef(env, contentObj, "time", timeObject)) {
1374 ANS_LOGD("SetNotificationMultiLineContent: set time failed");
1375 }
1376 }
1377 }
1378
WarpLocalLiveViewContentWithFalg(ani_env * env,std::shared_ptr<NotificationContent> nContent,ani_object & contentObj)1379 bool WarpLocalLiveViewContentWithFalg(
1380 ani_env* env, std::shared_ptr<NotificationContent> nContent, ani_object &contentObj)
1381 {
1382 auto content = std::reinterpret_pointer_cast<NotificationLocalLiveViewContent>(nContent->GetNotificationContent());
1383 if (content == nullptr) {
1384 ANS_LOGE("SetNotificationMultiLineContent: get LocalLiveViewContent failed");
1385 return false;
1386 }
1387 SetCapsule(env, content, contentObj);
1388 SetButton(env, content, contentObj);
1389 SetCardButtons(env, content, contentObj);
1390 SetProgress(env, content, contentObj);
1391 SetTime(env, content, contentObj);
1392 return true;
1393 }
1394
1395
WarpNotificationLocalLiveViewContent(ani_env * env,std::shared_ptr<NotificationContent> nContent,ani_object & contentObj)1396 bool WarpNotificationLocalLiveViewContent(
1397 ani_env* env, std::shared_ptr<NotificationContent> nContent, ani_object &contentObj)
1398 {
1399 auto content = std::reinterpret_pointer_cast<NotificationLocalLiveViewContent>(nContent->GetNotificationContent());
1400 if (content == nullptr) {
1401 ANS_LOGE("SetNotificationMultiLineContent: get LocalLiveViewContent failed");
1402 return false;
1403 }
1404 if (!SetNotificationBasicContent(env, content.get(), contentObj)) {
1405 ANS_LOGE("SetNotificationMultiLineContent: set BasicContent failed");
1406 return false;
1407 }
1408 if (!SetPropertyOptionalByInt(env, contentObj, "typeCode", content->GetType())) {
1409 ANS_LOGD("SetNotificationMultiLineContent: set typeCode failed");
1410 }
1411 ani_enum_item enumItem = nullptr;
1412 if (!LiveViewTypesCToEts(env, content->GetLiveViewType(), enumItem)
1413 || enumItem == nullptr || !SetPropertyByRef(env, contentObj, "liveViewType", enumItem)) {
1414 ANS_LOGD("SetNotificationMultiLineContent: set liveViewType failed");
1415 }
1416 if (!WarpLocalLiveViewContentWithFalg(env, nContent, contentObj)) {
1417 ANS_LOGE("WarpLocalLiveViewContentWithFalg faild");
1418 return false;
1419 }
1420 return true;
1421 }
1422
SetNotificationLocalLiveViewContent(ani_env * env,std::shared_ptr<NotificationContent> nContent,ani_object & ncObj)1423 bool SetNotificationLocalLiveViewContent(
1424 ani_env* env, std::shared_ptr<NotificationContent> nContent, ani_object &ncObj)
1425 {
1426 ANS_LOGD("SetNotificationMultiLineContent call");
1427 if (env == nullptr || nContent == nullptr || ncObj == nullptr) {
1428 ANS_LOGE("SetNotificationMultiLineContent failed, has nullptr");
1429 return false;
1430 }
1431 ani_class contentCls;
1432 ani_object contentObj;
1433 if (!CreateClassObjByClassName(env,
1434 "Lnotification/notificationContent/NotificationSystemLiveViewContentInner;", contentCls, contentObj)
1435 || contentObj == nullptr) {
1436 ANS_LOGE("SetNotificationMultiLineContent: create class failed");
1437 return false;
1438 }
1439 if (!WarpNotificationLocalLiveViewContent(env, nContent, contentObj)) {
1440 ANS_LOGE("WarpNotificationLocalLiveViewContent faild");
1441 return false;
1442 }
1443 return SetPropertyByRef(env, ncObj, "systemLiveView", contentObj);
1444 }
1445
WarpLiveViewContentBasicContent(ani_env * env,std::shared_ptr<NotificationContent> nContent,ani_object & contentObj)1446 bool WarpLiveViewContentBasicContent(
1447 ani_env *env, std::shared_ptr<NotificationContent> nContent, ani_object &contentObj)
1448 {
1449 auto content = std::reinterpret_pointer_cast<NotificationLiveViewContent>(nContent->GetNotificationContent());
1450 if (content == nullptr) {
1451 ANS_LOGE("SetNotificationLiveViewContent: get LiveViewContent failed");
1452 return false;
1453 }
1454 if (!SetNotificationBasicContent(env, content.get(), contentObj)) {
1455 ANS_LOGE("SetNotificationLiveViewContent: set BasicContent failed");
1456 return false;
1457 }
1458 return true;
1459 }
1460
WarpNotificationLiveViewContent(ani_env * env,std::shared_ptr<NotificationContent> nContent,ani_object & contentObj)1461 bool WarpNotificationLiveViewContent(
1462 ani_env *env, std::shared_ptr<NotificationContent> nContent, ani_object &contentObj)
1463 {
1464 if (!WarpLiveViewContentBasicContent(env, nContent, contentObj)) {
1465 ANS_LOGE("WarpLiveViewContentBasicContent faild");
1466 return false;
1467 }
1468 auto content = std::reinterpret_pointer_cast<NotificationLiveViewContent>(nContent->GetNotificationContent());
1469 if (content == nullptr) {
1470 ANS_LOGE("SetNotificationLiveViewContent: get LiveViewContent failed");
1471 return false;
1472 }
1473 ani_object lockScreenPicObj = CreateAniPixelMap(env, content->GetLockScreenPicture());
1474 if (lockScreenPicObj == nullptr || !SetPropertyByRef(env, contentObj, "lockScreenPicture", lockScreenPicObj)) {
1475 ANS_LOGD("SetNotificationLiveViewContent: set lockScreenPicture failed");
1476 }
1477 ani_enum_item enumItem = nullptr;
1478 if (!LiveViewStatusCToEts(env, content->GetLiveViewStatus(), enumItem)
1479 || enumItem == nullptr || !SetPropertyByRef(env, contentObj, "status", enumItem)) {
1480 ANS_LOGD("SetNotificationLiveViewContent: set status failed");
1481 }
1482 if (!SetPropertyOptionalByInt(env, contentObj, "version", static_cast<int32_t>(content->GetVersion()))) {
1483 ANS_LOGD("SetNotificationLiveViewContent: set version failed");
1484 }
1485 std::shared_ptr<AAFwk::WantParams> extraInfoData = content->GetExtraInfo();
1486 if (extraInfoData == nullptr) {
1487 ANS_LOGD("SetNotificationLiveViewContent: set extraInfo failed");
1488 } else {
1489 ani_ref extraInfoObj = WrapWantParams(env, *extraInfoData);
1490 if (extraInfoObj == nullptr || !SetPropertyByRef(env, contentObj, "extraInfo", extraInfoObj)) {
1491 ANS_LOGD("SetNotificationLiveViewContent: set extraInfo by ref failed");
1492 }
1493 }
1494 ani_object pictureInfoObj = nullptr;
1495 if (!GetAniPictrueInfo(env, content->GetPicture(), pictureInfoObj)
1496 || pictureInfoObj == nullptr || SetPropertyByRef(env, contentObj, "pictureInfo", pictureInfoObj)) {
1497 ANS_LOGD("SetNotificationLiveViewContent: set pictureInfo failed");
1498 }
1499 return true;
1500 }
1501
SetNotificationLiveViewContent(ani_env * env,std::shared_ptr<NotificationContent> nContent,ani_object & ncObj)1502 bool SetNotificationLiveViewContent(
1503 ani_env* env, std::shared_ptr<NotificationContent> nContent, ani_object &ncObj)
1504 {
1505 ANS_LOGD("SetNotificationLiveViewContent call");
1506 if (env == nullptr || nContent == nullptr || ncObj == nullptr) {
1507 ANS_LOGE("SetNotificationLiveViewContent failed, has nullptr");
1508 return false;
1509 }
1510 ani_class contentCls;
1511 ani_object contentObj;
1512 if (!CreateClassObjByClassName(env, "Lnotification/notificationContent/NotificationLiveViewContentInner;",
1513 contentCls, contentObj) || contentObj == nullptr) {
1514 ANS_LOGE("SetNotificationLiveViewContent: create class failed");
1515 return false;
1516 }
1517 if (!WarpNotificationLiveViewContent(env, nContent, contentObj)) {
1518 ANS_LOGE("WarpNotificationLiveViewContent faild");
1519 return false;
1520 }
1521 return SetPropertyByRef(env, ncObj, "liveView", contentObj);
1522 }
1523
SetNotificationContent(ani_env * env,std::shared_ptr<NotificationContent> ncContent,ani_object & ncObj)1524 bool SetNotificationContent(ani_env* env, std::shared_ptr<NotificationContent> ncContent, ani_object &ncObj)
1525 {
1526 ANS_LOGD("SetNotificationContent call");
1527 if (env == nullptr || ncContent == nullptr) {
1528 ANS_LOGE("SetNotificationContent failed, has nullptr");
1529 return false;
1530 }
1531 ani_class ncCls;
1532 if (!CreateClassObjByClassName(env, "Lnotification/notificationContent/NotificationContentInner;",
1533 ncCls, ncObj) || ncObj == nullptr) {
1534 ANS_LOGE("SetNotificationContent: create class failed");
1535 return false;
1536 }
1537 ContentType contentType = ncContent->GetContentType();
1538 ani_enum_item contentTypeItem {};
1539 if (!ContentTypeCToEts(env, contentType, contentTypeItem)
1540 || !SetPropertyByRef(env, ncObj, "notificationContentType", contentTypeItem)) {
1541 ANS_LOGE("SetNotificationContent: set notificationContentType failed");
1542 return false;
1543 }
1544 bool result = true;
1545 ANS_LOGD("SetNotificationContent: contentType = %{public}d", static_cast<int>(contentType));
1546 switch (contentType) {
1547 case ContentType::BASIC_TEXT: // normal?: NotificationBasicContent
1548 result = SetNotificationNormalContent(env, ncContent, ncObj);
1549 break;
1550 case ContentType::LONG_TEXT: // longText?: NotificationLongTextContent
1551 result = SetNotificationLongTextContent(env, ncContent, ncObj);
1552 break;
1553 case ContentType::PICTURE: // picture?: NotificationPictureContent
1554 result = SetNotificationPictureContent(env, ncContent, ncObj);
1555 break;
1556 case ContentType::MULTILINE: // multiLine?: NotificationMultiLineContent
1557 result = SetNotificationMultiLineContent(env, ncContent, ncObj);
1558 break;
1559 case ContentType::LOCAL_LIVE_VIEW: // systemLiveView?: NotificationLocalLiveViewContent
1560 result = SetNotificationLocalLiveViewContent(env, ncContent, ncObj);
1561 break;
1562 case ContentType::LIVE_VIEW: // liveView?: NotificationLiveViewContent
1563 result = SetNotificationLiveViewContent(env, ncContent, ncObj);
1564 break;
1565 default:
1566 result = false;
1567 break;
1568 }
1569 if (!result) {
1570 ANS_LOGE("SetNotificationContent failed");
1571 }
1572 ANS_LOGD("SetNotificationContent end");
1573 return result;
1574 }
1575 } // namespace NotificationSts
1576 } // OHOS
1577