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_request.h"
16
17 #include "sts_common.h"
18 #include "sts_convert_other.h"
19 #include "sts_notification_flag.h"
20 #include "sts_notification_manager.h"
21 #include "sts_notification_content.h"
22 #include "sts_action_button.h"
23 #include "sts_bundle_option.h"
24 #include "sts_template.h"
25 #include "ans_log_wrapper.h"
26 #include "want_params.h"
27 #include "ani_common_want.h"
28 #include "sts_bundle_option.h"
29 #include "sts_subscribe.h"
30
31 namespace OHOS {
32 namespace NotificationSts {
33 using namespace OHOS::AAFwk;
34 using namespace OHOS::AppExecFwk;
35
UnWarpDistributedOptions(ani_env * env,ani_object obj,StsDistributedOptions distributedOptions)36 void UnWarpDistributedOptions(ani_env *env, ani_object obj, StsDistributedOptions distributedOptions)
37 {
38 ANS_LOGD("UnWarpDistributedOptions start");
39 if (env == nullptr || obj == nullptr) {
40 ANS_LOGE("UnWarpDistributedOptions failed, has nullptr");
41 return;
42 }
43 // isDistributed?: boolean;
44 bool isDistributed = false;
45 ani_boolean isUndefined = ANI_TRUE;
46 if (ANI_OK == GetPropertyBool(env, obj, "isDistributed", isUndefined, isDistributed)
47 && isUndefined == ANI_FALSE) {
48 distributedOptions.isDistributed = isDistributed;
49 } else {
50 ANS_LOGD("UnWarpDistributedOptions: isDistributed get failed");
51 }
52 // supportDisplayDevices?: Array<string>;
53 std::vector<std::string> tempStrings = {};
54 isUndefined = ANI_TRUE;
55 if (GetPropertyStringArray(env, obj, "supportDisplayDevices", isUndefined, tempStrings) == ANI_OK
56 && isUndefined == ANI_FALSE) {
57 std::vector<std::string> supportDisplayDevices = {};
58 for (auto device: tempStrings) {
59 supportDisplayDevices.emplace_back(GetResizeStr(device, STR_MAX_SIZE));
60 }
61 distributedOptions.supportDisplayDevices = supportDisplayDevices;
62 } else {
63 ANS_LOGD("UnWarpDistributedOptions: supportDisplayDevices get failed");
64 }
65 // supportOperateDevices?: Array<string>;
66 tempStrings.clear();
67 isUndefined = ANI_TRUE;
68 if (GetPropertyStringArray(env, obj, "supportOperateDevices", isUndefined, tempStrings) == ANI_OK
69 && isUndefined == ANI_FALSE) {
70 std::vector<std::string> supportOperateDevices = {};
71 for (auto device: tempStrings) {
72 supportOperateDevices.emplace_back(GetResizeStr(device, STR_MAX_SIZE));
73 }
74 distributedOptions.supportOperateDevices = supportOperateDevices;
75 } else {
76 ANS_LOGD("UnWarpDistributedOptions: supportOperateDevices get failed");
77 }
78 // readonly remindType?: number;
79 ani_double remindType = 0.0;
80 isUndefined = ANI_TRUE;
81 if (ANI_OK == GetPropertyDouble(env, obj, "remindType", isUndefined, remindType)
82 && isUndefined == ANI_FALSE) {
83 distributedOptions.remindType = static_cast<int32_t>(remindType);
84 } else {
85 ANS_LOGD("UnWarpDistributedOptions: remindType get failed");
86 }
87 ANS_LOGD("UnWarpDistributedOptions end");
88 }
89
WarpNotificationUnifiedGroupInfo(ani_env * env,const std::shared_ptr<NotificationUnifiedGroupInfo> & groupInfo,ani_object & groupInfoObject)90 bool WarpNotificationUnifiedGroupInfo(ani_env* env,
91 const std::shared_ptr<NotificationUnifiedGroupInfo> &groupInfo, ani_object &groupInfoObject)
92 {
93 ANS_LOGD("WarpNotificationUnifiedGroupInfo start");
94 if (env == nullptr || groupInfo == nullptr) {
95 ANS_LOGE("WarpNotificationUnifiedGroupInfo failed, has nullptr");
96 return false;
97 }
98 ani_class groupInfoCls = nullptr;
99 if ((!CreateClassObjByClassName(env,
100 "Lnotification/notificationRequest/UnifiedGroupInfoInner;", groupInfoCls, groupInfoObject))
101 || groupInfoCls == nullptr || groupInfoObject == nullptr) {
102 ANS_LOGE("WarpNotificationUnifiedGroupInfo: create class failed");
103 return false;
104 }
105 // key?: string;
106 if (!groupInfo->GetKey().empty()
107 && !SetPropertyOptionalByString(env, groupInfoObject, "key", groupInfo->GetKey())) {
108 ANS_LOGE("WarpNotificationUnifiedGroupInfo: set key failed");
109 return false;
110 }
111 // title?: string;
112 if (!groupInfo->GetTitle().empty()
113 && !SetPropertyOptionalByString(env, groupInfoObject, "title", groupInfo->GetTitle())) {
114 ANS_LOGE("WarpNotificationUnifiedGroupInfo: set title failed");
115 return false;
116 }
117 // content?: string;
118 if (!groupInfo->GetContent().empty()
119 && !SetPropertyOptionalByString(env, groupInfoObject, "content", groupInfo->GetContent())) {
120 ANS_LOGE("WarpNotificationUnifiedGroupInfo: set content failed");
121 return false;
122 }
123 // sceneName?: string;
124 if (!groupInfo->GetSceneName().empty()
125 && !SetPropertyOptionalByString(env, groupInfoObject, "sceneName", groupInfo->GetSceneName())) {
126 ANS_LOGE("WarpNotificationUnifiedGroupInfo: set sceneName failed");
127 return false;
128 }
129 // extraInfo?: Record<string, Object>;
130 std::shared_ptr<AAFwk::WantParams> extraInfo = groupInfo->GetExtraInfo();
131 if (extraInfo) {
132 ani_ref valueRef = OHOS::AppExecFwk::WrapWantParams(env, *extraInfo);
133 if (valueRef == nullptr) {
134 ANS_LOGE("WrapWantParams faild. 'extraInfo'");
135 return false;
136 }
137 if (!SetPropertyByRef(env, groupInfoObject, "extraInfo", valueRef)) {
138 ANS_LOGE("WarpNotificationUnifiedGroupInfo: set extraInfo failed");
139 return false;
140 }
141 }
142 ANS_LOGD("WarpNotificationUnifiedGroupInfo end");
143 return true;
144 }
145
GetNotificationRequestByBooleanOne(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)146 void GetNotificationRequestByBooleanOne(ani_env *env, ani_object obj,
147 std::shared_ptr<NotificationRequest> &request)
148 {
149 ANS_LOGD("GetNotificationRequestByBooleanOne start");
150 if (env == nullptr || obj == nullptr || request == nullptr) {
151 ANS_LOGE("GetNotificationRequestByBooleanOne failed, has nullptr");
152 return;
153 }
154 bool mbool = false;
155 ani_boolean isUndefined = ANI_TRUE;
156 if (ANI_OK == GetPropertyBool(env, obj, "isOngoing", isUndefined, mbool)
157 && isUndefined == ANI_FALSE) {
158 request->SetInProgress(mbool);
159 }
160 if (ANI_OK == GetPropertyBool(env, obj, "isUnremovable", isUndefined, mbool)
161 && isUndefined == ANI_FALSE) {
162 request->SetUnremovable(mbool);
163 }
164 if (ANI_OK == GetPropertyBool(env, obj, "updateOnly", isUndefined, mbool)
165 && isUndefined == ANI_FALSE) {
166 request->SetUpdateOnly(mbool);
167 }
168 if (ANI_OK == GetPropertyBool(env, obj, "tapDismissed", isUndefined, mbool)
169 && isUndefined == ANI_FALSE) {
170 request->SetTapDismissed(mbool);
171 }
172 if (ANI_OK == GetPropertyBool(env, obj, "colorEnabled", isUndefined, mbool)
173 && isUndefined == ANI_FALSE) {
174 request->SetColorEnabled(mbool);
175 }
176 if (ANI_OK == GetPropertyBool(env, obj, "isAlertOnce", isUndefined, mbool)
177 && isUndefined == ANI_FALSE) {
178 request->SetAlertOneTime(mbool);
179 }
180 if (ANI_OK == GetPropertyBool(env, obj, "isStopwatch", isUndefined, mbool)
181 && isUndefined == ANI_FALSE) {
182 request->SetShowStopwatch(mbool);
183 }
184 ANS_LOGD("GetNotificationRequestByBooleanOne end");
185 }
186
GetNotificationRequestByBooleanTwo(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)187 void GetNotificationRequestByBooleanTwo(ani_env *env, ani_object obj,
188 std::shared_ptr<NotificationRequest> &request)
189 {
190 ANS_LOGD("GetNotificationRequestByBooleanTwo start");
191 if (env == nullptr || obj == nullptr || request == nullptr) {
192 ANS_LOGE("GetNotificationRequestByBooleanTwo failed, has nullptr");
193 return;
194 }
195 bool mbool = false;
196 ani_boolean isUndefined = ANI_TRUE;
197 if (ANI_OK == GetPropertyBool(env, obj, "isCountDown", isUndefined, mbool)
198 && isUndefined == ANI_FALSE) {
199 request->SetCountdownTimer(mbool);
200 }
201 if (ANI_OK == GetPropertyBool(env, obj, "isFloatingIcon", isUndefined, mbool)
202 && isUndefined == ANI_FALSE) {
203 request->SetFloatingIcon(mbool);
204 }
205 if (ANI_OK == GetPropertyBool(env, obj, "showDeliveryTime", isUndefined, mbool)
206 && isUndefined == ANI_FALSE) {
207 request->SetShowDeliveryTime(mbool);
208 }
209 if (ANI_OK == GetPropertyBool(env, obj, "isRemoveAllowed", isUndefined, mbool)
210 && isUndefined == ANI_FALSE) {
211 request->SetRemoveAllowed(mbool);
212 }
213 if (ANI_OK == GetPropertyBool(env, obj, "forceDistributed", isUndefined, mbool)
214 && isUndefined == ANI_FALSE) {
215 request->SetForceDistributed(mbool);
216 }
217 if (ANI_OK == GetPropertyBool(env, obj, "notDistributed", isUndefined, mbool)
218 && isUndefined == ANI_FALSE) {
219 request->SetNotDistributed(mbool);
220 }
221 ANS_LOGD("GetNotificationRequestByBooleanTwo end");
222 }
223
GetNotificationRequestByBoolean(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)224 void GetNotificationRequestByBoolean(ani_env *env, ani_object obj,
225 std::shared_ptr<NotificationRequest> &request)
226 {
227 GetNotificationRequestByBooleanOne(env, obj, request);
228 GetNotificationRequestByBooleanTwo(env, obj, request);
229 }
230
GetNotificationRequestByString(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)231 void GetNotificationRequestByString(ani_env *env, ani_object obj,
232 std::shared_ptr<NotificationRequest> &request)
233 {
234 ANS_LOGD("GetNotificationRequestByString start");
235 if (env == nullptr || obj == nullptr || request == nullptr) {
236 ANS_LOGE("GetNotificationRequestByString failed, has nullptr");
237 return;
238 }
239 std::string mString = "";
240 ani_boolean isUndefined = ANI_TRUE;
241 if (ANI_OK == GetPropertyString(env, obj, "classification", isUndefined, mString) && isUndefined == ANI_FALSE) {
242 request->SetClassification(GetResizeStr(mString, STR_MAX_SIZE));
243 }
244 if (ANI_OK == GetPropertyString(env, obj, "appMessageId", isUndefined, mString) && isUndefined == ANI_FALSE) {
245 request->SetAppMessageId(mString);
246 }
247 if (ANI_OK == GetPropertyString(env, obj, "label", isUndefined, mString) && isUndefined == ANI_FALSE) {
248 request->SetLabel(GetResizeStr(mString, STR_MAX_SIZE));
249 }
250 if (ANI_OK == GetPropertyString(env, obj, "groupName", isUndefined, mString) && isUndefined == ANI_FALSE) {
251 request->SetGroupName(GetResizeStr(mString, STR_MAX_SIZE));
252 }
253 if (ANI_OK == GetPropertyString(env, obj, "sound", isUndefined, mString) && isUndefined == ANI_FALSE) {
254 request->SetSound(mString);
255 }
256 ANS_LOGD("GetNotificationRequestByString start");
257 }
258
GetNotificationRequestByNumber(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)259 void GetNotificationRequestByNumber(ani_env *env, ani_object obj,
260 std::shared_ptr<NotificationRequest> &request)
261 {
262 ANS_LOGD("GetNotificationRequestByNumber start");
263 if (env == nullptr || obj == nullptr || request == nullptr) {
264 ANS_LOGD("GetNotificationRequestByNumber failed, has nullptr");
265 return;
266 }
267 ani_double mDouble = 0.0;
268 ani_boolean isUndefined = ANI_TRUE;
269 if (ANI_OK == GetPropertyDouble(env, obj, "id", isUndefined, mDouble) && isUndefined == ANI_FALSE) {
270 request->SetNotificationId(static_cast<int32_t>(mDouble));
271 } else {
272 request->SetNotificationId(0);
273 }
274 if (ANI_OK == GetPropertyDouble(env, obj, "deliveryTime", isUndefined, mDouble)
275 && isUndefined == ANI_FALSE) {
276 request->SetDeliveryTime(static_cast<int32_t>(mDouble));
277 }
278 if (ANI_OK == GetPropertyDouble(env, obj, "autoDeletedTime", isUndefined, mDouble)
279 && isUndefined == ANI_FALSE) {
280 request->SetAutoDeletedTime(static_cast<int32_t>(mDouble));
281 }
282 if (ANI_OK == GetPropertyDouble(env, obj, "color", isUndefined, mDouble)
283 && isUndefined == ANI_FALSE) {
284 request->SetColor(static_cast<int32_t>(mDouble));
285 }
286 if (ANI_OK == GetPropertyDouble(env, obj, "badgeIconStyle", isUndefined, mDouble)
287 && isUndefined == ANI_FALSE) {
288 int32_t style = static_cast<int32_t>(mDouble);
289 request->SetBadgeIconStyle(static_cast<NotificationRequest::BadgeStyle>(style));
290 }
291 if (ANI_OK == GetPropertyDouble(env, obj, "badgeNumber", isUndefined, mDouble)
292 && isUndefined == ANI_FALSE) {
293 request->SetBadgeNumber(static_cast<int32_t>(mDouble));
294 }
295 if (ANI_OK == GetPropertyDouble(env, obj, "notificationControlFlags", isUndefined, mDouble)
296 && isUndefined == ANI_FALSE) {
297 request->SetNotificationControlFlags(static_cast<int32_t>(mDouble));
298 }
299 ANS_LOGD("GetNotificationRequestByNumber end");
300 }
301
GetNotificationNormalContent(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)302 bool GetNotificationNormalContent(ani_env *env, ani_object obj, std::shared_ptr<NotificationRequest> &request)
303 {
304 ANS_LOGD("GetNotificationNormalContent start");
305 if (env == nullptr || obj == nullptr || request == nullptr) {
306 ANS_LOGE("GetNotificationNormalContent failed, has nullptr");
307 return false;
308 }
309 ani_boolean isUndefined = ANI_TRUE;
310 ani_ref contentRef = {};
311 if (ANI_OK != GetPropertyRef(env, obj, "normal", isUndefined, contentRef)
312 || isUndefined == ANI_TRUE || contentRef == nullptr) {
313 ANS_LOGE("GetNotificationNormalContent get ref failed");
314 return false;
315 }
316 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
317 UnWarpNotificationNormalContent(env, static_cast<ani_object>(contentRef), normalContent);
318 request->SetContent(std::make_shared<NotificationContent>(normalContent));
319 ANS_LOGD("GetNotificationNormalContent end");
320 return true;
321 }
322
GetNotificationLongTextContent(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)323 bool GetNotificationLongTextContent(ani_env *env, ani_object obj, std::shared_ptr<NotificationRequest> &request)
324 {
325 ANS_LOGD("GetNotificationLongTextContent start");
326 if (env == nullptr || obj == nullptr || request == nullptr) {
327 ANS_LOGE("GetNotificationLongTextContent failed, has nullptr");
328 return false;
329 }
330 ani_boolean isUndefined = ANI_TRUE;
331 ani_ref contentRef = {};
332 if (ANI_OK != GetPropertyRef(env, obj, "longText", isUndefined, contentRef)
333 || isUndefined == ANI_TRUE || contentRef == nullptr) {
334 ANS_LOGE("GetNotificationLongTextContent get ref failed");
335 return false;
336 }
337 std::shared_ptr<NotificationLongTextContent> longTextContent
338 = std::make_shared<NotificationLongTextContent>();
339 UnWarpNotificationLongTextContent(env, static_cast<ani_object>(contentRef), longTextContent);
340 request->SetContent(std::make_shared<NotificationContent>(longTextContent));
341 ANS_LOGD("GetNotificationLongTextContent end");
342 return true;
343 }
344
GetNotificationPictureContent(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)345 bool GetNotificationPictureContent(ani_env *env, ani_object obj, std::shared_ptr<NotificationRequest> &request)
346 {
347 ANS_LOGD("GetNotificationPictureContent start");
348 if (env == nullptr || obj == nullptr || request == nullptr) {
349 ANS_LOGE("GetNotificationPictureContent failed, has nullptr");
350 return false;
351 }
352 ani_boolean isUndefined = ANI_TRUE;
353 ani_ref contentRef = {};
354 if (ANI_OK != GetPropertyRef(env, obj, "picture", isUndefined, contentRef)
355 || isUndefined == ANI_TRUE || contentRef == nullptr) {
356 ANS_LOGE("GetNotificationPictureContent get ref failed");
357 return false;
358 }
359 std::shared_ptr<NotificationPictureContent> pictureContent
360 = std::make_shared<NotificationPictureContent>();
361 UnWarpNotificationPictureContent(env, static_cast<ani_object>(contentRef), pictureContent);
362 request->SetContent(std::make_shared<NotificationContent>(pictureContent));
363 ANS_LOGD("GetNotificationPictureContent end");
364 return true;
365 }
366
GetNotificationMultiLineContent(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)367 bool GetNotificationMultiLineContent(ani_env *env, ani_object obj, std::shared_ptr<NotificationRequest> &request)
368 {
369 ANS_LOGD("GetNotificationMultiLineContent start");
370 if (env == nullptr || obj == nullptr || request == nullptr) {
371 ANS_LOGE("GetNotificationMultiLineContent failed, has nullptr");
372 return false;
373 }
374 ani_boolean isUndefined = ANI_TRUE;
375 ani_ref contentRef = {};
376 if (ANI_OK != GetPropertyRef(env, obj, "multiLine", isUndefined, contentRef)
377 || isUndefined == ANI_TRUE || contentRef == nullptr) {
378 ANS_LOGE("GetNotificationMultiLineContent get ref failed");
379 return false;
380 }
381 std::shared_ptr<NotificationMultiLineContent> multiLineContent
382 = std::make_shared<NotificationMultiLineContent>();
383 UnWarpNotificationMultiLineContent(env, static_cast<ani_object>(contentRef), multiLineContent);
384 request->SetContent(std::make_shared<NotificationContent>(multiLineContent));
385 ANS_LOGD("GetNotificationMultiLineContent end");
386 return true;
387 }
388
GetNotificationLocalLiveViewContent(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)389 bool GetNotificationLocalLiveViewContent(ani_env *env, ani_object obj, std::shared_ptr<NotificationRequest> &request)
390 {
391 ANS_LOGD("GetNotificationLocalLiveViewContent start");
392 if (env == nullptr || obj == nullptr || request == nullptr) {
393 ANS_LOGE("GetNotificationLocalLiveViewContent failed, has nullptr");
394 return false;
395 }
396 ani_boolean isUndefined = ANI_TRUE;
397 ani_ref contentRef = {};
398 if (ANI_OK != GetPropertyRef(env, obj, "systemLiveView", isUndefined, contentRef)
399 || isUndefined == ANI_TRUE || contentRef == nullptr) {
400 ANS_LOGE("GetNotificationLocalLiveViewContent get ref failed");
401 return false;
402 }
403 std::shared_ptr<NotificationLocalLiveViewContent> localLiveView
404 = std::make_shared<NotificationLocalLiveViewContent>();
405 UnWarpNotificationLocalLiveViewContent(env, static_cast<ani_object>(contentRef), localLiveView);
406 request->SetContent(std::make_shared<NotificationContent>(localLiveView));
407 ANS_LOGD("GetNotificationLocalLiveViewContent end");
408 return true;
409 }
410
GetNotificationLiveViewContent(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)411 bool GetNotificationLiveViewContent(ani_env *env, ani_object obj, std::shared_ptr<NotificationRequest> &request)
412 {
413 ANS_LOGD("GetNotificationLiveViewContent start");
414 if (env == nullptr || obj == nullptr || request == nullptr) {
415 ANS_LOGE("GetNotificationLiveViewContent failed, has nullptr");
416 return false;
417 }
418 ani_boolean isUndefined = ANI_TRUE;
419 ani_ref contentRef = {};
420 if (ANI_OK != GetPropertyRef(env, obj, "liveView", isUndefined, contentRef)
421 || isUndefined == ANI_TRUE || contentRef == nullptr) {
422 ANS_LOGE("GetNotificationLiveViewContent get ref failed");
423 return false;
424 }
425 std::shared_ptr<NotificationLiveViewContent> liveViewContent
426 = std::make_shared<NotificationLiveViewContent>();
427 UnWarpNotificationLiveViewContent(env, static_cast<ani_object>(contentRef), liveViewContent);
428 request->SetContent(std::make_shared<NotificationContent>(liveViewContent));
429 ANS_LOGD("GetNotificationLiveViewContent end");
430 return true;
431 }
432
GetNotificationContent(ani_env * env,ani_object obj,ContentType outType,std::shared_ptr<NotificationRequest> & request)433 bool GetNotificationContent(ani_env *env, ani_object obj, ContentType outType,
434 std::shared_ptr<NotificationRequest> &request)
435 {
436 ANS_LOGD("GetNotificationContentWithType start");
437 if (env == nullptr || obj == nullptr || request == nullptr) {
438 ANS_LOGE("GetNotificationContent failed, has nullptr");
439 return false;
440 }
441 ANS_LOGD("GetNotificationContent ContentType = %{public}d", static_cast<int>(outType));
442 switch (outType) {
443 case ContentType::BASIC_TEXT:
444 return GetNotificationNormalContent(env, obj, request);
445 case ContentType::LONG_TEXT:
446 return GetNotificationLongTextContent(env, obj, request);
447 case ContentType::PICTURE:
448 return GetNotificationPictureContent(env, obj, request);
449 case ContentType::MULTILINE:
450 return GetNotificationMultiLineContent(env, obj, request);
451 case ContentType::LOCAL_LIVE_VIEW:
452 return GetNotificationLocalLiveViewContent(env, obj, request);
453 case ContentType::LIVE_VIEW:
454 return GetNotificationLiveViewContent(env, obj, request);
455 case ContentType::CONVERSATION:
456 break;
457 default:
458 ANS_LOGD("ContentType not find. type %{public}d", static_cast<int32_t>(outType));
459 break;
460 }
461 return true;
462 }
463
GetNotificationContent(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)464 ani_status GetNotificationContent(ani_env *env, ani_object obj,
465 std::shared_ptr<NotificationRequest> &request)
466 {
467 ANS_LOGD("GetNotificationContent start");
468 if (env == nullptr || obj == nullptr || request == nullptr) {
469 ANS_LOGE("GetNotificationContent failed, has nullptr");
470 return ANI_ERROR;
471 }
472 ani_status status = ANI_OK;
473 ani_boolean isUndefined = ANI_TRUE;
474 ani_ref notificationContentRef = {};
475 if (ANI_OK != (status = GetPropertyRef(env, obj, "content", isUndefined, notificationContentRef))
476 || isUndefined == ANI_TRUE) {
477 ANS_LOGE("GetNotificationContent:get contentRef failed. status %{public}d", status);
478 return status;
479 }
480 ani_ref contentTypeRef;
481 if (ANI_OK != (status = GetPropertyRef(env, static_cast<ani_object>(notificationContentRef),
482 "notificationContentType", isUndefined, contentTypeRef))
483 || isUndefined == ANI_TRUE || contentTypeRef == nullptr) {
484 ANS_LOGE("GetNotificationContent:get notificationContentType failed. status %{public}d", status);
485 return status;
486 }
487 ContentType type;
488 if (!ContentTypeEtsToC(env, static_cast<ani_enum_item>(contentTypeRef), type)) {
489 ANS_LOGE("GetNotificationContent:ContentTypeEtsToC failed");
490 return ANI_INVALID_ARGS;
491 }
492 if (!GetNotificationContent(env, static_cast<ani_object>(notificationContentRef), type, request)) {
493 ANS_LOGE("GetNotificationContent:GetNotificationContent failed");
494 return ANI_INVALID_ARGS;
495 }
496 ANS_LOGD("GetNotificationContent end");
497 return ANI_OK;
498 }
499
GetNotificationSlotType(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)500 void GetNotificationSlotType(ani_env *env, ani_object obj, std::shared_ptr<NotificationRequest> &request)
501 {
502 ANS_LOGD("GetNotificationSlotType start");
503 if (env == nullptr || obj == nullptr || request == nullptr) {
504 ANS_LOGE("GetNotificationSlotType failed, has nullptr");
505 return;
506 }
507 ani_status status = ANI_ERROR;
508 ani_boolean isUndefined = ANI_OK;
509 ani_ref slotTypeRef = {};
510 if (ANI_OK != (status = GetPropertyRef(env, obj, "notificationSlotType", isUndefined, slotTypeRef))
511 || isUndefined == ANI_TRUE || slotTypeRef == nullptr) {
512 ANS_LOGE("GetNotificationSlotType: get Ref failed");
513 return;
514 }
515 SlotType type = SlotType::OTHER;
516 if (!SlotTypeEtsToC(env, static_cast<ani_enum_item>(slotTypeRef), type)) {
517 ANS_LOGE("GetNotificationSlotType: SlotTypeEtsToC failed");
518 return;
519 }
520 request->SetSlotType(type);
521 ANS_LOGD("GetNotificationSlotType end");
522 }
523
GetNotificationWantAgent(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)524 void GetNotificationWantAgent(ani_env *env, ani_object obj, std::shared_ptr<NotificationRequest> &request)
525 {
526 ani_boolean isUndefined = ANI_TRUE;
527 ani_ref wantAgentRef = {};
528 if (ANI_OK != GetPropertyRef(env, obj, "wantAgent", isUndefined, wantAgentRef)
529 || isUndefined == ANI_TRUE || wantAgentRef == nullptr) {
530 ANS_LOGE("GetNotificationWantAgent: get ref failed");
531 return;
532 }
533 std::shared_ptr<WantAgent> wantAgent = UnwrapWantAgent(env, static_cast<ani_object>(wantAgentRef));
534 if (wantAgent == nullptr) {
535 ANS_LOGD("wantAgent is null");
536 return;
537 }
538 request->SetWantAgent(wantAgent);
539 }
540
GetNotificationExtraInfo(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)541 void GetNotificationExtraInfo(ani_env *env, ani_object obj, std::shared_ptr<NotificationRequest> &request)
542 {
543 ani_boolean isUndefined = ANI_TRUE;
544 ani_ref extraInfoRef = {};
545 if (ANI_OK != GetPropertyRef(env, obj, "extraInfo", isUndefined, extraInfoRef)
546 || isUndefined == ANI_TRUE || extraInfoRef == nullptr) {
547 ANS_LOGE("GetNotificationExtraInfo: get ref failed");
548 return;
549 }
550 WantParams wantParams = {};
551 UnwrapWantParams(env, extraInfoRef, wantParams);
552 std::shared_ptr<WantParams> extras = std::make_shared<WantParams>(wantParams);
553 request->SetAdditionalData(extras);
554 }
555
GetNotificationExtendInfo(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)556 void GetNotificationExtendInfo(ani_env *env, ani_object obj, std::shared_ptr<NotificationRequest> &request)
557 {
558 ani_boolean isUndefined = ANI_TRUE;
559 ani_ref extendInfoRef = {};
560 if (ANI_OK != GetPropertyRef(env, obj, "extendInfo", isUndefined, extendInfoRef)
561 || isUndefined == ANI_TRUE || extendInfoRef == nullptr) {
562 ANS_LOGE("GetNotificationExtendInfo: get ref failed");
563 return;
564 }
565 WantParams wantParams = {};
566 UnwrapWantParams(env, extendInfoRef, wantParams);
567 std::shared_ptr<WantParams> extendInfo = std::make_shared<WantParams>(wantParams);
568 request->SetExtendInfo(extendInfo);
569 }
570
GetNotificationRemovalWantAgent(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)571 void GetNotificationRemovalWantAgent(ani_env *env, ani_object obj,
572 std::shared_ptr<NotificationRequest> &request)
573 {
574 ani_boolean isUndefined = ANI_TRUE;
575 ani_ref wantAgentRef = {};
576 if (ANI_OK != GetPropertyRef(env, obj, "removalWantAgent", isUndefined, wantAgentRef)
577 || isUndefined == ANI_TRUE || wantAgentRef == nullptr) {
578 ANS_LOGE("GetNotificationRemovalWantAgent: get ref failed");
579 return;
580 }
581 std::shared_ptr<WantAgent> wantAgent = UnwrapWantAgent(env, static_cast<ani_object>(wantAgentRef));
582 if (wantAgent == nullptr) {
583 ANS_LOGD("wantAgent is null");
584 return;
585 }
586 request->SetRemovalWantAgent(wantAgent);
587 }
588
GetNotificationActionButtons(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)589 void GetNotificationActionButtons(ani_env *env, ani_object obj, std::shared_ptr<NotificationRequest> &request)
590 {
591 std::vector<std::shared_ptr<NotificationActionButton>> buttons = {};
592 ani_status status = GetNotificationActionButtonArray(env, obj, "actionButtons", buttons);
593 if (status == ANI_OK) {
594 for (auto button : buttons) {
595 request->AddActionButton(button);
596 }
597 }
598 }
599
GetNotificationSmallIcon(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)600 void GetNotificationSmallIcon(ani_env *env, ani_object obj, std::shared_ptr<NotificationRequest> &request)
601 {
602 ani_boolean isUndefined = ANI_TRUE;
603 ani_ref smallIconRef = {};
604 if (ANI_OK != GetPropertyRef(env, obj, "smallIcon", isUndefined, smallIconRef)
605 || isUndefined == ANI_TRUE || smallIconRef == nullptr) {
606 ANS_LOGE("GetNotificationSmallIcon: get ref failed");
607 return;
608 }
609 std::shared_ptr<PixelMap> pixelMap = GetPixelMapFromAni(env, static_cast<ani_object>(smallIconRef));
610 if (pixelMap != nullptr) {
611 request->SetLittleIcon(pixelMap);
612 }
613 }
614
GetNotificationLargeIcon(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)615 void GetNotificationLargeIcon(ani_env *env, ani_object obj, std::shared_ptr<NotificationRequest> &request)
616 {
617 ani_boolean isUndefined = ANI_TRUE;
618 ani_ref largeIconRef = {};
619 if (ANI_OK != GetPropertyRef(env, obj, "largeIcon", isUndefined, largeIconRef)
620 || isUndefined == ANI_TRUE || largeIconRef == nullptr) {
621 ANS_LOGE("GetNotificationLargeIcon: get ref failed");
622 return;
623 }
624 std::shared_ptr<PixelMap> pixelMap = GetPixelMapFromAni(env, static_cast<ani_object>(largeIconRef));
625 if (pixelMap != nullptr) {
626 request->SetBigIcon(pixelMap);
627 }
628 }
629
GetNotificationOverlayIcon(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)630 void GetNotificationOverlayIcon(ani_env *env, ani_object obj, std::shared_ptr<NotificationRequest> &request)
631 {
632 ani_boolean isUndefined = ANI_TRUE;
633 ani_ref overlayIconRef = {};
634 if (ANI_OK != GetPropertyRef(env, obj, "overlayIcon", isUndefined, overlayIconRef)
635 || isUndefined == ANI_TRUE || overlayIconRef == nullptr) {
636 ANS_LOGE("GetNotificationOverlayIcon: get ref failed");
637 return;
638 }
639 std::shared_ptr<PixelMap> pixelMap = GetPixelMapFromAni(env, static_cast<ani_object>(overlayIconRef));
640 if (pixelMap != nullptr) {
641 request->SetOverlayIcon(pixelMap);
642 }
643 }
644
GetNotificationRequestDistributedOptions(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)645 void GetNotificationRequestDistributedOptions(ani_env *env, ani_object obj,
646 std::shared_ptr<NotificationRequest> &request)
647 {
648 ani_status status = ANI_ERROR;
649 ani_ref optionRef = {};
650 ani_boolean isUndefind = ANI_TRUE;
651 status = GetPropertyRef(env, obj, "distributedOption", isUndefind, optionRef);
652 if (status == ANI_OK && isUndefind == ANI_FALSE) {
653 StsDistributedOptions options;
654 UnWarpDistributedOptions(env, static_cast<ani_object>(optionRef), options);
655 request->SetDistributed(options.isDistributed);
656 request->SetDevicesSupportDisplay(options.supportDisplayDevices);
657 request->SetDevicesSupportOperate(options.supportOperateDevices);
658 }
659 }
660
GetNotificationTemplate(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)661 void GetNotificationTemplate(ani_env *env, ani_object obj,
662 std::shared_ptr<NotificationRequest> &request)
663 {
664 ani_status status = ANI_ERROR;
665 ani_ref templateRef = {};
666 ani_boolean isUndefind = ANI_TRUE;
667 status = GetPropertyRef(env, obj, "template", isUndefind, templateRef);
668 if (status == ANI_OK && isUndefind == ANI_FALSE) {
669 OHOS::Notification::NotificationTemplate tmplate;
670 UnwrapNotificationTemplate(env, static_cast<ani_object>(templateRef), tmplate);
671 request->SetTemplate(std::make_shared<OHOS::Notification::NotificationTemplate>(tmplate));
672 }
673 }
674
GetNotificationUnifiedGroupInfo(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)675 void GetNotificationUnifiedGroupInfo(ani_env *env, ani_object obj,
676 std::shared_ptr<NotificationRequest> &request)
677 {
678 ani_status status = ANI_ERROR;
679 ani_ref infoRef = {};
680 ani_boolean isUndefind = ANI_TRUE;
681 status = GetPropertyRef(env, obj, "unifiedGroupInfo", isUndefind, infoRef);
682 if (status != ANI_OK || isUndefind == ANI_TRUE) {
683 return;
684 }
685 std::shared_ptr<NotificationUnifiedGroupInfo> unifiedGroupInfo = std::make_shared<NotificationUnifiedGroupInfo>();
686 std::string mString = "";
687 if (ANI_OK == GetPropertyString(env, static_cast<ani_object>(infoRef), "key", isUndefind, mString)
688 && isUndefind == ANI_FALSE) {
689 unifiedGroupInfo->SetKey(GetResizeStr(mString, STR_MAX_SIZE));
690 }
691 if (ANI_OK == GetPropertyString(env, static_cast<ani_object>(infoRef), "title", isUndefind, mString)
692 && isUndefind == ANI_FALSE) {
693 unifiedGroupInfo->SetTitle(GetResizeStr(mString, STR_MAX_SIZE));
694 }
695 if (ANI_OK == GetPropertyString(env, static_cast<ani_object>(infoRef), "content", isUndefind, mString)
696 && isUndefind == ANI_FALSE) {
697 unifiedGroupInfo->SetContent(GetResizeStr(mString, STR_MAX_SIZE));
698 }
699 if (ANI_OK == GetPropertyString(env, static_cast<ani_object>(infoRef), "sceneName", isUndefind, mString)
700 && isUndefind == ANI_FALSE) {
701 unifiedGroupInfo->SetSceneName(GetResizeStr(mString, STR_MAX_SIZE));
702 }
703 ani_ref extraInfoRef = {};
704 status = GetPropertyRef(env, static_cast<ani_object>(infoRef), "extraInfo", isUndefind, extraInfoRef);
705 if (status == ANI_OK && isUndefind == ANI_FALSE) {
706 WantParams wantParams = {};
707 UnwrapWantParams(env, extraInfoRef, wantParams);
708 std::shared_ptr<WantParams> extras = std::make_shared<WantParams>(wantParams);
709 unifiedGroupInfo->SetExtraInfo(extras);
710 }
711 request->SetUnifiedGroupInfo(unifiedGroupInfo);
712 }
713
GetNotificationBundleOption(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & request)714 void GetNotificationBundleOption(ani_env *env, ani_object obj,
715 std::shared_ptr<NotificationRequest> &request)
716 {
717 ani_status status = ANI_ERROR;
718 ani_ref optionRef = {};
719 ani_boolean isUndefind = ANI_TRUE;
720 status = GetPropertyRef(env, obj, "representativeBundle", isUndefind, optionRef);
721 if (status != ANI_OK || isUndefind == ANI_TRUE) {
722 ANS_LOGD("Cannot get the value of representativeBundle. status %{public}d isUndefind %{public}d",
723 status, isUndefind);
724 return;
725 }
726 OHOS::Notification::NotificationBundleOption option;
727 if (UnwrapBundleOption(env, static_cast<ani_object>(optionRef), option)) {
728 request->SetBundleOption(std::make_shared<OHOS::Notification::NotificationBundleOption>(option));
729 }
730 }
731
GetNotificationRequestByCustom(ani_env * env,ani_object obj,std::shared_ptr<OHOS::Notification::NotificationRequest> & notificationRequest)732 ani_status GetNotificationRequestByCustom(ani_env *env, ani_object obj,
733 std::shared_ptr<OHOS::Notification::NotificationRequest> ¬ificationRequest)
734 {
735 ani_status status = GetNotificationContent(env, obj, notificationRequest);
736 if (status != ANI_OK) {
737 return ANI_INVALID_ARGS;
738 }
739 GetNotificationSlotType(env, obj, notificationRequest);
740 GetNotificationWantAgent(env, obj, notificationRequest);
741 GetNotificationExtraInfo(env, obj, notificationRequest);
742 GetNotificationExtendInfo(env, obj, notificationRequest);
743 GetNotificationRemovalWantAgent(env, obj, notificationRequest);
744 GetNotificationActionButtons(env, obj, notificationRequest);
745 GetNotificationSmallIcon(env, obj, notificationRequest);
746 GetNotificationLargeIcon(env, obj, notificationRequest);
747 GetNotificationOverlayIcon(env, obj, notificationRequest);
748 GetNotificationRequestDistributedOptions(env, obj, notificationRequest);
749 GetNotificationTemplate(env, obj, notificationRequest);
750 GetNotificationUnifiedGroupInfo(env, obj, notificationRequest);
751 GetNotificationBundleOption(env, obj, notificationRequest);
752 return status;
753 }
754
UnWarpNotificationRequest(ani_env * env,ani_object obj,std::shared_ptr<NotificationRequest> & notificationRequest)755 ani_status UnWarpNotificationRequest(ani_env *env, ani_object obj,
756 std::shared_ptr<NotificationRequest> ¬ificationRequest)
757 {
758 ANS_LOGD("UnWarpNotificationRequest start");
759 if (env == nullptr || obj == nullptr) {
760 ANS_LOGD("UnWarpNotificationRequest has nullptr");
761 return ANI_ERROR;
762 }
763 ani_status status = ANI_ERROR;
764 GetNotificationRequestByNumber(env, obj, notificationRequest);
765 GetNotificationRequestByString(env, obj, notificationRequest);
766 GetNotificationRequestByBoolean(env, obj, notificationRequest);
767 status = GetNotificationRequestByCustom(env, obj, notificationRequest);
768 ANS_LOGD("UnWarpNotificationRequest end");
769 return status;
770 }
771
SetNotificationRequestByBool(ani_env * env,ani_class cls,const OHOS::Notification::NotificationRequest * request,ani_object & object)772 bool SetNotificationRequestByBool(ani_env* env, ani_class cls, const OHOS::Notification::NotificationRequest *request,
773 ani_object &object)
774 {
775 if (env == nullptr || cls == nullptr || object == nullptr || request == nullptr) {
776 ANS_LOGE("request is nullptr");
777 return false;
778 }
779 // isOngoing?: boolean
780 if (!SetPropertyOptionalByBoolean(env, object, "isOngoing", request->IsInProgress())) {
781 ANS_LOGD("SetNotificationRequest set 'isOngoing' faild");
782 }
783 // isUnremovable?: boolean
784 if (!SetPropertyOptionalByBoolean(env, object, "isUnremovable", request->IsUnremovable())) {
785 ANS_LOGD("SetNotificationRequest set 'isUnremovable' faild");
786 }
787 // tapDismissed?: boolean
788 if (!SetPropertyOptionalByBoolean(env, object, "tapDismissed", request->IsTapDismissed())) {
789 ANS_LOGD("SetNotificationRequest set 'tapDismissed' faild");
790 }
791 // colorEnabled?: boolean
792 if (!SetPropertyOptionalByBoolean(env, object, "colorEnabled", request->IsColorEnabled())) {
793 ANS_LOGD("SetNotificationRequest set 'colorEnabled' faild");
794 }
795 // isAlertOnce?: boolean
796 if (!SetPropertyOptionalByBoolean(env, object, "isAlertOnce", request->IsAlertOneTime())) {
797 ANS_LOGD("SetNotificationRequest set 'isAlertOnce' faild");
798 }
799 // isStopwatch?: boolean
800 if (!SetPropertyOptionalByBoolean(env, object, "isStopwatch", request->IsShowStopwatch())) {
801 ANS_LOGD("SetNotificationRequest set 'isStopwatch' faild");
802 }
803 // isCountDown?: boolean
804 if (!SetPropertyOptionalByBoolean(env, object, "isCountDown", request->IsCountdownTimer())) {
805 ANS_LOGD("SetNotificationRequest set 'isCountDown' faild");
806 }
807 // isFloatingIcon?: boolean
808 if (!SetPropertyOptionalByBoolean(env, object, "isFloatingIcon", request->IsFloatingIcon())) {
809 ANS_LOGD("SetNotificationRequest set 'isFloatingIcon' faild");
810 }
811 // showDeliveryTime?: boolean
812 if (!SetPropertyOptionalByBoolean(env, object, "showDeliveryTime", request->IsShowDeliveryTime())) {
813 ANS_LOGD("SetNotificationRequest set 'showDeliveryTime' faild");
814 }
815 // updateOnly?: boolean
816 if (!SetPropertyOptionalByBoolean(env, object, "updateOnly", request->IsUpdateOnly())) {
817 ANS_LOGD("SetNotificationRequest set 'updateOnly' faild");
818 }
819 return true;
820 }
821
SetNotificationRequestByString(ani_env * env,ani_class cls,const OHOS::Notification::NotificationRequest * request,ani_object & object)822 bool SetNotificationRequestByString(ani_env* env, ani_class cls, const OHOS::Notification::NotificationRequest *request,
823 ani_object &object)
824 {
825 if (request == nullptr) {
826 ANS_LOGE("request is nullptr");
827 return false;
828 }
829 // classification?: string
830 std::string str = request->GetClassification();
831 if (!SetPropertyOptionalByString(env, object, "classification", request->GetClassification())) {
832 ANS_LOGD("SetNotificationRequest set '' faild");
833 }
834 // label?: string
835 if (!SetPropertyOptionalByString(env, object, "label", request->GetLabel())) {
836 ANS_LOGD("SetNotificationRequest set 'label' faild");
837 }
838 // groupName?: string
839 if (!SetPropertyOptionalByString(env, object, "groupName", request->GetGroupName())) {
840 ANS_LOGD("SetNotificationRequest set 'groupName' faild");
841 }
842 // readonly creatorBundleName?: string
843 if (!SetPropertyOptionalByString(env, object, "creatorBundleName", request->GetCreatorBundleName())) {
844 ANS_LOGD("SetNotificationRequest set 'creatorBundleName' faild");
845 }
846 // readonly sound?: string
847 if (!SetPropertyOptionalByString(env, object, "sound", request->GetSound())) {
848 ANS_LOGD("SetNotificationRequest set 'sound' faild");
849 }
850 // readonly appInstanceKey?: string
851 if (!SetPropertyOptionalByString(env, object, "appInstanceKey", request->GetAppInstanceKey())) {
852 ANS_LOGD("SetNotificationRequest set 'appInstanceKey' faild");
853 }
854 return true;
855 }
856
SetNotificationRequestByNumber(ani_env * env,ani_class cls,const OHOS::Notification::NotificationRequest * request,ani_object & object)857 bool SetNotificationRequestByNumber(ani_env* env, ani_class cls, const OHOS::Notification::NotificationRequest *request,
858 ani_object &object)
859 {
860 if (request == nullptr) {
861 ANS_LOGE("request is nullptr");
862 return false;
863 }
864 // id?: number
865 SetPropertyOptionalByDouble(env, object, "id", request->GetNotificationId());
866 // slotType?: SlotType
867 ani_enum_item slotTypeItem {};
868 if (SlotTypeCToEts(env, request->GetSlotType(), slotTypeItem)) {
869 CallSetter(env, cls, object, "notificationSlotType", slotTypeItem);
870 }
871 // deliveryTime?: number
872 SetPropertyOptionalByDouble(env, object, "deliveryTime", request->GetDeliveryTime());
873 // autoDeletedTime?: number
874 SetPropertyOptionalByDouble(env, object, "autoDeletedTime", request->GetAutoDeletedTime());
875 // color ?: number
876 SetPropertyOptionalByDouble(env, object, "color", request->GetColor());
877 // badgeIconStyle ?: number
878 SetPropertyOptionalByDouble(env, object, "badgeIconStyle",
879 static_cast<int32_t>(request->GetBadgeIconStyle()));
880 // readonly creatorUid?: number
881 SetPropertyOptionalByDouble(env, object, "creatorUid", request->GetCreatorUid());
882 // readonly creatorPid?: number
883 SetPropertyOptionalByDouble(env, object, "creatorPid", request->GetCreatorPid());
884 // badgeNumber?: number
885 SetPropertyOptionalByDouble(env, object, "badgeNumber", request->GetBadgeNumber());
886 // readonly creatorInstanceKey?: number
887 SetPropertyOptionalByDouble(env, object, "creatorInstanceKey", request->GetCreatorInstanceKey());
888 return true;
889 }
890
SetNotificationRequestByWantAgent(ani_env * env,ani_class cls,const OHOS::Notification::NotificationRequest * request,ani_object & object)891 bool SetNotificationRequestByWantAgent(ani_env* env, ani_class cls,
892 const OHOS::Notification::NotificationRequest *request, ani_object &object)
893 {
894 if (request == nullptr) {
895 ANS_LOGE("request is nullptr");
896 return false;
897 }
898 // wantAgent?: WantAgent
899 std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> agent = request->GetWantAgent();
900 if (agent) {
901 ani_object wantAgent = AppExecFwk::WrapWantAgent(env, agent.get());
902 if (wantAgent == nullptr || !SetPropertyByRef(env, object, "wantAgent", wantAgent)) {
903 ANS_LOGD("SetNotificationRequest set 'wantAgent' faild");
904 }
905 }
906 // removalWantAgent?: WantAgent
907 std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> removalAgent = request->GetRemovalWantAgent();
908 if (removalAgent) {
909 ani_object wantAgent = AppExecFwk::WrapWantAgent(env, removalAgent.get());
910 if (wantAgent == nullptr || !SetPropertyByRef(env, object, "removalWantAgent", wantAgent)) {
911 ANS_LOGD("SetNotificationRequest set 'removalWantAgent' faild");
912 }
913 }
914 // maxScreenWantAgent?: WantAgent
915 std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> maxScreenAgent = request->GetMaxScreenWantAgent();
916 if (maxScreenAgent) {
917 ani_object wantAgent = AppExecFwk::WrapWantAgent(env, maxScreenAgent.get());
918 if (wantAgent == nullptr || !SetPropertyByRef(env, object, "maxScreenWantAgent", wantAgent)) {
919 ANS_LOGD("SetNotificationRequest set 'maxScreenWantAgent' faild");
920 }
921 }
922 return true;
923 }
924
SetNotificationRequestByPixelMap(ani_env * env,ani_class cls,const NotificationRequest * request,ani_object & object)925 bool SetNotificationRequestByPixelMap(ani_env* env, ani_class cls, const NotificationRequest *request,
926 ani_object &object)
927 {
928 if (request == nullptr) {
929 ANS_LOGE("request is nullptr");
930 return false;
931 }
932 // smallIcon?: image.PixelMap
933 std::shared_ptr<Media::PixelMap> littleIcon = request->GetLittleIcon();
934 if (littleIcon) {
935 ani_object smallIconResult = CreateAniPixelMap(env, littleIcon);
936 if (smallIconResult == nullptr || !SetPropertyByRef(env, object, "smallIcon", smallIconResult)) {
937 ANS_LOGD("SetNotificationRequest set 'smallIcon' faild");
938 }
939 }
940 // largeIcon?: image.PixelMap
941 std::shared_ptr<Media::PixelMap> largeIcon = request->GetBigIcon();
942 if (largeIcon) {
943 ani_object largeIconResult = CreateAniPixelMap(env, largeIcon);
944 if (largeIconResult == nullptr || !SetPropertyByRef(env, object, "largeIcon", largeIconResult)) {
945 ANS_LOGD("SetNotificationRequest set 'largeIcon' faild");
946 }
947 }
948 // overlayIcon?: image.PixelMap
949 std::shared_ptr<Media::PixelMap> overlayIcon = request->GetOverlayIcon();
950 if (overlayIcon) {
951 ani_object overlayIconResult = CreateAniPixelMap(env, overlayIcon);
952 if (overlayIconResult == nullptr || !SetPropertyByRef(env, object, "overlayIcon", overlayIconResult)) {
953 ANS_LOGD("SetNotificationRequest set 'overlayIcon' faild");
954 }
955 }
956 return true;
957 }
958
SetNotificationRequestByNotificationContent(ani_env * env,ani_class cls,const OHOS::Notification::NotificationRequest * request,ani_object & object)959 bool SetNotificationRequestByNotificationContent(ani_env* env, ani_class cls,
960 const OHOS::Notification::NotificationRequest *request, ani_object &object)
961 {
962 if (request == nullptr) {
963 ANS_LOGE("request is nullptr");
964 return false;
965 }
966 std::shared_ptr<NotificationContent> content = request->GetContent();
967 ani_object contentObj;
968 if (!SetNotificationContent(env, content, contentObj)) {
969 ANS_LOGD("SetNotificationContent faild");
970 return false;
971 }
972 if (contentObj == nullptr) {
973 ANS_LOGD("contentObj is nullptr");
974 return false;
975 }
976 if (!SetPropertyByRef(env, object, "content", contentObj)) {
977 ANS_LOGD("SetNotificationRequestByNotificationContent. set content faild");
978 return false;
979 }
980 return true;
981 }
982
SetRequestExtraInfo(ani_env * env,const OHOS::Notification::NotificationRequest * request,ani_object & object)983 bool SetRequestExtraInfo(ani_env *env, const OHOS::Notification::NotificationRequest *request, ani_object &object)
984 {
985 std::shared_ptr<AAFwk::WantParams> additionalData = request->GetAdditionalData();
986 if (additionalData == nullptr) {
987 ANS_LOGD("extraInfo is Undefine");
988 return true;
989 }
990 ani_ref extraInfo = OHOS::AppExecFwk::WrapWantParams(env, *additionalData);
991 if (extraInfo == nullptr || !SetPropertyByRef(env, object, "extraInfo", extraInfo)) {
992 ANS_LOGD("SetNotificationRequestByCustom: set extraInfo failed");
993 }
994 return true;
995 }
996
SetRequestExtendInfo(ani_env * env,const OHOS::Notification::NotificationRequest * request,ani_object & object)997 bool SetRequestExtendInfo(ani_env *env, const OHOS::Notification::NotificationRequest *request, ani_object &object)
998 {
999 std::shared_ptr<AAFwk::WantParams> extendInfoData = request->GetExtendInfo();
1000 if (extendInfoData == nullptr) {
1001 ANS_LOGD("extendInfo is Undefine");
1002 return true;
1003 }
1004 ani_ref extendInfo = OHOS::AppExecFwk::WrapWantParams(env, *extendInfoData);
1005 if (extendInfo == nullptr || !SetPropertyByRef(env, object, "extendInfo", extendInfo)) {
1006 ANS_LOGD("SetNotificationRequestByCustom: set extendInfo failed");
1007 }
1008 return true;
1009 }
1010
SetRequestActionButtons(ani_env * env,const OHOS::Notification::NotificationRequest * request,ani_object & object)1011 bool SetRequestActionButtons(ani_env *env, const OHOS::Notification::NotificationRequest *request, ani_object &object)
1012 {
1013 std::vector<std::shared_ptr<NotificationActionButton>> actionButtons = request->GetActionButtons();
1014 if (actionButtons.empty()) {
1015 ANS_LOGD("actionButtons is Undefine");
1016 return true;
1017 }
1018 ani_object actionButtonsArrayObj = GetAniArrayNotificationActionButton(env, actionButtons);
1019 if (actionButtonsArrayObj == nullptr
1020 || !SetPropertyByRef(env, object, "actionButtons", actionButtonsArrayObj)) {
1021 ANS_LOGD("SetNotificationRequest set 'actionButtons' faild");
1022 }
1023 return true;
1024 }
1025
SetRequestTemplate(ani_env * env,const OHOS::Notification::NotificationRequest * request,ani_object & object)1026 bool SetRequestTemplate(ani_env *env, const OHOS::Notification::NotificationRequest *request, ani_object &object)
1027 {
1028 std::shared_ptr<NotificationTemplate> templ = request->GetTemplate();
1029 if (templ == nullptr) {
1030 ANS_LOGD("template is Undefine");
1031 return true;
1032 }
1033 ani_object templateObject = WrapNotificationTemplate(env, templ);
1034 if (templateObject == nullptr || !SetPropertyByRef(env, object, "template", templateObject)) {
1035 ANS_LOGD("SetNotificationRequest set 'template' faild");
1036 }
1037 return true;
1038 }
1039
SetRequestNotificationFlags(ani_env * env,const OHOS::Notification::NotificationRequest * request,ani_object & object)1040 bool SetRequestNotificationFlags(
1041 ani_env *env, const OHOS::Notification::NotificationRequest *request, ani_object &object)
1042 {
1043 std::shared_ptr<NotificationFlags> flags = request->GetFlags();
1044 if (flags == nullptr) {
1045 ANS_LOGD("notificationFlags is Undefine");
1046 return true;
1047 }
1048 ani_object flagsObject = nullptr;
1049 if (!WarpNotificationFlags(env, flags, flagsObject) || flagsObject == nullptr) {
1050 ANS_LOGE("SetNotificationRequest Warp 'notificationFlags' faild");
1051 return false;
1052 }
1053 if (!SetPropertyByRef(env, object, "notificationFlags", flagsObject)) {
1054 ANS_LOGE("SetNotificationRequest set 'notificationFlags' faild");
1055 return false;
1056 }
1057 return true;
1058 }
1059
SetRequestAgentBundle(ani_env * env,const OHOS::Notification::NotificationRequest * request,ani_object & object)1060 bool SetRequestAgentBundle(ani_env *env, const OHOS::Notification::NotificationRequest *request, ani_object &object)
1061 {
1062 std::shared_ptr<NotificationBundleOption> agentBundle = request->GetAgentBundle();
1063 if (agentBundle == nullptr) {
1064 ANS_LOGD("agentBundle is Undefine");
1065 return true;
1066 }
1067 ani_object agentBundleObject = nullptr;
1068 if (!WrapBundleOption(env, agentBundle, agentBundleObject) || agentBundleObject == nullptr) {
1069 ANS_LOGE("SetNotificationRequest Warp 'agentBundle' faild");
1070 return false;
1071 }
1072 if (!SetPropertyByRef(env, object, "agentBundle", agentBundleObject)) {
1073 ANS_LOGE("SetNotificationRequest set 'agentBundle' faild");
1074 return false;
1075 }
1076 return true;
1077 }
1078
SetRequestUnifiedGroupInfo(ani_env * env,const OHOS::Notification::NotificationRequest * request,ani_object & object)1079 bool SetRequestUnifiedGroupInfo(
1080 ani_env *env, const OHOS::Notification::NotificationRequest *request, ani_object &object)
1081 {
1082 std::shared_ptr<NotificationUnifiedGroupInfo> groupInfo = request->GetUnifiedGroupInfo();
1083 if (groupInfo == nullptr) {
1084 ANS_LOGD("unifiedGroupInfo is Undefine");
1085 return true;
1086 }
1087 ani_object infoObject = nullptr;
1088 if (!WarpNotificationUnifiedGroupInfo(env, groupInfo, infoObject) || infoObject == nullptr) {
1089 ANS_LOGD("SetNotificationRequest Warp 'unifiedGroupInfo' faild");
1090 }
1091 if (!SetPropertyByRef(env, object, "unifiedGroupInfo", infoObject)) {
1092 ANS_LOGD("SetNotificationRequest set 'unifiedGroupInfo' faild");
1093 }
1094 return true;
1095 }
1096
SetNotificationRequestByCustom(ani_env * env,ani_class cls,const OHOS::Notification::NotificationRequest * request,ani_object & object)1097 bool SetNotificationRequestByCustom(ani_env* env, ani_class cls,
1098 const OHOS::Notification::NotificationRequest *request, ani_object &object)
1099 {
1100 if (request == nullptr) {
1101 ANS_LOGE("request is nullptr");
1102 return false;
1103 }
1104 // content: NotificationContent
1105 if (!SetNotificationRequestByNotificationContent(env, cls, request, object)) {
1106 ANS_LOGE("SetNotificationRequestByCustom: set content failed");
1107 return false;
1108 }
1109 // extraInfo?: Record<string, Object>
1110 if (!SetRequestExtraInfo(env, request, object)) {
1111 ANS_LOGE("set extraInfo faild");
1112 }
1113 // extendInfo?: Record<string, Object>
1114 if (!SetRequestExtendInfo(env, request, object)) {
1115 ANS_LOGE("set extendInfo faild");
1116 }
1117
1118 // actionButtons?: Array<NotificationActionButton>
1119 if (!SetRequestActionButtons(env, request, object)) {
1120 ANS_LOGD("set actionButtons faild");
1121 }
1122 // template?: NotificationTemplate
1123 if (!SetRequestTemplate(env, request, object)) {
1124 ANS_LOGD("set template faild");
1125 }
1126 // readonly notificationFlags?: NotificationFlags
1127 if (!SetRequestNotificationFlags(env, request, object)) {
1128 ANS_LOGD("set notificationFlags faild");
1129 }
1130 // readonly agentBundle?: agentBundle
1131 if (!SetRequestAgentBundle(env, request, object)) {
1132 ANS_LOGD("set agentBundle faild");
1133 }
1134 // unifiedGroupInfo?: unifiedGroupInfo
1135 if (!SetRequestUnifiedGroupInfo(env, request, object)) {
1136 ANS_LOGD("set unifiedGroupInfo faild");
1137 }
1138 return true;
1139 }
1140
WarpNotificationRequest(ani_env * env,const OHOS::Notification::NotificationRequest * notificationRequest,ani_class & cls,ani_object & outAniObj)1141 bool WarpNotificationRequest(ani_env *env, const OHOS::Notification::NotificationRequest *notificationRequest,
1142 ani_class &cls, ani_object &outAniObj)
1143 {
1144 ANS_LOGD("WarpNotificationRequest start");
1145 if (notificationRequest == nullptr) {
1146 ANS_LOGE("notification is null");
1147 return false;
1148 }
1149 if (!CreateClassObjByClassName(env,
1150 "Lnotification/notificationRequest/NotificationRequestInner;", cls, outAniObj)) {
1151 ANS_LOGE("WarpNotificationRequest: create class failed");
1152 return false;
1153 }
1154 if (!SetNotificationRequestByBool(env, cls, notificationRequest, outAniObj)) {
1155 ANS_LOGE("WarpNotificationRequest: set bools failed");
1156 return false;
1157 }
1158 if (!SetNotificationRequestByString(env, cls, notificationRequest, outAniObj)) {
1159 ANS_LOGE("WarpNotificationRequest: set strings failed");
1160 return false;
1161 }
1162 if (!SetNotificationRequestByNumber(env, cls, notificationRequest, outAniObj)) {
1163 ANS_LOGE("WarpNotificationRequest: set numbers failed");
1164 return false;
1165 }
1166 if (!SetNotificationRequestByWantAgent(env, cls, notificationRequest, outAniObj)) {
1167 ANS_LOGE("WarpNotificationRequest: set WantAgent failed");
1168 return false;
1169 }
1170 if (!SetNotificationRequestByPixelMap(env, cls, notificationRequest, outAniObj)) {
1171 ANS_LOGE("WarpNotificationRequest: set PixelMap failed");
1172 return false;
1173 }
1174 if (!SetNotificationRequestByCustom(env, cls, notificationRequest, outAniObj)) {
1175 ANS_LOGE("WarpNotificationRequest: set Customs failed");
1176 return false;
1177 }
1178 ANS_LOGD("WarpNotificationRequest end");
1179 return true;
1180 }
1181
GetAniNotificationRequestArray(ani_env * env,std::vector<sptr<NotificationRequest>> requests)1182 ani_object GetAniNotificationRequestArray(ani_env *env, std::vector<sptr<NotificationRequest>> requests)
1183 {
1184 ani_object arrayObj = newArrayClass(env, requests.size());
1185 if (arrayObj == nullptr) {
1186 ANS_LOGE("arrayObj is nullptr");
1187 return nullptr;
1188 }
1189 ani_size index = 0;
1190 for (auto &request : requests) {
1191 ani_class requestCls;
1192 ani_object requestObj;
1193 if (!WarpNotificationRequest(env, request.GetRefPtr(), requestCls, requestObj) || requestObj == nullptr) {
1194 ANS_LOGE("WarpNotificationRequest faild. index %{public}zu", index);
1195 return nullptr;
1196 }
1197 if (ANI_OK != env->Object_CallMethodByName_Void(
1198 arrayObj, "$_set", "ILstd/core/Object;:V", index, requestObj)) {
1199 ANS_LOGE("Object_CallMethodByName_Void faild. index %{public}zu", index);
1200 return nullptr;
1201 }
1202 index ++;
1203 }
1204 return arrayObj;
1205 }
1206
GetAniNotificationRequestArrayByNotifocations(ani_env * env,std::vector<sptr<NotificationSts>> requests)1207 ani_object GetAniNotificationRequestArrayByNotifocations(ani_env *env, std::vector<sptr<NotificationSts>> requests)
1208 {
1209 ani_object arrayObj = newArrayClass(env, requests.size());
1210 if (arrayObj == nullptr) {
1211 ANS_LOGE("arrayObj is nullptr");
1212 return nullptr;
1213 }
1214 ani_size index = 0;
1215 for (auto &request : requests) {
1216 ani_class requestCls;
1217 ani_object requestObj;
1218 if (!WarpNotificationRequest(
1219 env, request->GetNotificationRequestPoint().GetRefPtr(), requestCls, requestObj)
1220 || requestObj == nullptr) {
1221 ANS_LOGE("WarpNotificationRequest faild. index %{public}zu", index);
1222 return nullptr;
1223 }
1224 if (ANI_OK
1225 != env->Object_CallMethodByName_Void(arrayObj, "$_set", "ILstd/core/Object;:V", index, requestObj)) {
1226 ANS_LOGE("Object_CallMethodByName_Void faild. index %{public}zu", index);
1227 return nullptr;
1228 }
1229 index ++;
1230 }
1231 return arrayObj;
1232 }
1233
GetCheckRequestContent(ani_env * env,ani_object obj,NotificationContent::Type & outContentType)1234 bool GetCheckRequestContent(ani_env *env, ani_object obj, NotificationContent::Type &outContentType)
1235 {
1236 ani_status status = ANI_OK;
1237 ani_ref contentAniType;
1238 STSContentType contentType = NOTIFICATION_CONTENT_BASIC_TEXT;
1239 if (ANI_OK != (status = env->Object_GetPropertyByName_Ref(obj, "contentType", &contentAniType))) {
1240 ANS_LOGE("GetCheckRequestContent get contentType faild. status %{public}d", status);
1241 return false;
1242 }
1243 if (contentAniType == nullptr ||
1244 !EnumConvertAniToNative(env, static_cast<ani_enum_item>(contentAniType), contentType)) {
1245 ANS_LOGE("EnumConvertAniToNative contentType faild");
1246 return false;
1247 }
1248 if (!StsContentTypeUtils::StsToC(contentType, outContentType)) {
1249 ANS_LOGE("StsToC contentType faild");
1250 return false;
1251 }
1252 return true;
1253 }
1254
GetCheckRequestSlotType(ani_env * env,ani_object obj,NotificationConstant::SlotType & outSlotType)1255 bool GetCheckRequestSlotType(ani_env *env, ani_object obj, NotificationConstant::SlotType &outSlotType)
1256 {
1257 ani_status status = ANI_OK;
1258 ani_ref slotAniType;
1259 STSSlotType slotType = UNKNOWN_TYPE;
1260 if (ANI_OK != (status = env->Object_GetPropertyByName_Ref(obj, "slotType", &slotAniType))) {
1261 ANS_LOGE("UnWarpNotificationCheckRequest get slotType faild. status %{public}d", status);
1262 return false;
1263 }
1264 if (slotAniType == nullptr || !EnumConvertAniToNative(env, static_cast<ani_enum_item>(slotAniType), slotType)) {
1265 ANS_LOGE("EnumConvertAniToNative slotType faild");
1266 return false;
1267 }
1268 if (!StsSlotTypeUtils::StsToC(slotType, outSlotType)) {
1269 ANS_LOGE("StsToC slotType faild");
1270 return false;
1271 }
1272 return true;
1273 }
1274
UnWarpNotificationCheckRequest(ani_env * env,ani_object obj,sptr<NotificationCheckRequest> & checkRequest)1275 bool UnWarpNotificationCheckRequest(ani_env *env, ani_object obj, sptr<NotificationCheckRequest> &checkRequest)
1276 {
1277 if (env == nullptr || obj == nullptr || checkRequest == nullptr) {
1278 ANS_LOGE("UnWarpNotificationCheckRequest invalid parameters");
1279 return false;
1280 }
1281 ani_status status = ANI_OK;
1282 ani_ref extraInfoKeysObj;
1283 NotificationContent::Type outContentType = NotificationContent::Type::NONE;
1284 NotificationConstant::SlotType outSlotType = NotificationConstant::SlotType::OTHER;
1285 std::vector<std::string> extraInfoKeys;
1286 // contentType: notificationManager.ContentType;
1287 if (!GetCheckRequestContent(env, obj, outContentType)) {
1288 ANS_LOGE("GetCheckRequestContent faild.");
1289 return false;
1290 }
1291 checkRequest->SetContentType(outContentType);
1292 // slotType: notificationManager.SlotType;
1293 if (!GetCheckRequestSlotType(env, obj, outSlotType)) {
1294 ANS_LOGE("GetCheckRequestSlotType faild.");
1295 return false;
1296 }
1297 checkRequest->SetSlotType(outSlotType);
1298 // extraInfoKeys: Array<string>;
1299 if (ANI_OK != (status = env->Object_GetPropertyByName_Ref(obj, "extraInfoKeys", &extraInfoKeysObj))) {
1300 ANS_LOGE("UnWarpNotificationCheckRequest get extraInfoKeys faild. status %{public}d", status);
1301 return false;
1302 }
1303 if (!GetStringArrayByAniObj(env, static_cast<ani_object>(extraInfoKeysObj), extraInfoKeys)) {
1304 ANS_LOGE("UnWarpNotificationCheckRequest. extraInfoKeys GetStringArrayByAniObj faild.");
1305 return false;
1306 }
1307 checkRequest->SetExtraKeys(extraInfoKeys);
1308 ANS_LOGD("contentType %{public}d slotType %{public}d",
1309 checkRequest->GetContentType(), checkRequest->GetSlotType());
1310 for (auto &it : checkRequest->GetExtraKeys()) {
1311 ANS_LOGD("extrakey %{public}s", it.c_str());
1312 }
1313 return true;
1314 }
1315
UnWarpNotificationFilter(ani_env * env,ani_object obj,LiveViewFilter & filter)1316 bool UnWarpNotificationFilter(ani_env *env, ani_object obj, LiveViewFilter& filter)
1317 {
1318 ANS_LOGD("UnWarpNotificationFilter call");
1319 if (env == nullptr || obj == nullptr) {
1320 ANS_LOGE("UnWarpNotificationFilter failed, has nullptr");
1321 return false;
1322 }
1323
1324 ani_status status = ANI_OK;
1325 ani_boolean isUndefined = ANI_TRUE;
1326 ani_ref bundleObj = {};
1327 if (ANI_OK != (status = GetPropertyRef(env, obj, "bundle", isUndefined, bundleObj))
1328 || isUndefined == ANI_TRUE) {
1329 ANS_LOGE("UnWarpNotificationFilter:get bundle failed. status %{public}d", status);
1330 return false;
1331 }
1332 if (!OHOS::NotificationSts::UnwrapBundleOption(env, static_cast<ani_object>(bundleObj), filter.bundle)) {
1333 ANS_LOGE("UnWarpNotificationFilter:UnwrapBundleOption failed");
1334 return false;
1335 }
1336
1337 ani_ref notificationKeyObj = {};
1338 if (ANI_OK != (status = GetPropertyRef(env, obj, "notificationKey", isUndefined, notificationKeyObj))
1339 || isUndefined == ANI_TRUE) {
1340 ANS_LOGE("UnWarpNotificationFilter:get notificationKey failed. status %{public}d", status);
1341 return false;
1342 }
1343
1344 if (OHOS::NotificationSts::UnWarpNotificationKey(env, static_cast<ani_object>(notificationKeyObj),
1345 filter.notificationKey)) {
1346 ANS_LOGD("UnWarpNotificationFilter:UnWarpNotificationKey label is undefined");
1347 }
1348
1349 if (ANI_OK != (status = GetPropertyStringArray(env, obj, "extraInfoKeys", isUndefined, filter.extraInfoKeys))) {
1350 ANS_LOGD("UnWarpNotificationFilter:get extraInfoKeysObj failed. status %{public}d", status);
1351 }
1352 return true;
1353 }
1354 } // namespace NotificationSts
1355 } // OHOS