1 /*
2 * Copyright (c) 2021-2022 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 "ans_subscriber_proxy.h"
17
18 #include "ans_inner_errors.h"
19 #include "ans_log_wrapper.h"
20 #include "message_option.h"
21 #include "message_parcel.h"
22
23 namespace OHOS {
24 namespace Notification {
AnsSubscriberProxy(const sptr<IRemoteObject> & impl)25 AnsSubscriberProxy::AnsSubscriberProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<AnsSubscriberInterface>(impl)
26 {}
27
~AnsSubscriberProxy()28 AnsSubscriberProxy::~AnsSubscriberProxy()
29 {}
30
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)31 ErrCode AnsSubscriberProxy::InnerTransact(
32 uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
33 {
34 auto remote = Remote();
35 if (remote == nullptr) {
36 ANS_LOGE("[InnerTransact] fail: get Remote fail code %{public}u", code);
37 return ERR_DEAD_OBJECT;
38 }
39
40 int32_t err = remote->SendRequest(code, data, reply, flags);
41 switch (err) {
42 case NO_ERROR: {
43 return ERR_OK;
44 }
45 case DEAD_OBJECT: {
46 ANS_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
47 return ERR_DEAD_OBJECT;
48 }
49 default: {
50 ANS_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
51 return ERR_ANS_TRANSACT_FAILED;
52 }
53 }
54 }
55
OnConnected()56 void AnsSubscriberProxy::OnConnected()
57 {
58 MessageParcel data;
59 if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
60 ANS_LOGE("[OnConnected] fail: write interface token failed.");
61 return;
62 }
63
64 MessageParcel reply;
65 MessageOption option = {MessageOption::TF_ASYNC};
66 ErrCode result = InnerTransact(ON_CONNECTED, option, data, reply);
67 if (result != ERR_OK) {
68 ANS_LOGE("[OnConnected] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
69 return;
70 }
71 }
72
OnDisconnected()73 void AnsSubscriberProxy::OnDisconnected()
74 {
75 MessageParcel data;
76 if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
77 ANS_LOGE("[OnDisconnected] fail: write interface token failed.");
78 return;
79 }
80
81 MessageParcel reply;
82 MessageOption option = {MessageOption::TF_ASYNC};
83 ErrCode result = InnerTransact(ON_DISCONNECTED, option, data, reply);
84 if (result != ERR_OK) {
85 ANS_LOGE("[OnDisconnected] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
86 return;
87 }
88 }
89
OnConsumed(const sptr<Notification> & notification)90 void AnsSubscriberProxy::OnConsumed(const sptr<Notification> ¬ification)
91 {
92 if (notification == nullptr) {
93 ANS_LOGE("[OnConsumed] fail: notification is nullptr.");
94 return;
95 }
96
97 MessageParcel data;
98 if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
99 ANS_LOGE("[OnConsumed] fail: write interface token failed.");
100 return;
101 }
102
103 if (!data.WriteParcelable(notification)) {
104 ANS_LOGE("[OnConsumed] fail: write notification failed.");
105 return;
106 }
107
108 MessageParcel reply;
109 MessageOption option = {MessageOption::TF_ASYNC};
110 ErrCode result = InnerTransact(ON_CONSUMED, option, data, reply);
111 if (result != ERR_OK) {
112 ANS_LOGE("[OnConsumed] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
113 return;
114 }
115 }
116
OnConsumed(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap)117 void AnsSubscriberProxy::OnConsumed(
118 const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap)
119 {
120 if (notification == nullptr) {
121 ANS_LOGE("[OnConsumed] fail: notification is nullptr.");
122 return;
123 }
124
125 MessageParcel data;
126 if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
127 ANS_LOGE("[OnConsumed] fail: write interface token failed.");
128 return;
129 }
130
131 if (!data.WriteParcelable(notification)) {
132 ANS_LOGE("[OnConsumed] fail: write notification failed.");
133 return;
134 }
135
136 if (!data.WriteBool(notificationMap != nullptr)) {
137 ANS_LOGE("[OnConsumed] fail: write existMap failed");
138 return;
139 }
140
141 if (notificationMap != nullptr) {
142 if (!data.WriteParcelable(notificationMap)) {
143 ANS_LOGE("[OnConsumed] fail: write notificationMap failed");
144 return;
145 }
146 }
147
148 MessageParcel reply;
149 MessageOption option = {MessageOption::TF_ASYNC};
150 ErrCode result = InnerTransact(ON_CONSUMED_MAP, option, data, reply);
151 if (result != ERR_OK) {
152 ANS_LOGE("[OnConsumed] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
153 return;
154 }
155 }
156
OnCanceled(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)157 void AnsSubscriberProxy::OnCanceled(
158 const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap, int32_t deleteReason)
159 {
160 if (notification == nullptr) {
161 ANS_LOGE("[OnCanceled] fail: notification is nullptr.");
162 return;
163 }
164
165 MessageParcel data;
166 if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
167 ANS_LOGE("[OnCanceled] fail: write interface token failed.");
168 return;
169 }
170
171 if (!data.WriteParcelable(notification)) {
172 ANS_LOGE("[OnCanceled] fail: write notification failed.");
173 return;
174 }
175
176 if (!data.WriteBool(notificationMap != nullptr)) {
177 ANS_LOGE("[OnCanceled] fail: write existMap failed");
178 return;
179 }
180
181 if (notificationMap != nullptr) {
182 if (!data.WriteParcelable(notificationMap)) {
183 ANS_LOGE("[OnCanceled] fail: write notificationMap failed");
184 return;
185 }
186 }
187
188 if (!data.WriteInt32(deleteReason)) {
189 ANS_LOGE("[OnCanceled] fail: write deleteReason failed.");
190 return;
191 }
192
193 MessageParcel reply;
194 MessageOption option = {MessageOption::TF_ASYNC};
195 ErrCode result = InnerTransact(ON_CANCELED_MAP, option, data, reply);
196 if (result != ERR_OK) {
197 ANS_LOGE("[OnCanceled] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
198 return;
199 }
200 }
201
OnUpdated(const sptr<NotificationSortingMap> & notificationMap)202 void AnsSubscriberProxy::OnUpdated(const sptr<NotificationSortingMap> ¬ificationMap)
203 {
204 if (notificationMap == nullptr) {
205 ANS_LOGE("[OnUpdated] fail: notificationMap is empty.");
206 return;
207 }
208
209 MessageParcel data;
210 if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
211 ANS_LOGE("[OnUpdated] fail: write interface token failed.");
212 return;
213 }
214
215 if (!data.WriteParcelable(notificationMap)) {
216 ANS_LOGE("[OnUpdated] fail: write notificationMap failed.");
217 return;
218 }
219
220 MessageParcel reply;
221 MessageOption option = {MessageOption::TF_ASYNC};
222 ErrCode result = InnerTransact(ON_UPDATED, option, data, reply);
223 if (result != ERR_OK) {
224 ANS_LOGE("[OnUpdated] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
225 return;
226 }
227 }
228
OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> & date)229 void AnsSubscriberProxy::OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date)
230 {
231 MessageParcel data;
232 if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
233 ANS_LOGE("[OnDoNotDisturbDateChange] fail: write interface token failed.");
234 return;
235 }
236
237 if (!data.WriteParcelable(date)) {
238 ANS_LOGE("[OnDoNotDisturbDateChange] fail: write date failed");
239 return;
240 }
241
242 MessageParcel reply;
243 MessageOption option = {MessageOption::TF_ASYNC};
244 ErrCode result = InnerTransact(ON_DND_DATE_CHANGED, option, data, reply);
245 if (result != ERR_OK) {
246 ANS_LOGE("[OnDoNotDisturbDateChange] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
247 return;
248 }
249 }
250
OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> & callbackData)251 void AnsSubscriberProxy::OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData)
252 {
253 MessageParcel data;
254 if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
255 ANS_LOGE("[OnEnabledNotificationChanged] fail: write interface token failed.");
256 return;
257 }
258
259 if (!data.WriteParcelable(callbackData)) {
260 ANS_LOGE("[OnEnabledNotificationChanged] fail: write callbackData failed");
261 return;
262 }
263
264 MessageParcel reply;
265 MessageOption option = {MessageOption::TF_ASYNC};
266 ErrCode result = InnerTransact(ON_ENABLED_NOTIFICATION_CHANGED, option, data, reply);
267 if (result != ERR_OK) {
268 ANS_LOGE("[OnEnabledNotificationChanged] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
269 return;
270 }
271 }
272 } // namespace Notification
273 } // namespace OHOS
274