Fixing alt-tab is properly dispatched to the RecentsView

Change-Id: I14a5569877bc0e7fe3aa30d3cd1fa74920b3e9db
diff --git a/go/quickstep/src/com/android/quickstep/LauncherActivityControllerHelper.java b/go/quickstep/src/com/android/quickstep/LauncherActivityControllerHelper.java
index 35ea9c7..ac559ba 100644
--- a/go/quickstep/src/com/android/quickstep/LauncherActivityControllerHelper.java
+++ b/go/quickstep/src/com/android/quickstep/LauncherActivityControllerHelper.java
@@ -123,18 +123,17 @@
     }
 
     @Override
-    public boolean switchToRecentsIfVisible(boolean fromRecentsButton) {
+    public boolean switchToRecentsIfVisible(Runnable onCompleteCallback) {
         Launcher launcher = getVisibleLauncher();
         if (launcher == null) {
             return false;
         }
-        if (fromRecentsButton) {
-            launcher.getUserEventDispatcher().logActionCommand(
-                    LauncherLogProto.Action.Command.RECENTS_BUTTON,
-                    getContainerType(),
-                    LauncherLogProto.ContainerType.TASKSWITCHER);
-        }
-        launcher.getStateManager().goToState(OVERVIEW);
+        launcher.getUserEventDispatcher().logActionCommand(
+                LauncherLogProto.Action.Command.RECENTS_BUTTON,
+                getContainerType(),
+                LauncherLogProto.ContainerType.TASKSWITCHER);
+        launcher.getStateManager().goToState(OVERVIEW,
+                launcher.getStateManager().shouldAnimateStateChange(), onCompleteCallback);
         return true;
     }
 
diff --git a/quickstep/recents_ui_overrides/src/com/android/quickstep/LauncherActivityControllerHelper.java b/quickstep/recents_ui_overrides/src/com/android/quickstep/LauncherActivityControllerHelper.java
index fcd9a2b..43ebbe0 100644
--- a/quickstep/recents_ui_overrides/src/com/android/quickstep/LauncherActivityControllerHelper.java
+++ b/quickstep/recents_ui_overrides/src/com/android/quickstep/LauncherActivityControllerHelper.java
@@ -329,18 +329,18 @@
     }
 
     @Override
-    public boolean switchToRecentsIfVisible(boolean fromRecentsButton) {
+    public boolean switchToRecentsIfVisible(Runnable onCompleteCallback) {
         Launcher launcher = getVisibleLauncher();
         if (launcher == null) {
             return false;
         }
-        if (fromRecentsButton) {
-            launcher.getUserEventDispatcher().logActionCommand(
-                    LauncherLogProto.Action.Command.RECENTS_BUTTON,
-                    getContainerType(),
-                    LauncherLogProto.ContainerType.TASKSWITCHER);
-        }
-        launcher.getStateManager().goToState(OVERVIEW);
+
+        launcher.getUserEventDispatcher().logActionCommand(
+                LauncherLogProto.Action.Command.RECENTS_BUTTON,
+                getContainerType(),
+                LauncherLogProto.ContainerType.TASKSWITCHER);
+        launcher.getStateManager().goToState(OVERVIEW,
+                launcher.getStateManager().shouldAnimateStateChange(), onCompleteCallback);
         return true;
     }
 
diff --git a/quickstep/src/com/android/quickstep/ActivityControlHelper.java b/quickstep/src/com/android/quickstep/ActivityControlHelper.java
index 65e71a6..0bcc154 100644
--- a/quickstep/src/com/android/quickstep/ActivityControlHelper.java
+++ b/quickstep/src/com/android/quickstep/ActivityControlHelper.java
@@ -79,7 +79,7 @@
     <T extends View> T getVisibleRecentsView();
 
     @UiThread
-    boolean switchToRecentsIfVisible(boolean fromRecentsButton);
+    boolean switchToRecentsIfVisible(Runnable onCompleteCallback);
 
     Rect getOverviewWindowBounds(Rect homeBounds, RemoteAnimationTargetCompat target);
 
diff --git a/quickstep/src/com/android/quickstep/FallbackActivityControllerHelper.java b/quickstep/src/com/android/quickstep/FallbackActivityControllerHelper.java
index a914da9..b655d1d 100644
--- a/quickstep/src/com/android/quickstep/FallbackActivityControllerHelper.java
+++ b/quickstep/src/com/android/quickstep/FallbackActivityControllerHelper.java
@@ -194,7 +194,7 @@
     }
 
     @Override
