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
IsNeedSilentInDoNotDisturbMode(const std::string & phoneNumber,int32_t callerType)268 ErrCode AnsManagerProxy::IsNeedSilentInDoNotDisturbMode(const std::string &phoneNumber, int32_t callerType)
269 {
270 MessageParcel data;
271 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
272 ANS_LOGE("[IsNeedSilentInDoNotDisturbMode] fail: write interface token failed.");
273 return ERR_ANS_PARCELABLE_FAILED;
274 }
275
276 if (!data.WriteString(phoneNumber)) {
277 ANS_LOGE("[IsNeedSilentInDoNotDisturbMode] fail: write phoneNumber failed.");
278 return ERR_ANS_PARCELABLE_FAILED;
279 }
280
281 if (!data.WriteInt32(callerType)) {
282 ANS_LOGE("[IsNeedSilentInDoNotDisturbMode] fail: write callerType failed");
283 return ERR_ANS_PARCELABLE_FAILED;
284 }
285
286 MessageParcel reply;
287 MessageOption option = {MessageOption::TF_SYNC};
288 ErrCode result = InnerTransact(NotificationInterfaceCode::IS_NEED_SILENT_IN_DO_NOT_DISTURB_MODE,
289 option, data, reply);
290 if (result != ERR_OK) {
291 ANS_LOGE("[IsNeedSilentInDoNotDisturbMode] fail: transact ErrCode=%{public}d", result);
292 return ERR_ANS_TRANSACT_FAILED;
293 }
294
295 if (!reply.ReadInt32(result)) {
296 ANS_LOGE("[IsNeedSilentInDoNotDisturbMode] fail: read result failed.");
297 return ERR_ANS_PARCELABLE_FAILED;
298 }
299
300 return result;
301 }
302
GetDoNotDisturbProfile(int32_t id,sptr<NotificationDoNotDisturbProfile> & profile)303 ErrCode AnsManagerProxy::GetDoNotDisturbProfile(int32_t id, sptr<NotificationDoNotDisturbProfile> &profile)
304 {
305 MessageParcel data;
306 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
307 ANS_LOGE("GetDoNotDisturbProfile write interface token failed.");
308 return ERR_ANS_PARCELABLE_FAILED;
309 }
310
311 if (!data.WriteInt32(id)) {
312 ANS_LOGE("GetDoNotDisturbProfile write id failed");
313 return ERR_ANS_PARCELABLE_FAILED;
314 }
315
316 MessageParcel reply;
317 MessageOption option = {MessageOption::TF_SYNC};
318 ErrCode result = InnerTransact(NotificationInterfaceCode::GET_DONOTDISTURB_PROFILE, option, data, reply);
319 if (result != ERR_OK) {
320 ANS_LOGE("GetDoNotDisturbProfile transact ErrCode=%{public}d", result);
321 return ERR_ANS_TRANSACT_FAILED;
322 }
323
324 if (!reply.ReadInt32(result)) {
325 ANS_LOGE("GetDoNotDisturbProfile read result failed.");
326 return ERR_ANS_PARCELABLE_FAILED;
327 }
328
329 if (result != ERR_OK) {
330 ANS_LOGE("GetDoNotDisturbProfile result failed %{public}d.", result);
331 return result;
332 }
333
334 profile = reply.ReadParcelable<NotificationDoNotDisturbProfile>();
335 if (profile == nullptr) {
336 ANS_LOGE("GetDoNotDisturbProfile read data failed.");
337 return ERR_ANS_PARCELABLE_FAILED;
338 }
339
340 return ERR_OK;
341 }
342 } // namespace Notification
343 } // namespace OHOS
344