• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_remove.h"
16 
17 #include "notification_helper.h"
18 #include "ans_log_wrapper.h"
19 #include "sts_throw_erro.h"
20 #include "sts_common.h"
21 #include "notification_request.h"
22 #include "sts_bundle_option.h"
23 #include "sts_subscribe.h"
24 
25 namespace OHOS {
26 namespace NotificationSubScribeSts {
27 using namespace OHOS::Notification;
28 
AniRemoveForBundle(ani_env * env,ani_object bundle,ani_object notificationKey,ani_object reasonEnum)29 void AniRemoveForBundle(ani_env *env, ani_object bundle, ani_object notificationKey, ani_object reasonEnum)
30 {
31     ANS_LOGD("AniRemoveForBundle enter");
32     BundleOption option;
33     NotificationKey key;
34     int32_t reasonType = 0;
35     if (!NotificationSts::UnwrapBundleOption(env, bundle, option)) {
36         ANS_LOGE("bundle is valid");
37         std::string msg = "UnwrapBundleOption failed";
38         OHOS::NotificationSts::ThrowError(env, ERROR_PARAM_INVALID, msg);
39         return;
40     }
41     if (!NotificationSts::UnWarpNotificationKey(env, notificationKey, key)) {
42         ANS_LOGE("notificationKey is valid");
43         std::string msg = "UnWarpNotificationKey failed";
44         OHOS::NotificationSts::ThrowError(env, ERROR_PARAM_INVALID, msg);
45         return;
46     }
47     if (!NotificationSts::UnWarpReasonEnum(env, reasonEnum, reasonType)) {
48         ANS_LOGE("enum convert failed");
49         std::string msg = "UnWarpReasonEnum failed";
50         OHOS::NotificationSts::ThrowError(env, ERROR_PARAM_INVALID, msg);
51         return;
52     }
53     if (!NotificationSts::IsValidRemoveReason(reasonType)) {
54         ANS_LOGE("reasonType is valid");
55         std::string msg = "reasonType is valid";
56         OHOS::NotificationSts::ThrowError(env, ERROR_PARAM_INVALID, msg);
57         return;
58     }
59     int ret = NotificationHelper::RemoveNotification(option, key.id, key.label, reasonType);
60     if (ret != ERR_OK) {
61         int32_t externalErrorCode = NotificationSts::GetExternalCode(ret);
62         ANS_LOGD("StsRemoveForBundle ret %{public}d. ErrorToExternal %{public}d", ret, externalErrorCode);
63         std::string msg = OHOS::NotificationSts::FindAnsErrMsg(externalErrorCode);
64         OHOS::NotificationSts::ThrowError(env, externalErrorCode, msg);
65     }
66 }
67 
AniRemoveForHashCode(ani_env * env,ani_string hashCode,ani_object reasonEnum)68 void AniRemoveForHashCode(ani_env *env, ani_string hashCode, ani_object reasonEnum)
69 {
70     ANS_LOGD("AniRemoveForHashCode enter");
71     int32_t reasonType = -1;
72     std::string tempStr;
73     if (ANI_OK != NotificationSts::GetStringByAniString(env, hashCode, tempStr)) {
74         ANS_LOGE("hashCode is valid");
75         std::string msg = "hashCode is valid";
76         OHOS::NotificationSts::ThrowError(env, ERROR_PARAM_INVALID, msg);
77         return;
78     }
79     std::string hashCodeStd = NotificationSts::GetResizeStr(tempStr, NotificationSts::STR_MAX_SIZE);
80     if (!NotificationSts::UnWarpReasonEnum(env, reasonEnum, reasonType)) {
81         ANS_LOGE("enum convert failed");
82         std::string msg = "UnWarpReasonEnum failed";
83         OHOS::NotificationSts::ThrowError(env, ERROR_PARAM_INVALID, msg);
84         return;
85     }
86     if (!NotificationSts::IsValidRemoveReason(reasonType)) {
87         ANS_LOGE("reasonType is valid");
88         std::string msg = "reasonType is valid";
89         OHOS::NotificationSts::ThrowError(env, ERROR_PARAM_INVALID, msg);
90         return;
91     }
92     ANS_LOGD("hashCode: %{public}s, reasonType: %{public}d", hashCodeStd.c_str(), reasonType);
93     int ret = NotificationHelper::RemoveNotification(hashCodeStd, reasonType);
94     if (ret != ERR_OK) {
95         int32_t externalErrorCode = NotificationSts::GetExternalCode(ret);
96         ANS_LOGD("StsRemoveForHashCode ret %{public}d. ErrorToExternal %{public}d", ret, externalErrorCode);
97         std::string msg = OHOS::NotificationSts::FindAnsErrMsg(externalErrorCode);
98         OHOS::NotificationSts::ThrowError(env, externalErrorCode, msg);
99     }
100 }
101 
AniRemoveForHashCodes(ani_env * env,ani_object hashCodes,ani_object reasonEnum)102 void AniRemoveForHashCodes(ani_env *env, ani_object hashCodes, ani_object reasonEnum)
103 {
104     ANS_LOGD("StsRemoveForHashCodes enter");
105     std::vector<std::string> hashCodesTemp = {};
106     int32_t reasonType;
107     if (!NotificationSts::GetStringArrayByAniObj(env, hashCodes, hashCodesTemp)) {
108         ANS_LOGE("hashCodes is valid");
109         std::string msg = "hashCodes is valid";
110         OHOS::NotificationSts::ThrowError(env, ERROR_PARAM_INVALID, msg);
111         return;
112     }
113     std::vector<std::string> hashCodesStd = {};
114     for (auto hashcode : hashCodesStd) {
115         hashCodesStd.emplace_back(NotificationSts::GetResizeStr(hashcode, NotificationSts::STR_MAX_SIZE));
116     }
117     if (!NotificationSts::UnWarpReasonEnum(env, reasonEnum, reasonType)) {
118         ANS_LOGE("enum convert failed");
119         std::string msg = "UnWarpReasonEnum failed";
120         OHOS::NotificationSts::ThrowError(env, ERROR_PARAM_INVALID, msg);
121         return;
122     }
123     if (!NotificationSts::IsValidRemoveReason(reasonType)) {
124         ANS_LOGE("reasonType is valid");
125         std::string msg = "reasonType is valid";
126         OHOS::NotificationSts::ThrowError(env, ERROR_PARAM_INVALID, msg);
127         return;
128     }
129     int ret = NotificationHelper::RemoveNotifications(hashCodesStd, reasonType);
130     if (ret != ERR_OK) {
131         int32_t externalErrorCode = NotificationSts::GetExternalCode(ret);
132         ANS_LOGD("StsRemoveForHashCodes ret %{public}d. ErrorToExternal %{public}d", ret, externalErrorCode);
133         std::string msg = OHOS::NotificationSts::FindAnsErrMsg(externalErrorCode);
134         OHOS::NotificationSts::ThrowError(env, externalErrorCode, msg);
135     }
136 }
137 
AniRemoveAll(ani_env * env)138 void AniRemoveAll(ani_env *env)
139 {
140     ANS_LOGD("removeAll enter");
141     int ret = NotificationHelper::RemoveNotifications();
142     if (ret != ERR_OK) {
143         int32_t externalErrorCode = NotificationSts::GetExternalCode(ret);
144         ANS_LOGD("AniRemoveAll ret %{public}d. ErrorToExternal %{public}d", ret, externalErrorCode);
145         std::string msg = OHOS::NotificationSts::FindAnsErrMsg(externalErrorCode);
146         OHOS::NotificationSts::ThrowError(env, externalErrorCode, msg);
147     }
148 }
149 
AniRemoveAllForBundle(ani_env * env,ani_object bundle)150 void AniRemoveAllForBundle(ani_env *env, ani_object bundle)
151 {
152     ANS_LOGD("AniRemoveAllForBundle enter");
153     BundleOption option;
154     if (!NotificationSts::UnwrapBundleOption(env, bundle, option)) {
155         ANS_LOGE("bundle is valid");
156         std::string msg = "UnwrapBundleOption failed";
157         OHOS::NotificationSts::ThrowError(env, ERROR_PARAM_INVALID, msg);
158         return;
159     }
160     int ret = NotificationHelper::RemoveAllNotifications(option);
161     if (ret != ERR_OK) {
162         int32_t externalErrorCode = NotificationSts::GetExternalCode(ret);
163         ANS_LOGD("StsRemoveForBundle ret %{public}d. ErrorToExternal %{public}d", ret, externalErrorCode);
164         std::string msg = OHOS::NotificationSts::FindAnsErrMsg(externalErrorCode);
165         OHOS::NotificationSts::ThrowError(env, externalErrorCode, msg);
166     }
167 }
168 
AniRemoveAllForUserId(ani_env * env,ani_double userId)169 void AniRemoveAllForUserId(ani_env *env, ani_double userId)
170 {
171     ANS_LOGD("AniRemoveAllForUserId enter");
172     int ret = NotificationHelper::RemoveNotifications(userId);
173     if (ret != ERR_OK) {
174         int32_t externalErrorCode = NotificationSts::GetExternalCode(ret);
175         ANS_LOGD("StsRemoveForBundle ret %{public}d. ErrorToExternal %{public}d", ret, externalErrorCode);
176         std::string msg = OHOS::NotificationSts::FindAnsErrMsg(externalErrorCode);
177         OHOS::NotificationSts::ThrowError(env, externalErrorCode, msg);
178     }
179 }
180 }
181 }
182