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_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 {
AnsSubscriberLocalLiveViewProxy(const sptr<IRemoteObject> & impl)25 AnsSubscriberLocalLiveViewProxy::AnsSubscriberLocalLiveViewProxy(
26     const sptr<IRemoteObject> &impl) : IRemoteProxy<AnsSubscriberLocalLiveViewInterface>(impl)
27 {}
28 
~AnsSubscriberLocalLiveViewProxy()29 AnsSubscriberLocalLiveViewProxy::~AnsSubscriberLocalLiveViewProxy()
30 {}
31 
InnerTransact(NotificationInterfaceCode code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)32 ErrCode AnsSubscriberLocalLiveViewProxy::InnerTransact(
33     NotificationInterfaceCode code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
34 {
35     auto remote = Remote();
36     if (remote == nullptr) {
37         ANS_LOGE("[InnerTransact] fail: get Remote fail code %{public}u", code);
38         return ERR_DEAD_OBJECT;
39     }
40 
41     int32_t err = remote->SendRequest(static_cast<uint32_t>(code), data, reply, flags);
42     switch (err) {
43         case NO_ERROR: {
44             return ERR_OK;
45         }
46         case DEAD_OBJECT: {
47             ANS_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
48             return ERR_DEAD_OBJECT;
49         }
50         default: {
51             ANS_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
52             return ERR_ANS_TRANSACT_FAILED;
53         }
54     }
55 }
56 
OnConnected()57 void AnsSubscriberLocalLiveViewProxy::OnConnected()
58 {
59     MessageParcel data;
60     if (!data.WriteInterfaceToken(AnsSubscriberLocalLiveViewProxy::GetDescriptor())) {
61         ANS_LOGE("[OnConnected] fail: write interface token failed.");
62         return;
63     }
64 
65     MessageParcel reply;
66     MessageOption option = {MessageOption::TF_ASYNC};
67     ErrCode result = InnerTransact(NotificationInterfaceCode::ON_CONNECTED, option, data, reply);
68     if (result != ERR_OK) {
69         ANS_LOGE("[OnConnected] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
70         return;
71     }
72 }
73 
OnDisconnected()74 void AnsSubscriberLocalLiveViewProxy::OnDisconnected()
75 {
76     MessageParcel data;
77     if (!data.WriteInterfaceToken(AnsSubscriberLocalLiveViewProxy::GetDescriptor())) {
78         ANS_LOGE("[OnDisconnected] fail: write interface token failed.");
79         return;
80     }
81 
82     MessageParcel reply;
83     MessageOption option = {MessageOption::TF_ASYNC};
84     ErrCode result = InnerTransact(NotificationInterfaceCode::ON_DISCONNECTED, option, data, reply);
85     if (result != ERR_OK) {
86         ANS_LOGE("[OnDisconnected] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
87         return;
88     }
89 }
90 
91 
OnResponse(int32_t notificationId,sptr<NotificationButtonOption> buttonOption)92 void AnsSubscriberLocalLiveViewProxy::OnResponse(int32_t notificationId, sptr<NotificationButtonOption> buttonOption)
93 {
94     MessageParcel data;
95     if (!data.WriteInterfaceToken(AnsSubscriberLocalLiveViewProxy::GetDescriptor())) {
96         ANS_LOGE("[OnResponse] fail: write interface token failed.");
97         return;
98     }
99 
100     if (!data.WriteInt32(notificationId)) {
101         ANS_LOGE("[OnResponse] fail: write notificationId failed");
102         return;
103     }
104 
105     if (!data.WriteParcelable(buttonOption)) {
106         ANS_LOGE("[OnResponse] fail: write buttonName failed");
107         return;
108     }
109 
110     MessageParcel reply;
111     MessageOption option = {MessageOption::TF_ASYNC};
112     ErrCode result = InnerTransact(NotificationInterfaceCode::ON_RESPONSE, option, data, reply);
113     if (result != ERR_OK) {
114         ANS_LOGE("[OnResponse] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
115         return;
116     }
117 }
118 }  // namespace Notification
119 }  // namespace OHOS
120