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_cance.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 "sts_bundle_option.h"
22
23 namespace OHOS {
24 namespace NotificationManagerSts {
AniCancelAll(ani_env * env)25 void AniCancelAll(ani_env* env)
26 {
27 ANS_LOGD("AniCancelAll notifications call");
28 int returncode = Notification::NotificationHelper::CancelAllNotifications();
29 if (returncode != ERR_OK) {
30 int externalCode = NotificationSts::GetExternalCode(returncode);
31 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
32 ANS_LOGE("AniCancelAll -> error, errorCode: %{public}d", externalCode);
33 }
34 ANS_LOGD("AniCancelAll notifications end");
35 }
36
AniCancelWithId(ani_env * env,ani_double id)37 void AniCancelWithId(ani_env* env, ani_double id)
38 {
39 ANS_LOGD("AniCancelWithId call,id : %{public}lf", id);
40 int returncode = Notification::NotificationHelper::CancelNotification(static_cast<int32_t>(id));
41 if (returncode != ERR_OK) {
42 int externalCode = NotificationSts::GetExternalCode(returncode);
43 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
44 ANS_LOGE("AniCancelWithId -> error, errorCode: %{public}d", externalCode);
45 }
46 ANS_LOGD("AniCancelWithId notifications end");
47 }
48
AniCancelWithIdLabel(ani_env * env,ani_double id,ani_string label)49 void AniCancelWithIdLabel(ani_env* env, ani_double id, ani_string label)
50 {
51 ANS_LOGD("AniCancelWithIdLabel call");
52 std::string tempStr;
53 if (ANI_OK != NotificationSts::GetStringByAniString(env, label, tempStr)) {
54 NotificationSts::ThrowErrorWithMsg(env, "Label parse failed!");
55 return;
56 }
57 std::string labelStr = NotificationSts::GetResizeStr(tempStr, NotificationSts::STR_MAX_SIZE);
58 ANS_LOGD("Cancel by label id:%{public}lf label:%{public}s", id, labelStr.c_str());
59 int returncode = Notification::NotificationHelper::CancelNotification(labelStr, static_cast<int32_t>(id));
60 if (returncode != ERR_OK) {
61 int externalCode = NotificationSts::GetExternalCode(returncode);
62 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
63 ANS_LOGE("AniCancelWithIdLabel -> error, errorCode: %{public}d", externalCode);
64 }
65 ANS_LOGD("AniCancelWithIdLabel end");
66 }
67
AniCancelWithBundle(ani_env * env,ani_object bundleObj,ani_double id)68 void AniCancelWithBundle(ani_env* env, ani_object bundleObj, ani_double id)
69 {
70 ANS_LOGD("AniCancelWithBundle call");
71 Notification::NotificationBundleOption option;
72 if (!NotificationSts::UnwrapBundleOption(env, bundleObj, option)) {
73 NotificationSts::ThrowErrorWithMsg(env, "BundleOption parse failed!");
74 return;
75 }
76
77 ANS_LOGD("Cancel by bundle:%{public}s id:%{public}lf",
78 option.GetBundleName().c_str(), id);
79 int returncode = Notification::NotificationHelper::CancelAsBundle(option, static_cast<int32_t>(id));
80 if (returncode != ERR_OK) {
81 int externalCode = NotificationSts::GetExternalCode(returncode);
82 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
83 ANS_LOGE("AniCancelWithBundle -> error, errorCode: %{public}d", externalCode);
84 }
85 ANS_LOGD("AniCancelWithBundle end");
86 }
87
AniCancelWithIdOptinalLabel(ani_env * env,ani_double id,ani_string label)88 void AniCancelWithIdOptinalLabel(ani_env* env, ani_double id, ani_string label)
89 {
90 ANS_LOGD("sts AniCancelWithIdOptinalLabel call, id:%{public}lf", id);
91 ani_boolean isUndefined = ANI_FALSE;
92 env->Reference_IsUndefined(label, &isUndefined);
93 int32_t ret = -1;
94 if (isUndefined) {
95 ANS_LOGE("sts AniCancelWithIdOptinalLabel the label is undefined");
96 ret = Notification::NotificationHelper::CancelNotification(static_cast<int32_t>(id));
97 } else {
98 std::string tempStr;
99 if (ANI_OK != NotificationSts::GetStringByAniString(env, label, tempStr)) {
100 OHOS::NotificationSts::ThrowError(env, OHOS::Notification::ERROR_INTERNAL_ERROR,
101 NotificationSts::FindAnsErrMsg(OHOS::Notification::ERROR_INTERNAL_ERROR));
102 ANS_LOGE("sts AniCancelWithIdOptinalLabel ERROR_INTERNAL_ERROR");
103 return;
104 }
105 std::string labelStr = NotificationSts::GetResizeStr(tempStr, NotificationSts::STR_MAX_SIZE);
106 ANS_LOGD("sts AniCancelWithIdOptinalLabel id:%{public}lf label:%{public}s", id, labelStr.c_str());
107 ret = Notification::NotificationHelper::CancelNotification(labelStr, id);
108 }
109 if (ret != ERR_OK) {
110 int externalCode = NotificationSts::GetExternalCode(ret);
111 ANS_LOGE("sts AniCancelWithIdOptinalLabel error, errorCode: %{public}d", externalCode);
112 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
113 return;
114 }
115 ANS_LOGD("sts AniCancelWithIdOptinalLabel end");
116 }
117
AniCancelAsBundle(ani_env * env,ani_double id,ani_string representativeBundle,ani_double userId)118 void AniCancelAsBundle(ani_env *env, ani_double id, ani_string representativeBundle, ani_double userId)
119 {
120 ANS_LOGD("AniCancelAsBundle enter");
121 int32_t convertedId = static_cast<int32_t>(id);
122 int32_t UserId = static_cast<int32_t>(userId);
123 std::string bundleStr;
124
125 if (ANI_OK != NotificationSts::GetStringByAniString(env, representativeBundle, bundleStr)) {
126 ANS_LOGE("AniCancelAsBundle:: representativeBundle parse failed!");
127 NotificationSts::ThrowErrorWithMsg(env, "representativeBundle parse failed!");
128 return;
129 }
130 ANS_LOGD("AniCancelAsBundle, convertedId: %{public}d, UserId: %{public}d, bundleStr: %{public}s",
131 convertedId, UserId, bundleStr.c_str());
132
133 int returncode = Notification::NotificationHelper::CancelAsBundle(convertedId, bundleStr, UserId);
134 if (returncode != ERR_OK) {
135 int externalCode = NotificationSts::GetExternalCode(returncode);
136 ANS_LOGE("AniCancelAsBundle: CancelAsBundle retern erro. returncode: %{public}d, externalCode: %{public}d",
137 returncode, externalCode);
138 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
139 }
140
141 ANS_LOGD("AniCancelAsBundle end");
142 }
143
AniCancelAsBundleWithBundleOption(ani_env * env,ani_object representativeBundle,ani_double id)144 void AniCancelAsBundleWithBundleOption(ani_env *env, ani_object representativeBundle, ani_double id)
145 {
146 ANS_LOGD("AniCancelAsBundleWithBundleOption enter");
147 int32_t idTest = static_cast<int32_t>(id);
148 BundleOption option;
149 if (NotificationSts::UnwrapBundleOption(env, representativeBundle, option) != true) {
150 ANS_LOGE("AniPublishAsBundleWithBundleOption BundleOption parse failed!");
151 NotificationSts::ThrowErrorWithMsg(env, "AniPublishAsBundleWithBundleOption BundleOption parse failed!");
152 return;
153 }
154
155 ANS_LOGD("AniPublishAsBundleWithBundleOption: bundle %{public}s, uid: %{public}d, id: %{public}d",
156 option.GetBundleName().c_str(), option.GetUid(), idTest);
157
158 int returncode = Notification::NotificationHelper::CancelAsBundle(option, idTest);
159 if (returncode != ERR_OK) {
160 int externalCode = NotificationSts::GetExternalCode(returncode);
161 ANS_LOGE("CancelAsBundle retern error. returncode: %{public}d, externalCode: %{public}d",
162 returncode, externalCode);
163 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
164 }
165
166 ANS_LOGD("AniCancelAsBundleWithBundleOption end");
167 }
168
AniCancelGroup(ani_env * env,ani_string groupName)169 void AniCancelGroup(ani_env *env, ani_string groupName)
170 {
171 ANS_LOGD("AniCancelGroup enter");
172
173 std::string tempStr;
174 if (ANI_OK != NotificationSts::GetStringByAniString(env, groupName, tempStr)) {
175 NotificationSts::ThrowErrorWithMsg(env, "AniCancelGroup: groupName parse failed!");
176 return;
177 }
178 std::string groupNameStr = NotificationSts::GetResizeStr(tempStr, NotificationSts::STR_MAX_SIZE);
179 ANS_LOGD("AniCancelGroup groupNameStr: %{public}s", groupNameStr.c_str());
180 int returncode = Notification::NotificationHelper::CancelGroup(groupNameStr);
181 if (returncode != ERR_OK) {
182 int externalCode = NotificationSts::GetExternalCode(returncode);
183 ANS_LOGE("AniCancelGroup: CancelAsBundle retern erro. returncode: %{public}d, externalCode: %{public}d",
184 returncode, externalCode);
185 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
186 }
187 ANS_LOGD("AniCancelGroup end");
188 }
189 } // namespace NotificationManagerSts
190 } // namespace OHOS