From ff66ca5038acc286a99be6001d1cde7606db8699 Mon Sep 17 00:00:00 2001 From: Mady Mellor Date: Wed, 8 Jul 2015 12:31:45 -0700 Subject: [PATCH] Use hint text to position handle / floating tool bar popups In situations where the hint text does not match the direction of the TextView's primary selection, when you copy text, and long press in that EditText to paste, the cursor would use the direction of the hint text to be placed, whereas the handle and floating tool bar used the TextView's direction to be placed so they wouldn't match. This CL updates the handle view and floating tool bars to use the hint text direction when appropriate. Bug: 21480429 Change-Id: I8090a5b2738c035522c307535ffa165ca024e811 --- core/java/android/widget/Editor.java | 40 +++++++++++++++------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java index 7c4ff188ddafd..96e033a3231b9 100644 --- a/core/java/android/widget/Editor.java +++ b/core/java/android/widget/Editor.java @@ -1683,8 +1683,7 @@ public class Editor { return; } - Layout layout = mTextView.getLayout(); - Layout hintLayout = mTextView.getHintLayout(); + Layout layout = getActiveLayout(); final int offset = mTextView.getSelectionStart(); final int line = layout.getLineForOffset(offset); final int top = layout.getLineTop(line); @@ -1699,8 +1698,7 @@ public class Editor { } boolean clamped = layout.shouldClampCursor(line); - updateCursorPosition(0, top, middle, - getPrimaryHorizontal(layout, hintLayout, offset, clamped)); + updateCursorPosition(0, top, middle, layout.getPrimaryHorizontal(offset, clamped)); if (mCursorCount == 2) { updateCursorPosition(1, middle, bottom, @@ -1708,17 +1706,6 @@ public class Editor { } } - private float getPrimaryHorizontal(Layout layout, Layout hintLayout, int offset, - boolean clamped) { - if (TextUtils.isEmpty(layout.getText()) && - hintLayout != null && - !TextUtils.isEmpty(hintLayout.getText())) { - return hintLayout.getPrimaryHorizontal(offset, clamped); - } else { - return layout.getPrimaryHorizontal(offset, clamped); - } - } - /** * Start an Insertion action mode. */ @@ -3311,14 +3298,15 @@ public class Editor { + mHandleHeight); } else { // We have a single cursor. - int line = mTextView.getLayout().getLineForOffset(mTextView.getSelectionStart()); + Layout layout = getActiveLayout(); + int line = layout.getLineForOffset(mTextView.getSelectionStart()); float primaryHorizontal = - mTextView.getLayout().getPrimaryHorizontal(mTextView.getSelectionStart()); + layout.getPrimaryHorizontal(mTextView.getSelectionStart()); mSelectionBounds.set( primaryHorizontal, - mTextView.getLayout().getLineTop(line), + layout.getLineTop(line), primaryHorizontal + 1, - mTextView.getLayout().getLineTop(line + 1) + mHandleHeight); + layout.getLineTop(line + 1) + mHandleHeight); } // Take TextView's padding and scroll into account. int textHorizontalOffset = mTextView.viewportToContentHorizontalOffset(); @@ -3684,6 +3672,7 @@ public class Editor { prepareCursorControllers(); return; } + layout = getActiveLayout(); boolean offsetChanged = offset != mPreviousOffset; if (offsetChanged || parentScrolled) { @@ -3863,6 +3852,19 @@ public class Editor { public void onDetached() {} } + /** + * Returns the active layout (hint or text layout). Note that the text layout can be null. + */ + private Layout getActiveLayout() { + Layout layout = mTextView.getLayout(); + Layout hintLayout = mTextView.getHintLayout(); + if (TextUtils.isEmpty(layout.getText()) && hintLayout != null && + !TextUtils.isEmpty(hintLayout.getText())) { + layout = hintLayout; + } + return layout; + } + private class InsertionHandleView extends HandleView { private static final int DELAY_BEFORE_HANDLE_FADES_OUT = 4000; private static final int RECENT_CUT_COPY_DURATION = 15 * 1000; // seconds