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_DARK_MODE_MANAGER_H
17 #define UI_APPEARANCE_DARK_MODE_MANAGER_H
18 
19 #include <functional>
20 #include <vector>
21 
22 #include "errors.h"
23 #include "nocopyable.h"
24 #include "alarm_timer_manager.h"
25 
26 namespace OHOS::ArkUi::UiAppearance {
27 class DarkModeManager final : public NoCopyable {
28 public:
29     static DarkModeManager &GetInstance();
30 
31     ErrCode Initialize(const std::function<void(bool, int32_t)> &updateCallback);
32 
33     ErrCode LoadUserSettingData(int32_t userId, bool needUpdateCallback, bool &isDarkMode);
34 
35     void NotifyDarkModeUpdate(int32_t userId, bool isDarkMode);
36 
37     ErrCode OnSwitchUser(int32_t userId);
38 
39     ErrCode RestartTimer();
40 
41     void Dump();
42 
43 private:
44     enum DarkModeMode {
45         DARK_MODE_INVALID = -1,
46         DARK_MODE_ALWAYS_LIGHT = 0,
47         DARK_MODE_ALWAYS_DARK = 1,
48         DARK_MODE_CUSTOM_AUTO = 2,
49         DARK_MODE_SIZE,
50     };
51 
52     struct DarkModeState {
53         DarkModeMode settingMode = DARK_MODE_INVALID;
54         int32_t settingStartTime = -1;
55         int32_t settingEndTime = -1;
56     };
57 
58     void LoadSettingDataObserversCallback();
59 
60     ErrCode RegisterSettingDataObserversLocked(int32_t userId) const;
61 
62     void UnregisterSettingDataObserversLocked(int32_t userId) const;
63 
64     void SettingDataDarkModeModeUpdateFunc(const std::string& key, int32_t userId);
65 
66     void SettingDataDarkModeStartTimeUpdateFunc(const std::string& key, int32_t userId);
67 
68     void SettingDataDarkModeEndTimeUpdateFunc(const std::string& key, int32_t userId);
69 
70     ErrCode OnStateChangeLocked(int32_t userId, bool needUpdateCallback, bool &isDarkMode);
71 
72     ErrCode OnStateChangeToAllDayMode(int32_t userId, DarkModeMode darkMode, bool needUpdateCallback, bool &isDarkMode);
73 
74     ErrCode OnStateChangeToCustomAutoMode(
75         int32_t userId, const DarkModeState& state, bool needUpdateCallback, bool &isDarkMode);
76 
77     void OnChangeDarkMode(DarkModeMode mode, int32_t userId) const;
78 
79     ErrCode CreateOrUpdateTimers(int32_t startTime, int32_t endTime, int32_t userId);
80 
81     ErrCode CheckTimerCallbackParams(int32_t startTime, int32_t endTime, int32_t userId);
82 
83     std::mutex settingDataObserversMutex_;
84     std::vector<std::pair<std::string, std::function<void(const std::string&, int32_t)>>> settingDataObservers_;
85     int32_t settingDataObserversUserId_ = -1;
86 
87     AlarmTimerManager alarmTimerManager_;
88     std::mutex darkModeStatesMutex_;
89     std::map<int32_t, DarkModeState> darkModeStates_;
90 
91     std::function<void(bool, int32_t)> updateCallback_;
92 };
93 } // namespace OHOS::ArkUi::UiAppearance
94 
95 #endif // UI_APPEARANCE_DARK_MODE_MANAGER_H
96