From c14e1270cff7f12614af49b7f3a94299f4064438 Mon Sep 17 00:00:00 2001 From: Keisuke Kuroyanagi Date: Tue, 5 Apr 2016 15:39:24 +0900 Subject: [PATCH] Invalidate HandleView when handle position may have to be changed. Bug: 26499944 Change-Id: Iba539eb50df1495be7d64174de38b1e88cff9348 --- core/java/android/widget/Editor.java | 52 ++++++++++++++++++++++---- core/java/android/widget/TextView.java | 6 ++- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java index 47b034886ad83..9e8752d1763ee 100644 --- a/core/java/android/widget/Editor.java +++ b/core/java/android/widget/Editor.java @@ -1781,6 +1781,18 @@ public class Editor { if (translate) canvas.translate(0, -cursorOffsetVertical); } + void invalidateHandlesAndActionMode() { + if (mSelectionModifierCursorController != null) { + mSelectionModifierCursorController.invalidateHandles(); + } + if (mInsertionPointCursorController != null) { + mInsertionPointCursorController.invalidateHandle(); + } + if (mTextActionMode != null) { + mTextActionMode.invalidate(); + } + } + /** * Invalidates all the sub-display lists that overlap the specified character range */ @@ -4107,6 +4119,14 @@ public class Editor { setMeasuredDimension(getPreferredWidth(), getPreferredHeight()); } + @Override + public void invalidate() { + super.invalidate(); + if (isShowing()) { + positionAtCursorOffset(getCurrentCursorOffset(), true); + } + }; + private int getPreferredWidth() { return Math.max(mDrawable.getIntrinsicWidth(), mMinSize); } @@ -4173,7 +4193,12 @@ public class Editor { return mTextView.getOffsetAtCoordinate(line, x); } - protected void positionAtCursorOffset(int offset, boolean parentScrolled) { + /** + * @param offset Cursor offset. Must be in [-1, length]. + * @param forceUpdatePosition whether to force update the position. This should be true + * when If the parent has been scrolled, for example. + */ + protected void positionAtCursorOffset(int offset, boolean forceUpdatePosition) { // A HandleView relies on the layout, which may be nulled by external methods Layout layout = mTextView.getLayout(); if (layout == null) { @@ -4184,7 +4209,7 @@ public class Editor { layout = mTextView.getLayout(); boolean offsetChanged = offset != mPreviousOffset; - if (offsetChanged || parentScrolled) { + if (offsetChanged || forceUpdatePosition) { if (offsetChanged) { updateSelection(offset); addPositionToTouchUpFilter(offset); @@ -4785,13 +4810,9 @@ public class Editor { mPrevX = x; } - /** - * @param offset Cursor offset. Must be in [-1, length]. - * @param parentScrolled If the parent has been scrolled or not. - */ @Override - protected void positionAtCursorOffset(int offset, boolean parentScrolled) { - super.positionAtCursorOffset(offset, parentScrolled); + protected void positionAtCursorOffset(int offset, boolean forceUpdatePosition) { + super.positionAtCursorOffset(offset, forceUpdatePosition); mInWord = (offset != -1) && !getWordIteratorWithText().isBoundary(offset); } @@ -5016,6 +5037,12 @@ public class Editor { public boolean isActive() { return mHandle != null && mHandle.isShowing(); } + + public void invalidateHandle() { + if (mHandle != null) { + mHandle.invalidate(); + } + } } class SelectionModifierCursorController implements CursorController { @@ -5420,6 +5447,15 @@ public class Editor { public boolean isActive() { return mStartHandle != null && mStartHandle.isShowing(); } + + public void invalidateHandles() { + if (mStartHandle != null) { + mStartHandle.invalidate(); + } + if (mEndHandle != null) { + mEndHandle.invalidate(); + } + } } private class CorrectionHighlighter { diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 15d1bd6e08c69..b997c5e90267f 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -3354,7 +3354,10 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener mShadowColor = color; // Will change text clip region - if (mEditor != null) mEditor.invalidateTextDisplayList(); + if (mEditor != null) { + mEditor.invalidateTextDisplayList(); + mEditor.invalidateHandlesAndActionMode(); + } invalidate(); } @@ -8312,6 +8315,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener if (mEditor != null) { if (oldStart >= 0) mEditor.invalidateTextDisplayList(mLayout, oldStart, oldEnd); if (newStart >= 0) mEditor.invalidateTextDisplayList(mLayout, newStart, newEnd); + mEditor.invalidateHandlesAndActionMode(); } }