• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2025 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 package com.android.systemui.lowlightclock
17 
18 import com.android.internal.logging.UiEventLogger
19 import com.android.systemui.dagger.qualifiers.Background
20 import com.android.systemui.shared.condition.Condition
21 import javax.inject.Inject
22 import kotlinx.coroutines.CoroutineScope
23 
24 /** Condition for monitoring when the device enters and exits lowlight mode. */
25 class LowLightCondition
26 @Inject
27 constructor(
28     @Background scope: CoroutineScope,
29     private val ambientLightModeMonitor: AmbientLightModeMonitor,
30     private val uiEventLogger: UiEventLogger,
31 ) : Condition(scope) {
32     override suspend fun start() {
33         ambientLightModeMonitor.start { lowLightMode: Int -> onLowLightChanged(lowLightMode) }
34     }
35 
36     override fun stop() {
37         ambientLightModeMonitor.stop()
38 
39         // Reset condition met to false.
40         updateCondition(false)
41     }
42 
43     override val startStrategy: Int
44         get() = // As this condition keeps the lowlight sensor active, it should only run when
45             // needed.
46             START_WHEN_NEEDED
47 
48     private fun onLowLightChanged(lowLightMode: Int) {
49         if (lowLightMode == AmbientLightModeMonitor.AMBIENT_LIGHT_MODE_UNDECIDED) {
50             // Ignore undecided mode changes.
51             return
52         }
53 
54         val isLowLight = lowLightMode == AmbientLightModeMonitor.AMBIENT_LIGHT_MODE_DARK
55         if (isLowLight == isConditionMet) {
56             // No change in condition, don't do anything.
57             return
58         }
59         uiEventLogger.log(
60             if (isLowLight) LowLightDockEvent.AMBIENT_LIGHT_TO_DARK
61             else LowLightDockEvent.AMBIENT_LIGHT_TO_LIGHT
62         )
63         updateCondition(isLowLight)
64     }
65 }
66