1 /* 2 * Copyright (c) 2021 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 FOUNDATION_APPEXECFWK_SERVICES_FORMMGR_INCLUDE_FORM_TIMER_MGR_H 17 #define FOUNDATION_APPEXECFWK_SERVICES_FORMMGR_INCLUDE_FORM_TIMER_MGR_H 18 19 #include <array> 20 #include <chrono> 21 #include <ctime> 22 #include <limits.h> 23 #include <list> 24 #include <map> 25 #include <mutex> 26 #include <singleton.h> 27 #include <stdint.h> 28 #include <string> 29 #include <time.h> 30 #include <vector> 31 32 #include "common_event_subscriber.h" 33 #include "common_event_subscribe_info.h" 34 #include "form_refresh_limiter.h" 35 #include "form_timer.h" 36 #include "thread_pool.h" 37 // #include "timer.h" 38 39 namespace OHOS { 40 namespace AppExecFwk { 41 /** 42 * @class FormTimerMgr 43 * form timer task manager. 44 */ 45 class FormTimerMgr final : public DelayedRefSingleton<FormTimerMgr> { 46 DECLARE_DELAYED_REF_SINGLETON(FormTimerMgr) 47 public: 48 DISALLOW_COPY_AND_MOVE(FormTimerMgr); 49 /** 50 * @brief Add form timer by timer task. 51 * @param task The form timer task. 52 * @return Returns true on success, false on failure. 53 */ 54 bool AddFormTimer(const FormTimer &task); 55 /** 56 * @brief Add duration form timer. 57 * @param formId The Id of the form. 58 * @param updateDuration Update duration 59 * @return Returns true on success, false on failure. 60 */ 61 bool AddFormTimer(const int64_t formId, const long updateDuration); 62 /** 63 * @brief Add scheduled form timer. 64 * @param formId The Id of the form. 65 * @param updateAtHour Hour 66 * @param updateAtMin Min 67 * @return Returns true on success, false on failure. 68 */ 69 bool AddFormTimer(const int64_t formId, const long updateAtHour, const long updateAtMin); 70 /** 71 * @brief Remove form timer by form id. 72 * @param formId The Id of the form. 73 * @return Returns true on success, false on failure. 74 */ 75 bool RemoveFormTimer(const int64_t formId); 76 /** 77 * @brief Update form timer. 78 * @param formId The Id of the form. 79 * @param type Timer type. 80 * @param timerCfg Timer config. 81 * @return Returns true on success, false on failure. 82 */ 83 bool UpdateFormTimer(const int64_t formId, const UpdateType &type, const FormTimerCfg &timerCfg); 84 /** 85 * @brief Is limiter enable refresh. 86 * @param formId The Id of the form. 87 * @return Returns true on success, false on failure. 88 */ 89 bool IsLimiterEnableRefresh(const int64_t formId); 90 /** 91 * @brief Increase refresh count. 92 * @param formId The Id of the form. 93 */ 94 void IncreaseRefreshCount(const int64_t formId); 95 /** 96 * @brief Set next refresh time. 97 * @param formId The Id of the form. 98 * @param nextGapTime Next gap time. 99 * @return Returns true on success, false on failure. 100 */ 101 bool SetNextRefreshTime(const int64_t formId, const long nextGapTime); 102 /** 103 * @brief Get refresh count. 104 * @param formId The Id of the form. 105 * @return Returns refresh count. 106 */ 107 int GetRefreshCount(const int64_t formId) const; 108 /** 109 * @brief Mark remind. 110 * @param formId The Id of the form. 111 * @return true or false. 112 */ 113 void MarkRemind(const int64_t formId); 114 115 /** 116 * @brief Handle system time changed. 117 * @return Returns true on success, false on failure. 118 */ 119 bool HandleSystemTimeChanged(); 120 /** 121 * @brief Reset form limiter. 122 * @return Returns true on success, false on failure. 123 */ 124 bool HandleResetLimiter(); 125 /** 126 * @brief Update attime trigger. 127 * @param updateTime Update time. 128 * @return Returns true on success, false on failure. 129 */ 130 bool OnUpdateAtTrigger(long updateTime); 131 /** 132 * @brief Dynamic time trigger. 133 * @param updateTime Update time. 134 * @return Returns true on success, false on failure. 135 */ 136 bool OnDynamicTimeTrigger(long updateTime); 137 138 private: 139 /** 140 * @brief Add update at timer. 141 * @param task Update time task. 142 * @return Returns true on success, false on failure. 143 */ 144 bool AddUpdateAtTimer(const FormTimer &task); 145 /** 146 * @brief Add update at timer item. 147 * @param task Update at timer item. 148 */ 149 void AddUpdateAtItem(const UpdateAtItem &atItem); 150 /** 151 * @brief Add update interval timer task. 152 * @param task Update interval timer task. 153 * @return Returns true on success, false on failure. 154 */ 155 bool AddIntervalTimer(const FormTimer &task); 156 /** 157 * @brief interval timer task timeout. 158 */ 159 void OnIntervalTimeOut(); 160 161 /** 162 * @brief Get remind tasks. 163 * @param remindTasks Remind tasks. 164 * @return Returns true on success, false on failure. 165 */ 166 bool GetRemindTasks(std::vector<FormTimer> &remindTasks); 167 /** 168 * @brief Set enableFlag for interval timer task. 169 * @param formId The Id of the form. 170 * @param flag Enable flag. 171 */ 172 void SetIntervalEnableFlag(int64_t formId, bool flag); 173 /** 174 * @brief Update Interval timer task value. 175 * @param formId The Id of the form. 176 * @param timerCfg task value. 177 * @return Returns true on success, false on failure. 178 */ 179 bool UpdateIntervalValue(const int64_t formId, const FormTimerCfg &timerCfg); 180 /** 181 * @brief Update update at timer task value. 182 * @param formId The Id of the form. 183 * @param timerCfg task value. 184 * @return Returns true on success, false on failure. 185 */ 186 bool UpdateAtTimerValue(const int64_t formId, const FormTimerCfg &timerCfg); 187 /** 188 * @brief Interval timer task to update at timer task. 189 * @param formId The Id of the form. 190 * @param timerCfg task value. 191 * @return Returns true on success, false on failure. 192 */ 193 bool IntervalToAtTimer(const int64_t formId, const FormTimerCfg &timerCfg); 194 /** 195 * @brief Update at timer task to interval timer task. 196 * @param formId The Id of the form. 197 * @param timerCfg task value. 198 * @return Returns true on success, false on failure. 199 */ 200 bool AtTimerToIntervalTimer(const int64_t formId, const FormTimerCfg &timerCfg); 201 /** 202 * @brief Delete interval timer task. 203 * @param formId The Id of the form. 204 * @return Returns true on success, false on failure. 205 */ 206 bool DeleteIntervalTimer(const int64_t formId); 207 /** 208 * @brief Delete update at timer. 209 * @param formId The Id of the form. 210 * @return Returns true on success, false on failure. 211 */ 212 bool DeleteUpdateAtTimer(const int64_t formId); 213 /** 214 * @brief Update at timer task alarm. 215 * @return Returns true on success, false on failure. 216 */ 217 bool UpdateAtTimerAlarm(); 218 /** 219 * @brief Update limiter task alarm. 220 * @return Returns true on success, false on failure. 221 */ 222 bool UpdateLimiterAlarm(); 223 /** 224 * @brief Clear limiter timer resource. 225 */ 226 void ClearLimiterTimerResource(); 227 /** 228 * @brief Delete dynamic refresh item. 229 * @param formId The Id of the form. 230 * @return Returns true on success, false on failure. 231 */ 232 bool DeleteDynamicItem(const int64_t formId); 233 /** 234 * @brief Update dynamic refresh task alarm. 235 * @return Returns true on success, false on failure. 236 */ 237 bool UpdateDynamicAlarm(); 238 /** 239 * @brief Clear dynamic refresh resource. 240 */ 241 void ClearDynamicResource(); 242 /** 243 * @brief Fint next at timer item. 244 * @param nowTime Update time. 245 * @param updateAtItem Next at timer item. 246 * @return Returns true on success, false on failure. 247 */ 248 bool FindNextAtTimerItem(const int nowTime, UpdateAtItem &updateAtItem); 249 /** 250 * @brief Clear update at timer resource. 251 */ 252 void ClearUpdateAtTimerResource(); 253 254 /** 255 * @brief Execute Form timer task. 256 * @param task Form timer task. 257 */ 258 void ExecTimerTask(const FormTimer &task); 259 260 /** 261 * @brief Init. 262 */ 263 void Init(); 264 /** 265 * @brief Ensure init interval timer resource. 266 */ 267 void EnsureInitIntervalTimer(); 268 /** 269 * @brief Clear interval timer resource. 270 */ 271 void ClearIntervalTimer(); 272 /** 273 * @brief Get thread pool for timer task. 274 */ 275 OHOS::ThreadPool* GetTaskThreadExecutor(); 276 277 /** 278 * @brief Set enable flag. 279 * @param formId The Id of the form. 280 * @param flag Enable flag. 281 */ 282 void SetEnableFlag(int64_t formId, bool flag); 283 private: 284 /** 285 * @class TimerReceiver 286 * timer event receiver. 287 */ 288 class TimerReceiver : public EventFwk::CommonEventSubscriber { 289 public: 290 TimerReceiver() = default; 291 TimerReceiver(const EventFwk::CommonEventSubscribeInfo &subscriberInfo); 292 virtual ~TimerReceiver() = default; 293 /** 294 * @brief Receive common event. 295 * @param eventData Common event data. 296 */ 297 virtual void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override; 298 }; 299 300 struct { operator__anona84952a10108301 bool operator()(DynamicRefreshItem a, DynamicRefreshItem b) const 302 { 303 return (a.settedTime > b.settedTime ? true : false); 304 } 305 } CompareDynamicRefreshItem; 306 307 mutable std::mutex intervalMutex_; 308 mutable std::mutex updateAtMutex_; 309 mutable std::mutex dynamicMutex_; 310 mutable std::mutex refreshMutex_; 311 FormRefreshLimiter refreshLimiter_; 312 std::map<int64_t, FormTimer> intervalTimerTasks_; 313 std::list<UpdateAtItem> updateAtTimerTasks_; 314 std::vector<DynamicRefreshItem> dynamicRefreshTasks_; 315 316 std::shared_ptr<TimerReceiver> timerReceiver_ = nullptr; 317 OHOS::ThreadPool* taskExecutor_ = nullptr; 318 319 uint64_t intervalTimerId_ = 0L; 320 uint64_t updateAtTimerId_ = 0L; 321 uint64_t dynamicAlarmTimerId_ = 0L; 322 uint64_t limiterTimerId_= 0L; 323 324 325 326 }; 327 328 } // namespace AppExecFwk 329 } // namespace OHOS 330 331 #endif // FOUNDATION_APPEXECFWK_SERVICES_FORMMGR_INCLUDE_FORM_TIMER_MGR_H 332