• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "ans_subscriber_local_live_view_interface.h"
22 #include "distributed_notification_service_ipc_interface_code.h"
23 #include "message_option.h"
24 #include "message_parcel.h"
25 #include "parcel.h"
26 #include "ans_manager_proxy.h"
27 
28 namespace OHOS {
29 namespace Notification {
SetDoNotDisturbDate(const sptr<NotificationDoNotDisturbDate> & date)30 ErrCode AnsManagerProxy::SetDoNotDisturbDate(const sptr<NotificationDoNotDisturbDate> &date)
31 {
32     if (date == nullptr) {
33         ANS_LOGE("[SetDoNotDisturbDate] fail: date is empty.");
34         return ERR_ANS_INVALID_PARAM;
35     }
36 
37     MessageParcel data;
38     if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
39         ANS_LOGE("[SetDoNotDisturbDate] fail: write interface token error.");
40         return ERR_ANS_PARCELABLE_FAILED;
41     }
42 
43     if (!data.WriteParcelable(date)) {
44         ANS_LOGE("[SetDoNotDisturbDate] fail: write date failed");
45         return ERR_ANS_PARCELABLE_FAILED;
46     }
47 
48     MessageParcel reply;
49     MessageOption option = {MessageOption::TF_SYNC};
50     ErrCode result = InnerTransact(NotificationInterfaceCode::SET_DO_NOT_DISTURB_DATE, option, data, reply);
51     if (result != ERR_OK) {
52         ANS_LOGE("[SetDoNotDisturbDate] fail: transact ErrCode=%{public}d", result);
53         return ERR_ANS_TRANSACT_FAILED;
54     }
55 
56     if (!reply.ReadInt32(result)) {
57         ANS_LOGE("[SetDoNotDisturbDate] fail: read result error.");
58         return ERR_ANS_PARCELABLE_FAILED;
59     }
60 
61     return result;
62 }
63 
GetDoNotDisturbDate(sptr<NotificationDoNotDisturbDate> & date)64 ErrCode AnsManagerProxy::GetDoNotDisturbDate(sptr<NotificationDoNotDisturbDate> &date)
65 {
66     MessageParcel data;
67     if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
68         ANS_LOGE("[GetDoNotDisturbDate] fail: write interface token failed.");
69         return ERR_ANS_PARCELABLE_FAILED;
70     }
71 
72     MessageParcel reply;
73     MessageOption option = {MessageOption::TF_SYNC};
74     ErrCode result = InnerTransact(NotificationInterfaceCode::GET_DO_NOT_DISTURB_DATE, option, data, reply);
75     if (result != ERR_OK) {
76         ANS_LOGE("[GetDoNotDisturbDate] fail: transact ErrCode=%{public}d", result);
77         return ERR_ANS_TRANSACT_FAILED;
78     }
79 
80     if (!reply.ReadInt32(result)) {
81         ANS_LOGE("[GetDoNotDisturbDate] fail: read result error.");
82         return ERR_ANS_PARCELABLE_FAILED;
83     }
84 
85     if (result == ERR_OK) {
86         date = reply.ReadParcelable<NotificationDoNotDisturbDate>();
87         if (date == nullptr) {
88             ANS_LOGE("[GetDoNotDisturbDate] fail: read date error.");
89             return ERR_ANS_PARCELABLE_FAILED;
90         }
91     }
92 
93     return result;
94 }
95 
AddDoNotDisturbProfiles(const std::vector<sptr<NotificationDoNotDisturbProfile>> & profiles)96 ErrCode AnsManagerProxy::AddDoNotDisturbProfiles(const std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
97 {
98     if (profiles.empty()) {
99         ANS_LOGW("The profiles is empty.");
100         return ERR_ANS_INVALID_PARAM;
101     }
102     if (profiles.size() > MAX_STATUS_VECTOR_NUM) {
103         ANS_LOGE("The profiles is exceeds limit.");
104         return ERR_ANS_INVALID_PARAM;
105     }
106     MessageParcel data;
107     if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
108         ANS_LOGE("Write interface token error.");
109         return ERR_ANS_PARCELABLE_FAILED;
110     }
111     if (!WriteParcelableVector(profiles, data)) {
112         ANS_LOGE("Write profiles vector error.");
113         return ERR_ANS_PARCELABLE_FAILED;
114     }
115     MessageParcel reply;
116     MessageOption option = {MessageOption::TF_SYNC};
117     ErrCode result = InnerTransact(NotificationInterfaceCode::ADD_DO_NOTDISTURB_PROFILES, option, data, reply);
118     if (result != ERR_OK) {
119         ANS_LOGE("Transact ErrCode is %{public}d", result);
120         return ERR_ANS_TRANSACT_FAILED;
121     }
122 
123     if (!reply.ReadInt32(result)) {
124         ANS_LOGE("Read result error.");
125         return ERR_ANS_PARCELABLE_FAILED;
126     }
127     return result;
128 }
129 
RemoveDoNotDisturbProfiles(const std::vector<sptr<NotificationDoNotDisturbProfile>> & profiles)130 ErrCode AnsManagerProxy::RemoveDoNotDisturbProfiles(const std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
131 {
132     if (profiles.empty()) {
133         ANS_LOGW("The profiles is empty.");
134         return ERR_ANS_INVALID_PARAM;
135     }
136     if (profiles.size() > MAX_STATUS_VECTOR_NUM) {
137         ANS_LOGE("The profiles is exceeds limit.");
138         return ERR_ANS_INVALID_PARAM;
139     }
140     MessageParcel data;
141     if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
142         ANS_LOGE("Write interface token error.");
143         return ERR_ANS_PARCELABLE_FAILED;
144     }
145     if (!WriteParcelableVector(profiles, data)) {
146         ANS_LOGE("Write profiles vector error.");
147         return ERR_ANS_PARCELABLE_FAILED;
148     }
149     MessageParcel reply;
150     MessageOption option = {MessageOption::TF_SYNC};
151     ErrCode result = InnerTransact(NotificationInterfaceCode::REMOVE_DO_NOT_DISTURB_PROFILES, option, data, reply);
152     if (result != ERR_OK) {
153         ANS_LOGE("Transact ErrCode is %{public}d", result);
154         return ERR_ANS_TRANSACT_FAILED;
155     }
156     if (!reply.ReadInt32(result)) {
157         ANS_LOGE("Read result error.");
158         return ERR_ANS_PARCELABLE_FAILED;
159     }
160     return result;
161 }
162 
DoesSupportDoNotDisturbMode(bool & doesSupport)163 ErrCode AnsManagerProxy::DoesSupportDoNotDisturbMode(bool &doesSupport)
164 {
165     MessageParcel data;
166     if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
167         ANS_LOGE("[DoesSupportDoNotDisturbMode] fail: write interface token failed.");
168         return ERR_ANS_PARCELABLE_FAILED;
169     }
170 
171     MessageParcel reply;
172     MessageOption option = {MessageOption::TF_SYNC};
173     ErrCode result = InnerTransact(NotificationInterfaceCode::DOES_SUPPORT_DO_NOT_DISTURB_MODE, option, data, reply);
174     if (result != ERR_OK) {
175         ANS_LOGE("[DoesSupportDoNotDisturbMode] fail: transact ErrCode=%{public}d", result);
176         return ERR_ANS_TRANSACT_FAILED;
177     }
178 
179     if (!reply.ReadInt32(result)) {
180         ANS_LOGE("[DoesSupportDoNotDisturbMode] fail: read result failed.");
181         return ERR_ANS_PARCELABLE_FAILED;
182     }
183 
184     if (!reply.ReadBool(doesSupport)) {
185         ANS_LOGE("[DoesSupportDoNotDisturbMode] fail: read doesSupport failed.");
186         return ERR_ANS_PARCELABLE_FAILED;
187     }
188 
189     return result;
190 }
191 
SetDoNotDisturbDate(const int32_t & userId,const sptr<NotificationDoNotDisturbDate> & date)192 ErrCode AnsManagerProxy::SetDoNotDisturbDate(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date)
193 {
194     if (date == nullptr) {
195         ANS_LOGE("[SetDoNotDisturbDate] fail: date is empty.");
196         return ERR_ANS_INVALID_PARAM;
197     }
198 
199     MessageParcel data;
200     if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
201         ANS_LOGE("[SetDoNotDisturbDate] fail: write interface token failed.");
202         return ERR_ANS_PARCELABLE_FAILED;
203     }
204 
205     if (!data.WriteInt32(userId)) {
206         ANS_LOGE("[SetDoNotDisturbDate] fail: write userId failed");
207         return ERR_ANS_PARCELABLE_FAILED;
208     }
209 
210     if (!data.WriteParcelable(date)) {
211         ANS_LOGE("[SetDoNotDisturbDate] fail: write date failed");
212         return ERR_ANS_PARCELABLE_FAILED;
213     }
214 
215     MessageParcel reply;
216     MessageOption option = {MessageOption::TF_SYNC};
217     ErrCode result = InnerTransact(NotificationInterfaceCode::SET_DO_NOT_DISTURB_DATE_BY_USER, option, data, reply);
218     if (result != ERR_OK) {
219         ANS_LOGE("[SetDoNotDisturbDate] fail: transact ErrCode=%{public}d", result);
220         return ERR_ANS_TRANSACT_FAILED;
221     }
222 
223     if (!reply.ReadInt32(result)) {
224         ANS_LOGE("[SetDoNotDisturbDate] fail: read result failed.");
225         return ERR_ANS_PARCELABLE_FAILED;
226     }
227 
228     return result;
229 }
230 
GetDoNotDisturbDate(const int32_t & userId,sptr<NotificationDoNotDisturbDate> & date)231 ErrCode AnsManagerProxy::GetDoNotDisturbDate(const int32_t &userId, sptr<NotificationDoNotDisturbDate> &date)
232 {
233     MessageParcel data;
234     if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
235         ANS_LOGE("[GetDoNotDisturbDate] fail: write interface token failed.");
236         return ERR_ANS_PARCELABLE_FAILED;
237     }
238 
239     if (!data.WriteInt32(userId)) {
240         ANS_LOGE("[GetDoNotDisturbDate] fail: write userId failed");
241         return ERR_ANS_PARCELABLE_FAILED;
242     }
243 
244     MessageParcel reply;
245     MessageOption option = {MessageOption::TF_SYNC};
246     ErrCode result = InnerTransact(NotificationInterfaceCode::GET_DO_NOT_DISTURB_DATE_BY_USER, option, data, reply);
247     if (result != ERR_OK) {
248         ANS_LOGE("[GetDoNotDisturbDate] fail: transact ErrCode=%{public}d", result);
249         return ERR_ANS_TRANSACT_FAILED;
250     }
251 
252     if (!reply.ReadInt32(result)) {
253         ANS_LOGE("[GetDoNotDisturbDate] fail: read result failed.");
254         return ERR_ANS_PARCELABLE_FAILED;
255     }
256 
257     if (result == ERR_OK) {
258         date = reply.ReadParcelable<NotificationDoNotDisturbDate>();
259         if (date == nullptr) {
260             ANS_LOGE("[GetDoNotDisturbDate] fail: read date failed.");
261             return ERR_ANS_PARCELABLE_FAILED;
262         }
263     }
264 
265     return result;
266 }
267 
SetDistributedEnabledByBundle(const sptr<NotificationBundleOption> & bundleOption,const std::string & deviceType,const bool enabled)268 ErrCode AnsManagerProxy::SetDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
269     const std::string &deviceType, const bool enabled)
270 {
271     ANS_LOGD("enter");
272     if (bundleOption == nullptr) {
273         ANS_LOGE("[SetNotificationsEnabledForSpecialBundle] fail: bundleOption is empty.");
274         return ERR_ANS_INVALID_PARAM;
275     }
276 
277     MessageParcel data;
278     if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
279         ANS_LOGE("[SetNotificationsEnabledForSpecialBundle] fail: write interface token failed.");
280         return ERR_ANS_PARCELABLE_FAILED;
281     }
282 
283     if (!data.WriteParcelable(bundleOption)) {
284         ANS_LOGE("[SetNotificationsEnabledForSpecialBundle] fail: write bundleOption failed");
285         return ERR_ANS_PARCELABLE_FAILED;
286     }
287 
288     if (!data.WriteString(deviceType)) {
289         ANS_LOGE("[SetNotificationsEnabledForSpecialBundle] fail: write deviceType failed");
290         return ERR_ANS_PARCELABLE_FAILED;
291     }
292 
293     if (!data.WriteBool(enabled)) {
294         ANS_LOGE("[SetNotificationsEnabledForSpecialBundle] fail: write enabled failed");
295         return ERR_ANS_PARCELABLE_FAILED;
296     }
297 
298     MessageParcel reply;
299     MessageOption option = {MessageOption::TF_SYNC};
300     ErrCode result = InnerTransact(NotificationInterfaceCode::SET_DISTRIBUTED_ENABLED_BY_BUNDLE, option, data, reply);
301     if (result != ERR_OK) {
302         ANS_LOGE("[SetNotificationsEnabledForSpecialBundle] fail: transact ErrCode=%{public}d", result);
303         return ERR_ANS_TRANSACT_FAILED;
304     }
305 
306     if (!reply.ReadInt32(result)) {
307         ANS_LOGE("[SetNotificationsEnabledForSpecialBundle] fail: read result failed.");
308         return ERR_ANS_PARCELABLE_FAILED;
309     }
310 
311     return result;
312 }
313 
IsDistributedEnabledByBundle(const sptr<NotificationBundleOption> & bundleOption,const std::string & deviceType,bool & enabled)314 ErrCode AnsManagerProxy::IsDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
315     const std::string &deviceType, bool &enabled)
316 {
317     ANS_LOGD("enter");
318     if (bundleOption == nullptr) {
319         ANS_LOGE("[IsDistributedEnabledByBundle] fail: bundleOption is empty.");
320         return ERR_ANS_INVALID_PARAM;
321     }
322 
323     MessageParcel data;
324     if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
325         ANS_LOGE("[IsDistributedEnabledByBundle] fail: write interface token failed.");
326         return ERR_ANS_PARCELABLE_FAILED;
327     }
328 
329     if (!data.WriteParcelable(bundleOption)) {
330         ANS_LOGE("[IsDistributedEnabledByBundle] fail: write bundleOption failed");
331         return ERR_ANS_PARCELABLE_FAILED;
332     }
333 
334     if (!data.WriteString(deviceType)) {
335         ANS_LOGE("[IsDistributedEnabledByBundle] fail: write deviceType failed");
336         return ERR_ANS_PARCELABLE_FAILED;
337     }
338 
339     MessageParcel reply;
340     MessageOption option = {MessageOption::TF_SYNC};
341     ErrCode result = InnerTransact(NotificationInterfaceCode::GET_DISTRIBUTED_ENABLED_BY_BUNDLE, option, data, reply);
342     if (result != ERR_OK) {
343         ANS_LOGE("[IsDistributedEnabledByBundle] fail: transact ErrCode=%{public}d", result);
344         return ERR_ANS_TRANSACT_FAILED;
345     }
346 
347     if (!reply.ReadInt32(result)) {
348         ANS_LOGE("[IsDistributedEnabledByBundle] fail: read result failed.");
349         return ERR_ANS_PARCELABLE_FAILED;
350     }
351 
352     if (!reply.ReadBool(enabled)) {
353         ANS_LOGE("[IsDistributedEnabledByBundle] fail: read enabled failed.");
354         return ERR_ANS_PARCELABLE_FAILED;
355     }
356 
357     return result;
358 }
359 
SetSmartReminderEnabled(const std::string & deviceType,const bool enabled)360 ErrCode AnsManagerProxy::SetSmartReminderEnabled(const std::string &deviceType, const bool enabled)
361 {
362     ANS_LOGD("enter");
363     MessageParcel data;
364     if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
365         ANS_LOGE("[SetSmartReminderEnabled] fail: write interface token failed.");
366         return ERR_ANS_PARCELABLE_FAILED;
367     }
368 
369     if (!data.WriteString(deviceType)) {
370         ANS_LOGE("[SetSmartReminderEnabled] fail: write deviceType failed");
371         return ERR_ANS_PARCELABLE_FAILED;
372     }
373 
374     if (!data.WriteBool(enabled)) {
375         ANS_LOGE("[SetSmartReminderEnabled] fail: write enabled failed");
376         return ERR_ANS_PARCELABLE_FAILED;
377     }
378 
379     MessageParcel reply;
380     MessageOption option = {MessageOption::TF_SYNC};
381     ErrCode result = InnerTransact(NotificationInterfaceCode::SET_SMART_REMINDER_ENABLED, option, data, reply);
382     if (result != ERR_OK) {
383         ANS_LOGE("[SetSmartReminderEnabled] fail: transact ErrCode=%{public}d", result);
384         return ERR_ANS_TRANSACT_FAILED;
385     }
386 
387     if (!reply.ReadInt32(result)) {
388         ANS_LOGE("[SetSmartReminderEnabled] fail: read result failed.");
389         return ERR_ANS_PARCELABLE_FAILED;
390     }
391 
392     return result;
393 }
394 
IsSmartReminderEnabled(const std::string & deviceType,bool & enabled)395 ErrCode AnsManagerProxy::IsSmartReminderEnabled(const std::string &deviceType, bool &enabled)
396 {
397     ANS_LOGD("enter");
398     MessageParcel data;
399     if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
400         ANS_LOGE("[IsSmartReminderEnabled] fail: write interface token failed.");
401         return ERR_ANS_PARCELABLE_FAILED;
402     }
403 
404     if (!data.WriteString(deviceType)) {
405         ANS_LOGE("[IsSmartReminderEnabled] fail: write deviceType failed");
406         return ERR_ANS_PARCELABLE_FAILED;
407     }
408 
409     MessageParcel reply;
410     MessageOption option = {MessageOption::TF_SYNC};
411     ErrCode result = InnerTransact(NotificationInterfaceCode::GET_SMART_REMINDER_ENABLED, option, data, reply);
412     if (result != ERR_OK) {
413         ANS_LOGE("[IsSmartReminderEnabled] fail: transact ErrCode=%{public}d", result);
414         return ERR_ANS_TRANSACT_FAILED;
415     }
416 
417     if (!reply.ReadInt32(result)) {
418         ANS_LOGE("[IsSmartReminderEnabled] fail: read result failed.");
419         return ERR_ANS_PARCELABLE_FAILED;
420     }
421 
422     if (!reply.ReadBool(enabled)) {
423         ANS_LOGE("[IsSmartReminderEnabled] fail: read enabled failed.");
424         return ERR_ANS_PARCELABLE_FAILED;
425     }
426 
427     return result;
428 }
429 
IsNeedSilentInDoNotDisturbMode(const std::string & phoneNumber,int32_t callerType)430 ErrCode AnsManagerProxy::IsNeedSilentInDoNotDisturbMode(const std::string &phoneNumber, int32_t callerType)
431 {
432     MessageParcel data;
433     if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
434         ANS_LOGE("[IsNeedSilentInDoNotDisturbMode] fail: write interface token failed.");
435         return ERR_ANS_PARCELABLE_FAILED;
436     }
437 
438     if (!data.WriteString(phoneNumber)) {
439         ANS_LOGE("[IsNeedSilentInDoNotDisturbMode] fail: write phoneNumber failed.");
440         return ERR_ANS_PARCELABLE_FAILED;
441     }
442 
443     if (!data.WriteInt32(callerType)) {
444         ANS_LOGE("[IsNeedSilentInDoNotDisturbMode] fail: write callerType failed");
445         return ERR_ANS_PARCELABLE_FAILED;
446     }
447 
448     MessageParcel reply;
449     MessageOption option = {MessageOption::TF_SYNC};
450     ErrCode result = InnerTransact(NotificationInterfaceCode::IS_NEED_SILENT_IN_DO_NOT_DISTURB_MODE,
451         option, data, reply);
452     if (result != ERR_OK) {
453         ANS_LOGE("[IsNeedSilentInDoNotDisturbMode] fail: transact ErrCode=%{public}d", result);
454         return ERR_ANS_TRANSACT_FAILED;
455     }
456 
457     if (!reply.ReadInt32(result)) {
458         ANS_LOGE("[IsNeedSilentInDoNotDisturbMode] fail: read result failed.");
459         return ERR_ANS_PARCELABLE_FAILED;
460     }
461 
462     return result;
463 }
464 
GetDoNotDisturbProfile(int32_t id,sptr<NotificationDoNotDisturbProfile> & profile)465 ErrCode AnsManagerProxy::GetDoNotDisturbProfile(int32_t id, sptr<NotificationDoNotDisturbProfile> &profile)
466 {
467     MessageParcel data;
468     if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
469         ANS_LOGE("GetDoNotDisturbProfile write interface token failed.");
470         return ERR_ANS_PARCELABLE_FAILED;
471     }
472 
473     if (!data.WriteInt32(id)) {
474         ANS_LOGE("GetDoNotDisturbProfile write id failed");
475         return ERR_ANS_PARCELABLE_FAILED;
476     }
477 
478     MessageParcel reply;
479     MessageOption option = {MessageOption::TF_SYNC};
480     ErrCode result = InnerTransact(NotificationInterfaceCode::GET_DONOTDISTURB_PROFILE, option, data, reply);
481     if (result != ERR_OK) {
482         ANS_LOGE("GetDoNotDisturbProfile transact ErrCode=%{public}d", result);
483         return ERR_ANS_TRANSACT_FAILED;
484     }
485 
486     if (!reply.ReadInt32(result)) {
487         ANS_LOGE("GetDoNotDisturbProfile read result failed.");
488         return ERR_ANS_PARCELABLE_FAILED;
489     }
490 
491     if (result != ERR_OK) {
492         ANS_LOGE("GetDoNotDisturbProfile result failed %{public}d.", result);
493         return result;
494     }
495 
496     profile = reply.ReadParcelable<NotificationDoNotDisturbProfile>();
497     if (profile == nullptr) {
498         ANS_LOGE("GetDoNotDisturbProfile read data failed.");
499         return ERR_ANS_PARCELABLE_FAILED;
500     }
501 
502     return ERR_OK;
503 }
504 }  // namespace Notification
505 }  // namespace OHOS
506