diff options
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/widget/Editor.java | 57 |
1 files changed, 36 insertions, 21 deletions
diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java index 816612f1dcc7..31ccb6cb439c 100644 --- a/core/java/android/widget/Editor.java +++ b/core/java/android/widget/Editor.java @@ -404,6 +404,13 @@ public class Editor { // The actual zoom value may changes based on this initial zoom value. private float mInitialZoom = 1f; + // For calculating the line change slops while moving cursor/selection. + // The slop max/min value include line height and the slop on the upper/lower line. + private static final int LINE_CHANGE_SLOP_MAX_DP = 45; + private static final int LINE_CHANGE_SLOP_MIN_DP = 12; + private int mLineChangeSlopMax; + private int mLineChangeSlopMin; + Editor(TextView textView) { mTextView = textView; // Synchronize the filter list, which places the undo input filter at the end. @@ -429,6 +436,14 @@ public class Editor { logCursor("Editor", "New magnifier is %s.", mNewMagnifierEnabled ? "enabled" : "disabled"); } + + mLineChangeSlopMax = (int) TypedValue.applyDimension( + TypedValue.COMPLEX_UNIT_DIP, LINE_CHANGE_SLOP_MAX_DP, + mTextView.getContext().getResources().getDisplayMetrics()); + mLineChangeSlopMin = (int) TypedValue.applyDimension( + TypedValue.COMPLEX_UNIT_DIP, LINE_CHANGE_SLOP_MIN_DP, + mTextView.getContext().getResources().getDisplayMetrics()); + } @VisibleForTesting @@ -5997,7 +6012,14 @@ public class Editor { } } - private int getCurrentLineAdjustedForSlop(Layout layout, int prevLine, float y) { + @VisibleForTesting + public void setLineChangeSlopMinMaxForTesting(final int min, final int max) { + mLineChangeSlopMin = min; + mLineChangeSlopMax = max; + } + + @VisibleForTesting + public int getCurrentLineAdjustedForSlop(Layout layout, int prevLine, float y) { final int trueLine = mTextView.getLineAtCoordinate(y); if (layout == null || prevLine > layout.getLineCount() || layout.getLineCount() <= 0 || prevLine < 0) { @@ -6010,28 +6032,21 @@ public class Editor { return trueLine; } + final int lineHeight = layout.getLineBottom(prevLine) - layout.getLineTop(prevLine); + int slop = (int)(LINE_SLOP_MULTIPLIER_FOR_HANDLEVIEWS + * (layout.getLineBottom(trueLine) - layout.getLineTop(trueLine))); + slop = Math.max(mLineChangeSlopMin, + Math.min(mLineChangeSlopMax, lineHeight + slop)) - lineHeight; + slop = Math.max(0, slop); + final float verticalOffset = mTextView.viewportToContentVerticalOffset(); - final int lineCount = layout.getLineCount(); - final float slop = mTextView.getLineHeight() * LINE_SLOP_MULTIPLIER_FOR_HANDLEVIEWS; - - final float firstLineTop = layout.getLineTop(0) + verticalOffset; - final float prevLineTop = layout.getLineTop(prevLine) + verticalOffset; - final float yTopBound = Math.max(prevLineTop - slop, firstLineTop + slop); - - final float lastLineBottom = layout.getLineBottom(lineCount - 1) + verticalOffset; - final float prevLineBottom = layout.getLineBottom(prevLine) + verticalOffset; - final float yBottomBound = Math.min(prevLineBottom + slop, lastLineBottom - slop); - - // Determine if we've moved lines based on y position and previous line. - int currLine; - if (y <= yTopBound) { - currLine = Math.max(prevLine - 1, 0); - } else if (y >= yBottomBound) { - currLine = Math.min(prevLine + 1, lineCount - 1); - } else { - currLine = prevLine; + if (trueLine > prevLine && y >= layout.getLineBottom(prevLine) + slop + verticalOffset) { + return trueLine; + } + if (trueLine < prevLine && y <= layout.getLineTop(prevLine) - slop + verticalOffset) { + return trueLine; } - return currLine; + return prevLine; } /** |
