1 /* 2 * Copyright (c) 2024-2025 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 <list> 21 22 #include "errors.h" 23 #include "nocopyable.h" 24 #include "alarm_timer_manager.h" 25 #include "dark_mode_temp_state_manager.h" 26 #include "screen_switch_operator_manager.h" 27 28 namespace OHOS::ArkUi::UiAppearance { 29 constexpr int32_t HOUR_TO_MINUTE = 60; 30 constexpr int32_t DAY_TO_MINUTE = 24 * 60; 31 constexpr int32_t SUNSET_TIME_DEFAULT = 18 * HOUR_TO_MINUTE; 32 constexpr int32_t SUNRISE_TIME_DEFAULT = 7 * HOUR_TO_MINUTE + DAY_TO_MINUTE; 33 class DarkModeManager final : public NoCopyable { 34 public: 35 static DarkModeManager &GetInstance(); 36 37 ErrCode Initialize(const std::function<void(bool, int32_t)>& updateCallback); 38 39 ErrCode LoadUserSettingData(int32_t userId, bool needUpdateCallback, bool &isDarkMode, const bool bootLoadFlag); 40 41 void NotifyDarkModeUpdate(int32_t userId, bool isDarkMode); 42 43 ErrCode OnSwitchUser(int32_t userId); 44 45 void ScreenOnCallback(); 46 47 void ScreenOffCallback(); 48 49 ErrCode RestartTimer(); 50 51 void Dump(); 52 53 bool GetSettingTime(const int32_t userId, int32_t& settingStartTime, int32_t& settingEndTime); 54 55 bool IsColorModeNormal(const int32_t userId); 56 57 void DoSwitchTemporaryColorMode(const int32_t userId, bool isDarkMode); 58 59 private: 60 enum DarkModeMode { 61 DARK_MODE_INVALID = -1, 62 DARK_MODE_ALWAYS_LIGHT = 0, 63 DARK_MODE_ALWAYS_DARK = 1, 64 DARK_MODE_CUSTOM_AUTO = 2, 65 DARK_MODE_SUNRISE_SUNSET = 3, 66 DARK_MODE_SIZE, 67 }; 68 69 struct DarkModeState { 70 DarkModeMode settingMode = DARK_MODE_INVALID; 71 int32_t settingStartTime = -1; 72 int32_t settingEndTime = -1; 73 int32_t settingSunsetTime = SUNSET_TIME_DEFAULT; // Default sunset Time 6pm 74 int32_t settingSunriseTime = SUNRISE_TIME_DEFAULT; // Default sunrise time: 7am the next day 75 }; 76 77 void LoadSettingDataObserversCallback(); 78 79 ErrCode RegisterSettingDataObserversLocked(int32_t userId) const; 80 81 void UnregisterSettingDataObserversLocked(int32_t userId) const; 82 83 void SettingDataDarkModeModeUpdateFunc(const std::string& key, int32_t userId); 84 85 void SettingDataDarkModeStartTimeUpdateFunc(const std::string& key, int32_t userId); 86 87 void SettingDataDarkModeEndTimeUpdateFunc(const std::string& key, int32_t userId); 88 89 void SettingDataDarkModeSunsetTimeUpdateFunc(const std::string& key, int32_t userId); 90 91 void SettingDataDarkModeSunriseTimeUpdateFunc(const std::string& key, int32_t userId); 92 93 ErrCode OnStateChangeLocked(int32_t userId, bool needUpdateCallback, bool& isDarkMode, 94 const bool resetTempColorModeFlag, const bool bootLoadFlag); 95 96 ErrCode OnStateChangeToAllDayMode(int32_t userId, DarkModeMode darkMode, bool needUpdateCallback, bool& isDarkMode, 97 const bool resetTempColorModeFlag, const bool bootLoadFlag); 98 99 ErrCode OnStateChangeToCustomAutoMode(int32_t userId, const DarkModeState& state, bool needUpdateCallback, 100 bool& isDarkMode, const bool resetTempColorModeFlag, const bool bootLoadFlag); 101 102 void OnChangeDarkMode(DarkModeMode mode, int32_t userId); 103 104 ErrCode CreateOrUpdateTimers(int32_t startTime, int32_t endTime, int32_t userId); 105 106 ErrCode CheckTimerCallbackParams(int32_t startTime, int32_t endTime, int32_t userId); 107 108 void UpdateDarkModeSchedule(const DarkModeMode isDarkMode, const int32_t userId, const bool resetTempColorModeFlag, 109 const bool bootLoadFlag); 110 111 bool IsDarkModeCustomAuto(const int32_t userId); 112 113 bool IsDarkModeSunsetSunrise(const int32_t userId); 114 115 std::mutex settingDataObserversMutex_; 116 std::list<std::pair<std::string, std::function<void(const std::string&, int32_t)>>> settingDataObservers_; 117 int32_t settingDataObserversUserId_ = -1; 118 119 AlarmTimerManager alarmTimerManager_; 120 std::mutex darkModeStatesMutex_; 121 std::map<int32_t, DarkModeState> darkModeStates_; 122 123 std::function<void(bool, int32_t)> updateCallback_; 124 125 TemporaryColorModeManager temporaryColorModeMgr_; 126 ScreenSwitchOperatorManager screenSwitchOperatorMgr_; 127 }; 128 } // namespace OHOS::ArkUi::UiAppearance 129 130 #endif // UI_APPEARANCE_DARK_MODE_MANAGER_H 131