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 Updates a reminder request. 49 * 50 * @param reminderId Identifies the reminder id that needs to be updated. 51 * @param reminder Identifies the reminder request that needs to be updated. 52 * @return Returns ERR_OK on success, others on failure. 53 */ 54 ErrCode UpdateReminder(const int32_t reminderId, const ReminderRequest& reminder) override; 55 56 /** 57 * @brief Cancel a reminder request. 58 * 59 * @param reminderId Identifies the reminder id that needs to be canceled. 60 * @return Returns ERR_OK on success, others on failure. 61 */ 62 ErrCode CancelReminder(const int32_t reminderId) override; 63 64 /** 65 * @brief Cancel all reminder requests. 66 * 67 * @return Returns ERR_OK on success, others on failure. 68 */ 69 ErrCode CancelAllReminders() override; 70 71 /** 72 * @brief Get all valid reminder requests. 73 * 74 * @param reminders Identifies the list of all valid reminder requests. 75 * @return Returns ERR_OK on success, others on failure. 76 */ 77 ErrCode GetValidReminders(std::vector<ReminderRequestAdaptation>& reminders) override; 78 79 /** 80 * @brief Add exclude date for reminder. 81 * 82 * @param reminderId Identifies the reminder id. 83 * @param date Identifies the exclude date. 84 * @return Returns ERR_OK on success, others on failure. 85 */ 86 ErrCode AddExcludeDate(const int32_t reminderId, const int64_t date) override; 87 88 /** 89 * @brief Clear exclude date for reminder. 90 * 91 * @param reminderId Identifies the reminder id. 92 * @return Returns ERR_OK on success, others on failure. 93 */ 94 ErrCode DelExcludeDates(const int32_t reminderId) override; 95 96 /** 97 * @brief Get exclude date for reminder. 98 * 99 * @param reminderId Identifies the reminder id. 100 * @param dates Identifies the exclude dates. 101 * @return Returns ERR_OK on success, others on failure. 102 */ 103 ErrCode GetExcludeDates(const int32_t reminderId, std::vector<int64_t>& dates) override; 104 105 /** 106 * @brief Post unload services task. 107 */ 108 void TryPostDelayUnloadTask(const int64_t delayTime); 109 110 private: 111 ReminderAgentService() = default; 112 DISALLOW_COPY_AND_MOVE(ReminderAgentService); 113 114 void TryUnloadService(); 115 void ChangeReminderAgentLoadConfig(const int8_t reminderAgentState); 116 117 /** 118 * @brief Create reminder request pointer. 119 */ 120 sptr<ReminderRequest> CreateReminderRequest(const ReminderRequest& reminder); 121 122 /** 123 * @brief Init reminder info. 124 */ 125 ErrCode InitReminderRequest(sptr<ReminderRequest>& reminder, const std::string& bundle, 126 const int32_t callingUid); 127 128 /** 129 * @brief Check reminder permission. 130 */ 131 bool CheckReminderPermission(); 132 133 /** 134 * @brief Check caller is sysytem app. 135 */ 136 bool IsSystemApp(); 137 138 private: 139 std::mutex unloadMutex_; // for tryUnloadTask_ 140 ffrt::task_handle tryUnloadTask_ {nullptr}; 141 static std::mutex instanceMutex_; // for instance_ 142 static sptr<ReminderAgentService> instance_; 143 int8_t reminderAgentState_ {-1}; 144 }; 145 } // namespace OHOS::Notification 146 147 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_AGENT_SERVICE_H 148