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 "sts_disturb_mode.h"
16
17 #include "sts_bundle_option.h"
18 #include "sts_common.h"
19 #include "ans_log_wrapper.h"
20
21 namespace OHOS {
22 namespace NotificationSts {
UnwrapDoNotDisturbProfile(ani_env * env,ani_object param,sptr<NotificationDoNotDisturbProfile> & profile)23 bool UnwrapDoNotDisturbProfile(ani_env *env, ani_object param,
24 sptr<NotificationDoNotDisturbProfile> &profile)
25 {
26 ANS_LOGD("UnwrapDoNotDisturbProfile call");
27 if (env == nullptr || param == nullptr) {
28 ANS_LOGE("UnwrapDoNotDisturbProfile fail, has nullptr");
29 return false;
30 }
31 ani_boolean isUndefined = ANI_TRUE;
32 ani_double idAni = 0.0;
33 if (ANI_OK != env->Object_GetPropertyByName_Double(param, "id", &idAni)) {
34 ANS_LOGE("UnwrapDoNotDisturbProfile: get id failed");
35 return false;
36 }
37 profile->SetProfileId(static_cast<int64_t>(idAni));
38 std::string nameStr = "";
39 if (ANI_OK != GetPropertyString(env, param, "name", isUndefined, nameStr) || isUndefined == ANI_TRUE) {
40 ANS_LOGE("UnwrapDoNotDisturbProfile: get name failed");
41 return false;
42 }
43 profile->SetProfileName(nameStr);
44 ani_ref trustlistRef;
45 if (ANI_OK != GetPropertyRef(env, param, "trustlist", isUndefined, trustlistRef) || isUndefined == ANI_TRUE) {
46 ANS_LOGE("UnwrapDoNotDisturbProfile: get trustlist failed");
47 } else {
48 std::vector<BundleOption> trustlist = {};
49 UnwrapArrayBundleOption(env, static_cast<ani_object>(trustlistRef), trustlist);
50 if (!trustlist.empty()) {
51 profile->SetProfileTrustList(trustlist);
52 }
53 }
54 ANS_LOGD("UnwrapDoNotDisturbProfile end");
55 return true;
56 }
57
UnwrapArrayDoNotDisturbProfile(ani_env * env,ani_object arrayObj,std::vector<sptr<NotificationDoNotDisturbProfile>> & profiles)58 bool UnwrapArrayDoNotDisturbProfile(ani_env *env, ani_object arrayObj,
59 std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
60 {
61 ANS_LOGD("UnwrapArrayDoNotDisturbProfile call");
62 if (env == nullptr || arrayObj == nullptr) {
63 ANS_LOGE("UnwrapArrayDoNotDisturbProfile fail, has nullptr");
64 return false;
65 }
66 ani_status status;
67 ani_double length;
68 status = env->Object_GetPropertyByName_Double(arrayObj, "length", &length);
69 if (status != ANI_OK) {
70 ANS_LOGD("UnwrapArrayDoNotDisturbProfile: status = %{public}d", status);
71 return false;
72 }
73 for (int i = 0; i < static_cast<int>(length); i++) {
74 ani_ref optionRef;
75 status = env->Object_CallMethodByName_Ref(arrayObj, "$_get",
76 "I:Lstd/core/Object;", &optionRef, (ani_int)i);
77 if (status != ANI_OK) {
78 ANS_LOGE("UnwrapArrayDoNotDisturbProfile: status : %{public}d, index: %{public}d", status, i);
79 return false;
80 }
81 sptr<NotificationDoNotDisturbProfile> profile = new (std::nothrow)NotificationDoNotDisturbProfile();
82 if (!UnwrapDoNotDisturbProfile(env, static_cast<ani_object>(optionRef), profile)) {
83 ANS_LOGE("Get profile failed, index: %{public}d", i);
84 return false;
85 }
86 profiles.push_back(profile);
87 }
88 ANS_LOGD("UnwrapArrayDoNotDisturbProfile end");
89 return true;
90 }
91
WrapProfileTrustList(ani_env * env,sptr<NotificationDoNotDisturbProfile> profile,ani_object & outObj)92 bool WrapProfileTrustList(ani_env* env, sptr<NotificationDoNotDisturbProfile> profile,
93 ani_object &outObj)
94 {
95 ani_status status = ANI_OK;
96 const auto& trustList = profile->GetProfileTrustList();
97 if (trustList.empty()) {
98 ANS_LOGE("WrapProfileTrustList trustlist is nullptr");
99 return true;
100 }
101 ani_object arrayObj = newArrayClass(env, trustList.size());
102 if (arrayObj == nullptr) {
103 ANS_LOGE("WrapProfileTrustList Failed to create trustlist array");
104 return false;
105 }
106
107 int32_t index = 0;
108 for (const auto& bundle : trustList) {
109 auto bundlePtr = std::make_shared<Notification::NotificationBundleOption>(bundle);
110 ani_object bundleObj = nullptr;
111 if (!WrapBundleOption(env, bundlePtr, bundleObj)) {
112 ANS_LOGE("WrapProfileTrustList WrapBundleOption failed");
113 return false;
114 }
115 if (ANI_OK != (status = env->Object_CallMethodByName_Void(arrayObj, "$_set",
116 "ILstd/core/Object;:V", index, bundleObj))) {
117 ANS_LOGE("WrapProfileTrustList set object faild. index %{public}d status %{public}d",
118 index, status);
119 return false;
120 }
121 index++;
122 }
123 ani_ref arrayRef = arrayObj;
124 if (!SetPropertyByRef(env, outObj, "trustlist", arrayRef)) {
125 ANS_LOGE("WrapProfileTrustList Failed to set trustlist property");
126 return false;
127 }
128 return true;
129 }
130
WrapDoNotDisturbProfile(ani_env * env,sptr<NotificationDoNotDisturbProfile> profile,ani_object & outObj)131 bool WrapDoNotDisturbProfile(ani_env* env, sptr<NotificationDoNotDisturbProfile> profile,
132 ani_object &outObj)
133 {
134 ani_status status = ANI_OK;
135 ani_class cls = nullptr;
136 if (env == nullptr) {
137 ANS_LOGE("WrapDoNotDisturbProfile: Invalid input parameters");
138 return false;
139 }
140 const char* className = "L@ohos/notificationManager/notificationManager/DoNotDisturbProfileInner;";
141 if (!CreateClassObjByClassName(env, className, cls, outObj) || outObj == nullptr) {
142 ANS_LOGE("WrapDoNotDisturbProfile: Failed to create profile class object");
143 return false;
144 }
145 ani_double id = static_cast<double>(profile->GetProfileId());
146 if (ANI_OK != (status = env->Object_SetPropertyByName_Double(outObj, "id", id))) {
147 ANS_LOGE("WrapDoNotDisturbProfile : set reason faild. status %{public}d", status);
148 return false;
149 }
150 if (!SetPropertyOptionalByString(env, outObj, "name", profile->GetProfileName())) {
151 ANS_LOGE("WrapDoNotDisturbProfile: set name failed");
152 return false;
153 }
154 if (!WrapProfileTrustList(env, profile, outObj)) {
155 ANS_LOGE("WrapDoNotDisturbProfile: set trustList failed");
156 return false;
157 }
158 return true;
159 }
160 } // namespace NotificationSts
161 } // OHOS