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 "ani_get_active.h"
16
17 #include "notification_helper.h"
18 #include "ans_log_wrapper.h"
19 #include "sts_throw_erro.h"
20 #include "sts_request.h"
21 #include "sts_common.h"
22
23 namespace OHOS {
24 namespace NotificationManagerSts {
AniGetActiveNotificationCount(ani_env * env)25 ani_double AniGetActiveNotificationCount(ani_env *env)
26 {
27 ANS_LOGD("sts GetActiveNotificationCount call");
28 uint64_t num = 0;
29 int returncode = OHOS::Notification::NotificationHelper::GetActiveNotificationNums(num);
30 ANS_LOGD("sts GetActiveNotificationCount end, num: %{public}" PRIu64, num);
31 ani_double retNum = static_cast<ani_double>(num);
32 if (returncode != ERR_OK) {
33 int externalCode = NotificationSts::GetExternalCode(returncode);
34 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
35 ANS_LOGE("AniSetNotificationEnableSlotSync error, errorCode: %{public}d", externalCode);
36 return 0;
37 }
38 return retNum;
39 }
40
AniGetAllActiveNotifications(ani_env * env)41 ani_object AniGetAllActiveNotifications(ani_env *env)
42 {
43 ANS_LOGD("sts AniGetAllActiveNotifications call");
44 std::vector<sptr<NotificationSts::NotificationSts>> notifications;
45 int returncode = OHOS::Notification::NotificationHelper::GetAllActiveNotifications(notifications);
46 if (returncode != ERR_OK) {
47 int externalCode = NotificationSts::GetExternalCode(returncode);
48 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
49 ANS_LOGE("AniGetAllActiveNotifications error, errorCode: %{public}d", externalCode);
50 return nullptr;
51 }
52 ani_object arrayRequestObj;
53 if (notifications.size() == 0) {
54 arrayRequestObj = NotificationSts::newArrayClass(env, 0);
55 } else {
56 arrayRequestObj = NotificationSts::GetAniNotificationRequestArrayByNotifocations(env, notifications);
57 }
58 if (arrayRequestObj == nullptr) {
59 OHOS::NotificationSts::ThrowError(env, OHOS::Notification::ERROR_INTERNAL_ERROR,
60 NotificationSts::FindAnsErrMsg(OHOS::Notification::ERROR_INTERNAL_ERROR));
61 ANS_LOGE("AniGetAllActiveNotifications ERROR_INTERNAL_ERROR");
62 }
63 ANS_LOGD("sts AniGetAllActiveNotifications end");
64 return arrayRequestObj;
65 }
66
AniGetActiveNotifications(ani_env * env)67 ani_object AniGetActiveNotifications(ani_env *env)
68 {
69 ANS_LOGD("sts AniGetActiveNotifications call");
70 std::vector<sptr<NotificationSts::NotificationRequest>> requests;
71 int returncode = OHOS::Notification::NotificationHelper::GetActiveNotifications(requests);
72 if (returncode != ERR_OK) {
73 int externalCode = NotificationSts::GetExternalCode(returncode);
74 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
75 ANS_LOGE("AniGetActiveNotifications error, errorCode: %{public}d", externalCode);
76 return nullptr;
77 }
78 ani_object arrayRequestObj;
79 if (requests.size() == 0) {
80 arrayRequestObj = NotificationSts::newArrayClass(env, 0);
81 } else {
82 arrayRequestObj = NotificationSts::GetAniNotificationRequestArray(env, requests);
83 }
84 if (arrayRequestObj == nullptr) {
85 OHOS::NotificationSts::ThrowError(env, OHOS::Notification::ERROR_INTERNAL_ERROR,
86 NotificationSts::FindAnsErrMsg(OHOS::Notification::ERROR_INTERNAL_ERROR));
87 ANS_LOGE("AniGetActiveNotifications ERROR_INTERNAL_ERROR");
88 }
89 ANS_LOGD("sts AniGetActiveNotifications end");
90 return arrayRequestObj;
91 }
92
AniGetActiveNotificationByFilter(ani_env * env,ani_object obj)93 ani_object AniGetActiveNotificationByFilter(ani_env *env, ani_object obj)
94 {
95 ANS_LOGD("AniGetActiveNotificationByFilter call");
96 Notification::LiveViewFilter filter;
97 if (!OHOS::NotificationSts::UnWarpNotificationFilter(env, obj, filter)) {
98 NotificationSts::ThrowErrorWithMsg(env, "sts UnWarpNotificationFilter ERROR_INTERNAL_ERROR");
99 return nullptr;
100 }
101 sptr<OHOS::Notification::NotificationRequest> notificationRequest = nullptr;
102 int returncode = Notification::NotificationHelper::GetActiveNotificationByFilter(filter, notificationRequest);
103 if (returncode != ERR_OK) {
104 int externalCode = NotificationSts::GetExternalCode(returncode);
105 ANS_LOGE("AniGetActiveNotificationByFilter -> error, errorCode: %{public}d", externalCode);
106 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
107 return nullptr;
108 }
109
110 ani_object requestObj = nullptr;
111 ani_class requestCls;
112 if (!NotificationSts::WarpNotificationRequest(env, notificationRequest.GetRefPtr(), requestCls, requestObj)
113 || requestObj == nullptr) {
114 NotificationSts::ThrowErrorWithMsg(env, "sts UnWarpNotificationFilter ERROR_INTERNAL_ERROR");
115 ANS_LOGE("AniGetActiveNotificationByFilter WarpNotificationRequest faild");
116 return nullptr;
117 }
118 ANS_LOGD("AniGetActiveNotificationByFilter end");
119 return requestObj;
120 }
121 } // namespace NotificationManagerSts
122 } // namespace OHOS