Loading core/java/android/text/method/ArrowKeyMovementMethod.java +9 −7 Original line number Diff line number Diff line Loading @@ -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) { Loading @@ -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) { Loading @@ -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); Loading core/tests/coretests/src/android/widget/TextViewActivityMouseTest.java +13 −0 Original line number Diff line number Diff line Loading @@ -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")); } } Loading
core/java/android/text/method/ArrowKeyMovementMethod.java +9 −7 Original line number Diff line number Diff line Loading @@ -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) { Loading @@ -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) { Loading @@ -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); Loading
core/tests/coretests/src/android/widget/TextViewActivityMouseTest.java +13 −0 Original line number Diff line number Diff line Loading @@ -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")); } }