1 /*
2 * Copyright (c) 2021-2023 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 {
RemoveNotification(const sptr<NotificationBundleOption> & bundleOption,int32_t notificationId,const std::string & label,int32_t removeReason)30 ErrCode AnsManagerProxy::RemoveNotification(const sptr<NotificationBundleOption> &bundleOption,
31 int32_t notificationId, const std::string &label, int32_t removeReason)
32 {
33 if (bundleOption == nullptr) {
34 ANS_LOGE("[RemoveNotification] fail: bundle is empty.");
35 return ERR_ANS_INVALID_PARAM;
36 }
37
38 MessageParcel data;
39 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
40 ANS_LOGE("[RemoveNotification] fail:, write interface token failed.");
41 return ERR_ANS_PARCELABLE_FAILED;
42 }
43
44 if (!data.WriteStrongParcelable(bundleOption)) {
45 ANS_LOGE("[RemoveNotification] fail:: write bundle failed");
46 return ERR_ANS_PARCELABLE_FAILED;
47 }
48
49 if (!data.WriteInt32(notificationId)) {
50 ANS_LOGE("[RemoveNotification] fail: write notificationId failed");
51 return ERR_ANS_PARCELABLE_FAILED;
52 }
53
54 if (!data.WriteString(label)) {
55 ANS_LOGE("[RemoveNotification] fail: write label failed");
56 return ERR_ANS_PARCELABLE_FAILED;
57 }
58
59 if (!data.WriteInt32(removeReason)) {
60 ANS_LOGE("[RemoveNotification] fail: write removeReason failed");
61 return ERR_ANS_PARCELABLE_FAILED;
62 }
63
64 MessageParcel reply;
65 MessageOption option = {MessageOption::TF_SYNC};
66 ErrCode result = InnerTransact(NotificationInterfaceCode::REMOVE_NOTIFICATION, option, data, reply);
67 if (result != ERR_OK) {
68 ANS_LOGE("[RemoveNotification] fail: transact ErrCode=%{public}d", result);
69 return ERR_ANS_TRANSACT_FAILED;
70 }
71
72 if (!reply.ReadInt32(result)) {
73 ANS_LOGE("[RemoveNotification] fail: read result error.");
74 return ERR_ANS_PARCELABLE_FAILED;
75 }
76
77 return result;
78 }
79
RemoveAllNotifications(const sptr<NotificationBundleOption> & bundleOption)80 ErrCode AnsManagerProxy::RemoveAllNotifications(const sptr<NotificationBundleOption> &bundleOption)
81 {
82 if (bundleOption == nullptr) {
83 ANS_LOGE("[RemoveAllNotifications] fail: bundle is empty.");
84 return ERR_ANS_INVALID_PARAM;
85 }
86
87 MessageParcel data;
88 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
89 ANS_LOGE("[RemoveAllNotifications] fail:, write interface token failed.");
90 return ERR_ANS_PARCELABLE_FAILED;
91 }
92
93 if (!data.WriteStrongParcelable(bundleOption)) {
94 ANS_LOGE("[RemoveAllNotifications] fail:: write bundle failed");
95 return ERR_ANS_PARCELABLE_FAILED;
96 }
97
98 MessageParcel reply;
99 MessageOption option = {MessageOption::TF_SYNC};
100 ErrCode result = InnerTransact(NotificationInterfaceCode::REMOVE_ALL_NOTIFICATIONS, option, data, reply);
101 if (result != ERR_OK) {
102 ANS_LOGE("[RemoveNotification] fail: transact ErrCode=%{public}d", result);
103 return ERR_ANS_TRANSACT_FAILED;
104 }
105
106 if (!reply.ReadInt32(result)) {
107 ANS_LOGE("[RemoveNotification] fail: read result failed.");
108 return ERR_ANS_PARCELABLE_FAILED;
109 }
110
111 return result;
112 }
113
RemoveNotifications(const std::vector<std::string> & keys,int32_t removeReason)114 ErrCode AnsManagerProxy::RemoveNotifications(const std::vector<std::string> &keys, int32_t removeReason)
115 {
116 if (keys.empty()) {
117 ANS_LOGE("fail: keys is empty.");
118 return ERR_ANS_INVALID_PARAM;
119 }
120
121 MessageParcel data;
122 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
123 ANS_LOGE("fail:, write interface token failed.");
124 return ERR_ANS_PARCELABLE_FAILED;
125 }
126
127 if (!data.WriteInt32(keys.size())) {
128 ANS_LOGE("write keys size failed");
129 return false;
130 }
131
132 if (!data.WriteStringVector(keys)) {
133 ANS_LOGE("fail: write keys failed");
134 return ERR_ANS_PARCELABLE_FAILED;
135 }
136
137 if (!data.WriteInt32(removeReason)) {
138 ANS_LOGE("fail: write removeReason failed");
139 return ERR_ANS_PARCELABLE_FAILED;
140 }
141
142 MessageParcel reply;
143 MessageOption option = {MessageOption::TF_SYNC};
144 ErrCode result = InnerTransact(NotificationInterfaceCode::REMOVE_NOTIFICATIONS_BY_KEYS, option, data, reply);
145 if (result != ERR_OK) {
146 ANS_LOGE("fail: transact ErrCode=%{public}d", result);
147 return ERR_ANS_TRANSACT_FAILED;
148 }
149
150 if (!reply.ReadInt32(result)) {
151 ANS_LOGE("fail: read result failed.");
152 return ERR_ANS_PARCELABLE_FAILED;
153 }
154
155 return result;
156 }
157
Subscribe(const sptr<AnsSubscriberInterface> & subscriber,const sptr<NotificationSubscribeInfo> & info)158 ErrCode AnsManagerProxy::Subscribe(const sptr<AnsSubscriberInterface> &subscriber,
159 const sptr<NotificationSubscribeInfo> &info)
160 {
161 if (subscriber == nullptr) {
162 ANS_LOGE("[Subscribe] fail: subscriber is empty.");
163 return ERR_ANS_INVALID_PARAM;
164 }
165
166 MessageParcel data;
167 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
168 ANS_LOGE("[Subscribe] fail: write interface token failed.");
169 return ERR_ANS_PARCELABLE_FAILED;
170 }
171
172 bool ret = data.WriteRemoteObject(subscriber->AsObject());
173 if (!ret) {
174 ANS_LOGE("[Subscribe] fail: write subscriber failed.");
175 return ERR_ANS_PARCELABLE_FAILED;
176 }
177
178 if (!data.WriteBool(info != nullptr)) {
179 ANS_LOGE("[Subscribe] fail: write isSubcribeInfo failed");
180 return ERR_ANS_PARCELABLE_FAILED;
181 }
182
183 if (info != nullptr) {
184 if (!data.WriteParcelable(info)) {
185 ANS_LOGE("[Subscribe] fail: write subcribeInfo failed");
186 return ERR_ANS_PARCELABLE_FAILED;
187 }
188 }
189 MessageParcel reply;
190 MessageOption option = {MessageOption::TF_SYNC};
191 ErrCode result = InnerTransact(NotificationInterfaceCode::SUBSCRIBE_NOTIFICATION, option, data, reply);
192 if (result != ERR_OK) {
193 ANS_LOGE("[Subscribe] fail: transact ErrCode=%{public}d", result);
194 return ERR_ANS_TRANSACT_FAILED;
195 }
196
197 if (!reply.ReadInt32(result)) {
198 ANS_LOGE("[Subscribe] fail: read result failed.");
199 return ERR_ANS_PARCELABLE_FAILED;
200 }
201
202 return result;
203 }
204
SubscribeSelf(const sptr<AnsSubscriberInterface> & subscriber)205 ErrCode AnsManagerProxy::SubscribeSelf(const sptr<AnsSubscriberInterface> &subscriber)
206 {
207 if (subscriber == nullptr) {
208 ANS_LOGE("[SubscribeSelf] fail: subscriber is empty.");
209 return ERR_ANS_INVALID_PARAM;
210 }
211
212 MessageParcel data;
213 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
214 ANS_LOGE("[SubscribeSelf] fail: write interface token failed.");
215 return ERR_ANS_PARCELABLE_FAILED;
216 }
217
218 bool ret = data.WriteRemoteObject(subscriber->AsObject());
219 if (!ret) {
220 ANS_LOGE("[SubscribeSelf] fail: write subscriber failed.");
221 return ERR_ANS_PARCELABLE_FAILED;
222 }
223
224 MessageParcel reply;
225 MessageOption option = {MessageOption::TF_SYNC};
226 ErrCode result = InnerTransact(NotificationInterfaceCode::SUBSCRIBE_NOTIFICATION_SELF, option, data, reply);
227 if (result != ERR_OK) {
228 ANS_LOGE("[SubscribeSelf] fail: transact ErrCode=%{public}d", result);
229 return ERR_ANS_TRANSACT_FAILED;
230 }
231
232 if (!reply.ReadInt32(result)) {
233 ANS_LOGE("[SubscribeSelf] fail: read result failed.");
234 return ERR_ANS_PARCELABLE_FAILED;
235 }
236
237 return result;
238 }
239
SubscribeLocalLiveView(const sptr<AnsSubscriberLocalLiveViewInterface> & subscriber,const sptr<NotificationSubscribeInfo> & info,const bool isNative)240 ErrCode AnsManagerProxy::SubscribeLocalLiveView(const sptr<AnsSubscriberLocalLiveViewInterface> &subscriber,
241 const sptr<NotificationSubscribeInfo> &info, const bool isNative)
242 {
243 if (subscriber == nullptr) {
244 ANS_LOGE("[SubscribeLocalLiveView] fail: subscriber is empty.");
245 return ERR_ANS_INVALID_PARAM;
246 }
247
248 MessageParcel data;
249 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
250 ANS_LOGE("[SubscribeLocalLiveView] fail: write interface token failed.");
251 return ERR_ANS_PARCELABLE_FAILED;
252 }
253
254 bool ret = data.WriteRemoteObject(subscriber->AsObject());
255 if (!ret) {
256 ANS_LOGE("[SubscribeLocalLiveView] fail: write subscriber failed.");
257 return ERR_ANS_PARCELABLE_FAILED;
258 }
259
260 if (!data.WriteBool(info != nullptr)) {
261 ANS_LOGE("[SubscribeLocalLiveView] fail: write isSubcribeInfo failed");
262 return ERR_ANS_PARCELABLE_FAILED;
263 }
264
265 if (info != nullptr) {
266 if (!data.WriteParcelable(info)) {
267 ANS_LOGE("[SubscribeLocalLiveView] fail: write subcribeInfo failed");
268 return ERR_ANS_PARCELABLE_FAILED;
269 }
270 }
271
272 if (!data.WriteBool(isNative)) {
273 ANS_LOGE("[SubscribeLocalLiveView] fail: write isNative failed");
274 return ERR_ANS_PARCELABLE_FAILED;
275 }
276
277 MessageParcel reply;
278 MessageOption option = {MessageOption::TF_SYNC};
279 ErrCode result = InnerTransact(NotificationInterfaceCode::SUBSCRIBE_LOCAL_LIVE_VIEW_NOTIFICATION,
280 option, data, reply);
281 if (result != ERR_OK) {
282 ANS_LOGE("[Subscribe] fail: transact ErrCode=%{public}d", result);
283 return ERR_ANS_TRANSACT_FAILED;
284 }
285
286 if (!reply.ReadInt32(result)) {
287 ANS_LOGE("[Subscribe] fail: read result failed.");
288 return ERR_ANS_PARCELABLE_FAILED;
289 }
290 return result;
291 }
292
Unsubscribe(const sptr<AnsSubscriberInterface> & subscriber,const sptr<NotificationSubscribeInfo> & info)293 ErrCode AnsManagerProxy::Unsubscribe(
294 const sptr<AnsSubscriberInterface> &subscriber, const sptr<NotificationSubscribeInfo> &info)
295 {
296 if (subscriber == nullptr) {
297 ANS_LOGE("[Unsubscribe] fail: subscriber is empty.");
298 return ERR_ANS_INVALID_PARAM;
299 }
300
301 MessageParcel data;
302 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
303 ANS_LOGE("[Unsubscribe] fail: write interface token failed.");
304 return ERR_ANS_PARCELABLE_FAILED;
305 }
306
307 bool ret = data.WriteRemoteObject(subscriber->AsObject());
308 if (!ret) {
309 ANS_LOGE("[Unsubscribe] fail: write subscriber failed.");
310 return ERR_ANS_PARCELABLE_FAILED;
311 }
312
313 if (!data.WriteBool(info != nullptr)) {
314 ANS_LOGE("[Unsubscribe] fail: write isSubcribeInfo failed");
315 return ERR_ANS_PARCELABLE_FAILED;
316 }
317
318 if (info != nullptr) {
319 if (!data.WriteParcelable(info)) {
320 ANS_LOGE("[Unsubscribe] fail: write subcribeInfo failed");
321 return ERR_ANS_PARCELABLE_FAILED;
322 }
323 }
324
325 MessageParcel reply;
326 MessageOption option = {MessageOption::TF_SYNC};
327 ErrCode result = InnerTransact(NotificationInterfaceCode::UNSUBSCRIBE_NOTIFICATION, option, data, reply);
328 if (result != ERR_OK) {
329 ANS_LOGE("[Unsubscribe] fail: transact ErrCode=%{public}d", result);
330 return ERR_ANS_TRANSACT_FAILED;
331 }
332
333 if (!reply.ReadInt32(result)) {
334 ANS_LOGE("[Unsubscribe] fail: read result failed.");
335 return ERR_ANS_PARCELABLE_FAILED;
336 }
337
338 return result;
339 }
340 } // namespace Notification
341 } // namespace OHOS
342