Handle light sensor NullPointerException.
Light sensor is not available on all devices so add a check for availability before starting the ambient light mode monitor. Test: atest AmbientLightModeMonitorTest#shouldNotRegisterForSensorUpdatesIfSensorNotAvailable Bug: 203691187 Fix: 203691187 Change-Id: I1f77f1337f24409986029bf018b6e6f43857db87
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user