1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.customization.picker.mode.data.repository 18 19 import com.android.customization.picker.mode.shared.util.DarkModeUtil 20 import com.android.wallpaper.system.PowerManagerWrapper 21 import com.android.wallpaper.system.UiModeManagerWrapper 22 import javax.inject.Inject 23 import javax.inject.Singleton 24 import kotlinx.coroutines.flow.MutableStateFlow 25 import kotlinx.coroutines.flow.asStateFlow 26 import kotlinx.coroutines.flow.flowOf 27 import kotlinx.coroutines.flow.map 28 29 @Singleton 30 class DarkModeRepository 31 @Inject 32 constructor( 33 darkModeUtil: DarkModeUtil, 34 private val uiModeManager: UiModeManagerWrapper, 35 private val powerManager: PowerManagerWrapper, 36 ) { 37 private val isPowerSaveMode = MutableStateFlow(powerManager.getIsPowerSaveMode() ?: false) 38 39 private val isAvailable = darkModeUtil.isAvailable() 40 41 val isEnabled = 42 if (isAvailable) { <lambda>null43 isPowerSaveMode.map { !it } 44 } else flowOf(false) 45 46 private val _isDarkMode = MutableStateFlow(uiModeManager.getIsNightModeActivated()) 47 val isDarkMode = _isDarkMode.asStateFlow() 48 setDarkModeActivatednull49 fun setDarkModeActivated(isActive: Boolean) { 50 uiModeManager.setNightModeActivated(isActive) 51 refreshIsDarkModeActivated() 52 } 53 refreshIsDarkModeActivatednull54 fun refreshIsDarkModeActivated() { 55 _isDarkMode.value = uiModeManager.getIsNightModeActivated() 56 } 57 refreshIsPowerSaveModeActivatednull58 fun refreshIsPowerSaveModeActivated() { 59 powerManager.getIsPowerSaveMode()?.let { isPowerSaveMode.value = it } 60 } 61 } 62