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_display_badge.h"
16
17 #include "ans_log_wrapper.h"
18 #include "sts_bundle_option.h"
19 #include "sts_throw_erro.h"
20 #include "sts_common.h"
21 #include "notification_helper.h"
22 #include "notification_bundle_option.h"
23
24
25 namespace OHOS {
26 namespace NotificationManagerSts {
AniDisplayBadge(ani_env * env,ani_object obj,ani_boolean enable)27 void AniDisplayBadge(ani_env *env, ani_object obj, ani_boolean enable)
28 {
29 ANS_LOGD("DisplayBadgeAni call");
30 int returncode = 0;
31 BundleOption option;
32 if (NotificationSts::UnwrapBundleOption(env, obj, option)) {
33 returncode = Notification::NotificationHelper::SetShowBadgeEnabledForBundle(option,
34 NotificationSts::AniBooleanToBool(enable));
35 } else {
36 OHOS::NotificationSts::ThrowError(env, OHOS::Notification::ERROR_INTERNAL_ERROR,
37 NotificationSts::FindAnsErrMsg(OHOS::Notification::ERROR_INTERNAL_ERROR));
38 ANS_LOGE("sts DisplayBadge ERROR_INTERNAL_ERROR");
39 return;
40 }
41 if (returncode != ERR_OK) {
42 int externalCode = NotificationSts::GetExternalCode(returncode);
43 ANS_LOGE("sts DisplayBadge error, errorCode: %{public}d", externalCode);
44 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
45 return;
46 }
47 ANS_LOGD("DisplayBadgeAni end");
48 }
49
AniIsBadgeDisplayed(ani_env * env,ani_object obj)50 ani_boolean AniIsBadgeDisplayed(ani_env *env, ani_object obj)
51 {
52 ANS_LOGD("sts IsBadgeDisplayed call");
53 int returncode = 0;
54 bool isDisplayed = false;
55 if (obj == nullptr) {
56 returncode = Notification::NotificationHelper::GetShowBadgeEnabled(isDisplayed);
57 } else {
58 BundleOption option;
59 if (NotificationSts::UnwrapBundleOption(env, obj, option)) {
60 returncode = Notification::NotificationHelper::GetShowBadgeEnabledForBundle(option, isDisplayed);
61 } else {
62 OHOS::NotificationSts::ThrowError(env, OHOS::Notification::ERROR_INTERNAL_ERROR,
63 NotificationSts::FindAnsErrMsg(OHOS::Notification::ERROR_INTERNAL_ERROR));
64 ANS_LOGE("sts IsBadgeDisplayed ERROR_INTERNAL_ERROR");
65 return NotificationSts::BoolToAniBoolean(false);
66 }
67 }
68
69 if (returncode != ERR_OK) {
70 int externalCode = NotificationSts::GetExternalCode(returncode);
71 ANS_LOGE("sts IsBadgeDisplayed error, errorCode: %{public}d", externalCode);
72 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
73 return NotificationSts::BoolToAniBoolean(false);
74 }
75 ANS_LOGD("sts IsBadgeDisplayed end, isDisplayed: %{public}d", isDisplayed);
76 return NotificationSts::BoolToAniBoolean(isDisplayed);
77 }
78
AniSetBadgeNumber(ani_env * env,ani_double badgeNumber)79 void AniSetBadgeNumber(ani_env *env, ani_double badgeNumber)
80 {
81 ANS_LOGD("sts AniSetBadgeNumber call, BadgeNumber: %{public}lf", badgeNumber);
82 int returncode = Notification::NotificationHelper::SetBadgeNumber(static_cast<int32_t>(badgeNumber));
83 if (returncode != ERR_OK) {
84 int externalCode = NotificationSts::GetExternalCode(returncode);
85 ANS_LOGE("sts AniSetBadgeNumber error, errorCode: %{public}d", externalCode);
86 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
87 }
88 ANS_LOGD("sts AniSetBadgeNumber end");
89 }
90
AniSetBadgeNumberByBundle(ani_env * env,ani_object obj,ani_double badgeNumber)91 void AniSetBadgeNumberByBundle(ani_env *env, ani_object obj, ani_double badgeNumber)
92 {
93 ANS_LOGD("AniSetBadgeNumberByBundle call, badgeNumber: %{public}lf", badgeNumber);
94 int returncode = ERR_OK;
95 BundleOption option;
96 if (NotificationSts::UnwrapBundleOption(env, obj, option)) {
97 returncode = Notification::NotificationHelper::SetBadgeNumberByBundle(option,
98 static_cast<int32_t>(badgeNumber));
99 } else {
100 ANS_LOGE("sts AniSetBadgeNumberByBundle ERROR_INTERNAL_ERROR");
101 OHOS::NotificationSts::ThrowError(env, OHOS::Notification::ERROR_INTERNAL_ERROR,
102 NotificationSts::FindAnsErrMsg(OHOS::Notification::ERROR_INTERNAL_ERROR));
103 return;
104 }
105 if (returncode != ERR_OK) {
106 int externalCode = NotificationSts::GetExternalCode(returncode);
107 ANS_LOGE("sts AniSetBadgeNumberByBundle error, errorCode: %{public}d", externalCode);
108 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
109 }
110 ANS_LOGD("AniSetBadgeNumberByBundle end");
111 }
112 }
113 }