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

Commit 052e0e57 authored by Marcello Galhardo's avatar Marcello Galhardo Committed by Android (Google) Code Review
Browse files

Merge "Fix ANR due to `updateShortcuts` called in the main thread" into main

parents a32ca33e dd24f515
Loading
Loading
Loading
Loading
+17 −15
Original line number Diff line number Diff line
@@ -321,7 +321,7 @@ constructor(
        // When switched to a secondary user, the sysUI is still running in the main user, we will
        // need to update the shortcut in the secondary user.
        if (user == getCurrentRunningUser()) {
            updateNoteTaskAsUserInternal(user)
            launchUpdateNoteTaskAsUser(user)
        } else {
            // TODO(b/278729185): Replace fire and forget service with a bounded service.
            val intent = NoteTaskControllerUpdateService.createIntent(context)
@@ -330,10 +330,11 @@ constructor(
    }

    @InternalNoteTaskApi
    fun updateNoteTaskAsUserInternal(user: UserHandle) {
    fun launchUpdateNoteTaskAsUser(user: UserHandle) {
        applicationScope.launch {
            if (!userManager.isUserUnlocked(user)) {
                debugLog { "updateNoteTaskAsUserInternal call but user locked: user=$user" }
            return
                return@launch
            }

            val packageName = roleManager.getDefaultRoleHolderAsUser(ROLE_NOTES, user)
@@ -349,6 +350,7 @@ constructor(
                shortcutManager.disableShortcuts(listOf(SHORTCUT_ID))
            }
        }
    }

    /** @see OnRoleHoldersChangedListener */
    fun onRoleHoldersChanged(roleName: String, user: UserHandle) {
+1 −1
Original line number Diff line number Diff line
@@ -44,7 +44,7 @@ constructor(
    override fun onCreate() {
        super.onCreate()
        // TODO(b/278729185): Replace fire and forget service with a bounded service.
        controller.updateNoteTaskAsUserInternal(user)
        controller.launchUpdateNoteTaskAsUser(user)
        stopSelf()
    }

+11 −7
Original line number Diff line number Diff line
@@ -73,6 +73,7 @@ import kotlin.test.assertNotNull
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runCurrent
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@@ -705,12 +706,12 @@ internal class NoteTaskControllerTest : SysuiTestCase() {
    fun updateNoteTaskAsUser_sameUser_shouldUpdateShortcuts() {
        val user = UserHandle.CURRENT
        val controller = spy(createNoteTaskController())
        doNothing().whenever(controller).updateNoteTaskAsUserInternal(any())
        doNothing().whenever(controller).launchUpdateNoteTaskAsUser(any())
        whenever(controller.getCurrentRunningUser()).thenReturn(user)

        controller.updateNoteTaskAsUser(user)

        verify(controller).updateNoteTaskAsUserInternal(user)
        verify(controller).launchUpdateNoteTaskAsUser(user)
        verify(context, never()).startServiceAsUser(any(), any())
    }

@@ -718,12 +719,12 @@ internal class NoteTaskControllerTest : SysuiTestCase() {
    fun updateNoteTaskAsUser_differentUser_shouldUpdateShortcutsInUserProcess() {
        val user = UserHandle.CURRENT
        val controller = spy(createNoteTaskController(isEnabled = true))
        doNothing().whenever(controller).updateNoteTaskAsUserInternal(any())
        doNothing().whenever(controller).launchUpdateNoteTaskAsUser(any())
        whenever(controller.getCurrentRunningUser()).thenReturn(UserHandle.SYSTEM)

        controller.updateNoteTaskAsUser(user)

        verify(controller, never()).updateNoteTaskAsUserInternal(any())
        verify(controller, never()).launchUpdateNoteTaskAsUser(any())
        val intent = withArgCaptor { verify(context).startServiceAsUser(capture(), eq(user)) }
        assertThat(intent).hasComponentClass(NoteTaskControllerUpdateService::class.java)
    }
@@ -733,7 +734,8 @@ internal class NoteTaskControllerTest : SysuiTestCase() {
    @Test
    fun updateNoteTaskAsUserInternal_withNotesRole_withShortcuts_shouldUpdateShortcuts() {
        createNoteTaskController(isEnabled = true)
            .updateNoteTaskAsUserInternal(userTracker.userHandle)
            .launchUpdateNoteTaskAsUser(userTracker.userHandle)
        testScope.runCurrent()

        val actualComponent = argumentCaptor<ComponentName>()
        verify(context.packageManager)
@@ -768,7 +770,8 @@ internal class NoteTaskControllerTest : SysuiTestCase() {
            .thenReturn(emptyList())

        createNoteTaskController(isEnabled = true)
            .updateNoteTaskAsUserInternal(userTracker.userHandle)
            .launchUpdateNoteTaskAsUser(userTracker.userHandle)
        testScope.runCurrent()

        val argument = argumentCaptor<ComponentName>()
        verify(context.packageManager)
@@ -787,7 +790,8 @@ internal class NoteTaskControllerTest : SysuiTestCase() {
    @Test
    fun updateNoteTaskAsUserInternal_flagDisabled_shouldDisableShortcuts() {
        createNoteTaskController(isEnabled = false)
            .updateNoteTaskAsUserInternal(userTracker.userHandle)
            .launchUpdateNoteTaskAsUser(userTracker.userHandle)
        testScope.runCurrent()

        val argument = argumentCaptor<ComponentName>()
        verify(context.packageManager)