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 "reminder_request_alarm.h"
27 #include "reminder_request_calendar.h"
28 #include "reminder_request_timer.h"
29 #include "ans_manager_proxy.h"
30
31 namespace OHOS {
32 namespace Notification {
PublishReminder(sptr<ReminderRequest> & reminder)33 ErrCode AnsManagerProxy::PublishReminder(sptr<ReminderRequest> &reminder)
34 {
35 ANSR_LOGI("PublishReminder");
36 MessageParcel data;
37 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
38 ANSR_LOGE("[PublishReminder] fail: write interface token failed.");
39 return ERR_ANS_PARCELABLE_FAILED;
40 }
41 if (reminder == nullptr) {
42 ANSR_LOGW("[PublishReminder] fail: reminder is null ptr.");
43 return ERR_ANS_INVALID_PARAM;
44 }
45 if (!data.WriteUint8(static_cast<uint8_t>(reminder->GetReminderType()))) {
46 ANSR_LOGE("[PublishReminder] fail: write reminder type failed");
47 return ERR_ANS_PARCELABLE_FAILED;
48 }
49 if (!data.WriteParcelable(reminder)) {
50 ANSR_LOGE("[Publish] fail: write reminder parcelable failed");
51 return ERR_ANS_PARCELABLE_FAILED;
52 }
53
54 MessageParcel reply;
55 MessageOption option = {MessageOption::TF_SYNC};
56 ErrCode result = InnerTransact(NotificationInterfaceCode::PUBLISH_REMINDER, option, data, reply);
57 if (result != ERR_OK) {
58 ANSR_LOGE("[PublishReminder] fail: transact ErrCode=%{public}d", result);
59 return ERR_ANS_TRANSACT_FAILED;
60 }
61 int32_t reminderId = -1;
62 if (!reply.ReadInt32(reminderId)) {
63 ANSR_LOGE("[PublishReminder] fail: read reminder id failed.");
64 return ERR_ANS_PARCELABLE_FAILED;
65 }
66 reminder->SetReminderId(reminderId);
67 ANSR_LOGD("ReminderId=%{public}d", reminder->GetReminderId());
68 if (!reply.ReadInt32(result)) {
69 ANSR_LOGE("[PublishReminder] fail: read result failed.");
70 return ERR_ANS_PARCELABLE_FAILED;
71 }
72 return result;
73 }
74
CancelReminder(const int32_t reminderId)75 ErrCode AnsManagerProxy::CancelReminder(const int32_t reminderId)
76 {
77 ANSR_LOGI("[CancelReminder]");
78 MessageParcel data;
79 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
80 ANSR_LOGE("[CancelReminder] fail: write interface token failed.");
81 return ERR_ANS_PARCELABLE_FAILED;
82 }
83 if (!data.WriteInt32(reminderId)) {
84 ANSR_LOGE("[CancelReminder] fail: write reminder id failed");
85 return ERR_ANS_PARCELABLE_FAILED;
86 }
87
88 MessageParcel reply;
89 MessageOption option = {MessageOption::TF_SYNC};
90 ErrCode result = InnerTransact(NotificationInterfaceCode::CANCEL_REMINDER, option, data, reply);
91 if (result != ERR_OK) {
92 ANSR_LOGE("[CancelReminder] fail: transact ErrCode=%{public}d", result);
93 return ERR_ANS_TRANSACT_FAILED;
94 }
95 if (!reply.ReadInt32(result)) {
96 ANSR_LOGE("[CancelReminder] fail: read result failed.");
97 return ERR_ANS_PARCELABLE_FAILED;
98 }
99 return result;
100 }
101
CancelAllReminders()102 ErrCode AnsManagerProxy::CancelAllReminders()
103 {
104 ANSR_LOGI("[CancelAllReminders]");
105 MessageParcel data;
106 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
107 ANSR_LOGE("[CancelAllReminders] fail: write interface token failed.");
108 return ERR_ANS_PARCELABLE_FAILED;
109 }
110
111 MessageParcel reply;
112 MessageOption option = {MessageOption::TF_SYNC};
113 ErrCode result = InnerTransact(NotificationInterfaceCode::CANCEL_ALL_REMINDERS, option, data, reply);
114 if (result != ERR_OK) {
115 ANSR_LOGE("[CancelAllReminders] fail: transact ErrCode=%{public}d", result);
116 return ERR_ANS_TRANSACT_FAILED;
117 }
118 if (!reply.ReadInt32(result)) {
119 ANSR_LOGE("[CancelAllReminders] fail: read result failed.");
120 return ERR_ANS_PARCELABLE_FAILED;
121 }
122 return result;
123 }
124
GetValidReminders(std::vector<sptr<ReminderRequest>> & reminders)125 ErrCode AnsManagerProxy::GetValidReminders(std::vector<sptr<ReminderRequest>> &reminders)
126 {
127 ANSR_LOGI("[GetValidReminders]");
128 MessageParcel data;
129 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
130 ANSR_LOGE("[GetValidReminders] fail: write interface token failed.");
131 return ERR_ANS_PARCELABLE_FAILED;
132 }
133
134 MessageParcel reply;
135 MessageOption option = {MessageOption::TF_SYNC};
136 ErrCode result = InnerTransact(NotificationInterfaceCode::GET_ALL_VALID_REMINDERS, option, data, reply);
137 if (result != ERR_OK) {
138 ANSR_LOGE("[GetValidReminders] fail: transact ErrCode=%{public}d", result);
139 return ERR_ANS_TRANSACT_FAILED;
140 }
141 uint8_t count = 0;
142 if (!reply.ReadUint8(count)) {
143 ANSR_LOGE("[GetValidReminders] fail: read reminder count failed.");
144 return ERR_ANS_PARCELABLE_FAILED;
145 }
146 ANSR_LOGD("[GetValidReminders] count=%{public}hhu", count);
147 reminders.clear();
148 result = ReadReminders(count, reply, reminders);
149 if (result != ERR_OK) {
150 ANSR_LOGE("[GetValidReminders] fail: ReadReminders ErrCode=%{public}d", result);
151 return result;
152 } else {
153 ANSR_LOGD("[GetValidReminders], size=%{public}zu", reminders.size());
154 }
155 if (!reply.ReadInt32(result)) {
156 ANSR_LOGE("[GetValidReminders] fail: read result failed.");
157 return ERR_ANS_PARCELABLE_FAILED;
158 }
159 return result;
160 }
161
AddExcludeDate(const int32_t reminderId,const uint64_t date)162 ErrCode AnsManagerProxy::AddExcludeDate(const int32_t reminderId, const uint64_t date)
163 {
164 ANSR_LOGI("[AddExcludeDate]");
165 MessageParcel data;
166 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
167 ANSR_LOGE("[AddExcludeDate] fail: write interface token failed.");
168 return ERR_ANS_PARCELABLE_FAILED;
169 }
170 if (!data.WriteInt32(reminderId)) {
171 ANSR_LOGE("[AddExcludeDate] fail: write reminder id failed");
172 return ERR_ANS_PARCELABLE_FAILED;
173 }
174 if (!data.WriteUint64(date)) {
175 ANSR_LOGE("[AddExcludeDate] fail: write exclude date failed");
176 return ERR_ANS_PARCELABLE_FAILED;
177 }
178
179 MessageParcel reply;
180 MessageOption option = {MessageOption::TF_SYNC};
181 ErrCode result = InnerTransact(NotificationInterfaceCode::ADD_EXCLUDE_DATE_REMINDER, option, data, reply);
182 if (result != ERR_OK) {
183 ANSR_LOGE("[AddExcludeDate] fail: transact ErrCode=%{public}d", result);
184 return ERR_ANS_TRANSACT_FAILED;
185 }
186 if (!reply.ReadInt32(result)) {
187 ANSR_LOGE("[AddExcludeDate] fail: read result failed.");
188 return ERR_ANS_PARCELABLE_FAILED;
189 }
190 return result;
191 }
192
DelExcludeDates(const int32_t reminderId)193 ErrCode AnsManagerProxy::DelExcludeDates(const int32_t reminderId)
194 {
195 ANSR_LOGI("[DelExcludeDates]");
196 MessageParcel data;
197 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
198 ANSR_LOGE("[DelExcludeDates] fail: write interface token failed.");
199 return ERR_ANS_PARCELABLE_FAILED;
200 }
201 if (!data.WriteInt32(reminderId)) {
202 ANSR_LOGE("[DelExcludeDates] fail: write reminder id failed");
203 return ERR_ANS_PARCELABLE_FAILED;
204 }
205
206 MessageParcel reply;
207 MessageOption option = {MessageOption::TF_SYNC};
208 ErrCode result = InnerTransact(NotificationInterfaceCode::DEL_EXCLUDE_DATES_REMINDER, option, data, reply);
209 if (result != ERR_OK) {
210 ANSR_LOGE("[DelExcludeDates] fail: transact ErrCode=%{public}d", result);
211 return ERR_ANS_TRANSACT_FAILED;
212 }
213 if (!reply.ReadInt32(result)) {
214 ANSR_LOGE("[DelExcludeDates] fail: read result failed.");
215 return ERR_ANS_PARCELABLE_FAILED;
216 }
217 return result;
218 }
219
GetExcludeDates(const int32_t reminderId,std::vector<uint64_t> & dates)220 ErrCode AnsManagerProxy::GetExcludeDates(const int32_t reminderId, std::vector<uint64_t>& dates)
221 {
222 ANSR_LOGI("[GetExcludeDates]");
223 MessageParcel data;
224 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
225 ANSR_LOGE("[GetExcludeDates] fail: write interface token failed.");
226 return ERR_ANS_PARCELABLE_FAILED;
227 }
228 if (!data.WriteInt32(reminderId)) {
229 ANSR_LOGE("[GetExcludeDates] fail: write reminder id failed");
230 return ERR_ANS_PARCELABLE_FAILED;
231 }
232
233 MessageParcel reply;
234 MessageOption option = {MessageOption::TF_SYNC};
235 ErrCode result = InnerTransact(NotificationInterfaceCode::GET_EXCLUDE_DATES_REMINDER, option, data, reply);
236 if (result != ERR_OK) {
237 ANSR_LOGE("[GetExcludeDates] fail: transact ErrCode=%{public}d", result);
238 return ERR_ANS_TRANSACT_FAILED;
239 }
240 uint8_t count = 0;
241 if (!reply.ReadUint8(count)) {
242 ANSR_LOGE("[GetExcludeDates] fail: read exclude date count failed.");
243 return ERR_ANS_PARCELABLE_FAILED;
244 }
245 ANSR_LOGD("[GetExcludeDates] count=%{public}hhu", count);
246 dates.clear();
247 for (uint8_t i = 0; i < count; i++) {
248 uint64_t date = 0;
249 if (!reply.ReadUint64(date)) {
250 ANSR_LOGE("[GetExcludeDates] fail: read exclude date");
251 return ERR_ANS_PARCELABLE_FAILED;
252 }
253 dates.push_back(date);
254 }
255 if (!reply.ReadInt32(result)) {
256 ANSR_LOGE("[GetExcludeDates] fail: read result failed.");
257 return ERR_ANS_PARCELABLE_FAILED;
258 }
259 return result;
260 }
261
ReadReminders(uint8_t & count,MessageParcel & reply,std::vector<sptr<ReminderRequest>> & reminders)262 ErrCode AnsManagerProxy::ReadReminders(
263 uint8_t &count, MessageParcel &reply, std::vector<sptr<ReminderRequest>> &reminders)
264 {
265 for (uint8_t i = 0; i < count; i++) {
266 uint8_t typeInfo = static_cast<uint8_t>(ReminderRequest::ReminderType::INVALID);
267 if (!reply.ReadUint8(typeInfo)) {
268 ANSR_LOGE("Failed to read reminder type");
269 return ERR_ANS_PARCELABLE_FAILED;
270 }
271 auto reminderType = static_cast<ReminderRequest::ReminderType>(typeInfo);
272 sptr<ReminderRequest> reminder;
273 if (ReminderRequest::ReminderType::ALARM == reminderType) {
274 ANSR_LOGD("[GetValidReminders] alarm");
275 reminder = reply.ReadParcelable<ReminderRequestAlarm>();
276 } else if (ReminderRequest::ReminderType::TIMER == reminderType) {
277 ANSR_LOGD("[GetValidReminders] timer");
278 reminder = reply.ReadParcelable<ReminderRequestTimer>();
279 } else if (ReminderRequest::ReminderType::CALENDAR == reminderType) {
280 ANSR_LOGD("[GetValidReminders] calendar");
281 reminder = reply.ReadParcelable<ReminderRequestCalendar>();
282 } else {
283 ANSR_LOGW("[GetValidReminders] type=%{public}hhu", typeInfo);
284 return ERR_ANS_INVALID_PARAM;
285 }
286 if (!reminder) {
287 ANSR_LOGE("[GetValidReminders] fail: Reminder ReadParcelable failed");
288 return ERR_ANS_PARCELABLE_FAILED;
289 }
290 reminders.push_back(reminder);
291 }
292 return ERR_OK;
293 }
294 } // namespace Notification
295 } // namespace OHOS
296