am 53841df2: Merge "SelectAllOnFocus shows a higlighted text. DO NOT MERGE." into gingerbread

* commit '53841df2a49bffa53ba3d1e4b0580f4f23a64dc3':
  SelectAllOnFocus shows a higlighted text. DO NOT MERGE.
This commit is contained in:
Gilles Debunne
2011-02-04 15:49:56 -08:00
committed by Android Git Automerger
2 changed files with 37 additions and 58 deletions

View File

@@ -297,6 +297,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
Drawable mSelectHandleRight; Drawable mSelectHandleRight;
Drawable mSelectHandleCenter; Drawable mSelectHandleCenter;
// Set when this TextView gained focus with some text selected. Will start selection mode.
private boolean mCreatedWithASelection = false;
/* /*
* Kick-start the font cache for the zygote process (to pay the cost of * Kick-start the font cache for the zygote process (to pay the cost of
* initializing freetype for our default font only once). * initializing freetype for our default font only once).
@@ -3774,8 +3777,10 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
// - ExtractEditText does not call onFocus when it is displayed. Fixing this issue would // - ExtractEditText does not call onFocus when it is displayed. Fixing this issue would
// allow to test for hasSelection in onFocusChanged, which would trigger a // allow to test for hasSelection in onFocusChanged, which would trigger a
// startTextSelectionMode here. TODO // startTextSelectionMode here. TODO
if (this instanceof ExtractEditText && selectionController != null && hasSelection()) { if (mCreatedWithASelection ||
(this instanceof ExtractEditText && selectionController != null && hasSelection())) {
startTextSelectionMode(); startTextSelectionMode();
mCreatedWithASelection = false;
} }
mPreDrawState = PREDRAW_DONE; mPreDrawState = PREDRAW_DONE;
@@ -4191,6 +4196,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
mInsertionPointCursorController.isShowing()) { mInsertionPointCursorController.isShowing()) {
mInsertionPointCursorController.updatePosition(); mInsertionPointCursorController.updatePosition();
} }
if (mSelectionModifierCursorController != null && if (mSelectionModifierCursorController != null &&
mSelectionModifierCursorController.isShowing()) { mSelectionModifierCursorController.isShowing()) {
mSelectionModifierCursorController.updatePosition(); mSelectionModifierCursorController.updatePosition();
@@ -6601,6 +6607,12 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
int selStart = getSelectionStart(); int selStart = getSelectionStart();
int selEnd = getSelectionEnd(); int selEnd = getSelectionEnd();
// SelectAllOnFocus fields are highlighted and not selected. Do not start text selection
// mode for these, unless there was a specific selection already started.
final boolean isFocusHighlighted = mSelectAllOnFocus && selStart == 0 &&
selEnd == mText.length();
mCreatedWithASelection = mFrozenWithFocus && hasSelection() && !isFocusHighlighted;
if (!mFrozenWithFocus || (selStart < 0 || selEnd < 0)) { if (!mFrozenWithFocus || (selStart < 0 || selEnd < 0)) {
// If a tap was used to give focus to that view, move cursor at tap position. // If a tap was used to give focus to that view, move cursor at tap position.
// Has to be done before onTakeFocus, which can be overloaded. // Has to be done before onTakeFocus, which can be overloaded.
@@ -6613,10 +6625,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
mMovement.onTakeFocus(this, (Spannable) mText, direction); mMovement.onTakeFocus(this, (Spannable) mText, direction);
} }
if (mSelectAllOnFocus) {
Selection.setSelection((Spannable) mText, 0, mText.length());
}
// The DecorView does not have focus when the 'Done' ExtractEditText button is // The DecorView does not have focus when the 'Done' ExtractEditText button is
// pressed. Since it is the ViewRoot's mView, it requests focus before // pressed. Since it is the ViewRoot's mView, it requests focus before
// ExtractEditText clears focus, which gives focus to the ExtractEditText. // ExtractEditText clears focus, which gives focus to the ExtractEditText.
@@ -6635,6 +6643,11 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
*/ */
Selection.setSelection((Spannable) mText, selStart, selEnd); Selection.setSelection((Spannable) mText, selStart, selEnd);
} }
if (mSelectAllOnFocus) {
Selection.setSelection((Spannable) mText, 0, mText.length());
}
mTouchFocusSelected = true; mTouchFocusSelected = true;
} }
@@ -6664,7 +6677,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
// ExtractEditText goes out of focus. // ExtractEditText goes out of focus.
mIsInTextSelectionMode = false; mIsInTextSelectionMode = false;
} else { } else {
terminateTextSelectionMode(); stopTextSelectionMode();
} }
if (mSelectionModifierCursorController != null) { if (mSelectionModifierCursorController != null) {
@@ -6766,29 +6779,23 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
final int end = getSelectionEnd(); final int end = getSelectionEnd();
if (start == end) { if (start == end) {
if (start >= prevStart && start < prevEnd) { boolean tapInsideSelectAllOnFocus = mSelectAllOnFocus && prevStart == 0 &&
prevEnd == mText.length();
if (start >= prevStart && start < prevEnd && !tapInsideSelectAllOnFocus) {
// Restore previous selection // Restore previous selection
Selection.setSelection((Spannable)mText, prevStart, prevEnd); Selection.setSelection((Spannable)mText, prevStart, prevEnd);
if (hasSelectionController() && !getSelectionController().isShowing()) { // Tapping inside the selection displays the cut/copy/paste context menu
// If the anchors aren't showing, revive them. showContextMenu();
getSelectionController().show();
} else {
// Tapping inside the selection displays the cut/copy/paste context menu
// as long as the anchors are already showing.
showContextMenu();
}
return;
} else { } else {
// Tapping outside stops selection mode, if any // Tapping outside stops selection mode, if any
stopTextSelectionMode(); stopTextSelectionMode();
if (hasInsertionController() && mText.length() > 0) { boolean selectAllGotFocus = mSelectAllOnFocus && mTouchFocusSelected;
if (hasInsertionController() && !selectAllGotFocus) {
getInsertionController().show(); getInsertionController().show();
} }
} }
} else if (hasSelection() && hasSelectionController()) {
getSelectionController().show();
} }
} }
@@ -6811,7 +6818,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
int end = Math.min(len, mPrevEnd); int end = Math.min(len, mPrevEnd);
Selection.setSelection((Spannable)mText, start, end); Selection.setSelection((Spannable)mText, start, end);
if (hasSelection()) { boolean selectAllGotFocus = mSelectAllOnFocus && mTouchFocusSelected;
if (hasSelection() && !selectAllGotFocus) {
startTextSelectionMode(); startTextSelectionMode();
} }
} }
@@ -7169,7 +7177,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
} }
private boolean canSelectText() { private boolean canSelectText() {
return textCanBeSelected() && mText.length() != 0; return hasSelectionController() && mText.length() != 0;
} }
private boolean textCanBeSelected() { private boolean textCanBeSelected() {
@@ -7339,7 +7347,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
} }
int selectionStart, selectionEnd; int selectionStart, selectionEnd;
long wordLimits = getWordLimitsAt(minOffset); long wordLimits = getWordLimitsAt(minOffset);
if (wordLimits >= 0) { if (wordLimits >= 0) {
selectionStart = extractRangeStartFromLong(wordLimits); selectionStart = extractRangeStartFromLong(wordLimits);
@@ -7498,6 +7506,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
} }
if (added) { if (added) {
hideControllers();
menu.setHeaderTitle(com.android.internal.R.string.editTextMenuTitle); menu.setHeaderTitle(com.android.internal.R.string.editTextMenuTitle);
} }
} }
@@ -7674,29 +7683,18 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
return; return;
} }
if (!requestFocus()) { if (!canSelectText() || !requestFocus()) {
return; return;
} }
selectCurrentWord(); selectCurrentWord();
getSelectionController().show();
final InputMethodManager imm = (InputMethodManager)
getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(this, 0, null);
mIsInTextSelectionMode = true; mIsInTextSelectionMode = true;
} }
} }
/**
* Same as {@link #stopTextSelectionMode()}, except that there is no cursor controller
* fade out animation. Needed since the drawable and their alpha values are shared by all
* TextViews. Switching from one TextView to another would fade the cursor controllers in the
* new one otherwise.
*/
private void terminateTextSelectionMode() {
stopTextSelectionMode();
if (mSelectionModifierCursorController != null) {
SelectionModifierCursorController selectionModifierCursorController =
(SelectionModifierCursorController) mSelectionModifierCursorController;
selectionModifierCursorController.cancelFadeOutAnimation();
}
}
private void stopTextSelectionMode() { private void stopTextSelectionMode() {
if (mIsInTextSelectionMode) { if (mIsInTextSelectionMode) {
@@ -8074,14 +8072,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
// Whether selection anchors are active // Whether selection anchors are active
private boolean mIsShowing; private boolean mIsShowing;
private static final int DELAY_BEFORE_FADE_OUT = 4100;
private final Runnable mHider = new Runnable() {
public void run() {
hide();
}
};
SelectionModifierCursorController() { SelectionModifierCursorController() {
mStartHandle = new HandleView(this, HandleView.LEFT); mStartHandle = new HandleView(this, HandleView.LEFT);
mEndHandle = new HandleView(this, HandleView.RIGHT); mEndHandle = new HandleView(this, HandleView.RIGHT);
@@ -8098,29 +8088,18 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
mStartHandle.show(); mStartHandle.show();
mEndHandle.show(); mEndHandle.show();
hideInsertionPointCursorController(); hideInsertionPointCursorController();
hideDelayed(DELAY_BEFORE_FADE_OUT);
} }
public void hide() { public void hide() {
mStartHandle.hide(); mStartHandle.hide();
mEndHandle.hide(); mEndHandle.hide();
mIsShowing = false; mIsShowing = false;
removeCallbacks(mHider);
}
private void hideDelayed(int delay) {
removeCallbacks(mHider);
postDelayed(mHider, delay);
} }
public boolean isShowing() { public boolean isShowing() {
return mIsShowing; return mIsShowing;
} }
public void cancelFadeOutAnimation() {
hide();
}
public void updatePosition(HandleView handle, int x, int y) { public void updatePosition(HandleView handle, int x, int y) {
int selectionStart = getSelectionStart(); int selectionStart = getSelectionStart();
int selectionEnd = getSelectionEnd(); int selectionEnd = getSelectionEnd();
@@ -8172,7 +8151,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
mStartHandle.positionAtCursor(selectionStart, true); mStartHandle.positionAtCursor(selectionStart, true);
mEndHandle.positionAtCursor(selectionEnd, true); mEndHandle.positionAtCursor(selectionEnd, true);
hideDelayed(DELAY_BEFORE_FADE_OUT);
} }
public boolean onTouchEvent(MotionEvent event) { public boolean onTouchEvent(MotionEvent event) {

View File

@@ -75,6 +75,7 @@
android:drawablePadding="2dip" android:drawablePadding="2dip"
android:singleLine="true" android:singleLine="true"
android:ellipsize="end" android:ellipsize="end"
android:selectAllOnFocus="true"
android:inputType="text|textAutoComplete" android:inputType="text|textAutoComplete"
android:dropDownWidth="match_parent" android:dropDownWidth="match_parent"
android:dropDownHeight="match_parent" android:dropDownHeight="match_parent"