From c8731128108ece45cb8e1b359b8badea3a750346 Mon Sep 17 00:00:00 2001 From: Gilles Debunne Date: Thu, 7 Oct 2010 20:54:11 -0700 Subject: [PATCH] Long press with trackball selects word under cursor. Bug 3068305 Change-Id: Ib37fc98a61efdf3fbfccbb2174e2dbdbcb1a240e --- core/java/android/widget/TextView.java | 36 ++++++++++++++++---------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 548002fc6e92e..d1974dca36fed 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -4321,6 +4321,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener switch (keyCode) { case KeyEvent.KEYCODE_ENTER: + mEnterKeyIsDown = true; // If ALT modifier is held, then we always insert a // newline character. if ((event.getMetaState()&KeyEvent.META_ALT_ON) == 0) { @@ -4353,6 +4354,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener break; case KeyEvent.KEYCODE_DPAD_CENTER: + mDPadCenterIsDown = true; if (shouldAdvanceFocusOnEnter()) { return 0; } @@ -4447,6 +4449,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener switch (keyCode) { case KeyEvent.KEYCODE_DPAD_CENTER: + mDPadCenterIsDown = false; /* * If there is a click listener, just call through to * super, which will invoke it. @@ -4467,6 +4470,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener return super.onKeyUp(keyCode, event); case KeyEvent.KEYCODE_ENTER: + mEnterKeyIsDown = false; if (mInputContentType != null && mInputContentType.onEditorActionListener != null && mInputContentType.enterDown) { @@ -7249,13 +7253,20 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener return; } - int selectionStart, selectionEnd; + int minOffset, maxOffset; - // selectionModifierCursorController is not null at that point - SelectionModifierCursorController selectionModifierCursorController = - ((SelectionModifierCursorController) mSelectionModifierCursorController); - int minOffset = selectionModifierCursorController.getMinTouchOffset(); - int maxOffset = selectionModifierCursorController.getMaxTouchOffset(); + if (mDPadCenterIsDown || mEnterKeyIsDown) { + minOffset = getSelectionStart(); + maxOffset = getSelectionEnd(); + } else { + // selectionModifierCursorController is not null at that point + SelectionModifierCursorController selectionModifierCursorController = + ((SelectionModifierCursorController) mSelectionModifierCursorController); + minOffset = selectionModifierCursorController.getMinTouchOffset(); + maxOffset = selectionModifierCursorController.getMaxTouchOffset(); + } + + int selectionStart, selectionEnd; long wordLimits = getWordLimitsAt(minOffset); if (wordLimits >= 0) { @@ -7275,11 +7286,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } private String getWordForDictionary() { - if (mLastTouchOffset < 0) { - return null; - } - - long wordLimits = getWordLimitsAt(mLastTouchOffset); + long wordLimits = getWordLimitsAt(getSelectionStart()); if (wordLimits >= 0) { int start = extractRangeStartFromLong(wordLimits); int end = extractRangeEndFromLong(wordLimits); @@ -7881,8 +7888,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener final float newPosX = rawX - mTouchToWindowOffsetX + mHotspotX; final float newPosY = rawY - mTouchToWindowOffsetY + mHotspotY + mTouchOffsetY; - mController.updatePosition(this, (int) Math.round(newPosX), - (int) Math.round(newPosY)); + mController.updatePosition(this, Math.round(newPosX), Math.round(newPosY)); break; } @@ -8254,6 +8260,10 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener private CursorController mSelectionModifierCursorController; private boolean mIsInTextSelectionMode = false; private int mLastTouchOffset = -1; + // These are needed to desambiguate a long click. If the long click comes from ones of these, we + // select from the current cursor position. Otherwise, select from long pressed position. + private boolean mDPadCenterIsDown = false; + private boolean mEnterKeyIsDown = false; // Created once and shared by different CursorController helper methods. // Only one cursor controller is active at any time which prevent race conditions. private static Rect sCursorControllerTempRect = new Rect();