Merge "TextView refactoring to ease future merges in HC." into gingerbread
This commit is contained in:
committed by
Android (Google) Code Review
commit
3ecceb5b94
@@ -92,11 +92,11 @@ import android.view.MenuItem;
|
|||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewDebug;
|
import android.view.ViewDebug;
|
||||||
import android.view.WindowManager;
|
|
||||||
import android.view.ViewGroup.LayoutParams;
|
import android.view.ViewGroup.LayoutParams;
|
||||||
import android.view.ViewParent;
|
import android.view.ViewParent;
|
||||||
import android.view.ViewRoot;
|
import android.view.ViewRoot;
|
||||||
import android.view.ViewTreeObserver;
|
import android.view.ViewTreeObserver;
|
||||||
|
import android.view.WindowManager;
|
||||||
import android.view.accessibility.AccessibilityEvent;
|
import android.view.accessibility.AccessibilityEvent;
|
||||||
import android.view.accessibility.AccessibilityManager;
|
import android.view.accessibility.AccessibilityManager;
|
||||||
import android.view.animation.AnimationUtils;
|
import android.view.animation.AnimationUtils;
|
||||||
@@ -7227,9 +7227,21 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Two ints packed in a long
|
// Two ints packed in a long
|
||||||
|
return packRangeInLong(start, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static long packRangeInLong(int start, int end) {
|
||||||
return (((long) start) << 32) | end;
|
return (((long) start) << 32) | end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int extractRangeStartFromLong(long range) {
|
||||||
|
return (int) (range >>> 32);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int extractRangeEndFromLong(long range) {
|
||||||
|
return (int) (range & 0x00000000FFFFFFFFL);
|
||||||
|
}
|
||||||
|
|
||||||
private void selectCurrentWord() {
|
private void selectCurrentWord() {
|
||||||
// In case selection mode is started after an orientation change or after a select all,
|
// In case selection mode is started after an orientation change or after a select all,
|
||||||
// use the current selection instead of creating one
|
// use the current selection instead of creating one
|
||||||
@@ -7247,14 +7259,14 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
|||||||
|
|
||||||
long wordLimits = getWordLimitsAt(minOffset);
|
long wordLimits = getWordLimitsAt(minOffset);
|
||||||
if (wordLimits >= 0) {
|
if (wordLimits >= 0) {
|
||||||
selectionStart = (int) (wordLimits >>> 32);
|
selectionStart = extractRangeStartFromLong(wordLimits);
|
||||||
} else {
|
} else {
|
||||||
selectionStart = Math.max(minOffset - 5, 0);
|
selectionStart = Math.max(minOffset - 5, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
wordLimits = getWordLimitsAt(maxOffset);
|
wordLimits = getWordLimitsAt(maxOffset);
|
||||||
if (wordLimits >= 0) {
|
if (wordLimits >= 0) {
|
||||||
selectionEnd = (int) (wordLimits & 0x00000000FFFFFFFFL);
|
selectionEnd = extractRangeEndFromLong(wordLimits);
|
||||||
} else {
|
} else {
|
||||||
selectionEnd = Math.min(maxOffset + 5, mText.length());
|
selectionEnd = Math.min(maxOffset + 5, mText.length());
|
||||||
}
|
}
|
||||||
@@ -7269,8 +7281,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
|||||||
|
|
||||||
long wordLimits = getWordLimitsAt(mLastTouchOffset);
|
long wordLimits = getWordLimitsAt(mLastTouchOffset);
|
||||||
if (wordLimits >= 0) {
|
if (wordLimits >= 0) {
|
||||||
int start = (int) (wordLimits >>> 32);
|
int start = extractRangeStartFromLong(wordLimits);
|
||||||
int end = (int) (wordLimits & 0x00000000FFFFFFFFL);
|
int end = extractRangeEndFromLong(wordLimits);
|
||||||
return mTransformed.subSequence(start, end).toString();
|
return mTransformed.subSequence(start, end).toString();
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
@@ -7485,8 +7497,50 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
|||||||
CharSequence paste = clip.getText();
|
CharSequence paste = clip.getText();
|
||||||
|
|
||||||
if (paste != null && paste.length() > 0) {
|
if (paste != null && paste.length() > 0) {
|
||||||
// Paste adds/removes spaces before or after insertion as needed.
|
long minMax = prepareSpacesAroundPaste(min, max, paste);
|
||||||
|
min = extractRangeStartFromLong(minMax);
|
||||||
|
max = extractRangeEndFromLong(minMax);
|
||||||
|
Selection.setSelection((Spannable) mText, max);
|
||||||
|
((Editable) mText).replace(min, max, paste);
|
||||||
|
stopTextSelectionMode();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case ID_COPY_URL:
|
||||||
|
URLSpan[] urls = ((Spanned) mText).getSpans(min, max, URLSpan.class);
|
||||||
|
if (urls.length == 1) {
|
||||||
|
clip.setText(urls[0].getURL());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case ID_SWITCH_INPUT_METHOD:
|
||||||
|
InputMethodManager imm = InputMethodManager.peekInstance();
|
||||||
|
if (imm != null) {
|
||||||
|
imm.showInputMethodPicker();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case ID_ADD_TO_DICTIONARY:
|
||||||
|
String word = getWordForDictionary();
|
||||||
|
|
||||||
|
if (word != null) {
|
||||||
|
Intent i = new Intent("com.android.settings.USER_DICTIONARY_INSERT");
|
||||||
|
i.putExtra("word", word);
|
||||||
|
i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
|
getContext().startActivity(i);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare text so that there are not zero or two spaces at beginning and end of region defined
|
||||||
|
* by [min, max] when replacing this region by paste.
|
||||||
|
*/
|
||||||
|
private long prepareSpacesAroundPaste(int min, int max, CharSequence paste) {
|
||||||
|
// Paste adds/removes spaces before or after insertion as needed.
|
||||||
if (Character.isSpaceChar(paste.charAt(0))) {
|
if (Character.isSpaceChar(paste.charAt(0))) {
|
||||||
if (min > 0 && Character.isSpaceChar(mTransformed.charAt(min - 1))) {
|
if (min > 0 && Character.isSpaceChar(mTransformed.charAt(min - 1))) {
|
||||||
// Two spaces at beginning of paste: remove one
|
// Two spaces at beginning of paste: remove one
|
||||||
@@ -7521,40 +7575,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
|||||||
((Editable) mText).replace(max, max, " ");
|
((Editable) mText).replace(max, max, " ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return packRangeInLong(min, max);
|
||||||
Selection.setSelection((Spannable) mText, max);
|
|
||||||
((Editable) mText).replace(min, max, paste);
|
|
||||||
stopTextSelectionMode();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
|
|
||||||
case ID_COPY_URL:
|
|
||||||
URLSpan[] urls = ((Spanned) mText).getSpans(min, max, URLSpan.class);
|
|
||||||
if (urls.length == 1) {
|
|
||||||
clip.setText(urls[0].getURL());
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
|
|
||||||
case ID_SWITCH_INPUT_METHOD:
|
|
||||||
InputMethodManager imm = InputMethodManager.peekInstance();
|
|
||||||
if (imm != null) {
|
|
||||||
imm.showInputMethodPicker();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
|
|
||||||
case ID_ADD_TO_DICTIONARY:
|
|
||||||
String word = getWordForDictionary();
|
|
||||||
|
|
||||||
if (word != null) {
|
|
||||||
Intent i = new Intent("com.android.settings.USER_DICTIONARY_INSERT");
|
|
||||||
i.putExtra("word", word);
|
|
||||||
i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
||||||
getContext().startActivity(i);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user