From c1c572ef92bb282f9744af091b2be0584260952a Mon Sep 17 00:00:00 2001 From: Emilie Roberts Date: Wed, 31 Jan 2018 17:33:32 +0000 Subject: Close Dialog when ESC Pressed Listens for ESC key and calls onBackPressed() to close the dialog. Bug: 71907807 Test: atest cts.DialogTest Test: Manual testing on Marlin, P (master) Change-Id: I850cc9889869fa3b7d83bfab55e1f4d63035e162 --- core/java/android/app/Dialog.java | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/core/java/android/app/Dialog.java b/core/java/android/app/Dialog.java index 2b648ea6937d..eb260265c15f 100644 --- a/core/java/android/app/Dialog.java +++ b/core/java/android/app/Dialog.java @@ -609,18 +609,19 @@ public class Dialog implements DialogInterface, Window.Callback, /** * A key was pressed down. + *

+ * If the focused view didn't want this event, this method is called. + *

+ * Default implementation consumes {@link KeyEvent#KEYCODE_BACK KEYCODE_BACK} + * and, as of {@link android.os.Build.VERSION_CODES#P P}, {@link KeyEvent#KEYCODE_ESCAPE + * KEYCODE_ESCAPE} to later handle them in {@link #onKeyUp}. * - *

If the focused view didn't want this event, this method is called. - * - *

The default implementation consumed the KEYCODE_BACK to later - * handle it in {@link #onKeyUp}. - * * @see #onKeyUp * @see android.view.KeyEvent */ @Override public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) { - if (keyCode == KeyEvent.KEYCODE_BACK) { + if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE) { event.startTracking(); return true; } @@ -640,16 +641,18 @@ public class Dialog implements DialogInterface, Window.Callback, /** * A key was released. - * - *

The default implementation handles KEYCODE_BACK to close the - * dialog. + *

+ * Default implementation consumes {@link KeyEvent#KEYCODE_BACK KEYCODE_BACK} + * and, as of {@link android.os.Build.VERSION_CODES#P P}, {@link KeyEvent#KEYCODE_ESCAPE + * KEYCODE_ESCAPE} to close the dialog. * * @see #onKeyDown - * @see KeyEvent + * @see android.view.KeyEvent */ @Override public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) { - if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking() + if ((keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE) + && event.isTracking() && !event.isCanceled()) { onBackPressed(); return true; -- cgit v1.2.3-59-g8ed1b