1 /*
2 * Copyright (c) 2021-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 <unistd.h>
17
18 #include "ans_const_define.h"
19 #include "ans_inner_errors.h"
20 #include "ans_log_wrapper.h"
21 #include "ans_subscriber_local_live_view_interface.h"
22 #include "distributed_notification_service_ipc_interface_code.h"
23 #include "message_option.h"
24 #include "message_parcel.h"
25 #include "parcel.h"
26 #include "ans_manager_proxy.h"
27
28 namespace OHOS {
29 namespace Notification {
Subscribe(const sptr<AnsSubscriberInterface> & subscriber,const sptr<NotificationSubscribeInfo> & info)30 ErrCode AnsManagerProxy::Subscribe(const sptr<AnsSubscriberInterface> &subscriber,
31 const sptr<NotificationSubscribeInfo> &info)
32 {
33 if (subscriber == nullptr) {
34 ANS_LOGE("[Subscribe] fail: subscriber is empty.");
35 return ERR_ANS_INVALID_PARAM;
36 }
37
38 MessageParcel data;
39 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
40 ANS_LOGE("[Subscribe] fail: write interface token failed.");
41 return ERR_ANS_PARCELABLE_FAILED;
42 }
43
44 bool ret = data.WriteRemoteObject(subscriber->AsObject());
45 if (!ret) {
46 ANS_LOGE("[Subscribe] fail: write subscriber failed.");
47 return ERR_ANS_PARCELABLE_FAILED;
48 }
49
50 if (!data.WriteBool(info != nullptr)) {
51 ANS_LOGE("[Subscribe] fail: write isSubcribeInfo failed");
52 return ERR_ANS_PARCELABLE_FAILED;
53 }
54
55 if (info != nullptr) {
56 if (!data.WriteParcelable(info)) {
57 ANS_LOGE("[Subscribe] fail: write subcribeInfo failed");
58 return ERR_ANS_PARCELABLE_FAILED;
59 }
60 }
61 MessageParcel reply;
62 MessageOption option = {MessageOption::TF_SYNC};
63 ErrCode result = InnerTransact(NotificationInterfaceCode::SUBSCRIBE_NOTIFICATION, option, data, reply);
64 if (result != ERR_OK) {
65 ANS_LOGE("[Subscribe] fail: transact ErrCode=%{public}d", result);
66 return ERR_ANS_TRANSACT_FAILED;
67 }
68
69 if (!reply.ReadInt32(result)) {
70 ANS_LOGE("[Subscribe] fail: read result failed.");
71 return ERR_ANS_PARCELABLE_FAILED;
72 }
73
74 return result;
75 }
76
SubscribeSelf(const sptr<AnsSubscriberInterface> & subscriber)77 ErrCode AnsManagerProxy::SubscribeSelf(const sptr<AnsSubscriberInterface> &subscriber)
78 {
79 if (subscriber == nullptr) {
80 ANS_LOGE("[SubscribeSelf] fail: subscriber is empty.");
81 return ERR_ANS_INVALID_PARAM;
82 }
83
84 MessageParcel data;
85 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
86 ANS_LOGE("[SubscribeSelf] fail: write interface token failed.");
87 return ERR_ANS_PARCELABLE_FAILED;
88 }
89
90 bool ret = data.WriteRemoteObject(subscriber->AsObject());
91 if (!ret) {
92 ANS_LOGE("[SubscribeSelf] fail: write subscriber failed.");
93 return ERR_ANS_PARCELABLE_FAILED;
94 }
95
96 MessageParcel reply;
97 MessageOption option = {MessageOption::TF_SYNC};
98 ErrCode result = InnerTransact(NotificationInterfaceCode::SUBSCRIBE_NOTIFICATION_SELF, option, data, reply);
99 if (result != ERR_OK) {
100 ANS_LOGE("[SubscribeSelf] fail: transact ErrCode=%{public}d", result);
101 return ERR_ANS_TRANSACT_FAILED;
102 }
103
104 if (!reply.ReadInt32(result)) {
105 ANS_LOGE("[SubscribeSelf] fail: read result failed.");
106 return ERR_ANS_PARCELABLE_FAILED;
107 }
108
109 return result;
110 }
111
SubscribeLocalLiveView(const sptr<AnsSubscriberLocalLiveViewInterface> & subscriber,const sptr<NotificationSubscribeInfo> & info,const bool isNative)112 ErrCode AnsManagerProxy::SubscribeLocalLiveView(const sptr<AnsSubscriberLocalLiveViewInterface> &subscriber,
113 const sptr<NotificationSubscribeInfo> &info, const bool isNative)
114 {
115 if (subscriber == nullptr) {
116 ANS_LOGE("[SubscribeLocalLiveView] fail: subscriber is empty.");
117 return ERR_ANS_INVALID_PARAM;
118 }
119
120 MessageParcel data;
121 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
122 ANS_LOGE("[SubscribeLocalLiveView] fail: write interface token failed.");
123 return ERR_ANS_PARCELABLE_FAILED;
124 }
125
126 bool ret = data.WriteRemoteObject(subscriber->AsObject());
127 if (!ret) {
128 ANS_LOGE("[SubscribeLocalLiveView] fail: write subscriber failed.");
129 return ERR_ANS_PARCELABLE_FAILED;
130 }
131
132 if (!data.WriteBool(info != nullptr)) {
133 ANS_LOGE("[SubscribeLocalLiveView] fail: write isSubcribeInfo failed");
134 return ERR_ANS_PARCELABLE_FAILED;
135 }
136
137 if (info != nullptr) {
138 if (!data.WriteParcelable(info)) {
139 ANS_LOGE("[SubscribeLocalLiveView] fail: write subcribeInfo failed");
140 return ERR_ANS_PARCELABLE_FAILED;
141 }
142 }
143
144 if (!data.WriteBool(isNative)) {
145 ANS_LOGE("[SubscribeLocalLiveView] fail: write isNative failed");
146 return ERR_ANS_PARCELABLE_FAILED;
147 }
148
149 MessageParcel reply;
150 MessageOption option = {MessageOption::TF_SYNC};
151 ErrCode result = InnerTransact(NotificationInterfaceCode::SUBSCRIBE_LOCAL_LIVE_VIEW_NOTIFICATION,
152 option, data, reply);
153 if (result != ERR_OK) {
154 ANS_LOGE("[Subscribe] fail: transact ErrCode=%{public}d", result);
155 return ERR_ANS_TRANSACT_FAILED;
156 }
157
158 if (!reply.ReadInt32(result)) {
159 ANS_LOGE("[Subscribe] fail: read result failed.");
160 return ERR_ANS_PARCELABLE_FAILED;
161 }
162 return result;
163 }
164
Unsubscribe(const sptr<AnsSubscriberInterface> & subscriber,const sptr<NotificationSubscribeInfo> & info)165 ErrCode AnsManagerProxy::Unsubscribe(
166 const sptr<AnsSubscriberInterface> &subscriber, const sptr<NotificationSubscribeInfo> &info)
167 {
168 if (subscriber == nullptr) {
169 ANS_LOGE("[Unsubscribe] fail: subscriber is empty.");
170 return ERR_ANS_INVALID_PARAM;
171 }
172
173 MessageParcel data;
174 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
175 ANS_LOGE("[Unsubscribe] fail: write interface token failed.");
176 return ERR_ANS_PARCELABLE_FAILED;
177 }
178
179 bool ret = data.WriteRemoteObject(subscriber->AsObject());
180 if (!ret) {
181 ANS_LOGE("[Unsubscribe] fail: write subscriber failed.");
182 return ERR_ANS_PARCELABLE_FAILED;
183 }
184
185 if (!data.WriteBool(info != nullptr)) {
186 ANS_LOGE("[Unsubscribe] fail: write isSubcribeInfo failed");
187 return ERR_ANS_PARCELABLE_FAILED;
188 }
189
190 if (info != nullptr) {
191 if (!data.WriteParcelable(info)) {
192 ANS_LOGE("[Unsubscribe] fail: write subcribeInfo failed");
193 return ERR_ANS_PARCELABLE_FAILED;
194 }
195 }
196
197 MessageParcel reply;
198 MessageOption option = {MessageOption::TF_SYNC};
199 ErrCode result = InnerTransact(NotificationInterfaceCode::UNSUBSCRIBE_NOTIFICATION, option, data, reply);
200 if (result != ERR_OK) {
201 ANS_LOGE("[Unsubscribe] fail: transact ErrCode=%{public}d", result);
202 return ERR_ANS_TRANSACT_FAILED;
203 }
204
205 if (!reply.ReadInt32(result)) {
206 ANS_LOGE("[Unsubscribe] fail: read result failed.");
207 return ERR_ANS_PARCELABLE_FAILED;
208 }
209
210 return result;
211 }
212 } // namespace Notification
213 } // namespace OHOS
214