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 
~AnsSubscriberLocalLiveViewStub()33 AnsSubscriberLocalLiveViewStub::~AnsSubscriberLocalLiveViewStub() {}
34 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & flags)35 int32_t AnsSubscriberLocalLiveViewStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
36     MessageOption &flags)
37 {
38     std::u16string descriptor = AnsSubscriberLocalLiveViewStub::GetDescriptor();
39     std::u16string remoteDescriptor = data.ReadInterfaceToken();
40     if (descriptor != remoteDescriptor) {
41         ANS_LOGW("[OnRemoteRequest] fail: invalid interface token!");
42         return OBJECT_NULL;
43     }
44     ErrCode result = NO_ERROR;
45     switch (code) {
46         case static_cast<uint32_t>(NotificationInterfaceCode::ON_CONNECTED): {
47             result = HandleOnConnected(data, reply);
48             break;
49         }
50         case static_cast<uint32_t>(NotificationInterfaceCode::ON_DISCONNECTED): {
51             result = HandleOnDisconnected(data, reply);
52             break;
53         }
54         case static_cast<uint32_t>(NotificationInterfaceCode::ON_RESPONSE): {
55             result = HandleOnResponse(data, reply);
56             break;
57         }
58         default: {
59             ANS_LOGE("[OnRemoteRequest] fail: unknown code!");
60             return IPCObjectStub::OnRemoteRequest(code, data, reply, flags);
61         }
62     }
63     return result;
64 }
65 
HandleOnConnected(MessageParcel & data,MessageParcel & reply)66 ErrCode AnsSubscriberLocalLiveViewStub::HandleOnConnected(MessageParcel &data, MessageParcel &reply)
67 {
68     OnConnected();
69     return ERR_OK;
70 }
71 
HandleOnDisconnected(MessageParcel & data,MessageParcel & reply)72 ErrCode AnsSubscriberLocalLiveViewStub::HandleOnDisconnected(MessageParcel &data, MessageParcel &reply)
73 {
74     OnDisconnected();
75     return ERR_OK;
76 }
77 
78 template <typename T>
ReadParcelableVector(std::vector<sptr<T>> & parcelableInfos,MessageParcel & data)79 bool AnsSubscriberLocalLiveViewStub::ReadParcelableVector(std::vector<sptr<T>> &parcelableInfos, MessageParcel &data)
80 {
81     int32_t infoSize = 0;
82     if (!data.ReadInt32(infoSize)) {
83         ANS_LOGE("read Parcelable size failed.");
84         return false;
85     }
86 
87     parcelableInfos.clear();
88     infoSize = (infoSize < MAX_PARCELABLE_VECTOR_NUM) ? infoSize : MAX_PARCELABLE_VECTOR_NUM;
89     for (int32_t index = 0; index < infoSize; index++) {
90         sptr<T> info = data.ReadStrongParcelable<T>();
91         if (info == nullptr) {
92             ANS_LOGE("read Parcelable infos failed.");
93             return false;
94         }
95         parcelableInfos.emplace_back(info);
96     }
97 
98     return true;
99 }
100 
HandleOnResponse(MessageParcel & data,MessageParcel & reply)101 ErrCode AnsSubscriberLocalLiveViewStub::HandleOnResponse(MessageParcel &data, MessageParcel &reply)
102 {
103     int32_t notificationId = 0;
104     if (!data.ReadInt32(notificationId)) {
105         ANS_LOGE("[HandleOnResponse] fail : read notificationId failed");
106         return ERR_ANS_PARCELABLE_FAILED;
107     }
108     sptr<NotificationButtonOption> buttonOption = nullptr;
109     buttonOption = data.ReadParcelable<NotificationButtonOption>();
110     if (buttonOption == nullptr) {
111         ANS_LOGE("[HandleOnResponse] fail : read buttonOption failed");
112         return ERR_ANS_PARCELABLE_FAILED;
113     }
114     OnResponse(notificationId, buttonOption);
115     return ERR_OK;
116 }
117 
OnConnected()118 void AnsSubscriberLocalLiveViewStub::OnConnected() {}
119 
OnDisconnected()120 void AnsSubscriberLocalLiveViewStub::OnDisconnected() {}
121 
OnResponse(int32_t notificationId,sptr<NotificationButtonOption> buttonOption)122 void AnsSubscriberLocalLiveViewStub::OnResponse(int32_t notificationId, sptr<NotificationButtonOption> buttonOption) {}
123 } // namespace Notification
124 } // namespace OHOS
125