1 /*
2 * Copyright (c) 2024 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
16 #include <unistd.h>
17
18 #include "ans_const_define.h"
19 #include "ans_inner_errors.h"
20 #include "ans_log_wrapper.h"
21 #include "distributed_notification_service_ipc_interface_code.h"
22 #include "message_option.h"
23 #include "message_parcel.h"
24 #include "parcel.h"
25 #include "ans_manager_proxy.h"
26
27 namespace OHOS {
28 namespace Notification {
IsDistributedEnabled(bool & enabled)29 ErrCode AnsManagerProxy::IsDistributedEnabled(bool &enabled)
30 {
31 MessageParcel data;
32 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
33 ANS_LOGE("[IsDistributedEnabled] fail: write interface token failed.");
34 return ERR_ANS_PARCELABLE_FAILED;
35 }
36
37 MessageParcel reply;
38 MessageOption option = {MessageOption::TF_SYNC};
39 ErrCode result = InnerTransact(NotificationInterfaceCode::IS_DISTRIBUTED_ENABLED, option, data, reply);
40 if (result != ERR_OK) {
41 ANS_LOGE("[IsDistributedEnabled] fail: transact ErrCode=%{public}d", result);
42 return ERR_ANS_TRANSACT_FAILED;
43 }
44
45 if (!reply.ReadInt32(result)) {
46 ANS_LOGE("[IsDistributedEnabled] fail: read result failed.");
47 return ERR_ANS_PARCELABLE_FAILED;
48 }
49
50 if (!reply.ReadBool(enabled)) {
51 ANS_LOGE("[IsDistributedEnabled] fail: read enabled failed.");
52 return ERR_ANS_PARCELABLE_FAILED;
53 }
54
55 return result;
56 }
57
EnableDistributed(bool enabled)58 ErrCode AnsManagerProxy::EnableDistributed(bool enabled)
59 {
60 MessageParcel data;
61 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
62 ANS_LOGE("[EnableDistributed] fail: write interface token failed.");
63 return ERR_ANS_PARCELABLE_FAILED;
64 }
65
66 if (!data.WriteBool(enabled)) {
67 ANS_LOGE("[EnableDistributed] fail: write enabled failed");
68 return ERR_ANS_PARCELABLE_FAILED;
69 }
70
71 MessageParcel reply;
72 MessageOption option = {MessageOption::TF_SYNC};
73 ErrCode result = InnerTransact(NotificationInterfaceCode::ENABLE_DISTRIBUTED, option, data, reply);
74 if (result != ERR_OK) {
75 ANS_LOGE("[EnableDistributed] fail: transact ErrCode=%{public}d", result);
76 return ERR_ANS_TRANSACT_FAILED;
77 }
78
79 if (!reply.ReadInt32(result)) {
80 ANS_LOGE("[EnableDistributed] fail: read result failed.");
81 return ERR_ANS_PARCELABLE_FAILED;
82 }
83
84 return result;
85 }
86
EnableDistributedByBundle(const sptr<NotificationBundleOption> & bundleOption,bool enabled)87 ErrCode AnsManagerProxy::EnableDistributedByBundle(const sptr<NotificationBundleOption> &bundleOption, bool enabled)
88 {
89 if (bundleOption == nullptr) {
90 ANS_LOGE("[EnableDistributedByBundle] fail: bundle is empty.");
91 return ERR_ANS_INVALID_PARAM;
92 }
93
94 MessageParcel data;
95 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
96 ANS_LOGE("[EnableDistributedByBundle] fail: write interface token failed.");
97 return ERR_ANS_PARCELABLE_FAILED;
98 }
99
100 if (!data.WriteParcelable(bundleOption)) {
101 ANS_LOGE("[EnableDistributedByBundle] fail:: write bundle failed");
102 return ERR_ANS_PARCELABLE_FAILED;
103 }
104
105 if (!data.WriteBool(enabled)) {
106 ANS_LOGE("[EnableDistributedByBundle] fail:: write enabled failed");
107 return ERR_ANS_PARCELABLE_FAILED;
108 }
109
110 MessageParcel reply;
111 MessageOption option = {MessageOption::TF_SYNC};
112 ErrCode result = InnerTransact(NotificationInterfaceCode::ENABLE_DISTRIBUTED_BY_BUNDLE, option, data, reply);
113 if (result != ERR_OK) {
114 ANS_LOGE("[EnableDistributedByBundle] fail: transact ErrCode=%{public}d", result);
115 return ERR_ANS_TRANSACT_FAILED;
116 }
117
118 if (!reply.ReadInt32(result)) {
119 ANS_LOGE("[EnableDistributedByBundle] fail: read result failed.");
120 return ERR_ANS_PARCELABLE_FAILED;
121 }
122
123 return result;
124 }
125
EnableDistributedSelf(bool enabled)126 ErrCode AnsManagerProxy::EnableDistributedSelf(bool enabled)
127 {
128 MessageParcel data;
129 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
130 ANS_LOGE("[EnableDistributedSelf] fail: write interface token failed.");
131 return ERR_ANS_PARCELABLE_FAILED;
132 }
133
134 if (!data.WriteBool(enabled)) {
135 ANS_LOGE("[EnableDistributedSelf] fail: write enabled failed");
136 return ERR_ANS_PARCELABLE_FAILED;
137 }
138
139 MessageParcel reply;
140 MessageOption option = {MessageOption::TF_SYNC};
141 ErrCode result = InnerTransact(NotificationInterfaceCode::ENABLE_DISTRIBUTED_SELF, option, data, reply);
142 if (result != ERR_OK) {
143 ANS_LOGE("[EnableDistributedSelf] fail: transact ErrCode=%{public}d", result);
144 return ERR_ANS_TRANSACT_FAILED;
145 }
146
147 if (!reply.ReadInt32(result)) {
148 ANS_LOGE("[EnableDistributedSelf] fail: read result failed.");
149 return ERR_ANS_PARCELABLE_FAILED;
150 }
151
152 return result;
153 }
154
IsDistributedEnableByBundle(const sptr<NotificationBundleOption> & bundleOption,bool & enabled)155 ErrCode AnsManagerProxy::IsDistributedEnableByBundle(const sptr<NotificationBundleOption> &bundleOption, bool &enabled)
156 {
157 if (bundleOption == nullptr) {
158 ANS_LOGE("[IsDistributedEnableByBundle] fail: bundle is empty.");
159 return ERR_ANS_INVALID_PARAM;
160 }
161
162 MessageParcel data;
163 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
164 ANS_LOGE("[IsDistributedEnableByBundle] fail: write interface token failed.");
165 return ERR_ANS_PARCELABLE_FAILED;
166 }
167
168 if (!data.WriteParcelable(bundleOption)) {
169 ANS_LOGE("[IsDistributedEnableByBundle] fail: write bundle failed");
170 return ERR_ANS_PARCELABLE_FAILED;
171 }
172
173 MessageParcel reply;
174 MessageOption option = {MessageOption::TF_SYNC};
175 ErrCode result = InnerTransact(NotificationInterfaceCode::IS_DISTRIBUTED_ENABLED_BY_BUNDLE, option, data, reply);
176 if (result != ERR_OK) {
177 ANS_LOGE("[IsDistributedEnableByBundle] fail: transact ErrCode=%{public}d", result);
178 return ERR_ANS_TRANSACT_FAILED;
179 }
180
181 if (!reply.ReadInt32(result)) {
182 ANS_LOGE("[IsDistributedEnableByBundle] fail: read result failed.");
183 return ERR_ANS_PARCELABLE_FAILED;
184 }
185
186 if (!reply.ReadBool(enabled)) {
187 ANS_LOGE("[IsDistributedEnableByBundle] fail: read enabled failed.");
188 return ERR_ANS_PARCELABLE_FAILED;
189 }
190
191 return result;
192 }
193
GetDeviceRemindType(NotificationConstant::RemindType & remindType)194 ErrCode AnsManagerProxy::GetDeviceRemindType(NotificationConstant::RemindType &remindType)
195 {
196 MessageParcel data;
197 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
198 ANS_LOGE("[GetDeviceRemindType] fail: write interface token failed.");
199 return ERR_ANS_PARCELABLE_FAILED;
200 }
201
202 MessageParcel reply;
203 MessageOption option = {MessageOption::TF_SYNC};
204 ErrCode result = InnerTransact(NotificationInterfaceCode::GET_DEVICE_REMIND_TYPE, option, data, reply);
205 if (result != ERR_OK) {
206 ANS_LOGE("[GetDeviceRemindType] fail: transact ErrCode=%{public}d", result);
207 return ERR_ANS_TRANSACT_FAILED;
208 }
209
210 if (!reply.ReadInt32(result)) {
211 ANS_LOGE("[GetDeviceRemindType] fail: read result failed.");
212 return ERR_ANS_PARCELABLE_FAILED;
213 }
214
215 if (result == ERR_OK) {
216 int32_t rType {-1};
217 if (!reply.ReadInt32(rType)) {
218 ANS_LOGE("[GetDeviceRemindType] fail: read remind type failed.");
219 return ERR_ANS_PARCELABLE_FAILED;
220 }
221
222 remindType = static_cast<NotificationConstant::RemindType>(rType);
223 }
224
225 return result;
226 }
227
SetDistributedEnabledByBundle(const sptr<NotificationBundleOption> & bundleOption,const std::string & deviceType,const bool enabled)228 ErrCode AnsManagerProxy::SetDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
229 const std::string &deviceType, const bool enabled)
230 {
231 ANS_LOGD("enter");
232 if (bundleOption == nullptr) {
233 ANS_LOGE("[SetNotificationsEnabledForSpecialBundle] fail: bundleOption is empty.");
234 return ERR_ANS_INVALID_PARAM;
235 }
236
237 MessageParcel data;
238 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
239 ANS_LOGE("[SetNotificationsEnabledForSpecialBundle] fail: write interface token failed.");
240 return ERR_ANS_PARCELABLE_FAILED;
241 }
242
243 if (!data.WriteParcelable(bundleOption)) {
244 ANS_LOGE("[SetNotificationsEnabledForSpecialBundle] fail: write bundleOption failed");
245 return ERR_ANS_PARCELABLE_FAILED;
246 }
247
248 if (!data.WriteString(deviceType)) {
249 ANS_LOGE("[SetNotificationsEnabledForSpecialBundle] fail: write deviceType failed");
250 return ERR_ANS_PARCELABLE_FAILED;
251 }
252
253 if (!data.WriteBool(enabled)) {
254 ANS_LOGE("[SetNotificationsEnabledForSpecialBundle] fail: write enabled failed");
255 return ERR_ANS_PARCELABLE_FAILED;
256 }
257
258 MessageParcel reply;
259 MessageOption option = {MessageOption::TF_SYNC};
260 ErrCode result = InnerTransact(NotificationInterfaceCode::SET_DISTRIBUTED_ENABLED_BY_BUNDLE, option, data, reply);
261 if (result != ERR_OK) {
262 ANS_LOGE("[SetNotificationsEnabledForSpecialBundle] fail: transact ErrCode=%{public}d", result);
263 return ERR_ANS_TRANSACT_FAILED;
264 }
265
266 if (!reply.ReadInt32(result)) {
267 ANS_LOGE("[SetNotificationsEnabledForSpecialBundle] fail: read result failed.");
268 return ERR_ANS_PARCELABLE_FAILED;
269 }
270
271 return result;
272 }
273
IsDistributedEnabledByBundle(const sptr<NotificationBundleOption> & bundleOption,const std::string & deviceType,bool & enabled)274 ErrCode AnsManagerProxy::IsDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
275 const std::string &deviceType, bool &enabled)
276 {
277 ANS_LOGD("enter");
278 if (bundleOption == nullptr) {
279 ANS_LOGE("[IsDistributedEnabledByBundle] fail: bundleOption is empty.");
280 return ERR_ANS_INVALID_PARAM;
281 }
282
283 MessageParcel data;
284 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
285 ANS_LOGE("[IsDistributedEnabledByBundle] fail: write interface token failed.");
286 return ERR_ANS_PARCELABLE_FAILED;
287 }
288
289 if (!data.WriteParcelable(bundleOption)) {
290 ANS_LOGE("[IsDistributedEnabledByBundle] fail: write bundleOption failed");
291 return ERR_ANS_PARCELABLE_FAILED;
292 }
293
294 if (!data.WriteString(deviceType)) {
295 ANS_LOGE("[IsDistributedEnabledByBundle] fail: write deviceType failed");
296 return ERR_ANS_PARCELABLE_FAILED;
297 }
298
299 MessageParcel reply;
300 MessageOption option = {MessageOption::TF_SYNC};
301 ErrCode result = InnerTransact(NotificationInterfaceCode::GET_DISTRIBUTED_ENABLED_BY_BUNDLE, option, data, reply);
302 if (result != ERR_OK) {
303 ANS_LOGE("[IsDistributedEnabledByBundle] fail: transact ErrCode=%{public}d", result);
304 return ERR_ANS_TRANSACT_FAILED;
305 }
306
307 if (!reply.ReadInt32(result)) {
308 ANS_LOGE("[IsDistributedEnabledByBundle] fail: read result failed.");
309 return ERR_ANS_PARCELABLE_FAILED;
310 }
311
312 if (!reply.ReadBool(enabled)) {
313 ANS_LOGE("[IsDistributedEnabledByBundle] fail: read enabled failed.");
314 return ERR_ANS_PARCELABLE_FAILED;
315 }
316
317 return result;
318 }
SetDistributedEnabledBySlot(const NotificationConstant::SlotType & slotType,const std::string & deviceType,const bool enabled)319 ErrCode AnsManagerProxy::SetDistributedEnabledBySlot(
320 const NotificationConstant::SlotType &slotType, const std::string &deviceType, const bool enabled)
321 {
322 ANS_LOGD("enter");
323
324 MessageParcel data;
325 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
326 ANS_LOGE("[SetDistributedEnabledBySlot] fail: write interface token failed.");
327 return ERR_ANS_PARCELABLE_FAILED;
328 }
329
330 if (!data.WriteInt32(slotType)) {
331 ANS_LOGE("[SetDistributedEnabledBySlot] fail: write slotType failed");
332 return ERR_ANS_PARCELABLE_FAILED;
333 }
334
335 if (!data.WriteString(deviceType)) {
336 ANS_LOGE("[SetDistributedEnabledBySlot] fail: write deviceType failed");
337 return ERR_ANS_PARCELABLE_FAILED;
338 }
339
340 if (!data.WriteBool(enabled)) {
341 ANS_LOGE("[SetDistributedEnabledBySlot] fail: write enabled failed");
342 return ERR_ANS_PARCELABLE_FAILED;
343 }
344
345 MessageParcel reply;
346 MessageOption option = {MessageOption::TF_SYNC};
347 ErrCode result = InnerTransact(NotificationInterfaceCode::SET_DISTRIBUTED_ENABLED_BY_SLOT, option, data, reply);
348 if (result != ERR_OK) {
349 ANS_LOGE("[SetDistributedEnabledBySlot] fail: transact ErrCode=%{public}d", result);
350 return ERR_ANS_TRANSACT_FAILED;
351 }
352
353 if (!reply.ReadInt32(result)) {
354 ANS_LOGE("[SetDistributedEnabledBySlot] fail: read result failed.");
355 return ERR_ANS_PARCELABLE_FAILED;
356 }
357
358 return result;
359 }
360
IsDistributedEnabledBySlot(const NotificationConstant::SlotType & slotType,const std::string & deviceType,bool & enabled)361 ErrCode AnsManagerProxy::IsDistributedEnabledBySlot(
362 const NotificationConstant::SlotType &slotType, const std::string &deviceType, bool &enabled)
363 {
364 ANS_LOGD("enter");
365
366 MessageParcel data;
367 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
368 ANS_LOGE("[IsDistributedEnabledBySlot] fail: write interface token failed.");
369 return ERR_ANS_PARCELABLE_FAILED;
370 }
371
372 if (!data.WriteInt32(slotType)) {
373 ANS_LOGE("[IsDistributedEnabledBySlot] fail: write slotType failed");
374 return ERR_ANS_PARCELABLE_FAILED;
375 }
376
377 if (!data.WriteString(deviceType)) {
378 ANS_LOGE("[IsDistributedEnabledBySlot] fail: write deviceType failed");
379 return ERR_ANS_PARCELABLE_FAILED;
380 }
381
382 MessageParcel reply;
383 MessageOption option = {MessageOption::TF_SYNC};
384 ErrCode result = InnerTransact(NotificationInterfaceCode::GET_DISTRIBUTED_ENABLED_BY_SLOT, option, data, reply);
385 if (result != ERR_OK) {
386 ANS_LOGE("[IsDistributedEnabledBySlot] fail: transact ErrCode=%{public}d", result);
387 return ERR_ANS_TRANSACT_FAILED;
388 }
389
390 if (!reply.ReadInt32(result)) {
391 ANS_LOGE("[IsDistributedEnabledBySlot] fail: read result failed.");
392 return ERR_ANS_PARCELABLE_FAILED;
393 }
394
395 if (!reply.ReadBool(enabled)) {
396 ANS_LOGE("[IsDistributedEnabledBySlot] fail: read enabled failed.");
397 return ERR_ANS_PARCELABLE_FAILED;
398 }
399
400 return result;
401 }
402 } // namespace Notification
403 } // namespace OHOS
404