Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c1c572ef authored by Emilie Roberts's avatar Emilie Roberts
Browse files

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
parent 3a247b19
Loading
Loading
Loading
Loading
+14 −11
Original line number Diff line number Diff line
@@ -609,18 +609,19 @@ public class Dialog implements DialogInterface, Window.Callback,

    /**
     * A key was pressed down.
     * 
     * <p>If the focused view didn't want this event, this method is called.
     *
     * <p>The default implementation consumed the KEYCODE_BACK to later
     * handle it in {@link #onKeyUp}.
     * <p>
     * If the focused view didn't want this event, this method is called.
     * <p>
     * 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}.
     * 
     * @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.
     * 
     * <p>The default implementation handles KEYCODE_BACK to close the
     * dialog.
     * <p>
     * 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;