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

Commit cad8b461 authored by Marcello Galhardo's avatar Marcello Galhardo
Browse files

Hide Note Task if triggered twice from the lock screen

Test: atest NoteTaskControllerTest

Fixes: b/274070542
Change-Id: Ia7e8ee1a44b80df41f95c6420ea9d5da46e3c822
parent 7871be0c
Loading
Loading
Loading
Loading
+16 −4
Original line number Diff line number Diff line
@@ -174,21 +174,26 @@ constructor(

        infoReference.set(info)

        // TODO(b/266686199): We should handle when app not available. For now, we log.
        val intent = createNoteTaskIntent(info)
        try {
            // TODO(b/266686199): We should handle when app not available. For now, we log.
            logDebug { "onShowNoteTask - start: $info on user#${user.identifier}" }
            when (info.launchMode) {
                is NoteTaskLaunchMode.AppBubble -> {
                    // TODO: provide app bubble icon
                    val intent = createNoteTaskIntent(info)
                    bubbles.showOrHideAppBubble(intent, user, null /* icon */)
                    // App bubble logging happens on `onBubbleExpandChanged`.
                    logDebug { "onShowNoteTask - opened as app bubble: $info" }
                }
                is NoteTaskLaunchMode.Activity -> {
                    if (activityManager.isInForeground(info.packageName)) {
                        logDebug { "onShowNoteTask - already opened as activity: $info" }
                        // Force note task into background by calling home.
                        val intent = createHomeIntent()
                        context.startActivityAsUser(intent, user)
                        eventLogger.logNoteTaskClosed(info)
                        logDebug { "onShowNoteTask - closed as activity: $info" }
                    } else {
                        val intent = createNoteTaskIntent(info)
                        context.startActivityAsUser(intent, user)
                        eventLogger.logNoteTaskOpened(info)
                        logDebug { "onShowNoteTask - opened as activity: $info" }
@@ -199,7 +204,7 @@ constructor(
        } catch (e: ActivityNotFoundException) {
            logDebug { "onShowNoteTask - failed: $info" }
        }
        logDebug { "onShowNoteTask - compoleted: $info" }
        logDebug { "onShowNoteTask - completed: $info" }
    }

    /**
@@ -306,3 +311,10 @@ private fun createNoteTaskIntent(info: NoteTaskInfo): Intent =
private inline fun Any.logDebug(message: () -> String) {
    if (Build.IS_DEBUGGABLE) Log.d(this::class.java.simpleName.orEmpty(), message())
}

/** Creates an [Intent] which forces the current app to background by calling home. */
private fun createHomeIntent(): Intent =
    Intent(Intent.ACTION_MAIN).apply {
        addCategory(Intent.CATEGORY_HOME)
        flags = Intent.FLAG_ACTIVITY_NEW_TASK
    }
+14 −3
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@ import android.app.role.RoleManager.ROLE_NOTES
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.Intent.ACTION_MAIN
import android.content.Intent.CATEGORY_HOME
import android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK
import android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
@@ -278,7 +280,7 @@ internal class NoteTaskControllerTest : SysuiTestCase() {
    }

    @Test
    fun showNoteTask_keyguardIsLocked_noteIsOpen_shouldStartActivityAndLogUiEvent() {
    fun showNoteTask_keyguardIsLocked_noteIsOpen_shouldCloseActivityAndLogUiEvent() {
        val expectedInfo =
            NOTE_TASK_INFO.copy(
                entryPoint = NoteTaskEntryPoint.TAIL_BUTTON,
@@ -291,8 +293,17 @@ internal class NoteTaskControllerTest : SysuiTestCase() {

        createNoteTaskController().showNoteTask(entryPoint = expectedInfo.entryPoint!!)

        verify(context, never()).startActivityAsUser(any(), any())
        verifyZeroInteractions(bubbles, eventLogger)
        val intentCaptor = argumentCaptor<Intent>()
        val userCaptor = argumentCaptor<UserHandle>()
        verify(context).startActivityAsUser(capture(intentCaptor), capture(userCaptor))
        intentCaptor.value.let { intent ->
            assertThat(intent.action).isEqualTo(ACTION_MAIN)
            assertThat(intent.categories).contains(CATEGORY_HOME)
            assertThat(intent.flags and FLAG_ACTIVITY_NEW_TASK).isEqualTo(FLAG_ACTIVITY_NEW_TASK)
        }
        assertThat(userCaptor.value).isEqualTo(userTracker.userHandle)
        verify(eventLogger).logNoteTaskClosed(expectedInfo)
        verifyZeroInteractions(bubbles)
    }

    @Test