/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "reminder_request_client.h" #include "reminder_service_load_callback.h" #include "ans_manager_proxy.h" #include "reminder_agent_service_proxy.h" #include "ans_const_define.h" #include "ans_inner_errors.h" #include "ans_log_wrapper.h" #include "ipc_skeleton.h" #include "iservice_registry.h" #include "system_ability_definition.h" #include #include namespace OHOS { namespace Notification { constexpr int32_t REMINDER_SERVICE_LOADSA_TIMEOUT_MS = 10000; constexpr int32_t REMINDER_AGENT_SERVICE_ID = 3204; ErrCode ReminderRequestClient::AddSlotByType(const NotificationConstant::SlotType &slotType) { sptr proxy = GetAnsManagerProxy(); if (!proxy) { ANS_LOGE("GetAnsManagerProxy fail."); return ERR_ANS_SERVICE_NOT_CONNECTED; } return proxy->AddSlotByType(slotType); } ErrCode ReminderRequestClient::AddNotificationSlot(const NotificationSlot &slot) { sptr proxy = GetAnsManagerProxy(); if (!proxy) { ANS_LOGE("GetAnsManagerProxy fail."); return ERR_ANS_SERVICE_NOT_CONNECTED; } std::vector> slotsSptr; sptr slotSptr = new (std::nothrow) NotificationSlot(slot); if (slotSptr == nullptr) { ANS_LOGE("null slotSptr"); return ERR_ANS_NO_MEMORY; } slotsSptr.emplace_back(slotSptr); return proxy->AddSlots(slotsSptr); } ErrCode ReminderRequestClient::RemoveNotificationSlot(const NotificationConstant::SlotType &slotType) { ANS_LOGD("called, slotType:%{public}d", slotType); sptr proxy = GetAnsManagerProxy(); if (!proxy) { ANS_LOGE("GetAnsManagerProxy fail."); return ERR_ANS_SERVICE_NOT_CONNECTED; } return proxy->RemoveSlotByType(slotType); } ErrCode ReminderRequestClient::PublishReminder(const ReminderRequest& reminder, int32_t& reminderId) { AddSlotByType(reminder.GetSlotType()); sptr proxy = GetReminderServiceProxy(); if (!proxy) { ANS_LOGE("GetReminderServiceProxy fail."); return ERR_ANS_SERVICE_NOT_CONNECTED; } return proxy->PublishReminder(reminder, reminderId); } ErrCode ReminderRequestClient::UpdateReminder(const int32_t reminderId, const ReminderRequest& reminder) { AddSlotByType(reminder.GetSlotType()); sptr proxy = GetReminderServiceProxy(); if (!proxy) { ANS_LOGE("GetReminderServiceProxy fail."); return ERR_ANS_SERVICE_NOT_CONNECTED; } return proxy->UpdateReminder(reminderId, reminder); } ErrCode ReminderRequestClient::CancelReminder(const int32_t reminderId) { sptr proxy = GetReminderServiceProxy(); if (!proxy) { ANS_LOGE("GetReminderServiceProxy fail."); return ERR_ANS_SERVICE_NOT_CONNECTED; } return proxy->CancelReminder(reminderId); } ErrCode ReminderRequestClient::CancelAllReminders() { sptr proxy = GetReminderServiceProxy(); if (!proxy) { ANS_LOGE("GetReminderServiceProxy fail."); return ERR_ANS_SERVICE_NOT_CONNECTED; } return proxy->CancelAllReminders(); } ErrCode ReminderRequestClient::GetValidReminders(std::vector &validReminders) { sptr proxy = GetReminderServiceProxy(); if (!proxy) { ANS_LOGE("GetReminderServiceProxy fail."); return ERR_ANS_SERVICE_NOT_CONNECTED; } return proxy->GetValidReminders(validReminders); } ErrCode ReminderRequestClient::AddExcludeDate(const int32_t reminderId, const int64_t date) { sptr proxy = GetReminderServiceProxy(); if (!proxy) { ANS_LOGE("GetReminderServiceProxy fail."); return ERR_ANS_SERVICE_NOT_CONNECTED; } return proxy->AddExcludeDate(reminderId, date); } ErrCode ReminderRequestClient::DelExcludeDates(const int32_t reminderId) { sptr proxy = GetReminderServiceProxy(); if (!proxy) { ANS_LOGE("GetReminderServiceProxy fail."); return ERR_ANS_SERVICE_NOT_CONNECTED; } return proxy->DelExcludeDates(reminderId); } ErrCode ReminderRequestClient::GetExcludeDates(const int32_t reminderId, std::vector& dates) { sptr proxy = GetReminderServiceProxy(); if (!proxy) { ANS_LOGE("GetReminderServiceProxy fail."); return ERR_ANS_SERVICE_NOT_CONNECTED; } return proxy->GetExcludeDates(reminderId, dates); } sptr ReminderRequestClient::GetAnsManagerProxy() { sptr systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (!systemAbilityManager) { ANS_LOGE("Failed to get system ability mgr."); return nullptr; } sptr remoteObject = systemAbilityManager->GetSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID); if (!remoteObject) { ANS_LOGE("Failed to get notification Manager."); return nullptr; } sptr proxy = iface_cast(remoteObject); if ((!proxy) || (!proxy->AsObject())) { ANS_LOGE("Failed to get notification Manager's proxy"); return nullptr; } return proxy; } sptr ReminderRequestClient::GetReminderServiceProxy() { { std::lock_guard lock(serviceLock_); if (proxy_ != nullptr) { return proxy_; } auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (samgrProxy == nullptr) { ANS_LOGE("null samgrProxy"); return nullptr; } auto object = samgrProxy->CheckSystemAbility(REMINDER_AGENT_SERVICE_ID); if (object != nullptr) { ANS_LOGI("get service succeeded"); proxy_ = iface_cast(object); return proxy_; } } ANS_LOGE("null object"); if (LoadReminderService()) { std::lock_guard lock(serviceLock_); if (proxy_ != nullptr) { return proxy_; } else { ANS_LOGE("load reminder service failed"); return nullptr; } } ANS_LOGE("load reminder service failed"); return nullptr; } bool ReminderRequestClient::LoadReminderService() { std::unique_lock lock(serviceLock_); sptr loadCallback = sptr(new ReminderServiceCallback()); if (loadCallback == nullptr) { ANS_LOGE("null loadCallback"); return false; } auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (samgrProxy == nullptr) { ANS_LOGE("null samgrProxy"); return false; } int32_t ret = samgrProxy->LoadSystemAbility(REMINDER_AGENT_SERVICE_ID, loadCallback); if (ret != ERR_OK) { ANS_LOGE("Failed to Load systemAbility"); return false; } auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(REMINDER_SERVICE_LOADSA_TIMEOUT_MS), [this]() { return proxy_ != nullptr; }); if (!waitStatus) { ANS_LOGE("reminder service load sa timeout"); return false; } return true; } void ReminderRequestClient::LoadSystemAbilitySuccess(const sptr &remoteObject) { ANS_LOGD("called"); std::lock_guard lock(serviceLock_); if (remoteObject != nullptr) { proxy_ = iface_cast(remoteObject); proxyConVar_.notify_one(); } } void ReminderRequestClient::LoadSystemAbilityFail() { std::lock_guard lock(serviceLock_); proxy_ = nullptr; } void ReminderRequestClient::StartReminderAgentService() { auto reminderServiceProxy = GetReminderServiceProxy(); if (reminderServiceProxy == nullptr) { ANS_LOGE("null reminderServiceProxy"); return; } ANS_LOGD("StartReminderService success"); } } // namespace Notification } // namespace OHOS