• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.systemui.qs.tiles.impl.uimodenight.domain.interactor
18 
19 import android.app.UiModeManager
20 import android.content.Context
21 import android.content.res.Configuration
22 import android.os.UserHandle
23 import com.android.systemui.common.coroutine.ConflatedCallbackFlow
24 import com.android.systemui.qs.tiles.base.domain.interactor.QSTileDataInteractor
25 import com.android.systemui.qs.tiles.base.domain.model.DataUpdateTrigger
26 import com.android.systemui.qs.tiles.impl.uimodenight.domain.interactor.model.UiModeNightTileModel
27 import com.android.systemui.shade.ShadeDisplayAware
28 import com.android.systemui.statusbar.policy.BatteryController
29 import com.android.systemui.statusbar.policy.ConfigurationController
30 import com.android.systemui.statusbar.policy.LocationController
31 import com.android.systemui.util.time.DateFormatUtil
32 import javax.inject.Inject
33 import kotlinx.coroutines.channels.awaitClose
34 import kotlinx.coroutines.flow.Flow
35 import kotlinx.coroutines.flow.flowOf
36 
37 /** Observes ui mode night state changes providing the [UiModeNightTileModel]. */
38 class UiModeNightTileDataInteractor
39 @Inject
40 constructor(
41     @ShadeDisplayAware private val context: Context,
42     @ShadeDisplayAware private val configurationController: ConfigurationController,
43     private val uiModeManager: UiModeManager,
44     private val batteryController: BatteryController,
45     private val locationController: LocationController,
46     private val dateFormatUtil: DateFormatUtil,
47 ) : QSTileDataInteractor<UiModeNightTileModel> {
48 
tileDatanull49     override fun tileData(
50         user: UserHandle,
51         triggers: Flow<DataUpdateTrigger>,
52     ): Flow<UiModeNightTileModel> =
53         ConflatedCallbackFlow.conflatedCallbackFlow {
54             // send initial state
55             trySend(createModel())
56 
57             val configurationCallback =
58                 object : ConfigurationController.ConfigurationListener {
59                     override fun onUiModeChanged() {
60                         trySend(createModel())
61                     }
62                 }
63             configurationController.addCallback(configurationCallback)
64 
65             val batteryCallback =
66                 object : BatteryController.BatteryStateChangeCallback {
67                     override fun onPowerSaveChanged(isPowerSave: Boolean) {
68                         trySend(createModel())
69                     }
70                 }
71             batteryController.addCallback(batteryCallback)
72 
73             val locationCallback =
74                 object : LocationController.LocationChangeCallback {
75                     override fun onLocationSettingsChanged(locationEnabled: Boolean) {
76                         trySend(createModel())
77                     }
78                 }
79             locationController.addCallback(locationCallback)
80 
81             awaitClose {
82                 configurationController.removeCallback(configurationCallback)
83                 batteryController.removeCallback(batteryCallback)
84                 locationController.removeCallback(locationCallback)
85             }
86         }
87 
createModelnull88     private fun createModel(): UiModeNightTileModel {
89         val uiMode = uiModeManager.nightMode
90         val nightMode =
91             (context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) ==
92                 Configuration.UI_MODE_NIGHT_YES
93         val powerSave = batteryController.isPowerSave
94         val locationEnabled = locationController.isLocationEnabled
95         val nightModeCustomType = uiModeManager.nightModeCustomType
96         val use24HourFormat = dateFormatUtil.is24HourFormat
97         val customNightModeEnd = uiModeManager.customNightModeEnd
98         val customNightModeStart = uiModeManager.customNightModeStart
99 
100         return UiModeNightTileModel(
101             uiMode,
102             nightMode,
103             powerSave,
104             locationEnabled,
105             nightModeCustomType,
106             use24HourFormat,
107             customNightModeEnd,
108             customNightModeStart,
109         )
110     }
111 
availabilitynull112     override fun availability(user: UserHandle): Flow<Boolean> = flowOf(true)
113 }
114