Initial implementation of Overview keyboard interactions

Lots of things are not yet implemented or don't work: animations,
fallback mode, taking params into account, dozing etc.

Bug: 73090995
Test: Press, Alt-tab, tab with and without shift, press Alt-Tab on
already open Overview.

Change-Id: Ifd140e27bead4fa52532a04000c0b60923b485be
diff --git a/quickstep/src/com/android/quickstep/OverviewCommandHelper.java b/quickstep/src/com/android/quickstep/OverviewCommandHelper.java
index 031624a..b1afff5 100644
--- a/quickstep/src/com/android/quickstep/OverviewCommandHelper.java
+++ b/quickstep/src/com/android/quickstep/OverviewCommandHelper.java
@@ -33,6 +33,7 @@
 import com.android.launcher3.Launcher;
 import com.android.launcher3.LauncherAppState;
 import com.android.launcher3.states.InternalStateHandler;
+import com.android.quickstep.views.RecentsView;
 import com.android.systemui.shared.system.ActivityManagerWrapper;
 
 /**
@@ -65,6 +66,12 @@
         homeIntent.setComponent(launcher).setPackage(null);
     }
 
+    private void openRecents() {
+        Intent intent = addToIntent(new Intent(homeIntent));
+        mContext.startActivity(intent);
+        initWhenReady();
+    }
+
     public void onOverviewToggle() {
         if (DEBUG_START_FALLBACK_ACTIVITY) {
             mContext.startActivity(new Intent(mContext, RecentsActivity.class)
@@ -79,17 +86,30 @@
             boolean isQuickTap = elapsedTime < ViewConfiguration.getDoubleTapTimeout();
             startNonLauncherTask(isQuickTap ? 2 : 1);
         } else {
-            Intent intent = addToIntent(new Intent(homeIntent));
-            mContext.startActivity(intent);
-            initWhenReady();
+            openRecents();
         }
     }
 
+    public void onOverviewShown() {
+        if (isOverviewAlmostVisible()) {
+            final RecentsView rv = getLauncher().getOverviewPanel();
+            rv.selectNextTask();
+        } else {
+            openRecents();
+        }
+    }
+
+    public void onOverviewHidden() {
+        final RecentsView rv = getLauncher().getOverviewPanel();
+        rv.launchCurrentTask();
+    }
+
     private void startNonLauncherTask(int backStackCount) {
         for (RecentTaskInfo rti : mAM.getRecentTasks(backStackCount, UserHandle.myUserId())) {
             backStackCount--;
             if (backStackCount == 0) {
                 mAM.startActivityFromRecents(rti.id, null);
+                break;
             }
         }
     }
diff --git a/quickstep/src/com/android/quickstep/TouchInteractionService.java b/quickstep/src/com/android/quickstep/TouchInteractionService.java
index a522ef1..1d25fc2 100644
--- a/quickstep/src/com/android/quickstep/TouchInteractionService.java
+++ b/quickstep/src/com/android/quickstep/TouchInteractionService.java
@@ -62,6 +62,7 @@
 public class TouchInteractionService extends Service {
 
     public static final boolean DEBUG_SHOW_OVERVIEW_BUTTON = false;
+    public static final boolean DEBUG_OPEN_OVERVIEW_VIA_ALT_TAB = false;
 
     private static final SparseArray<String> sMotionEventNames;
 
@@ -140,10 +141,18 @@
         }
 
         @Override
-        public void onOverviewShown(boolean triggeredFromAltTab) { }
+        public void onOverviewShown(boolean triggeredFromAltTab) {
+            if (DEBUG_OPEN_OVERVIEW_VIA_ALT_TAB) {
+                mOverviewCommandHelper.onOverviewShown();
+            }
+        }
 
         @Override
-        public void onOverviewHidden(boolean triggeredFromAltTab, boolean triggeredFromHomeKey) { }
+        public void onOverviewHidden(boolean triggeredFromAltTab, boolean triggeredFromHomeKey) {
+            if (DEBUG_OPEN_OVERVIEW_VIA_ALT_TAB) {
+                mOverviewCommandHelper.onOverviewHidden();
+            }
+        }
     };
 
     private final TouchConsumer mNoOpTouchConsumer = (ev) -> {};
diff --git a/quickstep/src/com/android/quickstep/views/RecentsView.java b/quickstep/src/com/android/quickstep/views/RecentsView.java
index 23e6e5b..14476f7 100644
--- a/quickstep/src/com/android/quickstep/views/RecentsView.java
+++ b/quickstep/src/com/android/quickstep/views/RecentsView.java
@@ -32,6 +32,7 @@
 import android.os.Build;
 import android.util.AttributeSet;
 import android.util.SparseBooleanArray;
+import android.view.KeyEvent;
 import android.view.LayoutInflater;
 import android.view.View;
 
@@ -585,4 +586,26 @@
         anim.setDuration(duration).setInterpolator(interpolator);
         set.play(anim);
     }
+
+    @Override
+    public boolean dispatchKeyEvent(KeyEvent event) {
+        if (event.getKeyCode() == KeyEvent.KEYCODE_TAB
+                && event.getAction() == KeyEvent.ACTION_DOWN) {
+            setCurrentPage((getCurrentPage()
+                    + (event.isShiftPressed() ? getPageCount() - 1 : 1)) % getPageCount());
+            loadVisibleTaskData();
+            return true;
+        }
+        return super.dispatchKeyEvent(event);
+    }
+
+    public void selectNextTask() {
+        setCurrentPage((getCurrentPage() + 1) % getPageCount());
+        loadVisibleTaskData();
+    }
+
+    public void launchCurrentTask() {
+        final TaskView currentTask = (TaskView) getChildAt(getCurrentPage());
+        currentTask.launchTask(true);
+    }
 }