Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2f9c923c authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Handle light sensor NullPointerException."

parents e2323bb5 58715f8c
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -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)
+12 −0
Original line number Diff line number Diff line
@@ -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)