1 /* 2 * Copyright (c) 2021-2025 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 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_AGENT_SERVICE_H 17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_AGENT_SERVICE_H 18 19 #include <mutex> 20 #include <memory> 21 #include <vector> 22 23 #include "ffrt.h" 24 #include "reminder_agent_service_stub.h" 25 26 namespace OHOS::Notification { 27 class ReminderAgentService final : public ReminderAgentServiceStub, 28 public std::enable_shared_from_this<ReminderAgentService> { 29 public: 30 ~ReminderAgentService() override = default; 31 32 /** 33 * @brief Get the instance of service. 34 * 35 * @return Returns the instance. 36 */ 37 static sptr<ReminderAgentService> GetInstance(); 38 39 /** 40 * @brief Publishes a reminder request. 41 * 42 * @param reminder Identifies the reminder request that needs to be published. 43 * @return Returns ERR_OK on success, others on failure. 44 */ 45 ErrCode PublishReminder(const ReminderRequest& reminder, int32_t& reminderId) override; 46 47 /** 48 * @brief Cancel a reminder request. 49 * 50 * @param reminderId Identifies the reminder id that needs to be canceled. 51 * @return Returns ERR_OK on success, others on failure. 52 */ 53 ErrCode CancelReminder(const int32_t reminderId) override; 54 55 /** 56 * @brief Cancel all reminder requests. 57 * 58 * @return Returns ERR_OK on success, others on failure. 59 */ 60 ErrCode CancelAllReminders() override; 61 62 /** 63 * @brief Get all valid reminder requests. 64 * 65 * @param reminders Identifies the list of all valid reminder requests. 66 * @return Returns ERR_OK on success, others on failure. 67 */ 68 ErrCode GetValidReminders(std::vector<ReminderRequestAdaptation>& reminders) override; 69 70 /** 71 * @brief Add exclude date for reminder. 72 * 73 * @param reminderId Identifies the reminder id. 74 * @param date Identifies the exclude date. 75 * @return Returns ERR_OK on success, others on failure. 76 */ 77 ErrCode AddExcludeDate(const int32_t reminderId, const int64_t date) override; 78 79 /** 80 * @brief Clear exclude date for reminder. 81 * 82 * @param reminderId Identifies the reminder id. 83 * @return Returns ERR_OK on success, others on failure. 84 */ 85 ErrCode DelExcludeDates(const int32_t reminderId) override; 86 87 /** 88 * @brief Get exclude date for reminder. 89 * 90 * @param reminderId Identifies the reminder id. 91 * @param dates Identifies the exclude dates. 92 * @return Returns ERR_OK on success, others on failure. 93 */ 94 ErrCode GetExcludeDates(const int32_t reminderId, std::vector<int64_t>& dates) override; 95 96 /** 97 * @brief Post unload services task. 98 */ 99 void TryPostDelayUnloadTask(const int64_t delayTime); 100 101 private: 102 ReminderAgentService() = default; 103 DISALLOW_COPY_AND_MOVE(ReminderAgentService); 104 105 void TryUnloadService(); 106 void ChangeReminderAgentLoadConfig(const int8_t reminderAgentState); 107 108 /** 109 * @brief Create reminder request pointer. 110 */ 111 sptr<ReminderRequest> CreateReminderRequest(const ReminderRequest& reminder); 112 113 /** 114 * @brief Init reminder info. 115 */ 116 ErrCode InitReminderRequest(sptr<ReminderRequest>& reminder, const std::string& bundle, 117 const int32_t callingUid); 118 119 /** 120 * @brief Check reminder permission. 121 */ 122 bool CheckReminderPermission(); 123 124 /** 125 * @brief Check caller is sysytem app. 126 */ 127 bool IsSystemApp(); 128 129 /** 130 * @brief Get client bundle name. 131 */ 132 std::string GetClientBundleName(const int32_t callingUid); 133 134 private: 135 std::mutex unloadMutex_; // for tryUnloadTask_ 136 ffrt::task_handle tryUnloadTask_ {nullptr}; 137 static std::mutex instanceMutex_; // for instance_ 138 static sptr<ReminderAgentService> instance_; 139 int8_t reminderAgentState_ {-1}; 140 }; 141 } // namespace OHOS::Notification 142 143 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_AGENT_SERVICE_H 144