Merge "Hide Note Task if triggered twice from the lock screen" into udc-dev
diff --git a/packages/SystemUI/src/com/android/systemui/notetask/NoteTaskController.kt b/packages/SystemUI/src/com/android/systemui/notetask/NoteTaskController.kt
index 2d7861b..f5c0a94 100644
--- a/packages/SystemUI/src/com/android/systemui/notetask/NoteTaskController.kt
+++ b/packages/SystemUI/src/com/android/systemui/notetask/NoteTaskController.kt
@@ -174,21 +174,26 @@
 
         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 @@
         } catch (e: ActivityNotFoundException) {
             logDebug { "onShowNoteTask - failed: $info" }
         }
-        logDebug { "onShowNoteTask - compoleted: $info" }
+        logDebug { "onShowNoteTask - completed: $info" }
     }
 
     /**
@@ -306,3 +311,10 @@
 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
+    }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/notetask/NoteTaskControllerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/notetask/NoteTaskControllerTest.kt
index 9897ce1..fbe089a 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/notetask/NoteTaskControllerTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/notetask/NoteTaskControllerTest.kt
@@ -25,6 +25,8 @@
 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 @@
     }
 
     @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 @@
 
         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