summaryrefslogtreecommitdiff
path: root/core/java/android/widget/TextView.java
diff options
context:
space:
mode:
authorEvan Rosky <erosky@google.com>2017-04-27 21:28:08 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2017-04-27 21:28:13 +0000
commitd0c6cf9bc0de41afd04d729d961a6f7e7615d731 (patch)
treea8dd0ce031a87df971d604d9646b442b1442c120 /core/java/android/widget/TextView.java
parente669ae438f8cff2f8e1440c81bfd41c250ce47e9 (diff)
parent53cfed57c9ec1aec67d14901dab21a357ed23c4b (diff)
Merge "Only restrict arrows out of text fields from keyboard" into oc-dev
Diffstat (limited to 'core/java/android/widget/TextView.java')
-rw-r--r--core/java/android/widget/TextView.java35
1 files changed, 32 insertions, 3 deletions
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java
index 99b9bd2bfb5f..bde1708bc0dd 100644
--- a/core/java/android/widget/TextView.java
+++ b/core/java/android/widget/TextView.java
@@ -126,6 +126,7 @@ import android.view.ContextMenu;
import android.view.DragEvent;
import android.view.Gravity;
import android.view.HapticFeedbackConstants;
+import android.view.InputDevice;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.MotionEvent;
@@ -395,6 +396,14 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
private TextClassifier mTextClassifier;
+ // A flag to prevent repeated movements from escaping the enclosing text view. The idea here is
+ // that if a user is holding down a movement key to traverse text, we shouldn't also traverse
+ // the view hierarchy. On the other hand, if the user is using the movement key to traverse
+ // views (i.e. the first movement was to traverse out of this view, or this view was traversed
+ // into by the user holding the movement key down) then we shouldn't prevent the focus from
+ // changing.
+ private boolean mPreventDefaultMovement;
+
private TextUtils.TruncateAt mEllipsize;
static class Drawables {
@@ -7155,6 +7164,15 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
return KEY_EVENT_NOT_HANDLED;
}
+ // If this is the initial keydown, we don't want to prevent a movement away from this view.
+ // While this shouldn't be necessary because any time we're preventing default movement we
+ // should be restricting the focus to remain within this view, thus we'll also receive
+ // the key up event, occasionally key up events will get dropped and we don't want to
+ // prevent the user from traversing out of this on the next key down.
+ if (event.getRepeatCount() == 0 && !KeyEvent.isModifierKey(keyCode)) {
+ mPreventDefaultMovement = false;
+ }
+
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
if (event.hasNoModifiers()) {
@@ -7286,16 +7304,23 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
}
if (doDown) {
if (mMovement.onKeyDown(this, (Spannable) mText, keyCode, event)) {
+ if (event.getRepeatCount() == 0 && !KeyEvent.isModifierKey(keyCode)) {
+ mPreventDefaultMovement = true;
+ }
return KEY_DOWN_HANDLED_BY_MOVEMENT_METHOD;
}
}
- // Consume arrows to prevent focus leaving the editor.
- if (isDirectionalNavigationKey(keyCode)) {
+ // Consume arrows from keyboard devices to prevent focus leaving the editor.
+ // DPAD/JOY devices (Gamepads, TV remotes) often lack a TAB key so allow those
+ // to move focus with arrows.
+ if (event.getSource() == InputDevice.SOURCE_KEYBOARD
+ && isDirectionalNavigationKey(keyCode)) {
return KEY_EVENT_HANDLED;
}
}
- return KEY_EVENT_NOT_HANDLED;
+ return mPreventDefaultMovement && !KeyEvent.isModifierKey(keyCode)
+ ? KEY_EVENT_HANDLED : KEY_EVENT_NOT_HANDLED;
}
/**
@@ -7328,6 +7353,10 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
return super.onKeyUp(keyCode, event);
}
+ if (!KeyEvent.isModifierKey(keyCode)) {
+ mPreventDefaultMovement = false;
+ }
+
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
if (event.hasNoModifiers()) {