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 "common/timer_mgr/form_refresh_limiter.h" 29 #include "common/timer_mgr/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 Update at timer task alarm. 173 * @return Returns true on success, false on failure. 174 */ 175 bool UpdateAtTimerAlarm(); 176 /** 177 * @brief Update limiter task alarm. 178 * @return Returns true on success, false on failure. 179 */ 180 bool UpdateLimiterAlarm(); 181 /** 182 * @brief Update dynamic refresh task alarm. 183 * @return Returns true on success, false on failure. 184 */ 185 bool UpdateDynamicAlarm(); 186 /** 187 * @brief per 24h report disk use info. 188 */ 189 void StartDiskUseInfoReportTimer(); 190 191 private: 192 /** 193 * @brief Add update at timer. 194 * @param task Update time task. 195 * @return Returns true on success, false on failure. 196 */ 197 bool AddUpdateAtTimer(const FormTimer &task); 198 /** 199 * @brief Add update at timer item. 200 * @param atItem Update at timer item. 201 */ 202 void AddUpdateAtItem(const UpdateAtItem &atItem); 203 /** 204 * @brief Add update interval timer task. 205 * @param task Update interval timer task. 206 * @return Returns true on success, false on failure. 207 */ 208 bool AddIntervalTimer(const FormTimer &task); 209 /** 210 * @brief interval timer task timeout. 211 */ 212 void OnIntervalTimeOut(); 213 /** 214 * @brief Get remind tasks. 215 * @param remindTasks Remind tasks. 216 * @return Returns true on success, false on failure. 217 */ 218 bool GetRemindTasks(std::vector<FormTimer> &remindTasks); 219 /** 220 * @brief Set enableFlag for interval timer task. 221 * @param formId The Id of the form. 222 * @param flag Enable flag. 223 */ 224 void SetIntervalEnableFlag(int64_t formId, bool flag); 225 /** 226 * @brief Update Interval timer task value. 227 * @param formId The Id of the form. 228 * @param timerCfg task value. 229 * @return Returns true on success, false on failure. 230 */ 231 bool UpdateIntervalValue(int64_t formId, const FormTimerCfg &timerCfg); 232 /** 233 * @brief Update update at timer task value. 234 * @param formId The Id of the form. 235 * @param timerCfg task value. 236 * @return Returns true on success, false on failure. 237 */ 238 bool UpdateAtTimerValue(int64_t formId, const FormTimerCfg &timerCfg); 239 /** 240 * @brief Interval timer task to update at timer task. 241 * @param formId The Id of the form. 242 * @param timerCfg task value. 243 * @return Returns true on success, false on failure. 244 */ 245 bool IntervalToAtTimer(int64_t formId, const FormTimerCfg &timerCfg); 246 /** 247 * @brief Update at timer task to interval timer task. 248 * @param formId The Id of the form. 249 * @param timerCfg task value. 250 * @return Returns true on success, false on failure. 251 */ 252 bool AtTimerToIntervalTimer(int64_t formId, const FormTimerCfg &timerCfg); 253 /** 254 * @brief Delete interval timer task. 255 * @param formId The Id of the form. 256 * @return Returns true on success, false on failure. 257 */ 258 bool DeleteIntervalTimer(int64_t formId); 259 /** 260 * @brief Delete update at timer. 261 * @param formId The Id of the form. 262 * @return Returns true on success, false on failure. 263 */ 264 bool DeleteUpdateAtTimer(int64_t formId); 265 /** 266 * @brief Clear limiter timer resource. 267 */ 268 void ClearLimiterTimerResource(); 269 /** 270 * @brief Delete dynamic refresh item. 271 * @param formId The Id of the form. 272 * @return Returns true on success, false on failure. 273 */ 274 bool DeleteDynamicItem(int64_t formId); 275 /** 276 * @brief Clear dynamic refresh resource. 277 */ 278 void ClearDynamicResource(); 279 /** 280 * @brief Find next at timer item. 281 * @param nowTime Update time. 282 * @param updateAtItem Next at timer item. 283 * @return Returns true on success, false on failure. 284 */ 285 bool FindNextAtTimerItem(long nowTime, UpdateAtItem &updateAtItem); 286 /** 287 * @brief Clear update at timer resource. 288 */ 289 void ClearUpdateAtTimerResource(); 290 291 /** 292 * @brief Execute Form timer task. 293 * @param task Form timer task. 294 */ 295 void ExecTimerTask(const FormTimer &task); 296 297 /** 298 * @brief create limiter timer 299 * @return Returns true on success, false on failure. 300 */ 301 bool CreateLimiterTimer(); 302 303 #ifdef RES_SCHEDULE_ENABLE 304 /** 305 * @brief Execute Form timer task. 306 * @param task Form timer task core. 307 */ 308 void ExecTimerTaskCore(const FormTimer &task); 309 #endif // RES_SCHEDULE_ENABLE 310 311 /** 312 * @brief Init. 313 */ 314 void Init(); 315 /** 316 * @brief Ensure init interval timer resource. 317 */ 318 void EnsureInitIntervalTimer(); 319 /** 320 * @brief Clear interval timer resource. 321 */ 322 void ClearIntervalTimer(); 323 /** 324 * @brief Set enable flag. 325 * @param formId The Id of the form. 326 * @param flag Enable flag. 327 */ 328 void SetEnableFlag(int64_t formId, bool flag); 329 /** 330 * @brief Get WantAgent. 331 * @param updateAtTime The next update time. 332 * @return Returns WantAgent. 333 */ 334 std::shared_ptr<WantAgent> GetUpdateAtWantAgent(long updateAtTime, int32_t userId); 335 /** 336 * @brief Get WantAgent. 337 * @return Returns WantAgent. 338 */ 339 std::shared_ptr<WantAgent> GetLimiterWantAgent(); 340 /** 341 * @brief Get WantAgent. 342 * @param nextTime The next update time. 343 * @return Returns WantAgent. 344 */ 345 std::shared_ptr<WantAgent> GetDynamicWantAgent(int64_t nextTime, int32_t userId); 346 347 /** 348 * @brief check if user is active or not. 349 * @param userId User ID. 350 * @return true:active, false:inactive 351 */ 352 bool IsActiveUser(int32_t userId); 353 private: 354 /** 355 * @class TimerReceiver 356 * timer event receiver. 357 */ 358 class TimerReceiver : public EventFwk::CommonEventSubscriber { 359 public: 360 TimerReceiver() = default; 361 TimerReceiver(const EventFwk::CommonEventSubscribeInfo &subscriberInfo); 362 virtual ~TimerReceiver() = default; 363 /** 364 * @brief Receive common event. 365 * @param eventData Common event data. 366 */ 367 virtual void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override; 368 }; 369 370 struct { operator__anona4b0b4fa0108371 bool operator()(const DynamicRefreshItem& a, const DynamicRefreshItem& b) const 372 { 373 return a.settedTime < b.settedTime; 374 } 375 } CompareDynamicRefreshItem; 376 377 int64_t GetBootTimeMs(); 378 379 bool IsNeedUpdate(); 380 381 void FormPeriodReport(); 382 void ClearDiskInfoReportTimer(); 383 void InnerClearIntervalTimer(); 384 void InnerClearIntervalReportTimer(); 385 386 mutable std::mutex intervalMutex_; 387 mutable std::mutex updateAtMutex_; 388 mutable std::mutex dynamicMutex_; 389 FormRefreshLimiter refreshLimiter_; 390 std::map<int64_t, FormTimer> intervalTimerTasks_; 391 std::list<UpdateAtItem> updateAtTimerTasks_; 392 std::list<DynamicRefreshItem> dynamicRefreshTasks_; 393 std::shared_ptr<TimerReceiver> systemTimerEventReceiver_ = nullptr; 394 std::shared_ptr<TimerReceiver> customTimerEventReceiver_ = nullptr; 395 int32_t timeSpeed_ = 1; 396 397 uint64_t intervalTimerId_ = 0L; 398 uint64_t updateAtTimerId_ = 0L; 399 uint64_t dynamicAlarmTimerId_ = 0L; 400 uint64_t limiterTimerId_ = 0L; 401 uint64_t limiterTimerReportId_ = 0L; 402 uint64_t reportDiskUseTimerId_ = 0L; 403 404 mutable std::mutex currentLimiterWantAgentMutex_; 405 mutable std::mutex currentUpdateWantAgentMutex_; 406 std::shared_ptr<WantAgent> currentUpdateAtWantAgent_ = nullptr; 407 std::shared_ptr<WantAgent> currentDynamicWantAgent_ = nullptr; 408 std::shared_ptr<WantAgent> currentLimiterWantAgent_ = nullptr; 409 410 int64_t dynamicWakeUpTime_ = INT64_MAX; 411 long atTimerWakeUpTime_ = LONG_MAX; 412 }; 413 } // namespace AppExecFwk 414 } // namespace OHOS 415 #endif // OHOS_FORM_FWK_FORM_TIMER_MGR_H 416