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 TIMER_MANAGER_H 17 #define TIMER_MANAGER_H 18 19 #include <functional> 20 #include <list> 21 #include <memory> 22 #include "define_multimodal.h" 23 #include "mmi_log.h" 24 #include "singleton.h" 25 #include "util.h" 26 27 namespace OHOS { 28 namespace MMI { 29 class TimerManager : public DelayedSingleton<TimerManager> { 30 public: 31 int32_t AddTimer(int32_t intervalMs, int32_t repeatCount, std::function<void()> callback); 32 int32_t RemoveTimer(int32_t timerId); 33 int32_t ResetTimer(int32_t timerId); 34 bool IsExist(int32_t timerId); 35 int32_t CalcNextDelay(); 36 void ProcessTimers(); 37 38 private: 39 static constexpr int32_t MIN_DELAY = 36; 40 static constexpr int32_t MIN_INTERVAL = 50; 41 static constexpr int32_t MAX_INTERVAL = 4096; 42 static constexpr int32_t MAX_TIMER_COUNT = 32; 43 static constexpr int32_t NONEXISTENT_ID = -1; 44 45 private: 46 struct TimerItem { 47 int32_t id; 48 int32_t intervalMs; 49 int32_t repeatCount; 50 int32_t callbackCount; 51 int64_t nextCallTime; 52 std::function<void()> callback; 53 }; 54 private: 55 int32_t TakeNextTimerId(); 56 int32_t AddTimerInternal(int32_t intervalMs, int32_t repeatCount, std::function<void()> callback); 57 int32_t RemoveTimerInternal(int32_t timerId); 58 int32_t ResetTimerInternal(int32_t timerId); 59 bool IsExistInternal(int32_t timerId); 60 std::unique_ptr<TimerItem>& InsertTimerInternal(std::unique_ptr<TimerItem>& timer); 61 int32_t CalcNextDelayInternal(); 62 void ProcessTimersInternal(); 63 64 private: 65 std::list<std::unique_ptr<TimerItem>> timers_; 66 }; 67 } // namespace MMI 68 } // namespace OHOS 69 #define TimerMgr OHOS::MMI::TimerManager::GetInstance() 70 #endif // TIMER_MANAGER_H