Skip to content

Instantly share code, notes, and snippets.

@mik9
Created August 21, 2012 15:04
Show Gist options
  • Select an option

  • Save mik9/3416301 to your computer and use it in GitHub Desktop.

Select an option

Save mik9/3416301 to your computer and use it in GitHub Desktop.
EditText selection patch
diff --git a/base/core/java/android/text/Layout.java b/base2/core/java/android/text/Layout.java
old mode 100644
new mode 100755
index 9695533..b5f32d4
--- a/base/core/java/android/text/Layout.java
+++ b/base2/core/java/android/text/Layout.java
@@ -61,6 +61,7 @@ public abstract class Layout {
};
private RectF mEmojiRect;
+ public int mCurrentCursorLine;
/**
* Return how wide a layout must be in order to display the
@@ -128,6 +129,7 @@ public abstract class Layout {
mSpacingMult = spacingMult;
mSpacingAdd = spacingAdd;
mSpannedText = text instanceof Spanned;
+ mCurrentCursorLine = 0;
}
/**
@@ -877,7 +879,7 @@ public abstract class Layout {
while (high - low > 1) {
guess = (high + low) / 2;
- if (getLineStart(guess) > offset)
+ if ( (getLineStart(guess) + (guess == 0 ? 0 : 1)) > offset)
high = guess;
else
low = guess;
@@ -885,8 +887,23 @@ public abstract class Layout {
if (low < 0)
return 0;
- else
- return low;
+ else {
+ if (offset == getLineStart(low)) {
+ if (mCurrentCursorLine < low) {
+ return low - 1;
+ } else {
+ return low;
+ }
+ } else if (offset == getLineStart(low + 1)) {
+ if (mCurrentCursorLine > low) {
+ return low + 1;
+ } else {
+ return low;
+ }
+ } else {
+ return low;
+ }
+ }
}
//SISO_MEA_JP - Start
@@ -918,13 +935,10 @@ public abstract class Layout {
* closest to the specified horizontal position.
*/
public int getOffsetForHorizontal(int line, float horiz) {
- int max = getLineEnd(line) - 1;
+ int max = getLineEnd(line);
int min = getLineStart(line);
Directions dirs = getLineDirections(line);
- if (line == getLineCount() - 1)
- max++;
-
int best = min;
float bestdist = Math.abs(getPrimaryHorizontal(best) - horiz);
diff --git a/base/core/java/android/widget/TextView.java b/base2/core/java/android/widget/TextView.java
old mode 100644
new mode 100755
index f3950e8..895447e
--- a/base/core/java/android/widget/TextView.java
+++ b/base2/core/java/android/widget/TextView.java
@@ -3898,6 +3898,17 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
left + getWidth() - getCompoundPaddingLeft() -
getCompoundPaddingRight(),
bottom + voffset + getExtendedPaddingTop());
+ if (line != 0) {
+ int left2 = (int) mLayout.getPrimaryHorizontal(mLayout.getLineStart(line) - 1);
+ int top2 = mLayout.getLineTop(line - 1);
+ if (line > 1) {
+ top -= mLayout.getLineDescent(line - 2);
+ }
+ invalidate(left2, top2+voffset+getExtendedPaddingTop(),
+ left + getWidth() - getCompoundPaddingLeft() -
+ getCompoundPaddingRight(),
+ top + voffset + getExtendedPaddingTop());
+ }
}
}
}
@@ -8928,6 +8939,15 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
if (offset < 0) {
Log.w(LOG_TAG, "offset is greater than or equal to zero - offset :: "+offset);
}
+
+
+ y -= TextView.this.getTotalPaddingTop();
+ // Clamp the position to inside of the view.
+ y = Math.max(0, y);
+ y = Math.min(TextView.this.getHeight() - TextView.this.getTotalPaddingBottom() - 1, y);
+ y += TextView.this.getScrollY();
+
+ getLayout().mCurrentCursorLine = mLayout.getLineForVertical(y);
if (offset != previousOffset && offset >= 0) {
Selection.setSelection((Spannable) mText, offset);
@@ -9144,6 +9164,14 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
/* Swap start and end cursor controller */
mSwapCursorController = selectionEnd < selectionStart ? true : false;
/* end of Swap start and end cursor controller */
+
+ y -= TextView.this.getTotalPaddingTop();
+ // Clamp the position to inside of the view.
+ y = Math.max(0, y);
+ y = Math.min(TextView.this.getHeight() - TextView.this.getTotalPaddingBottom() - 1, y);
+ y += TextView.this.getScrollY();
+
+ getLayout().mCurrentCursorLine = getLayout().getLineForVertical(y);;
/* Comviva.fix for FC issue P110506-2970 */
if(selectionEnd>mText.length())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment