1 /* 2 * Copyright (c) 2021-2022 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 OHOS_FORM_FWK_FORM_TIMER_MGR_H 17 #define OHOS_FORM_FWK_FORM_TIMER_MGR_H 18 19 #include <list> 20 #include <map> 21 #include <mutex> 22 #include <singleton.h> 23 #include <vector> 24 25 #include "common_event_subscriber.h" 26 #include "common_event_subscribe_info.h" 27 #include "ffrt.h" 28 #include "form_refresh_limiter.h" 29 #include "form_timer.h" 30 #include "timer.h" 31 #include "want_agent.h" 32 33 namespace OHOS { 34 namespace AppExecFwk { 35 using namespace OHOS::AbilityRuntime::WantAgent; 36 /** 37 * @class FormTimerMgr 38 * form timer task manager. 39 */ 40 class FormTimerMgr final : public DelayedRefSingleton<FormTimerMgr> { 41 DECLARE_DELAYED_REF_SINGLETON(FormTimerMgr) 42 public: 43 DISALLOW_COPY_AND_MOVE(FormTimerMgr); 44 /** 45 * @brief Add form timer by timer task. 46 * @param task The form timer task. 47 * @return Returns true on success, false on failure. 48 */ 49 bool AddFormTimer(const FormTimer &task); 50 /** 51 * @brief Add duration form timer. 52 * @param formId The Id of the form. 53 * @param updateDuration Update duration. 54 * @param userId User ID. 55 * @return Returns true on success, false on failure. 56 */ 57 bool AddFormTimer(int64_t formId, long updateDuration, int32_t userId = 0); 58 /** 59 * @brief Add scheduled form timer. 60 * @param formId The Id of the form. 61 * @param updateAtHour Hour 62 * @param updateAtMin Min 63 * @param userId User ID. 64 * @return Returns true on success, false on failure. 65 */ 66 bool AddFormTimer(int64_t formId, long updateAtHour, long updateAtMin, int32_t userId = 0); 67 /** 68 * @brief Remove form timer by form id. 69 * @param formId The Id of the form. 70 * @return Returns true on success, false on failure. 71 */ 72 bool RemoveFormTimer(int64_t formId); 73 /** 74 * @brief Update form timer. 75 * @param formId The Id of the form. 76 * @param type Timer type. 77 * @param timerCfg Timer config. 78 * @return Returns true on success, false on failure. 79 */ 80 bool UpdateFormTimer(int64_t formId, const UpdateType &type, const FormTimerCfg &timerCfg); 81 /** 82 * @brief Is limiter enable refresh. 83 * @param formId The Id of the form. 84 * @return Returns true on success, false on failure. 85 */ 86 bool IsLimiterEnableRefresh(int64_t formId); 87 /** 88 * @brief Increase refresh count. 89 * @param formId The Id of the form. 90 */ 91 void IncreaseRefreshCount(int64_t formId); 92 /** 93 * @brief Set next refresh time. 94 * @param formId The Id of the form. 95 * @param nextGapTime Next gap time. 96 * @param userId User ID. 97 * @return Returns true on success, false on failure. 98 */ 99 bool SetNextRefreshTime(int64_t formId, long nextGapTime, int32_t userId = 0); 100 /** 101 * @brief Get refresh count. 102 * @param formId The Id of the form. 103 * @return Returns refresh count. 104 */ 105 int GetRefreshCount(int64_t formId) const; 106 /** 107 * @brief Mark remind. 108 * @param formId The Id of the form. 109 * @return true or false. 110 */ 111 void MarkRemind(int64_t formId); 112 113 /** 114 * @brief Handle system time changed. 115 * @return Returns true on success, false on failure. 116 */ 117 bool HandleSystemTimeChanged(); 118 /** 119 * @brief Reset form limiter. 120 * @return Returns true on success, false on failure. 121 */ 122 bool HandleResetLimiter(); 123 /** 124 * @brief Update at time trigger. 125 * @param updateTime Update time. 126 * @return Returns true on success, false on failure. 127 */ 128 bool OnUpdateAtTrigger(long updateTime); 129 /** 130 * @brief Dynamic time trigger. 131 * @param updateTime Update time. 132 * @return Returns true on success, false on failure. 133 */ 134 bool OnDynamicTimeTrigger(int64_t updateTime); 135 /** 136 * @brief Get interval timer task. 137 * @param formId The Id of the form. 138 * @param formTimer update timer. 139 * @return Returns true on success, false on failure. 140 */ 141 bool GetIntervalTimer(int64_t formId, FormTimer &formTimer); 142 /** 143 * @brief Get update at timer. 144 * @param formId The Id of the form. 145 * @param updateAtItem update timer. 146 * @return Returns true on success, false on failure. 147 */ 148 bool GetUpdateAtTimer(int64_t formId, UpdateAtItem &updateAtItem); 149 /** 150 * @brief Get dynamic refresh item. 151 * @param formId The Id of the form. 152 * @param dynamicItem update timer. 153 * @return Returns true on success, false on failure. 154 */ 155 bool GetDynamicItem(int64_t formId, DynamicRefreshItem &dynamicItem); 156 /** 157 * @brief Set time speed. 158 * @param timeSpeed The time speed. 159 */ 160 void SetTimeSpeed(int32_t timeSpeed); 161 /** 162 * @brief refresh form when form visible. 163 * @param formId The Id of the form. 164 * @param userId User ID. 165 */ 166 void RefreshWhenFormVisible(const int64_t &formId, const int32_t &userId); 167 /** 168 * @brief Update at timer task alarm. 169 * @return Returns true on success, false on failure. 170 */ 171 bool UpdateAtTimerAlarm(); 172 /** 173 * @brief Update limiter task alarm. 174 * @return Returns true on success, false on failure. 175 */ 176 bool UpdateLimiterAlarm(); 177 /** 178 * @brief Update dynamic refresh task alarm. 179 * @return Returns true on success, false on failure. 180 */ 181 bool UpdateDynamicAlarm(); 182 183 #ifdef RES_SCHEDULE_ENABLE 184 /** 185 * @brief Set the value which indicate whether Refresh Timer task should be triggered. 186 * @param isTimerTaskNeeded The value of whether Refresh Timer task should be triggered. 187 */ 188 void SetTimerTaskNeeded(bool isTimerTaskNeeded); 189 #endif // RES_SCHEDULE_ENABLE 190 private: 191 /** 192 * @brief Add update at timer. 193 * @param task Update time task. 194 * @return Returns true on success, false on failure. 195 */ 196 bool AddUpdateAtTimer(const FormTimer &task); 197 /** 198 * @brief Add update at timer item. 199 * @param atItem Update at timer item. 200 */ 201 void AddUpdateAtItem(const UpdateAtItem &atItem); 202 /** 203 * @brief Add update interval timer task. 204 * @param task Update interval timer task. 205 * @return Returns true on success, false on failure. 206 */ 207 bool AddIntervalTimer(const FormTimer &task); 208 /** 209 * @brief interval timer task timeout. 210 */ 211 void OnIntervalTimeOut(); 212 /** 213 * @brief Get remind tasks. 214 * @param remindTasks Remind tasks. 215 * @return Returns true on success, false on failure. 216 */ 217 bool GetRemindTasks(std::vector<FormTimer> &remindTasks); 218 /** 219 * @brief Set enableFlag for interval timer task. 220 * @param formId The Id of the form. 221 * @param flag Enable flag. 222 */ 223 void SetIntervalEnableFlag(int64_t formId, bool flag); 224 /** 225 * @brief Update Interval timer task value. 226 * @param formId The Id of the form. 227 * @param timerCfg task value. 228 * @return Returns true on success, false on failure. 229 */ 230 bool UpdateIntervalValue(int64_t formId, const FormTimerCfg &timerCfg); 231 /** 232 * @brief Update update at timer task value. 233 * @param formId The Id of the form. 234 * @param timerCfg task value. 235 * @return Returns true on success, false on failure. 236 */ 237 bool UpdateAtTimerValue(int64_t formId, const FormTimerCfg &timerCfg); 238 /** 239 * @brief Interval timer task to update at timer task. 240 * @param formId The Id of the form. 241 * @param timerCfg task value. 242 * @return Returns true on success, false on failure. 243 */ 244 bool IntervalToAtTimer(int64_t formId, const FormTimerCfg &timerCfg); 245 /** 246 * @brief Update at timer task to interval timer task. 247 * @param formId The Id of the form. 248 * @param timerCfg task value. 249 * @return Returns true on success, false on failure. 250 */ 251 bool AtTimerToIntervalTimer(int64_t formId, const FormTimerCfg &timerCfg); 252 /** 253 * @brief Delete interval timer task. 254 * @param formId The Id of the form. 255 * @return Returns true on success, false on failure. 256 */ 257 bool DeleteIntervalTimer(int64_t formId); 258 /** 259 * @brief Delete update at timer. 260 * @param formId The Id of the form. 261 * @return Returns true on success, false on failure. 262 */ 263 bool DeleteUpdateAtTimer(int64_t formId); 264 /** 265 * @brief Clear limiter timer resource. 266 */ 267 void ClearLimiterTimerResource(); 268 /** 269 * @brief Delete dynamic refresh item. 270 * @param formId The Id of the form. 271 * @return Returns true on success, false on failure. 272 */ 273 bool DeleteDynamicItem(int64_t formId); 274 /** 275 * @brief Clear dynamic refresh resource. 276 */ 277 void ClearDynamicResource(); 278 /** 279 * @brief Find next at timer item. 280 * @param nowTime Update time. 281 * @param updateAtItem Next at timer item. 282 * @return Returns true on success, false on failure. 283 */ 284 bool FindNextAtTimerItem(long nowTime, UpdateAtItem &updateAtItem); 285 /** 286 * @brief Clear update at timer resource. 287 */ 288 void ClearUpdateAtTimerResource(); 289 290 /** 291 * @brief Execute Form timer task. 292 * @param task Form timer task. 293 */ 294 void ExecTimerTask(const FormTimer &task); 295 296 /** 297 * @brief create limiter timer 298 * @return Returns true on success, false on failure. 299 */ 300 bool CreateLimiterTimer(); 301 302 #ifdef RES_SCHEDULE_ENABLE 303 /** 304 * @brief Execute Form timer task. 305 * @param task Form timer task core. 306 */ 307 void ExecTimerTaskCore(const FormTimer &task); 308 309 /** 310 * @brief Add to the task vector triggered on systemloadlevel down. 311 * @param task Form timer task. 312 */ 313 void AddToNotExecTaskVec(const FormTimer &task); 314 315 /** 316 * @brief tiggered and clear the task vector triggered on systemloadlevel down. 317 */ 318 void TriggerAndClearNotExecTaskVec(); 319 #endif // RES_SCHEDULE_ENABLE 320 321 /** 322 * @brief Init. 323 */ 324 void Init(); 325 /** 326 * @brief Ensure init interval timer resource. 327 */ 328 void EnsureInitIntervalTimer(); 329 /** 330 * @brief Clear interval timer resource. 331 */ 332 void ClearIntervalTimer(); 333 /** 334 * @brief Set enable flag. 335 * @param formId The Id of the form. 336 * @param flag Enable flag. 337 */ 338 void SetEnableFlag(int64_t formId, bool flag); 339 /** 340 * @brief Get WantAgent. 341 * @param updateAtTime The next update time. 342 * @return Returns WantAgent. 343 */ 344 std::shared_ptr<WantAgent> GetUpdateAtWantAgent(long updateAtTime, int32_t userId); 345 /** 346 * @brief Get WantAgent. 347 * @return Returns WantAgent. 348 */ 349 std::shared_ptr<WantAgent> GetLimiterWantAgent(); 350 /** 351 * @brief Get WantAgent. 352 * @param nextTime The next update time. 353 * @return Returns WantAgent. 354 */ 355 std::shared_ptr<WantAgent> GetDynamicWantAgent(int64_t nextTime, int32_t userId); 356 357 /** 358 * @brief check if user is active or not. 359 * @param userId User ID. 360 * @return true:active, false:inactive 361 */ 362 bool IsActiveUser(int32_t userId); 363 private: 364 /** 365 * @class TimerReceiver 366 * timer event receiver. 367 */ 368 class TimerReceiver : public EventFwk::CommonEventSubscriber { 369 public: 370 TimerReceiver() = default; 371 TimerReceiver(const EventFwk::CommonEventSubscribeInfo &subscriberInfo); 372 virtual ~TimerReceiver() = default; 373 /** 374 * @brief Receive common event. 375 * @param eventData Common event data. 376 */ 377 virtual void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override; 378 }; 379 380 struct { operator__anona54f22a30108381 bool operator()(const DynamicRefreshItem& a, const DynamicRefreshItem& b) const 382 { 383 return a.settedTime < b.settedTime; 384 } 385 } CompareDynamicRefreshItem; 386 387 int64_t GetBootTimeMs(); 388 389 bool IsNeedUpdate(); 390 391 void FormRefreshCountReport(); 392 void InnerClearIntervalTimer(); 393 void InnerClearIntervalReportTimer(); 394 395 mutable std::mutex intervalMutex_; 396 mutable std::mutex updateAtMutex_; 397 mutable std::mutex dynamicMutex_; 398 FormRefreshLimiter refreshLimiter_; 399 std::map<int64_t, FormTimer> intervalTimerTasks_; 400 std::list<UpdateAtItem> updateAtTimerTasks_; 401 std::list<DynamicRefreshItem> dynamicRefreshTasks_; 402 std::shared_ptr<TimerReceiver> systemTimerEventReceiver_ = nullptr; 403 std::shared_ptr<TimerReceiver> customTimerEventReceiver_ = nullptr; 404 int32_t timeSpeed_ = 1; 405 406 uint64_t intervalTimerId_ = 0L; 407 uint64_t updateAtTimerId_ = 0L; 408 uint64_t dynamicAlarmTimerId_ = 0L; 409 uint64_t limiterTimerId_ = 0L; 410 uint64_t limiterTimerReportId_ = 0L; 411 412 mutable std::mutex currentLimiterWantAgentMutex_; 413 mutable std::mutex currentUpdateWantAgentMutex_; 414 std::shared_ptr<WantAgent> currentUpdateAtWantAgent_ = nullptr; 415 std::shared_ptr<WantAgent> currentDynamicWantAgent_ = nullptr; 416 std::shared_ptr<WantAgent> currentLimiterWantAgent_ = nullptr; 417 418 int64_t dynamicWakeUpTime_ = INT64_MAX; 419 long atTimerWakeUpTime_ = LONG_MAX; 420 421 #ifdef RES_SCHEDULE_ENABLE 422 bool isTimerTaskNeeded_ = true; 423 std::vector<FormTimer> notExecTaskVec_; 424 #endif // RES_SCHEDULE_ENABLE 425 }; 426 } // namespace AppExecFwk 427 } // namespace OHOS 428 #endif // OHOS_FORM_FWK_FORM_TIMER_MGR_H 429