• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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<AnsManagerInterface> 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<AnsManagerInterface> 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("Failed to create NotificationSlot ptr.");
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_LOGI("enter RemoveNotificationSlot,slotType:%{public}d", slotType);
65     sptr<AnsManagerInterface> 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 
CancelReminder(const int32_t reminderId)84 ErrCode ReminderRequestClient::CancelReminder(const int32_t reminderId)
85 {
86     sptr<IReminderAgentService> proxy = GetReminderServiceProxy();
87     if (!proxy) {
88         ANS_LOGE("GetReminderServiceProxy fail.");
89         return ERR_ANS_SERVICE_NOT_CONNECTED;
90     }
91     return proxy->CancelReminder(reminderId);
92 }
93 
CancelAllReminders()94 ErrCode ReminderRequestClient::CancelAllReminders()
95 {
96     sptr<IReminderAgentService> proxy = GetReminderServiceProxy();
97     if (!proxy) {
98         ANS_LOGE("GetReminderServiceProxy fail.");
99         return ERR_ANS_SERVICE_NOT_CONNECTED;
100     }
101     return proxy->CancelAllReminders();
102 }
103 
GetValidReminders(std::vector<ReminderRequestAdaptation> & validReminders)104 ErrCode ReminderRequestClient::GetValidReminders(std::vector<ReminderRequestAdaptation> &validReminders)
105 {
106     sptr<IReminderAgentService> proxy = GetReminderServiceProxy();
107     if (!proxy) {
108         ANS_LOGE("GetReminderServiceProxy fail.");
109         return ERR_ANS_SERVICE_NOT_CONNECTED;
110     }
111     return proxy->GetValidReminders(validReminders);
112 }
113 
AddExcludeDate(const int32_t reminderId,const int64_t date)114 ErrCode ReminderRequestClient::AddExcludeDate(const int32_t reminderId, const int64_t date)
115 {
116     sptr<IReminderAgentService> proxy = GetReminderServiceProxy();
117     if (!proxy) {
118         ANS_LOGE("GetReminderServiceProxy fail.");
119         return ERR_ANS_SERVICE_NOT_CONNECTED;
120     }
121     return proxy->AddExcludeDate(reminderId, date);
122 }
123 
DelExcludeDates(const int32_t reminderId)124 ErrCode ReminderRequestClient::DelExcludeDates(const int32_t reminderId)
125 {
126     sptr<IReminderAgentService> proxy = GetReminderServiceProxy();
127     if (!proxy) {
128         ANS_LOGE("GetReminderServiceProxy fail.");
129         return ERR_ANS_SERVICE_NOT_CONNECTED;
130     }
131     return proxy->DelExcludeDates(reminderId);
132 }
133 
GetExcludeDates(const int32_t reminderId,std::vector<int64_t> & dates)134 ErrCode ReminderRequestClient::GetExcludeDates(const int32_t reminderId, std::vector<int64_t>& dates)
135 {
136     sptr<IReminderAgentService> proxy = GetReminderServiceProxy();
137     if (!proxy) {
138         ANS_LOGE("GetReminderServiceProxy fail.");
139         return ERR_ANS_SERVICE_NOT_CONNECTED;
140     }
141     return proxy->GetExcludeDates(reminderId, dates);
142 }
143 
GetAnsManagerProxy()144 sptr<AnsManagerInterface> ReminderRequestClient::GetAnsManagerProxy()
145 {
146     sptr<ISystemAbilityManager> systemAbilityManager =
147         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
148     if (!systemAbilityManager) {
149         ANS_LOGE("Failed to get system ability mgr.");
150         return nullptr;
151     }
152 
153     sptr<IRemoteObject> remoteObject =
154         systemAbilityManager->GetSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID);
155     if (!remoteObject) {
156         ANS_LOGE("Failed to get notification Manager.");
157         return nullptr;
158     }
159 
160     sptr<AnsManagerInterface> proxy = iface_cast<AnsManagerInterface>(remoteObject);
161     if ((!proxy) || (!proxy->AsObject())) {
162         ANS_LOGE("Failed to get notification Manager's proxy");
163         return nullptr;
164     }
165     return proxy;
166 }
167 
GetReminderServiceProxy()168 sptr<IReminderAgentService> ReminderRequestClient::GetReminderServiceProxy()
169 {
170     {
171         std::lock_guard<ffrt::mutex> lock(serviceLock_);
172         if (proxy_ != nullptr) {
173             return proxy_;
174         }
175         auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
176         if (samgrProxy == nullptr) {
177             ANS_LOGE("get samgr failed");
178             return nullptr;
179         }
180         auto object = samgrProxy->CheckSystemAbility(REMINDER_AGENT_SERVICE_ID);
181         if (object != nullptr) {
182             ANS_LOGI("get service succeeded");
183             proxy_ = iface_cast<IReminderAgentService>(object);
184             return proxy_;
185         }
186     }
187 
188     ANS_LOGE("object is null");
189     if (LoadReminderService()) {
190         std::lock_guard<ffrt::mutex> lock(serviceLock_);
191         if (proxy_ != nullptr) {
192             return proxy_;
193         } else {
194             ANS_LOGE("load reminder service failed");
195             return nullptr;
196         }
197     }
198     ANS_LOGE("load reminder service failed");
199     return nullptr;
200 }
201 
LoadReminderService()202 bool ReminderRequestClient::LoadReminderService()
203 {
204     std::unique_lock<ffrt::mutex> lock(serviceLock_);
205     sptr<ReminderServiceCallback> loadCallback = sptr<ReminderServiceCallback>(new ReminderServiceCallback());
206     if (loadCallback == nullptr) {
207         ANS_LOGE("loadCallback is nullptr.");
208         return false;
209     }
210 
211     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
212     if (samgrProxy == nullptr) {
213         ANS_LOGE("get samgr failed");
214         return false;
215     }
216 
217     int32_t ret = samgrProxy->LoadSystemAbility(REMINDER_AGENT_SERVICE_ID, loadCallback);
218     if (ret != ERR_OK) {
219         ANS_LOGE("Failed to Load systemAbility");
220         return false;
221     }
222 
223     auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(REMINDER_SERVICE_LOADSA_TIMEOUT_MS),
224         [this]() { return proxy_ != nullptr; });
225     if (!waitStatus) {
226         ANS_LOGE("reminder service load sa timeout");
227         return false;
228     }
229     return true;
230 }
231 
LoadSystemAbilitySuccess(const sptr<IRemoteObject> & remoteObject)232 void ReminderRequestClient::LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject)
233 {
234     ANS_LOGI("ReminderRequestClient FinishStartSA");
235     std::lock_guard<ffrt::mutex> lock(serviceLock_);
236     if (remoteObject != nullptr) {
237         proxy_ = iface_cast<IReminderAgentService>(remoteObject);
238         proxyConVar_.notify_one();
239     }
240 }
241 
LoadSystemAbilityFail()242 void ReminderRequestClient::LoadSystemAbilityFail()
243 {
244     std::lock_guard<ffrt::mutex> lock(serviceLock_);
245     proxy_ = nullptr;
246 }
247 
StartReminderAgentService()248 void ReminderRequestClient::StartReminderAgentService()
249 {
250     auto reminderServiceProxy = GetReminderServiceProxy();
251     if (reminderServiceProxy == nullptr) {
252         ANS_LOGE("StartReminderService failed");
253         return;
254     }
255     ANS_LOGI("StartReminderService success");
256 }
257 
258 }  // namespace Notification
259 }  // namespace OHOS
260