1 /*
2 * Copyright (c) 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 "ans_subscriber_local_live_view_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 "notification_bundle_option.h"
24 #include "notification_button_option.h"
25 #include "parcel.h"
26 #include "refbase.h"
27 #include <string>
28
29 namespace OHOS {
30 namespace Notification {
AnsSubscriberLocalLiveViewStub()31 AnsSubscriberLocalLiveViewStub::AnsSubscriberLocalLiveViewStub()
32 {
33 interfaces_.emplace(NotificationInterfaceCode::ON_CONNECTED,
34 std::bind(&AnsSubscriberLocalLiveViewStub::HandleOnConnected,
35 this, std::placeholders::_1, std::placeholders::_2));
36 interfaces_.emplace(NotificationInterfaceCode::ON_DISCONNECTED,
37 std::bind(&AnsSubscriberLocalLiveViewStub::HandleOnDisconnected,
38 this, std::placeholders::_1, std::placeholders::_2));
39 interfaces_.emplace(NotificationInterfaceCode::ON_RESPONSE,
40 std::bind(&AnsSubscriberLocalLiveViewStub::HandleOnResponse,
41 this, std::placeholders::_1, std::placeholders::_2));
42 }
43
~AnsSubscriberLocalLiveViewStub()44 AnsSubscriberLocalLiveViewStub::~AnsSubscriberLocalLiveViewStub()
45 {}
46
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & flags)47 int32_t AnsSubscriberLocalLiveViewStub::OnRemoteRequest(
48 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &flags)
49 {
50 std::u16string descriptor = AnsSubscriberLocalLiveViewStub::GetDescriptor();
51 std::u16string remoteDescriptor = data.ReadInterfaceToken();
52 if (descriptor != remoteDescriptor) {
53 ANS_LOGW("[OnRemoteRequest] fail: invalid interface token!");
54 return OBJECT_NULL;
55 }
56
57 auto it = interfaces_.find(static_cast<NotificationInterfaceCode>(code));
58 if (it == interfaces_.end()) {
59 ANS_LOGW("[OnRemoteRequest] fail: unknown code!");
60 return IPCObjectStub::OnRemoteRequest(code, data, reply, flags);
61 }
62
63 auto fun = it->second;
64 if (fun == nullptr) {
65 ANS_LOGW("[OnRemoteRequest] fail: not find function!");
66 return IPCObjectStub::OnRemoteRequest(code, data, reply, flags);
67 }
68
69 fun(data, reply);
70 return NO_ERROR;
71 }
72
HandleOnConnected(MessageParcel & data,MessageParcel & reply)73 ErrCode AnsSubscriberLocalLiveViewStub::HandleOnConnected(MessageParcel &data, MessageParcel &reply)
74 {
75 OnConnected();
76 return ERR_OK;
77 }
78
HandleOnDisconnected(MessageParcel & data,MessageParcel & reply)79 ErrCode AnsSubscriberLocalLiveViewStub::HandleOnDisconnected(MessageParcel &data, MessageParcel &reply)
80 {
81 OnDisconnected();
82 return ERR_OK;
83 }
84
85 template<typename T>
ReadParcelableVector(std::vector<sptr<T>> & parcelableInfos,MessageParcel & data)86 bool AnsSubscriberLocalLiveViewStub::ReadParcelableVector(std::vector<sptr<T>> &parcelableInfos, MessageParcel &data)
87 {
88 int32_t infoSize = 0;
89 if (!data.ReadInt32(infoSize)) {
90 ANS_LOGE("read Parcelable size failed.");
91 return false;
92 }
93
94 parcelableInfos.clear();
95 infoSize = (infoSize < MAX_PARCELABLE_VECTOR_NUM) ? infoSize : MAX_PARCELABLE_VECTOR_NUM;
96 for (int32_t index = 0; index < infoSize; index++) {
97 sptr<T> info = data.ReadStrongParcelable<T>();
98 if (info == nullptr) {
99 ANS_LOGE("read Parcelable infos failed.");
100 return false;
101 }
102 parcelableInfos.emplace_back(info);
103 }
104
105 return true;
106 }
107
HandleOnResponse(MessageParcel & data,MessageParcel & reply)108 ErrCode AnsSubscriberLocalLiveViewStub::HandleOnResponse(MessageParcel &data, MessageParcel &reply)
109 {
110 int32_t notificationId = 0;
111 if (!data.ReadInt32(notificationId)) {
112 ANS_LOGE("[HandleOnResponse] fail : read notificationId failed");
113 return ERR_ANS_PARCELABLE_FAILED;
114 }
115 sptr<NotificationButtonOption> buttonOption = nullptr;
116 buttonOption = data.ReadParcelable<NotificationButtonOption>();
117 if (buttonOption == nullptr) {
118 ANS_LOGE("[HandleOnResponse] fail : read buttonOption failed");
119 return ERR_ANS_PARCELABLE_FAILED;
120 }
121 OnResponse(notificationId, buttonOption);
122 return ERR_OK;
123 }
124
OnConnected()125 void AnsSubscriberLocalLiveViewStub::OnConnected()
126 {}
127
OnDisconnected()128 void AnsSubscriberLocalLiveViewStub::OnDisconnected()
129 {}
130
OnResponse(int32_t notificationId,sptr<NotificationButtonOption> buttonOption)131 void AnsSubscriberLocalLiveViewStub::OnResponse(int32_t notificationId, sptr<NotificationButtonOption> buttonOption)
132 {}
133 } // namespace Notification
134 } // namespace OHOS
135