• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_stub.h"
17 
18 #include "ans_const_define.h"
19 #include "ans_inner_errors.h"
20 #include "ans_log_wrapper.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23 #include "parcel.h"
24 
25 namespace OHOS {
26 namespace Notification {
AnsSubscriberStub()27 AnsSubscriberStub::AnsSubscriberStub()
28 {
29     interfaces_.emplace(NotificationInterfaceCode::ON_CONNECTED,
30         std::bind(&AnsSubscriberStub::HandleOnConnected, this, std::placeholders::_1, std::placeholders::_2));
31     interfaces_.emplace(NotificationInterfaceCode::ON_DISCONNECTED,
32         std::bind(&AnsSubscriberStub::HandleOnDisconnected, this, std::placeholders::_1, std::placeholders::_2));
33     interfaces_.emplace(NotificationInterfaceCode::ON_CONSUMED_MAP,
34         std::bind(&AnsSubscriberStub::HandleOnConsumedMap, this, std::placeholders::_1, std::placeholders::_2));
35     interfaces_.emplace(NotificationInterfaceCode::ON_CONSUMED_LIST_MAP,
36         std::bind(&AnsSubscriberStub::HandleOnConsumedListMap, this, std::placeholders::_1, std::placeholders::_2));
37     interfaces_.emplace(NotificationInterfaceCode::ON_CANCELED_MAP,
38         std::bind(&AnsSubscriberStub::HandleOnCanceledMap, this, std::placeholders::_1, std::placeholders::_2));
39     interfaces_.emplace(NotificationInterfaceCode::ON_CANCELED_LIST_MAP,
40         std::bind(&AnsSubscriberStub::HandleOnCanceledListMap, this, std::placeholders::_1, std::placeholders::_2));
41     interfaces_.emplace(NotificationInterfaceCode::ON_UPDATED,
42         std::bind(&AnsSubscriberStub::HandleOnUpdated, this, std::placeholders::_1, std::placeholders::_2));
43     interfaces_.emplace(NotificationInterfaceCode::ON_DND_DATE_CHANGED,
44         std::bind(
45             &AnsSubscriberStub::HandleOnDoNotDisturbDateChange, this, std::placeholders::_1, std::placeholders::_2));
46     interfaces_.emplace(NotificationInterfaceCode::ON_ENABLED_NOTIFICATION_CHANGED,
47         std::bind(&AnsSubscriberStub::HandleOnEnabledNotificationChanged, this, std::placeholders::_1,
48             std::placeholders::_2));
49     interfaces_.emplace(NotificationInterfaceCode::ON_BADGE_CHANGED,
50         std::bind(&AnsSubscriberStub::HandleOnBadgeChanged, this, std::placeholders::_1, std::placeholders::_2));
51 }
52 
~AnsSubscriberStub()53 AnsSubscriberStub::~AnsSubscriberStub()
54 {}
55 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & flags)56 int32_t AnsSubscriberStub::OnRemoteRequest(
57     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &flags)
58 {
59     std::u16string descriptor = AnsSubscriberStub::GetDescriptor();
60     std::u16string remoteDescriptor = data.ReadInterfaceToken();
61     if (descriptor != remoteDescriptor) {
62         ANS_LOGW("[OnRemoteRequest] fail: invalid interface token!");
63         return OBJECT_NULL;
64     }
65 
66     auto it = interfaces_.find(static_cast<NotificationInterfaceCode>(code));
67     if (it == interfaces_.end()) {
68         ANS_LOGW("[OnRemoteRequest] fail: unknown code!");
69         return IPCObjectStub::OnRemoteRequest(code, data, reply, flags);
70     }
71 
72     auto fun = it->second;
73     if (fun == nullptr) {
74         ANS_LOGW("[OnRemoteRequest] fail: not find function!");
75         return IPCObjectStub::OnRemoteRequest(code, data, reply, flags);
76     }
77 
78     fun(data, reply);
79     return NO_ERROR;
80 }
81 
HandleOnConnected(MessageParcel & data,MessageParcel & reply)82 ErrCode AnsSubscriberStub::HandleOnConnected(MessageParcel &data, MessageParcel &reply)
83 {
84     OnConnected();
85     return ERR_OK;
86 }
87 
HandleOnDisconnected(MessageParcel & data,MessageParcel & reply)88 ErrCode AnsSubscriberStub::HandleOnDisconnected(MessageParcel &data, MessageParcel &reply)
89 {
90     OnDisconnected();
91     return ERR_OK;
92 }
93 
HandleOnConsumedMap(MessageParcel & data,MessageParcel & reply)94 ErrCode AnsSubscriberStub::HandleOnConsumedMap(MessageParcel &data, MessageParcel &reply)
95 {
96     sptr<Notification> notification = data.ReadParcelable<Notification>();
97     if (!notification) {
98         ANS_LOGW("[HandleOnConsumedMap] fail: notification ReadParcelable failed");
99         return ERR_ANS_PARCELABLE_FAILED;
100     }
101 
102     bool existMap = false;
103     if (!data.ReadBool(existMap)) {
104         ANS_LOGW("[HandleOnConsumedMap] fail: read existMap failed");
105         return ERR_ANS_PARCELABLE_FAILED;
106     }
107 
108     sptr<NotificationSortingMap> notificationMap = nullptr;
109     if (existMap) {
110         notificationMap = data.ReadParcelable<NotificationSortingMap>();
111         if (notificationMap == nullptr) {
112             ANS_LOGW("[HandleOnConsumedMap] fail: read NotificationSortingMap failed");
113             return ERR_ANS_PARCELABLE_FAILED;
114         }
115     }
116 
117     OnConsumed(notification, notificationMap);
118     return ERR_OK;
119 }
120 
HandleOnConsumedListMap(MessageParcel & data,MessageParcel & reply)121 ErrCode AnsSubscriberStub::HandleOnConsumedListMap(MessageParcel &data, MessageParcel &reply)
122 {
123     ANS_LOGI("Start handle notifications in consumed list.");
124 
125     std::vector<sptr<Notification>> notifications;
126     if (!ReadParcelableVector(notifications, data)) {
127         ANS_LOGE("read notifications failed");
128         return ERR_ANS_PARCELABLE_FAILED;
129     }
130 
131     bool existMap = false;
132     if (!data.ReadBool(existMap)) {
133         ANS_LOGE("read existMap failed");
134         return ERR_ANS_PARCELABLE_FAILED;
135     }
136 
137     sptr<NotificationSortingMap> notificationMap = nullptr;
138     if (existMap) {
139         notificationMap = data.ReadParcelable<NotificationSortingMap>();
140         if (notificationMap == nullptr) {
141             ANS_LOGE("read NotificationSortingMap failed");
142             return ERR_ANS_PARCELABLE_FAILED;
143         }
144     }
145 
146     OnConsumedList(notifications, notificationMap);
147     return ERR_OK;
148 }
149 
HandleOnCanceledMap(MessageParcel & data,MessageParcel & reply)150 ErrCode AnsSubscriberStub::HandleOnCanceledMap(MessageParcel &data, MessageParcel &reply)
151 {
152     sptr<Notification> notification = data.ReadParcelable<Notification>();
153     if (!notification) {
154         ANS_LOGW("[HandleOnCanceledMap] fail: notification ReadParcelable failed");
155         return ERR_ANS_PARCELABLE_FAILED;
156     }
157 
158     bool existMap = false;
159     if (!data.ReadBool(existMap)) {
160         ANS_LOGW("[HandleOnCanceledMap] fail: read existMap failed");
161         return ERR_ANS_PARCELABLE_FAILED;
162     }
163 
164     sptr<NotificationSortingMap> notificationMap = nullptr;
165     if (existMap) {
166         notificationMap = data.ReadParcelable<NotificationSortingMap>();
167         if (notificationMap == nullptr) {
168             ANS_LOGW("[HandleOnCanceledMap] fail: read NotificationSortingMap failed");
169             return ERR_ANS_PARCELABLE_FAILED;
170         }
171     }
172 
173     int32_t reason = 0;
174     if (!data.ReadInt32(reason)) {
175         ANS_LOGW("[HandleOnCanceledMap] fail: read reason failed");
176         return ERR_ANS_PARCELABLE_FAILED;
177     }
178 
179     OnCanceled(notification, notificationMap, reason);
180     return ERR_OK;
181 }
182 
183 
HandleOnCanceledListMap(MessageParcel & data,MessageParcel & reply)184 ErrCode AnsSubscriberStub::HandleOnCanceledListMap(MessageParcel &data, MessageParcel &reply)
185 {
186     std::vector<sptr<Notification>> notifications;
187     if (!ReadParcelableVector(notifications, data)) {
188         ANS_LOGE("read notifications failed");
189         return ERR_ANS_PARCELABLE_FAILED;
190     }
191 
192     bool existMap = false;
193     if (!data.ReadBool(existMap)) {
194         ANS_LOGE("read existMap failed");
195         return ERR_ANS_PARCELABLE_FAILED;
196     }
197 
198     sptr<NotificationSortingMap> notificationMap = nullptr;
199     if (existMap) {
200         notificationMap = data.ReadParcelable<NotificationSortingMap>();
201         if (notificationMap == nullptr) {
202             ANS_LOGE("read NotificationSortingMap failed");
203             return ERR_ANS_PARCELABLE_FAILED;
204         }
205     }
206 
207     int32_t reason = 0;
208     if (!data.ReadInt32(reason)) {
209         ANS_LOGE("read reason failed");
210         return ERR_ANS_PARCELABLE_FAILED;
211     }
212 
213     OnCanceledList(notifications, notificationMap, reason);
214     return ERR_OK;
215 }
216 
217 
218 template<typename T>
ReadParcelableVector(std::vector<sptr<T>> & parcelableInfos,MessageParcel & data)219 bool AnsSubscriberStub::ReadParcelableVector(std::vector<sptr<T>> &parcelableInfos, MessageParcel &data)
220 {
221     int32_t infoSize = 0;
222     if (!data.ReadInt32(infoSize)) {
223         ANS_LOGE("read Parcelable size failed.");
224         return false;
225     }
226 
227     parcelableInfos.clear();
228     infoSize = (infoSize < MAX_PARCELABLE_VECTOR_NUM) ? infoSize : MAX_PARCELABLE_VECTOR_NUM;
229     for (int32_t index = 0; index < infoSize; index++) {
230         sptr<T> info = data.ReadStrongParcelable<T>();
231         if (info == nullptr) {
232             ANS_LOGE("read Parcelable infos failed.");
233             return false;
234         }
235         parcelableInfos.emplace_back(info);
236     }
237 
238     return true;
239 }
240 
HandleOnUpdated(MessageParcel & data,MessageParcel & reply)241 ErrCode AnsSubscriberStub::HandleOnUpdated(MessageParcel &data, MessageParcel &reply)
242 {
243     sptr<NotificationSortingMap> notificationMap = data.ReadParcelable<NotificationSortingMap>();
244     if (!notificationMap) {
245         ANS_LOGW("[HandleOnUpdated] fail: notificationMap ReadParcelable failed");
246         return ERR_ANS_PARCELABLE_FAILED;
247     }
248 
249     OnUpdated(notificationMap);
250     return ERR_OK;
251 }
252 
HandleOnDoNotDisturbDateChange(MessageParcel & data,MessageParcel & reply)253 ErrCode AnsSubscriberStub::HandleOnDoNotDisturbDateChange(MessageParcel &data, MessageParcel &reply)
254 {
255     sptr<NotificationDoNotDisturbDate> date = data.ReadParcelable<NotificationDoNotDisturbDate>();
256     if (!date) {
257         ANS_LOGW("[HandleOnDoNotDisturbDateChange] fail: date ReadParcelable failed");
258         return ERR_ANS_PARCELABLE_FAILED;
259     }
260     OnDoNotDisturbDateChange(date);
261     return ERR_OK;
262 }
263 
HandleOnEnabledNotificationChanged(MessageParcel & data,MessageParcel & reply)264 ErrCode AnsSubscriberStub::HandleOnEnabledNotificationChanged(MessageParcel &data, MessageParcel &reply)
265 {
266     sptr<EnabledNotificationCallbackData> callbackData = data.ReadParcelable<EnabledNotificationCallbackData>();
267     if (!callbackData) {
268         ANS_LOGW("[HandleOnEnabledNotificationChanged] fail: callbackData ReadParcelable failed");
269         return ERR_ANS_PARCELABLE_FAILED;
270     }
271     OnEnabledNotificationChanged(callbackData);
272     return ERR_OK;
273 }
274 
HandleOnBadgeChanged(MessageParcel & data,MessageParcel & reply)275 ErrCode AnsSubscriberStub::HandleOnBadgeChanged(MessageParcel &data, MessageParcel &reply)
276 {
277     sptr<BadgeNumberCallbackData> callbackData = data.ReadParcelable<BadgeNumberCallbackData>();
278     if (!callbackData) {
279         ANS_LOGW("[HandleOnBadgeChanged] fail: callbackData ReadParcelable failed");
280         return ERR_ANS_PARCELABLE_FAILED;
281     }
282     OnBadgeChanged(callbackData);
283     return ERR_OK;
284 }
285 
OnConnected()286 void AnsSubscriberStub::OnConnected()
287 {}
288 
OnDisconnected()289 void AnsSubscriberStub::OnDisconnected()
290 {}
291 
OnConsumed(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap)292 void AnsSubscriberStub::OnConsumed(
293     const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap)
294 {}
295 
OnConsumedList(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap)296 void AnsSubscriberStub::OnConsumedList(const std::vector<sptr<Notification>> &notifications,
297     const sptr<NotificationSortingMap> &notificationMap)
298 {}
299 
OnCanceled(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)300 void AnsSubscriberStub::OnCanceled(
301     const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason)
302 {}
303 
OnCanceledList(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)304 void AnsSubscriberStub::OnCanceledList(const std::vector<sptr<Notification>> &notifications,
305     const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason)
306 {}
307 
OnUpdated(const sptr<NotificationSortingMap> & notificationMap)308 void AnsSubscriberStub::OnUpdated(const sptr<NotificationSortingMap> &notificationMap)
309 {}
310 
OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> & date)311 void AnsSubscriberStub::OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date)
312 {}
313 
OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> & callbackData)314 void AnsSubscriberStub::OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData)
315 {}
316 
OnBadgeChanged(const sptr<BadgeNumberCallbackData> & badgeData)317 void AnsSubscriberStub::OnBadgeChanged(const sptr<BadgeNumberCallbackData> &badgeData)
318 {}
319 }  // namespace Notification
320 }  // namespace OHOS
321