• 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_do_not_disturb_profile.h"
16 
17 #include "notification_helper.h"
18 #include "ans_log_wrapper.h"
19 #include "sts_common.h"
20 #include "sts_throw_erro.h"
21 #include "sts_disturb_mode.h"
22 #include "sts_bundle_option.h"
23 
24 namespace OHOS {
25 namespace NotificationManagerSts {
AniAddDoNotDisturbProfile(ani_env * env,ani_object obj)26 void AniAddDoNotDisturbProfile(ani_env *env, ani_object obj)
27 {
28     ANS_LOGD("AniAddDoNotDisturbProfile call");
29     int returncode = 0;
30     std::vector<sptr<NotificationDoNotDisturbProfile>> profiles;
31     if (NotificationSts::UnwrapArrayDoNotDisturbProfile(env, obj, profiles)) {
32         returncode = Notification::NotificationHelper::AddDoNotDisturbProfiles(profiles);
33     } else {
34         OHOS::NotificationSts::ThrowError(env, OHOS::Notification::ERROR_INTERNAL_ERROR,
35             NotificationSts::FindAnsErrMsg(OHOS::Notification::ERROR_INTERNAL_ERROR));
36         ANS_LOGE("AniAddDoNotDisturbProfile failed : ERROR_INTERNAL_ERROR");
37         return;
38     }
39     if (returncode != ERR_OK) {
40         int externalCode = NotificationSts::GetExternalCode(returncode);
41         OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
42         ANS_LOGE("AniAddDoNotDisturbProfile error, errorCode: %{public}d", externalCode);
43     }
44     ANS_LOGD("AniAddDoNotDisturbProfile end");
45 }
46 
AniRemoveDoNotDisturbProfile(ani_env * env,ani_object obj)47 void AniRemoveDoNotDisturbProfile(ani_env *env, ani_object obj)
48 {
49     ANS_LOGD("AniRemoveDoNotDisturbProfile call");
50     int returncode = 0;
51     std::vector<sptr<NotificationDoNotDisturbProfile>> profiles;
52     if (NotificationSts::UnwrapArrayDoNotDisturbProfile(env, obj, profiles)) {
53         returncode = Notification::NotificationHelper::RemoveDoNotDisturbProfiles(profiles);
54     } else {
55         OHOS::NotificationSts::ThrowError(env, OHOS::Notification::ERROR_INTERNAL_ERROR,
56             NotificationSts::FindAnsErrMsg(OHOS::Notification::ERROR_INTERNAL_ERROR));
57         ANS_LOGE("AniRemoveDoNotDisturbProfile failed : ERROR_INTERNAL_ERROR");
58         return;
59     }
60     if (returncode != ERR_OK) {
61         int externalCode = NotificationSts::GetExternalCode(returncode);
62         OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
63         ANS_LOGE("AniRemoveDoNotDisturbProfile error, errorCode: %{public}d", externalCode);
64     }
65     ANS_LOGD("AniRemoveDoNotDisturbProfile end");
66 }
67 
AniGetDoNotDisturbProfile(ani_env * env,ani_double id)68 ani_object AniGetDoNotDisturbProfile(ani_env *env, ani_double id)
69 {
70     ani_object profile;
71     ANS_LOGD("AniGetDoNotDisturbProfile enter");
72 
73     sptr<NotificationDoNotDisturbProfile> doNotDisturbProfile = new (std::nothrow) NotificationDoNotDisturbProfile();
74     if (doNotDisturbProfile == nullptr) {
75         ANS_LOGE("Failed to create NotificationDoNotDisturbProfile.");
76         OHOS::NotificationSts::ThrowError(env, OHOS::Notification::ERROR_INTERNAL_ERROR,
77             NotificationSts::FindAnsErrMsg(OHOS::Notification::ERROR_INTERNAL_ERROR));
78         return nullptr;
79     }
80 
81     int32_t idTest = static_cast<int32_t>(id);
82     ANS_LOGD("AniGetDoNotDisturbProfile: idTest: %{public}d", idTest);
83     int returncode = Notification::NotificationHelper::GetDoNotDisturbProfile(idTest, doNotDisturbProfile);
84     if (returncode != ERR_OK) {
85         int externalCode = NotificationSts::GetExternalCode(returncode);
86         ANS_LOGE("AniSetDoNotDisturbDateWithId error, errorCode: %{public}d, returncode: %{public}d",
87             externalCode, returncode);
88         OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
89     }
90 
91     if (!NotificationSts::WrapDoNotDisturbProfile(env, doNotDisturbProfile, profile)) {
92         ANS_LOGE("AniGetDoNotDisturbProfile WrapDoNotDisturbProfile failed");
93         OHOS::NotificationSts::ThrowError(env, OHOS::Notification::ERROR_INTERNAL_ERROR,
94             NotificationSts::FindAnsErrMsg(OHOS::Notification::ERROR_INTERNAL_ERROR));
95         return nullptr;
96     }
97 
98     ANS_LOGD("AniGetDoNotDisturbProfile end");
99     return profile;
100 }
101 }
102 }