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