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