Add overview button handling to Go recents.

Add the same overview button handling as in the original launcher.
Specifically, pressing overview on recents will launch the next
appropriate task (e.g. the next most recent task if you came from an
app).

Bug: 114136250
Test: Go to recents from launcher, press overview => launch task.
Test: Go to recents from app, press overview => launches next task
Change-Id: I946974b4c2b65b6d0f212d8e8c0816983386d952
diff --git a/go/quickstep/src/com/android/launcher3/uioverrides/RecentsUiFactory.java b/go/quickstep/src/com/android/launcher3/uioverrides/RecentsUiFactory.java
index d7cba39..29e650c 100644
--- a/go/quickstep/src/com/android/launcher3/uioverrides/RecentsUiFactory.java
+++ b/go/quickstep/src/com/android/launcher3/uioverrides/RecentsUiFactory.java
@@ -28,6 +28,7 @@
 import com.android.launcher3.config.FeatureFlags;
 import com.android.launcher3.util.TouchController;
 import com.android.quickstep.OverviewInteractionState;
+import com.android.quickstep.views.IconRecentsView;
 
 import java.util.ArrayList;
 
@@ -35,7 +36,7 @@
  * Provides recents-related {@link UiFactory} logic and classes.
  */
 public abstract class RecentsUiFactory {
-    
+
     public static final boolean GO_LOW_RAM_RECENTS_ENABLED = true;
     // Scale recents takes before animating in
     private static final float RECENTS_PREPARE_SCALE = 1.33f;
@@ -87,7 +88,10 @@
      *
      * @param launcher the launcher activity
      */
-    public static void resetOverview(Launcher launcher) {}
+    public static void resetOverview(Launcher launcher) {
+        IconRecentsView recentsView = launcher.getOverviewPanel();
+        recentsView.setTransitionedFromApp(false);
+    }
 
     /**
      * Recents logic that triggers when launcher state changes or launcher activity stops/resumes.
diff --git a/go/quickstep/src/com/android/quickstep/AppToOverviewAnimationProvider.java b/go/quickstep/src/com/android/quickstep/AppToOverviewAnimationProvider.java
index defed84..051c80f 100644
--- a/go/quickstep/src/com/android/quickstep/AppToOverviewAnimationProvider.java
+++ b/go/quickstep/src/com/android/quickstep/AppToOverviewAnimationProvider.java
@@ -34,6 +34,7 @@
 import com.android.launcher3.BaseDraggingActivity;
 import com.android.quickstep.util.MultiValueUpdateListener;
 import com.android.quickstep.util.RemoteAnimationProvider;
+import com.android.quickstep.util.RemoteAnimationTargetSet;
 import com.android.quickstep.views.IconRecentsView;
 import com.android.systemui.shared.system.RemoteAnimationTargetCompat;
 import com.android.systemui.shared.system.SyncRtSurfaceTransactionApplierCompat;
@@ -100,6 +101,10 @@
             return anim;
         }
 
+        RemoteAnimationTargetSet targetSet =
+                new RemoteAnimationTargetSet(targetCompats, MODE_CLOSING);
+        mRecentsView.setTransitionedFromApp(!targetSet.isAnimatingHome());
+
         RemoteAnimationTargetCompat recentsTarget = null;
         RemoteAnimationTargetCompat closingAppTarget = null;
 
diff --git a/go/quickstep/src/com/android/quickstep/OverviewCommandHelper.java b/go/quickstep/src/com/android/quickstep/OverviewCommandHelper.java
index d9873fe..379cc10 100644
--- a/go/quickstep/src/com/android/quickstep/OverviewCommandHelper.java
+++ b/go/quickstep/src/com/android/quickstep/OverviewCommandHelper.java
@@ -96,7 +96,7 @@
             if (recents == null) {
                 return false;
             }
-            //TODO: Launch last running task or go to home.
+            recents.handleOverviewCommand();
             return true;
         }
     }
@@ -146,7 +146,7 @@
         protected boolean handleCommand(long elapsedTime) {
             IconRecentsView recents = mHelper.getVisibleRecentsView();
             if (recents != null) {
-                //TODO: Launch next task in icon recents.
+                recents.handleOverviewCommand();
                 return true;
             } else if (elapsedTime < ViewConfiguration.getDoubleTapTimeout()) {
                 // The user tried to launch back into overview too quickly, either after
diff --git a/go/quickstep/src/com/android/quickstep/TaskHolder.java b/go/quickstep/src/com/android/quickstep/TaskHolder.java
index 8d5e4d5..a89229f 100644
--- a/go/quickstep/src/com/android/quickstep/TaskHolder.java
+++ b/go/quickstep/src/com/android/quickstep/TaskHolder.java
@@ -25,7 +25,7 @@
  * A recycler view holder that holds the task view and binds {@link Task} content (app title, icon,
  * etc.) to the view.
  */
-final class TaskHolder extends ViewHolder {
+public final class TaskHolder extends ViewHolder {
 
     private final TaskItemView mTaskItemView;
     private Task mTask;
diff --git a/go/quickstep/src/com/android/quickstep/views/IconRecentsView.java b/go/quickstep/src/com/android/quickstep/views/IconRecentsView.java
index 0e5dbc4..a1d62c2 100644
--- a/go/quickstep/src/com/android/quickstep/views/IconRecentsView.java
+++ b/go/quickstep/src/com/android/quickstep/views/IconRecentsView.java
@@ -35,8 +35,9 @@
 
 import com.android.launcher3.R;
 import com.android.quickstep.RecentsToActivityHelper;
-import com.android.quickstep.TaskAdapter;
 import com.android.quickstep.TaskActionController;
+import com.android.quickstep.TaskAdapter;
+import com.android.quickstep.TaskHolder;
 import com.android.quickstep.TaskListLoader;
 import com.android.quickstep.TaskSwipeCallback;
 
@@ -80,6 +81,7 @@
     private RecyclerView mTaskRecyclerView;
     private View mEmptyView;
     private View mContentView;
+    private boolean mTransitionedFromApp;
 
     public IconRecentsView(Context context, AttributeSet attrs) {
         super(context, attrs);
@@ -147,6 +149,38 @@
     }
 
     /**
+     * Set whether we transitioned to recents from the most recent app.
+     *
+     * @param transitionedFromApp true if transitioned from the most recent app, false otherwise
+     */
+    public void setTransitionedFromApp(boolean transitionedFromApp) {
+        mTransitionedFromApp = transitionedFromApp;
+    }
+
+    /**
+     * Handles input from the overview button. Launch the most recent task unless we just came from
+     * the app. In that case, we launch the next most recent.
+     */
+    public void handleOverviewCommand() {
+        int childCount = mTaskRecyclerView.getChildCount();
+        if (childCount == 0) {
+            // Do nothing
+            return;
+        }
+        TaskHolder taskToLaunch;
+        if (mTransitionedFromApp && childCount > 1) {
+            // Launch the next most recent app
+            TaskItemView itemView = (TaskItemView) mTaskRecyclerView.getChildAt(1);
+            taskToLaunch = (TaskHolder) mTaskRecyclerView.getChildViewHolder(itemView);
+        } else {
+            // Launch the most recent app
+            TaskItemView itemView = (TaskItemView) mTaskRecyclerView.getChildAt(0);
+            taskToLaunch = (TaskHolder) mTaskRecyclerView.getChildViewHolder(itemView);
+        }
+        mTaskActionController.launchTask(taskToLaunch);
+    }
+
+    /**
      * Get the thumbnail view associated with a task for the purposes of animation.
      *
      * @param taskId task id of thumbnail view to get