1 /*
2 * Copyright (c) 2024 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 "reminder_request_client.h"
17 #include "reminder_service_load_callback.h"
18 #include "ans_manager_proxy.h"
19 #include "reminder_agent_service_proxy.h"
20 #include "ans_const_define.h"
21 #include "ans_inner_errors.h"
22 #include "ans_log_wrapper.h"
23
24 #include "ipc_skeleton.h"
25 #include "iservice_registry.h"
26 #include "system_ability_definition.h"
27
28 #include <memory>
29 #include <thread>
30
31 namespace OHOS {
32 namespace Notification {
33 constexpr int32_t REMINDER_SERVICE_LOADSA_TIMEOUT_MS = 10000;
34 constexpr int32_t REMINDER_AGENT_SERVICE_ID = 3204;
AddSlotByType(const NotificationConstant::SlotType & slotType)35 ErrCode ReminderRequestClient::AddSlotByType(const NotificationConstant::SlotType &slotType)
36 {
37 sptr<IAnsManager> proxy = GetAnsManagerProxy();
38 if (!proxy) {
39 ANS_LOGE("GetAnsManagerProxy fail.");
40 return ERR_ANS_SERVICE_NOT_CONNECTED;
41 }
42 return proxy->AddSlotByType(slotType);
43 }
44
AddNotificationSlot(const NotificationSlot & slot)45 ErrCode ReminderRequestClient::AddNotificationSlot(const NotificationSlot &slot)
46 {
47 sptr<IAnsManager> proxy = GetAnsManagerProxy();
48 if (!proxy) {
49 ANS_LOGE("GetAnsManagerProxy fail.");
50 return ERR_ANS_SERVICE_NOT_CONNECTED;
51 }
52 std::vector<sptr<NotificationSlot>> slotsSptr;
53 sptr<NotificationSlot> slotSptr = new (std::nothrow) NotificationSlot(slot);
54 if (slotSptr == nullptr) {
55 ANS_LOGE("null slotSptr");
56 return ERR_ANS_NO_MEMORY;
57 }
58 slotsSptr.emplace_back(slotSptr);
59 return proxy->AddSlots(slotsSptr);
60 }
61
RemoveNotificationSlot(const NotificationConstant::SlotType & slotType)62 ErrCode ReminderRequestClient::RemoveNotificationSlot(const NotificationConstant::SlotType &slotType)
63 {
64 ANS_LOGD("called, slotType:%{public}d", slotType);
65 sptr<IAnsManager> proxy = GetAnsManagerProxy();
66 if (!proxy) {
67 ANS_LOGE("GetAnsManagerProxy fail.");
68 return ERR_ANS_SERVICE_NOT_CONNECTED;
69 }
70 return proxy->RemoveSlotByType(slotType);
71 }
72
PublishReminder(const ReminderRequest & reminder,int32_t & reminderId)73 ErrCode ReminderRequestClient::PublishReminder(const ReminderRequest& reminder, int32_t& reminderId)
74 {
75 AddSlotByType(reminder.GetSlotType());
76 sptr<IReminderAgentService> proxy = GetReminderServiceProxy();
77 if (!proxy) {
78 ANS_LOGE("GetReminderServiceProxy fail.");
79 return ERR_ANS_SERVICE_NOT_CONNECTED;
80 }
81 return proxy->PublishReminder(reminder, reminderId);
82 }
83
UpdateReminder(const int32_t reminderId,const ReminderRequest & reminder)84 ErrCode ReminderRequestClient::UpdateReminder(const int32_t reminderId, const ReminderRequest& reminder)
85 {
86 AddSlotByType(reminder.GetSlotType());
87 sptr<IReminderAgentService> proxy = GetReminderServiceProxy();
88 if (!proxy) {
89 ANS_LOGE("GetReminderServiceProxy fail.");
90 return ERR_ANS_SERVICE_NOT_CONNECTED;
91 }
92 return proxy->UpdateReminder(reminderId, reminder);
93 }
94
CancelReminder(const int32_t reminderId)95 ErrCode ReminderRequestClient::CancelReminder(const int32_t reminderId)
96 {
97 sptr<IReminderAgentService> proxy = GetReminderServiceProxy();
98 if (!proxy) {
99 ANS_LOGE("GetReminderServiceProxy fail.");
100 return ERR_ANS_SERVICE_NOT_CONNECTED;
101 }
102 return proxy->CancelReminder(reminderId);
103 }
104
CancelAllReminders()105 ErrCode ReminderRequestClient::CancelAllReminders()
106 {
107 sptr<IReminderAgentService> proxy = GetReminderServiceProxy();
108 if (!proxy) {
109 ANS_LOGE("GetReminderServiceProxy fail.");
110 return ERR_ANS_SERVICE_NOT_CONNECTED;
111 }
112 return proxy->CancelAllReminders();
113 }
114
GetValidReminders(std::vector<ReminderRequestAdaptation> & validReminders)115 ErrCode ReminderRequestClient::GetValidReminders(std::vector<ReminderRequestAdaptation> &validReminders)
116 {
117 sptr<IReminderAgentService> proxy = GetReminderServiceProxy();
118 if (!proxy) {
119 ANS_LOGE("GetReminderServiceProxy fail.");
120 return ERR_ANS_SERVICE_NOT_CONNECTED;
121 }
122 return proxy->GetValidReminders(validReminders);
123 }
124
AddExcludeDate(const int32_t reminderId,const int64_t date)125 ErrCode ReminderRequestClient::AddExcludeDate(const int32_t reminderId, const int64_t date)
126 {
127 sptr<IReminderAgentService> proxy = GetReminderServiceProxy();
128 if (!proxy) {
129 ANS_LOGE("GetReminderServiceProxy fail.");
130 return ERR_ANS_SERVICE_NOT_CONNECTED;
131 }
132 return proxy->AddExcludeDate(reminderId, date);
133 }
134
DelExcludeDates(const int32_t reminderId)135 ErrCode ReminderRequestClient::DelExcludeDates(const int32_t reminderId)
136 {
137 sptr<IReminderAgentService> proxy = GetReminderServiceProxy();
138 if (!proxy) {
139 ANS_LOGE("GetReminderServiceProxy fail.");
140 return ERR_ANS_SERVICE_NOT_CONNECTED;
141 }
142 return proxy->DelExcludeDates(reminderId);
143 }
144
GetExcludeDates(const int32_t reminderId,std::vector<int64_t> & dates)145 ErrCode ReminderRequestClient::GetExcludeDates(const int32_t reminderId, std::vector<int64_t>& dates)
146 {
147 sptr<IReminderAgentService> proxy = GetReminderServiceProxy();
148 if (!proxy) {
149 ANS_LOGE("GetReminderServiceProxy fail.");
150 return ERR_ANS_SERVICE_NOT_CONNECTED;
151 }
152 return proxy->GetExcludeDates(reminderId, dates);
153 }
154
GetAnsManagerProxy()155 sptr<IAnsManager> ReminderRequestClient::GetAnsManagerProxy()
156 {
157 sptr<ISystemAbilityManager> systemAbilityManager =
158 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
159 if (!systemAbilityManager) {
160 ANS_LOGE("Failed to get system ability mgr.");
161 return nullptr;
162 }
163
164 sptr<IRemoteObject> remoteObject =
165 systemAbilityManager->GetSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID);
166 if (!remoteObject) {
167 ANS_LOGE("Failed to get notification Manager.");
168 return nullptr;
169 }
170
171 sptr<IAnsManager> proxy = iface_cast<IAnsManager>(remoteObject);
172 if ((!proxy) || (!proxy->AsObject())) {
173 ANS_LOGE("Failed to get notification Manager's proxy");
174 return nullptr;
175 }
176 return proxy;
177 }
178
GetReminderServiceProxy()179 sptr<IReminderAgentService> ReminderRequestClient::GetReminderServiceProxy()
180 {
181 {
182 std::lock_guard<ffrt::mutex> lock(serviceLock_);
183 if (proxy_ != nullptr) {
184 return proxy_;
185 }
186 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
187 if (samgrProxy == nullptr) {
188 ANS_LOGE("null samgrProxy");
189 return nullptr;
190 }
191 auto object = samgrProxy->CheckSystemAbility(REMINDER_AGENT_SERVICE_ID);
192 if (object != nullptr) {
193 ANS_LOGI("get service succeeded");
194 proxy_ = iface_cast<IReminderAgentService>(object);
195 return proxy_;
196 }
197 }
198
199 ANS_LOGE("null object");
200 if (LoadReminderService()) {
201 std::lock_guard<ffrt::mutex> lock(serviceLock_);
202 if (proxy_ != nullptr) {
203 return proxy_;
204 } else {
205 ANS_LOGE("load reminder service failed");
206 return nullptr;
207 }
208 }
209 ANS_LOGE("load reminder service failed");
210 return nullptr;
211 }
212
LoadReminderService()213 bool ReminderRequestClient::LoadReminderService()
214 {
215 std::unique_lock<ffrt::mutex> lock(serviceLock_);
216 sptr<ReminderServiceCallback> loadCallback = sptr<ReminderServiceCallback>(new ReminderServiceCallback());
217 if (loadCallback == nullptr) {
218 ANS_LOGE("null loadCallback");
219 return false;
220 }
221
222 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
223 if (samgrProxy == nullptr) {
224 ANS_LOGE("null samgrProxy");
225 return false;
226 }
227
228 int32_t ret = samgrProxy->LoadSystemAbility(REMINDER_AGENT_SERVICE_ID, loadCallback);
229 if (ret != ERR_OK) {
230 ANS_LOGE("Failed to Load systemAbility");
231 return false;
232 }
233
234 auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(REMINDER_SERVICE_LOADSA_TIMEOUT_MS),
235 [this]() { return proxy_ != nullptr; });
236 if (!waitStatus) {
237 ANS_LOGE("reminder service load sa timeout");
238 return false;
239 }
240 return true;
241 }
242
LoadSystemAbilitySuccess(const sptr<IRemoteObject> & remoteObject)243 void ReminderRequestClient::LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject)
244 {
245 ANS_LOGD("called");
246 std::lock_guard<ffrt::mutex> lock(serviceLock_);
247 if (remoteObject != nullptr) {
248 proxy_ = iface_cast<IReminderAgentService>(remoteObject);
249 proxyConVar_.notify_one();
250 }
251 }
252
LoadSystemAbilityFail()253 void ReminderRequestClient::LoadSystemAbilityFail()
254 {
255 std::lock_guard<ffrt::mutex> lock(serviceLock_);
256 proxy_ = nullptr;
257 }
258
StartReminderAgentService()259 void ReminderRequestClient::StartReminderAgentService()
260 {
261 auto reminderServiceProxy = GetReminderServiceProxy();
262 if (reminderServiceProxy == nullptr) {
263 ANS_LOGE("null reminderServiceProxy");
264 return;
265 }
266 ANS_LOGD("StartReminderService success");
267 }
268
269 } // namespace Notification
270 } // namespace OHOS
271