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 <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 class DarkModeManager final : public NoCopyable { 30 public: 31 static DarkModeManager &GetInstance(); 32 33 ErrCode Initialize(const std::function<void(bool, int32_t)>& updateCallback); 34 35 ErrCode LoadUserSettingData(int32_t userId, bool needUpdateCallback, bool &isDarkMode); 36 37 void NotifyDarkModeUpdate(int32_t userId, bool isDarkMode); 38 39 ErrCode OnSwitchUser(int32_t userId); 40 41 void ScreenOnCallback(); 42 43 void ScreenOffCallback(); 44 45 ErrCode RestartTimer(); 46 47 void Dump(); 48 49 bool GetSettingTime(const int32_t userId, int32_t& settingStartTime, int32_t& settingEndTime); 50 51 bool IsColorModeNormal(const int32_t userId); 52 53 void DoSwitchTemporaryColorMode(const int32_t userId, bool isDarkMode); 54 55 private: 56 enum DarkModeMode { 57 DARK_MODE_INVALID = -1, 58 DARK_MODE_ALWAYS_LIGHT = 0, 59 DARK_MODE_ALWAYS_DARK = 1, 60 DARK_MODE_CUSTOM_AUTO = 2, 61 DARK_MODE_SIZE, 62 }; 63 64 struct DarkModeState { 65 DarkModeMode settingMode = DARK_MODE_INVALID; 66 int32_t settingStartTime = -1; 67 int32_t settingEndTime = -1; 68 }; 69 70 void LoadSettingDataObserversCallback(); 71 72 ErrCode RegisterSettingDataObserversLocked(int32_t userId) const; 73 74 void UnregisterSettingDataObserversLocked(int32_t userId) const; 75 76 void SettingDataDarkModeModeUpdateFunc(const std::string& key, int32_t userId); 77 78 void SettingDataDarkModeStartTimeUpdateFunc(const std::string& key, int32_t userId); 79 80 void SettingDataDarkModeEndTimeUpdateFunc(const std::string& key, int32_t userId); 81 82 ErrCode OnStateChangeLocked( 83 int32_t userId, bool needUpdateCallback, bool& isDarkMode, const bool resetTempColorModeFlag); 84 85 ErrCode OnStateChangeToAllDayMode(int32_t userId, DarkModeMode darkMode, bool needUpdateCallback, bool& isDarkMode, 86 const bool resetTempColorModeFlag); 87 88 ErrCode OnStateChangeToCustomAutoMode(int32_t userId, const DarkModeState& state, bool needUpdateCallback, 89 bool& isDarkMode, const bool resetTempColorModeFlag); 90 91 void OnChangeDarkMode(DarkModeMode mode, int32_t userId); 92 93 ErrCode CreateOrUpdateTimers(int32_t startTime, int32_t endTime, int32_t userId); 94 95 ErrCode CheckTimerCallbackParams(int32_t startTime, int32_t endTime, int32_t userId); 96 97 void UpdateDarkModeSchedule(const DarkModeMode isDarkMode, const int32_t userId, const bool resetTempColorModeFlag); 98 99 bool IsDarkModeCustomAuto(const int32_t userId); 100 101 std::mutex settingDataObserversMutex_; 102 std::list<std::pair<std::string, std::function<void(const std::string&, int32_t)>>> settingDataObservers_; 103 int32_t settingDataObserversUserId_ = -1; 104 105 AlarmTimerManager alarmTimerManager_; 106 std::mutex darkModeStatesMutex_; 107 std::map<int32_t, DarkModeState> darkModeStates_; 108 109 std::function<void(bool, int32_t)> updateCallback_; 110 111 TemporaryColorModeManager temporaryColorModeMgr_; 112 ScreenSwitchOperatorManager screenSwitchOperatorMgr_; 113 }; 114 } // namespace OHOS::ArkUi::UiAppearance 115 116 #endif // UI_APPEARANCE_DARK_MODE_MANAGER_H 117