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