From 041219c9bc9f2be42f7543dd9601f78e9361d743 Mon Sep 17 00:00:00 2001 From: Josep del Rio Date: Fri, 17 Feb 2023 12:28:04 +0000 Subject: [PATCH] Add IME action to Enter editor action At the moment apps need to support pressing "Enter" to search differently if they want to support physical keyboard, as currently no editor action is sent from it. This is a proposal that will resolve that issue by sending that information. Bug: 269672396 Test: built, flashed on device, confirmed pressing enter starts the search Change-Id: I1694303540295603097db2fd4e4206f1ca71cec0 --- core/java/android/widget/TextView.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 5de807936c8ac..fd8f5495464ce 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -1117,7 +1117,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener * @param actionId Identifier of the action. This will be either the * identifier you supplied, or {@link EditorInfo#IME_NULL * EditorInfo.IME_NULL} if being called due to the enter key - * being pressed. + * being pressed. Starting from Android 14, the action identifier will + * also be included when triggered by an enter key if the input is + * constrained to a single line. * @param event If triggered by an enter key, this is the event; * otherwise, this is null. * @return Return true if you have consumed the action, else false. @@ -9272,7 +9274,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener // chance to consume the event. if (mEditor.mInputContentType.onEditorActionListener != null && mEditor.mInputContentType.onEditorActionListener.onEditorAction( - this, EditorInfo.IME_NULL, event)) { + this, + getActionIdForEnterEvent(), + event)) { mEditor.mInputContentType.enterDown = true; // We are consuming the enter key for them. return KEY_EVENT_HANDLED; @@ -9495,7 +9499,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener && mEditor.mInputContentType.enterDown) { mEditor.mInputContentType.enterDown = false; if (mEditor.mInputContentType.onEditorActionListener.onEditorAction( - this, EditorInfo.IME_NULL, event)) { + this, getActionIdForEnterEvent(), event)) { return true; } } @@ -9559,6 +9563,15 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener return super.onKeyUp(keyCode, event); } + private int getActionIdForEnterEvent() { + // If it's not single line, no action + if (!isSingleLine()) { + return EditorInfo.IME_NULL; + } + // Return the action that was specified for Enter + return getImeOptions() & EditorInfo.IME_MASK_ACTION; + } + @Override public boolean onCheckIsTextEditor() { return mEditor != null && mEditor.mInputType != EditorInfo.TYPE_NULL;