• 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(ON_CONNECTED,
30         std::bind(&AnsSubscriberStub::HandleOnConnected, this, std::placeholders::_1, std::placeholders::_2));
31     interfaces_.emplace(ON_DISCONNECTED,
32         std::bind(&AnsSubscriberStub::HandleOnDisconnected, this, std::placeholders::_1, std::placeholders::_2));
33     interfaces_.emplace(ON_CONSUMED,
34         std::bind(&AnsSubscriberStub::HandleOnConsumed, this, std::placeholders::_1, std::placeholders::_2));
35     interfaces_.emplace(ON_CONSUMED_MAP,
36         std::bind(&AnsSubscriberStub::HandleOnConsumedMap, this, std::placeholders::_1, std::placeholders::_2));
37     interfaces_.emplace(ON_CANCELED,
38         std::bind(&AnsSubscriberStub::HandleOnCanceled, this, std::placeholders::_1, std::placeholders::_2));
39     interfaces_.emplace(ON_CANCELED_MAP,
40         std::bind(&AnsSubscriberStub::HandleOnCanceledMap, this, std::placeholders::_1, std::placeholders::_2));
41     interfaces_.emplace(
42         ON_UPDATED, std::bind(&AnsSubscriberStub::HandleOnUpdated, this, std::placeholders::_1, std::placeholders::_2));
43     interfaces_.emplace(ON_DND_DATE_CHANGED,
44         std::bind(
45             &AnsSubscriberStub::HandleOnDoNotDisturbDateChange, this, std::placeholders::_1, std::placeholders::_2));
46     interfaces_.emplace(ON_ENABLED_NOTIFICATION_CHANGED,
47         std::bind(&AnsSubscriberStub::HandleOnEnabledNotificationChanged, this, std::placeholders::_1,
48             std::placeholders::_2));
49 }
50 
~AnsSubscriberStub()51 AnsSubscriberStub::~AnsSubscriberStub()
52 {}
53 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & flags)54 int32_t AnsSubscriberStub::OnRemoteRequest(
55     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &flags)
56 {
57     std::u16string descriptor = AnsSubscriberStub::GetDescriptor();
58     std::u16string remoteDescriptor = data.ReadInterfaceToken();
59     if (descriptor != remoteDescriptor) {
60         ANS_LOGW("[OnRemoteRequest] fail: invalid interface token!");
61         return OBJECT_NULL;
62     }
63 
64     auto it = interfaces_.find(code);
65     if (it == interfaces_.end()) {
66         ANS_LOGW("[OnRemoteRequest] fail: unknown code!");
67         return IRemoteStub<IAnsSubscriber>::OnRemoteRequest(code, data, reply, flags);
68     }
69 
70     auto fun = it->second;
71     if (fun == nullptr) {
72         ANS_LOGW("[OnRemoteRequest] fail: not find function!");
73         return IRemoteStub<IAnsSubscriber>::OnRemoteRequest(code, data, reply, flags);
74     }
75 
76     fun(data, reply);
77     return NO_ERROR;
78 }
79 
HandleOnConnected(MessageParcel & data,MessageParcel & reply)80 ErrCode AnsSubscriberStub::HandleOnConnected(MessageParcel &data, MessageParcel &reply)
81 {
82     OnConnected();
83     return ERR_OK;
84 }
85 
HandleOnDisconnected(MessageParcel & data,MessageParcel & reply)86 ErrCode AnsSubscriberStub::HandleOnDisconnected(MessageParcel &data, MessageParcel &reply)
87 {
88     OnDisconnected();
89     return ERR_OK;
90 }
91 
HandleOnConsumed(MessageParcel & data,MessageParcel & reply)92 ErrCode AnsSubscriberStub::HandleOnConsumed(MessageParcel &data, MessageParcel &reply)
93 {
94     sptr<Notification> notification = data.ReadParcelable<Notification>();
95     if (!notification) {
96         ANS_LOGW("[HandleOnConsumed] fail: notification ReadParcelable failed");
97         return ERR_ANS_PARCELABLE_FAILED;
98     }
99 
100     OnConsumed(notification);
101     return ERR_OK;
102 }
103 
HandleOnConsumedMap(MessageParcel & data,MessageParcel & reply)104 ErrCode AnsSubscriberStub::HandleOnConsumedMap(MessageParcel &data, MessageParcel &reply)
105 {
106     sptr<Notification> notification = data.ReadParcelable<Notification>();
107     if (!notification) {
108         ANS_LOGW("[HandleOnConsumedMap] fail: notification ReadParcelable failed");
109         return ERR_ANS_PARCELABLE_FAILED;
110     }
111 
112     bool existMap = false;
113     if (!data.ReadBool(existMap)) {
114         ANS_LOGW("[HandleOnConsumedMap] fail: read existMap failed");
115         return ERR_ANS_PARCELABLE_FAILED;
116     }
117 
118     sptr<NotificationSortingMap> notificationMap = nullptr;
119     if (existMap) {
120         notificationMap = data.ReadParcelable<NotificationSortingMap>();
121         if (notificationMap == nullptr) {
122             ANS_LOGW("[HandleOnConsumedMap] fail: read NotificationSortingMap failed");
123             return ERR_ANS_PARCELABLE_FAILED;
124         }
125     }
126 
127     OnConsumed(notification, notificationMap);
128     return ERR_OK;
129 }
130 
HandleOnCanceled(MessageParcel & data,MessageParcel & reply)131 ErrCode AnsSubscriberStub::HandleOnCanceled(MessageParcel &data, MessageParcel &reply)
132 {
133     sptr<Notification> notification = data.ReadParcelable<Notification>();
134     if (!notification) {
135         ANS_LOGW("[HandleOnCanceled] fail: notification ReadParcelable failed");
136         return ERR_ANS_PARCELABLE_FAILED;
137     }
138 
139     OnCanceled(notification);
140     return ERR_OK;
141 }
142 
HandleOnCanceledMap(MessageParcel & data,MessageParcel & reply)143 ErrCode AnsSubscriberStub::HandleOnCanceledMap(MessageParcel &data, MessageParcel &reply)
144 {
145     sptr<Notification> notification = data.ReadParcelable<Notification>();
146     if (!notification) {
147         ANS_LOGW("[HandleOnCanceledMap] fail: notification ReadParcelable failed");
148         return ERR_ANS_PARCELABLE_FAILED;
149     }
150 
151     bool existMap = false;
152     if (!data.ReadBool(existMap)) {
153         ANS_LOGW("[HandleOnCanceledMap] fail: read existMap failed");
154         return ERR_ANS_PARCELABLE_FAILED;
155     }
156 
157     sptr<NotificationSortingMap> notificationMap = nullptr;
158     if (existMap) {
159         notificationMap = data.ReadParcelable<NotificationSortingMap>();
160         if (notificationMap == nullptr) {
161             ANS_LOGW("[HandleOnCanceledMap] fail: read NotificationSortingMap failed");
162             return ERR_ANS_PARCELABLE_FAILED;
163         }
164     }
165 
166     int reason = 0;
167     if (!data.ReadInt32(reason)) {
168         ANS_LOGW("[HandleOnCanceledMap] fail: read reason failed");
169         return ERR_ANS_PARCELABLE_FAILED;
170     }
171 
172     OnCanceled(notification, notificationMap, reason);
173     return ERR_OK;
174 }
175 
HandleOnUpdated(MessageParcel & data,MessageParcel & reply)176 ErrCode AnsSubscriberStub::HandleOnUpdated(MessageParcel &data, MessageParcel &reply)
177 {
178     sptr<NotificationSortingMap> notificationMap = data.ReadParcelable<NotificationSortingMap>();
179     if (!notificationMap) {
180         ANS_LOGW("[HandleOnUpdated] fail: notificationMap ReadParcelable failed");
181         return ERR_ANS_PARCELABLE_FAILED;
182     }
183 
184     OnUpdated(notificationMap);
185     return ERR_OK;
186 }
187 
HandleOnDoNotDisturbDateChange(MessageParcel & data,MessageParcel & reply)188 ErrCode AnsSubscriberStub::HandleOnDoNotDisturbDateChange(MessageParcel &data, MessageParcel &reply)
189 {
190     sptr<NotificationDoNotDisturbDate> date = data.ReadParcelable<NotificationDoNotDisturbDate>();
191     if (!date) {
192         ANS_LOGW("[HandleOnDoNotDisturbDateChange] fail: date ReadParcelable failed");
193         return ERR_ANS_PARCELABLE_FAILED;
194     }
195     OnDoNotDisturbDateChange(date);
196     return ERR_OK;
197 }
198 
HandleOnEnabledNotificationChanged(MessageParcel & data,MessageParcel & reply)199 ErrCode AnsSubscriberStub::HandleOnEnabledNotificationChanged(MessageParcel &data, MessageParcel &reply)
200 {
201     sptr<EnabledNotificationCallbackData> callbackData = data.ReadParcelable<EnabledNotificationCallbackData>();
202     if (!callbackData) {
203         ANS_LOGW("[HandleOnEnabledNotificationChanged] fail: callbackData ReadParcelable failed");
204         return ERR_ANS_PARCELABLE_FAILED;
205     }
206     OnEnabledNotificationChanged(callbackData);
207     return ERR_OK;
208 }
209 
OnConnected()210 void AnsSubscriberStub::OnConnected()
211 {}
212 
OnDisconnected()213 void AnsSubscriberStub::OnDisconnected()
214 {}
215 
OnConsumed(const sptr<Notification> & notification)216 void AnsSubscriberStub::OnConsumed(const sptr<Notification> &notification)
217 {}
218 
OnConsumed(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap)219 void AnsSubscriberStub::OnConsumed(
220     const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap)
221 {}
222 
OnCanceled(const sptr<Notification> & notification)223 void AnsSubscriberStub::OnCanceled(const sptr<Notification> &notification)
224 {}
225 
OnCanceled(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap,int deleteReason)226 void AnsSubscriberStub::OnCanceled(
227     const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap, int deleteReason)
228 {}
229 
OnUpdated(const sptr<NotificationSortingMap> & notificationMap)230 void AnsSubscriberStub::OnUpdated(const sptr<NotificationSortingMap> &notificationMap)
231 {}
232 
OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> & date)233 void AnsSubscriberStub::OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date)
234 {}
235 
OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> & callbackData)236 void AnsSubscriberStub::OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData)
237 {}
238 }  // namespace Notification
239 }  // namespace OHOS
240