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

Commit c1d6fb91 authored by helen cheuk's avatar helen cheuk
Browse files

[Action Corner] Handle exception when creating input monitor

Catch exception from input monitor creation to avoid causing crash.

Bug: 417208071
Test: MultiDisplayCursorPositionRepositoryTest
Flag: com.android.systemui.shared.cursor_hot_corner
Change-Id: I6a02ef29f370fd2dcfa526b5e97425924bb56011
parent 6ae73da2
Loading
Loading
Loading
Loading
+25 −11
Original line number Diff line number Diff line
@@ -83,31 +83,34 @@ class MultiDisplayCursorPositionRepositoryTest(private val cursorEventSource: In
    @Before
    fun setup() {
        testableLooper = TestableLooper.get(this)
        val testHandler = Handler(testableLooper.looper)
        whenever(inputMonitor.getInputReceiver(any(), any(), any())).thenReturn(inputReceiver)
        displayLifecycleManager.displayIds.value = setOf(DEFAULT_DISPLAY, DISPLAY_2)

        underTest = createMultiDisplayCursorPositionRepository { _: String, _: Int -> inputMonitor }
    }

    private fun createMultiDisplayCursorPositionRepository(
        inputMonitorBuilder: InputMonitorBuilder
    ): MultiDisplayCursorPositionRepository {
        val cursorPerDisplayRepository =
            PerDisplayInstanceRepositoryImpl(
                debugName = "testCursorPositionPerDisplayInstanceRepository",
                instanceProvider =
                    TestCursorPositionRepositoryInstanceProvider(
                        testHandler,
                        Handler(testableLooper.looper),
                        { channel ->
                            listener = defaultInputEventListenerBuilder.build(channel)
                            listener
                        },
                    ) { _: String, _: Int ->
                        inputMonitor
                    },
                        inputMonitorBuilder,
                    ),
                displayLifecycleManager,
                kosmos.backgroundScope,
                displayRepository,
                kosmos.perDisplayDumpHelper,
            )

        underTest =
            MultiDisplayCursorPositionRepositoryImpl(
        return MultiDisplayCursorPositionRepositoryImpl(
            displayRepository,
            backgroundScope = kosmos.backgroundScope,
            cursorPerDisplayRepository,
@@ -152,6 +155,17 @@ class MultiDisplayCursorPositionRepositoryTest(private val cursorEventSource: In
        verify(inputReceiver).dispose()
    }

    @Test
    fun notThrowExceptionFromInputMonitorBuilder() {
        val inputMonitorBuilder = InputMonitorBuilder { _: String, _: Int ->
            throw IllegalArgumentException()
        }
        underTest = createMultiDisplayCursorPositionRepository(inputMonitorBuilder)

        setUpAndRunTest { addDisplay(id = DISPLAY_2, type = TYPE_EXTERNAL) }
        // No exception is thrown, otherwise it would fail the test
    }

    private fun setUpAndRunTest(block: suspend () -> Unit) =
        kosmos.runTest {
            // Add default display before creating cursor repository
+5 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.systemui.cursorposition.data.repository

import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.Choreographer
import android.view.InputDevice.SOURCE_MOUSE
import android.view.InputDevice.SOURCE_TOUCHPAD
@@ -40,6 +41,7 @@ import kotlinx.coroutines.channels.ProducerScope
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn

/** Repository for cursor position in single display. */
@@ -90,6 +92,9 @@ constructor(
            // "backgroundDispatcher" which does not have a looper) and input receiver could use
            // its background looper and choreographer
            .flowOn(backgroundHandler.asCoroutineDispatcher())
            .catch { exception ->
                Log.e(TAG, "Error creating input monitor for display $displayId", exception)
            }

    override val cursorPositions: Flow<CursorPosition> = createInputMonitorCallbackFlow(displayId)