Merge "Choose focused task by default with moveToNextDisplay" into main
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/WMShellModule.java b/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/WMShellModule.java
index 48fadc0..79fbd32 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/WMShellModule.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/WMShellModule.java
@@ -802,6 +802,7 @@
                 recentTasksController.orElse(null),
                 interactionJankMonitor,
                 mainHandler,
+                focusTransitionObserver,
                 desktopModeEventLogger,
                 desktopModeUiEventLogger,
                 desktopWallpaperActivityTokenProvider,
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopModeShellCommandHandler.kt b/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopModeShellCommandHandler.kt
index 0cc8a6a..aa50772 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopModeShellCommandHandler.kt
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopModeShellCommandHandler.kt
@@ -16,14 +16,18 @@
 
 package com.android.wm.shell.desktopmode
 
+import android.app.ActivityTaskManager.INVALID_TASK_ID
 import android.window.DesktopExperienceFlags
 import com.android.wm.shell.shared.desktopmode.DesktopModeTransitionSource.UNKNOWN
 import com.android.wm.shell.sysui.ShellCommandHandler
+import com.android.wm.shell.transition.FocusTransitionObserver
 import java.io.PrintWriter
 
 /** Handles the shell commands for the DesktopTasksController. */
-class DesktopModeShellCommandHandler(private val controller: DesktopTasksController) :
-    ShellCommandHandler.ShellCommandActionHandler {
+class DesktopModeShellCommandHandler(
+    private val controller: DesktopTasksController,
+    private val focusTransitionObserver: FocusTransitionObserver,
+) : ShellCommandHandler.ShellCommandActionHandler {
 
     override fun onShellCommand(args: Array<String>, pw: PrintWriter): Boolean =
         when (args[0]) {
@@ -76,20 +80,21 @@
     }
 
     private fun runMoveToNextDisplay(args: Array<String>, pw: PrintWriter): Boolean {
+        var taskId = INVALID_TASK_ID
         if (args.size < 2) {
-            // First argument is the action name.
-            pw.println("Error: task id should be provided as arguments")
-            return false
-        }
-
-        val taskId =
+            taskId = focusTransitionObserver.globallyFocusedTaskId
+        } else {
             try {
-                args[1].toInt()
+                taskId = args[1].toInt()
             } catch (e: NumberFormatException) {
                 pw.println("Error: task id should be an integer")
                 return false
             }
-
+        }
+        if (taskId == INVALID_TASK_ID) {
+            pw.println("Error: no appropriate task found")
+            return false
+        }
         controller.moveToNextDisplay(taskId)
         return true
     }
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopTasksController.kt b/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopTasksController.kt
index 42d4b33..644c5b0 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopTasksController.kt
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopTasksController.kt
@@ -138,6 +138,7 @@
 import com.android.wm.shell.sysui.ShellController
 import com.android.wm.shell.sysui.ShellInit
 import com.android.wm.shell.sysui.UserChangeListener
+import com.android.wm.shell.transition.FocusTransitionObserver
 import com.android.wm.shell.transition.OneShotRemoteHandler
 import com.android.wm.shell.transition.Transitions
 import com.android.wm.shell.transition.Transitions.TransitionFinishCallback
@@ -196,6 +197,7 @@
     private val recentTasksController: RecentTasksController?,
     private val interactionJankMonitor: InteractionJankMonitor,
     @ShellMainThread private val handler: Handler,
+    private val focusTransitionObserver: FocusTransitionObserver,
     private val desktopModeEventLogger: DesktopModeEventLogger,
     private val desktopModeUiEventLogger: DesktopModeUiEventLogger,
     private val desktopWallpaperActivityTokenProvider: DesktopWallpaperActivityTokenProvider,
@@ -217,7 +219,7 @@
     private var visualIndicator: DesktopModeVisualIndicator? = null
     private var userId: Int
     private val desktopModeShellCommandHandler: DesktopModeShellCommandHandler =
-        DesktopModeShellCommandHandler(this)
+        DesktopModeShellCommandHandler(this, focusTransitionObserver)
 
     private val mOnAnimationFinishedCallback = { releaseVisualIndicator() }
     private lateinit var snapEventHandler: SnapEventHandler
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/transition/FocusTransitionObserver.java b/libs/WindowManager/Shell/src/com/android/wm/shell/transition/FocusTransitionObserver.java
index e04682a..f0f1ad0 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/transition/FocusTransitionObserver.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/transition/FocusTransitionObserver.java
@@ -16,6 +16,7 @@
 
 package com.android.wm.shell.transition;
 
+import static android.app.ActivityTaskManager.INVALID_TASK_ID;
 import static android.view.Display.DEFAULT_DISPLAY;
 import static android.view.Display.INVALID_DISPLAY;
 import static android.view.WindowManager.TRANSIT_CHANGE;
@@ -216,6 +217,17 @@
     }
 
     /**
+     * Gets the globally focused task ID.
+     */
+    public int getGloballyFocusedTaskId() {
+        if (!enableDisplayFocusInShellTransitions() || mFocusedDisplayId == INVALID_DISPLAY) {
+            return INVALID_TASK_ID;
+        }
+        final RunningTaskInfo globallyFocusedTask = mFocusedTaskOnDisplay.get(mFocusedDisplayId);
+        return globallyFocusedTask != null ? globallyFocusedTask.taskId : INVALID_TASK_ID;
+    }
+
+    /**
      * Checks whether the given task has focused globally on the system.
      * (Note {@link RunningTaskInfo#isFocused} represents per-display focus.)
      */
diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/desktopmode/DesktopTasksControllerTest.kt b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/desktopmode/DesktopTasksControllerTest.kt
index f48c433..8805071 100644
--- a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/desktopmode/DesktopTasksControllerTest.kt
+++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/desktopmode/DesktopTasksControllerTest.kt
@@ -145,6 +145,7 @@
 import com.android.wm.shell.sysui.ShellCommandHandler
 import com.android.wm.shell.sysui.ShellController
 import com.android.wm.shell.sysui.ShellInit
+import com.android.wm.shell.transition.FocusTransitionObserver
 import com.android.wm.shell.transition.OneShotRemoteHandler
 import com.android.wm.shell.transition.TestRemoteTransition
 import com.android.wm.shell.transition.Transitions
@@ -239,6 +240,7 @@
     @Mock private lateinit var taskbarDesktopTaskListener: TaskbarDesktopTaskListener
     @Mock private lateinit var freeformTaskTransitionStarter: FreeformTaskTransitionStarter
     @Mock private lateinit var mockHandler: Handler
+    @Mock private lateinit var focusTransitionObserver: FocusTransitionObserver
     @Mock private lateinit var desktopModeEventLogger: DesktopModeEventLogger
     @Mock private lateinit var desktopModeUiEventLogger: DesktopModeUiEventLogger
     @Mock lateinit var persistentRepository: DesktopPersistentRepository
@@ -423,6 +425,7 @@
             recentTasksController,
             mockInteractionJankMonitor,
             mockHandler,
+            focusTransitionObserver,
             desktopModeEventLogger,
             desktopModeUiEventLogger,
             desktopWallpaperActivityTokenProvider,