1 /*
2  * Copyright (c) 2024 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 UI_APPEARANCE_UTILS_ALARM_TIMER_MANAGER_H
17 #define UI_APPEARANCE_UTILS_ALARM_TIMER_MANAGER_H
18 
19 #include <array>
20 #include <map>
21 #include <cstdint>
22 
23 #include "alarm_timer.h"
24 #include "errors.h"
25 
26 namespace OHOS::ArkUi::UiAppearance {
27 constexpr uint32_t TRIGGER_ARRAY_SIZE = 2;
28 
29 class AlarmTimerManager {
30 public:
31     AlarmTimerManager() = default;
32 
33     virtual ~AlarmTimerManager() = default;
34 
35     ErrCode SetScheduleTime(uint64_t startTime, uint64_t endTime, uint32_t userId,
36         const std::function<void()>& startCallback, const std::function<void()>& endCallback);
37 
38     void ClearTimerByUserId(uint64_t userId);
39 
40     bool RestartAllTimer();
41 
42     static bool IsValidScheduleTime(uint64_t startTime, uint64_t endTime);
43 
44     static bool IsWithinTimeInterval(uint64_t startTime, uint64_t endTime);
45 
46     static void SetTimerTriggerTime(uint64_t startTime, uint64_t endTime,
47         std::array<uint64_t, TRIGGER_ARRAY_SIZE>& triggerTimeInterval);
48 
49     void Dump();
50 
51 private:
52     std::map<uint32_t, std::array<uint64_t, TRIGGER_ARRAY_SIZE>> timerIdMap_;
53     std::map<uint32_t, std::array<uint64_t, TRIGGER_ARRAY_SIZE>> initialSetupTimeMap_;
54     std::mutex timerMapMutex_;
55 
56     static void RestartTimerByTimerId(uint64_t timerId, uint64_t time);
57 
58     static uint64_t InitTimer(uint64_t time, const std::function<void()>& callback);
59 
60     static uint64_t UpdateTimer(uint64_t id, uint64_t time, const std::function<void()>& callback);
61 
62     static void ClearTimer(uint64_t id);
63 
64     void RecordInitialSetupTime(uint64_t startTime, uint64_t endTime, uint32_t userId);
65 
66     void SetTimer(int8_t index, uint32_t userId, uint64_t time, const std::function<void()>& callback);
67 
68     bool RestartTimerByUserId(uint64_t userId);
69 };
70 } // namespace OHOS::ArkUi::UiAppearance
71 #endif // UI_APPEARANCE_UTILS_ALARM_TIMER_MANAGER_H
72