From ead6ba3a639e2737e1f39475a497bf4613723ebd Mon Sep 17 00:00:00 2001 From: Keisuke Kuroyanagi Date: Tue, 27 Oct 2015 17:26:07 +0900 Subject: [PATCH] Stop making a reversed selection with mice. Bug: 24889605 Bug: 24475013 Change-Id: I6d39e050656dc28e523fab862c84054169e6300b --- .../java/android/text/method/ArrowKeyMovementMethod.java | 16 +++++++++------- .../src/android/widget/TextViewActivityMouseTest.java | 13 +++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/core/java/android/text/method/ArrowKeyMovementMethod.java b/core/java/android/text/method/ArrowKeyMovementMethod.java index 84707985b180..2459cfa84636 100644 --- a/core/java/android/text/method/ArrowKeyMovementMethod.java +++ b/core/java/android/text/method/ArrowKeyMovementMethod.java @@ -268,9 +268,9 @@ public class ArrowKeyMovementMethod extends BaseMovementMethod implements Moveme // Cursor can be active at any location in the text while mouse pointer can start // selection from a totally different location. Use LAST_TAP_DOWN span to ensure // text selection will start from mouse pointer location. + final int startOffset = buffer.getSpanStart(LAST_TAP_DOWN); if (isMouse && Touch.isSelectionStarted(buffer)) { - int offset = buffer.getSpanStart(LAST_TAP_DOWN); - Selection.setSelection(buffer, offset); + Selection.setSelection(buffer, startOffset); } if (isTouchSelecting(isMouse, buffer) && handled) { @@ -285,9 +285,9 @@ public class ArrowKeyMovementMethod extends BaseMovementMethod implements Moveme // Update selection as we're moving the selection area. // Get the current touch position - int offset = widget.getOffsetForPosition(event.getX(), event.getY()); - - Selection.extendSelection(buffer, offset); + final int offset = widget.getOffsetForPosition(event.getX(), event.getY()); + Selection.setSelection(buffer, Math.min(startOffset, offset), + Math.max(startOffset, offset)); return true; } } else if (action == MotionEvent.ACTION_UP) { @@ -301,10 +301,12 @@ public class ArrowKeyMovementMethod extends BaseMovementMethod implements Moveme return true; } - int offset = widget.getOffsetForPosition(event.getX(), event.getY()); if (wasTouchSelecting) { + final int startOffset = buffer.getSpanStart(LAST_TAP_DOWN); + final int endOffset = widget.getOffsetForPosition(event.getX(), event.getY()); + Selection.setSelection(buffer, Math.min(startOffset, endOffset), + Math.max(startOffset, endOffset)); buffer.removeSpan(LAST_TAP_DOWN); - Selection.extendSelection(buffer, offset); } MetaKeyKeyListener.adjustMetaAfterKeypress(buffer); diff --git a/core/tests/coretests/src/android/widget/TextViewActivityMouseTest.java b/core/tests/coretests/src/android/widget/TextViewActivityMouseTest.java index 2c2d39375f8a..c5e2ae67affc 100644 --- a/core/tests/coretests/src/android/widget/TextViewActivityMouseTest.java +++ b/core/tests/coretests/src/android/widget/TextViewActivityMouseTest.java @@ -49,4 +49,17 @@ public class TextViewActivityMouseTest extends ActivityInstrumentationTestCase2< onView(withId(R.id.textview)).check(hasSelection("llo wor")); } + + @SmallTest + public void testSelectTextByDrag_reverse() throws Exception { + getActivity(); + + final String helloWorld = "Hello world!"; + onView(withId(R.id.textview)).perform(click()); + onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld)); + onView(withId(R.id.textview)).perform( + mouseDragOnText( helloWorld.indexOf("ld!"), helloWorld.indexOf("llo"))); + + onView(withId(R.id.textview)).check(hasSelection("llo wor")); + } } -- 2.11.0