diff options
| -rw-r--r-- | packages/SystemUI/src/com/android/systemui/idle/AmbientLightModeMonitor.kt | 7 | ||||
| -rw-r--r-- | packages/SystemUI/tests/src/com/android/systemui/idle/AmbientLightModeMonitorTest.kt | 12 |
2 files changed, 18 insertions, 1 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/idle/AmbientLightModeMonitor.kt b/packages/SystemUI/src/com/android/systemui/idle/AmbientLightModeMonitor.kt index 4395773c0904..fa00dbb808fb 100644 --- a/packages/SystemUI/src/com/android/systemui/idle/AmbientLightModeMonitor.kt +++ b/packages/SystemUI/src/com/android/systemui/idle/AmbientLightModeMonitor.kt @@ -47,7 +47,7 @@ class AmbientLightModeMonitor @Inject constructor( } // Light sensor used to detect ambient lighting conditions. - private val lightSensor: Sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT) + private val lightSensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT) // Represents all ambient light modes. @Retention(AnnotationRetention.SOURCE) @@ -62,6 +62,11 @@ class AmbientLightModeMonitor @Inject constructor( fun start(callback: Callback) { if (DEBUG) Log.d(TAG, "start monitoring ambient light mode") + if (lightSensor == null) { + if (DEBUG) Log.w(TAG, "light sensor not available") + return + } + algorithm.start(callback) sensorManager.registerListener(mSensorEventListener, lightSensor, SensorManager.SENSOR_DELAY_NORMAL) diff --git a/packages/SystemUI/tests/src/com/android/systemui/idle/AmbientLightModeMonitorTest.kt b/packages/SystemUI/tests/src/com/android/systemui/idle/AmbientLightModeMonitorTest.kt index 1368714ed6aa..98b9252ff52b 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/idle/AmbientLightModeMonitorTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/idle/AmbientLightModeMonitorTest.kt @@ -32,6 +32,7 @@ import org.mockito.Mockito.mock import org.mockito.Mockito.anyInt import org.mockito.Mockito.any import org.mockito.Mockito.eq +import org.mockito.Mockito.never import org.mockito.Mockito.`when` import org.mockito.MockitoAnnotations @@ -90,6 +91,17 @@ class AmbientLightModeMonitorTest : SysuiTestCase() { verify(algorithm).stop() } + @Test + fun shouldNotRegisterForSensorUpdatesIfSensorNotAvailable() { + `when`(sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT)).thenReturn(null) + val ambientLightModeMonitor = AmbientLightModeMonitor(algorithm, sensorManager) + + val callback = mock(AmbientLightModeMonitor.Callback::class.java) + ambientLightModeMonitor.start(callback) + + verify(sensorManager, never()).registerListener(any(), any(Sensor::class.java), anyInt()) + } + // Captures [SensorEventListener], assuming it has been registered with [sensorManager]. private fun captureSensorEventListener(): SensorEventListener { val captor = ArgumentCaptor.forClass(SensorEventListener::class.java) |