Close allapps on press of ESC key.

Fix: 310206847
Test: TaplOpenCloseAllApps
Flag: NONE
Change-Id: I9b9f958eb2889680b731dd7134fce2793e62eb74
diff --git a/quickstep/src/com/android/launcher3/taskbar/overlay/TaskbarOverlayDragLayer.java b/quickstep/src/com/android/launcher3/taskbar/overlay/TaskbarOverlayDragLayer.java
index e742a3d..432d272 100644
--- a/quickstep/src/com/android/launcher3/taskbar/overlay/TaskbarOverlayDragLayer.java
+++ b/quickstep/src/com/android/launcher3/taskbar/overlay/TaskbarOverlayDragLayer.java
@@ -118,6 +118,15 @@
                 topView.onBackInvoked();
                 return true;
             }
+        } else if (event.getAction() == KeyEvent.ACTION_DOWN
+                && event.getKeyCode() == KeyEvent.KEYCODE_ESCAPE && event.hasNoModifiers()) {
+            // Ignore escape if pressed in conjunction with any modifier keys. Close each
+            // floating view one at a time for each key press.
+            AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(mActivity);
+            if (topView != null) {
+                topView.close(/* animate= */ true);
+                return true;
+            }
         }
         return super.dispatchKeyEvent(event);
     }
diff --git a/src/com/android/launcher3/util/KeyboardShortcutsDelegate.java b/src/com/android/launcher3/util/KeyboardShortcutsDelegate.java
index 3ec339d..c9db83d 100644
--- a/src/com/android/launcher3/util/KeyboardShortcutsDelegate.java
+++ b/src/com/android/launcher3/util/KeyboardShortcutsDelegate.java
@@ -25,6 +25,7 @@
 import android.view.KeyboardShortcutInfo;
 import android.view.Menu;
 
+import com.android.launcher3.AbstractFloatingView;
 import com.android.launcher3.Launcher;
 import com.android.launcher3.R;
 import com.android.launcher3.Utilities;
@@ -108,14 +109,26 @@
      * @see android.view.KeyEvent
      */
     public Boolean onKeyDown(int keyCode, KeyEvent event) {
-        if (keyCode == KeyEvent.KEYCODE_ESCAPE) {
-            // Close any open floating views.
-            mLauncher.closeOpenViews();
-            return true;
+        // Ignore escape if pressed in conjunction with any modifier keys.
+        if (keyCode == KeyEvent.KEYCODE_ESCAPE && event.hasNoModifiers()) {
+            AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(mLauncher);
+            if (topView != null) {
+                // Close each floating view one at a time for each key press.
+                topView.close(/* animate= */ true);
+                return true;
+            } else if (mLauncher.getAppsView().isInAllApps()) {
+                // Close all apps if there are no open floating views.
+                closeAllApps();
+                return true;
+            }
         }
         return null;
     }
 
+    private void closeAllApps() {
+        mLauncher.getStateManager().goToState(NORMAL, true);
+    }
+
     /**
      * Handle key up event.
      * @param keyCode code of the key being pressed.
diff --git a/tests/src/com/android/launcher3/allapps/TaplOpenCloseAllApps.java b/tests/src/com/android/launcher3/allapps/TaplOpenCloseAllApps.java
index 39dbcb2..2771483 100644
--- a/tests/src/com/android/launcher3/allapps/TaplOpenCloseAllApps.java
+++ b/tests/src/com/android/launcher3/allapps/TaplOpenCloseAllApps.java
@@ -217,4 +217,10 @@
         mLauncher.getWorkspace();
         waitForState("Launcher internal state didn't switch to Home", () -> LauncherState.NORMAL);
     }
+
+    @Test
+    public void testDismissAllAppsWithEscKey() {
+        mLauncher.goHome().switchToAllApps().dismissByEscKey();
+        waitForState("Launcher internal state didn't switch to Home", () -> LauncherState.NORMAL);
+    }
 }
diff --git a/tests/tapl/com/android/launcher3/tapl/AllApps.java b/tests/tapl/com/android/launcher3/tapl/AllApps.java
index dbb3cc3..7d25121 100644
--- a/tests/tapl/com/android/launcher3/tapl/AllApps.java
+++ b/tests/tapl/com/android/launcher3/tapl/AllApps.java
@@ -16,6 +16,7 @@
 
 package com.android.launcher3.tapl;
 
+import static android.view.KeyEvent.KEYCODE_ESCAPE;
 import static android.view.KeyEvent.KEYCODE_META_RIGHT;
 
 import static com.android.launcher3.tapl.LauncherInstrumentation.DEFAULT_POLL_INTERVAL;
@@ -39,6 +40,7 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
+import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
 /**
@@ -53,6 +55,11 @@
 
     private static final String BOTTOM_SHEET_RES_ID = "bottom_sheet_background";
 
+    private static final Pattern EVENT_ALT_ESC_DOWN = Pattern.compile(
+            "Key event: KeyEvent.*?action=ACTION_DOWN.*?keyCode=KEYCODE_ESCAPE.*?metaState=0");
+    private static final Pattern EVENT_ALT_ESC_UP = Pattern.compile(
+            "Key event: KeyEvent.*?action=ACTION_UP.*?keyCode=KEYCODE_ESCAPE.*?metaState=0");
+
     private final int mHeight;
     private final int mIconHeight;
 
@@ -383,6 +390,19 @@
         }
     }
 
+    /** Presses the esc key to dismiss AllApps. */
+    public void dismissByEscKey() {
+        try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck()) {
+            mLauncher.expectEvent(TestProtocol.SEQUENCE_MAIN, EVENT_ALT_ESC_DOWN);
+            mLauncher.expectEvent(TestProtocol.SEQUENCE_MAIN, EVENT_ALT_ESC_UP);
+            mLauncher.getDevice().pressKeyCode(KEYCODE_ESCAPE);
+            try (LauncherInstrumentation.Closable c = mLauncher.addContextLayer(
+                    "pressed esc key")) {
+                verifyVisibleContainerOnDismiss();
+            }
+        }
+    }
+
     protected abstract void verifyVisibleContainerOnDismiss();
 
     /**