-    public boolean switchToRecentsIfVisible(boolean fromRecentsButton) {
+    public boolean switchToRecentsIfVisible(Runnable onCompleteCallback) {
         return false;
     }
 
diff --git a/quickstep/src/com/android/quickstep/OverviewCommandHelper.java b/quickstep/src/com/android/quickstep/OverviewCommandHelper.java
index 6d2008e..6533c63 100644
--- a/quickstep/src/com/android/quickstep/OverviewCommandHelper.java
+++ b/quickstep/src/com/android/quickstep/OverviewCommandHelper.java
@@ -18,6 +18,8 @@
 import static com.android.systemui.shared.system.ActivityManagerWrapper
         .CLOSE_SYSTEM_WINDOWS_REASON_RECENTS;
 
+import android.animation.Animator;
+import android.animation.AnimatorListenerAdapter;
 import android.animation.AnimatorSet;
 import android.annotation.TargetApi;
 import android.content.Context;
@@ -68,8 +70,8 @@
         mMainThreadExecutor.execute(new RecentsActivityCommand<>());
     }
 
-    public void onOverviewShown() {
-        mMainThreadExecutor.execute(new ShowRecentsCommand());
+    public void onOverviewShown(boolean triggeredFromAltTab) {
+        mMainThreadExecutor.execute(new ShowRecentsCommand(triggeredFromAltTab));
     }
 
     public void onOverviewHidden() {
@@ -83,11 +85,40 @@
 
     private class ShowRecentsCommand extends RecentsActivityCommand {
 
+        private final boolean mTriggeredFromAltTab;
+
+        ShowRecentsCommand(boolean triggeredFromAltTab) {
+            mTriggeredFromAltTab = triggeredFromAltTab;
+        }
+
         @Override
         protected boolean handleCommand(long elapsedTime) {
             // TODO: Go to the next page if started from alt-tab.
             return mHelper.getVisibleRecentsView() != null;
         }
+
+        @Override
+        protected void onTransitionComplete() {
+            if (mTriggeredFromAltTab) {
+                RecentsView rv = (RecentsView) mHelper.getVisibleRecentsView();
+                if (rv == null) {
+                    return;
+                }
+
+                // Ensure that recents view has focus so that it receives the followup key inputs
+                TaskView taskView = rv.getNextTaskView();
+                if (taskView == null) {
+                    if (rv.getTaskViewCount() > 0) {
+                        taskView = (TaskView) rv.getPageAt(0);
+                        taskView.requestFocus();
+                    } else {
+                        rv.requestFocus();
+                    }
+                } else {
+                    taskView.requestFocus();
+                }
+            }
+        }
     }
 
     private class HideRecentsCommand extends RecentsActivityCommand {
@@ -138,7 +169,7 @@
                 return;
             }
 
-            if (mHelper.switchToRecentsIfVisible(true)) {
+            if (mHelper.switchToRecentsIfVisible(this::onTransitionComplete)) {
                 // If successfully switched, then return
                 return;
             }
@@ -186,7 +217,16 @@
 
             mListener.unregister();
 
-            return mAnimationProvider.createWindowAnimation(targetCompats);
+            AnimatorSet animatorSet = mAnimationProvider.createWindowAnimation(targetCompats);
+            animatorSet.addListener(new AnimatorListenerAdapter() {
+                @Override
+                public void onAnimationEnd(Animator animation) {
+                    onTransitionComplete();
+                }
+            });
+            return animatorSet;
         }
+
+        protected void onTransitionComplete() { }
     }
 }
diff --git a/quickstep/src/com/android/quickstep/TouchInteractionService.java b/quickstep/src/com/android/quickstep/TouchInteractionService.java
index 9f0ee49..74ac1c6 100644
--- a/quickstep/src/com/android/quickstep/TouchInteractionService.java
+++ b/quickstep/src/com/android/quickstep/TouchInteractionService.java
@@ -86,7 +86,7 @@
 
         @Override
         public void onOverviewShown(boolean triggeredFromAltTab) {
-            mOverviewCommandHelper.onOverviewShown();
+            mOverviewCommandHelper.onOverviewShown(triggeredFromAltTab);
         }
 
         @Override
diff --git a/src/com/android/launcher3/LauncherStateManager.java b/src/com/android/launcher3/LauncherStateManager.java
index df8ac99..a877141 100644
--- a/src/com/android/launcher3/LauncherStateManager.java
+++ b/src/com/android/launcher3/LauncherStateManager.java
@@ -147,10 +147,17 @@
     }
 
     /**
+     * Returns true if the state changes should be animated.
+     */
+    public boolean shouldAnimateStateChange() {
+        return !mLauncher.isForceInvisible() && mLauncher.isStarted();
+    }
+
+    /**
      * @see #goToState(LauncherState, boolean, Runnable)
      */
     public void goToState(LauncherState state) {
-        goToState(state, !mLauncher.isForceInvisible() && mLauncher.isStarted() /* animated */);
+        goToState(state, shouldAnimateStateChange());
     }
 
     /**