Merge "SelectAllOnFocus shows a higlighted text. DO NOT MERGE." into gingerbread
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java
index 41c9736..1cdaafb 100644
--- a/core/java/android/widget/TextView.java
+++ b/core/java/android/widget/TextView.java
@@ -297,6 +297,9 @@
     Drawable mSelectHandleRight;
     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
      * initializing freetype for our default font only once).
@@ -3758,8 +3761,10 @@
         // - ExtractEditText does not call onFocus when it is displayed. Fixing this issue would
         //   allow to test for hasSelection in onFocusChanged, which would trigger a
         //   startTextSelectionMode here. TODO
-        if (this instanceof ExtractEditText && selectionController != null && hasSelection()) {
+        if (mCreatedWithASelection ||
+           (this instanceof ExtractEditText && selectionController != null && hasSelection())) {
             startTextSelectionMode();
+            mCreatedWithASelection = false;
         }
 
         mPreDrawState = PREDRAW_DONE;
@@ -4175,6 +4180,7 @@
                 mInsertionPointCursorController.isShowing()) {
             mInsertionPointCursorController.updatePosition();
         }
+
         if (mSelectionModifierCursorController != null &&
                 mSelectionModifierCursorController.isShowing()) {
             mSelectionModifierCursorController.updatePosition();
@@ -6580,6 +6586,12 @@
             int selStart = getSelectionStart();
             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 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.
@@ -6592,10 +6604,6 @@
                     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
                 // pressed. Since it is the ViewRoot's mView, it requests focus before
                 // ExtractEditText clears focus, which gives focus to the ExtractEditText.
@@ -6614,6 +6622,11 @@
                      */
                     Selection.setSelection((Spannable) mText, selStart, selEnd);
                 }
+
+                if (mSelectAllOnFocus) {
+                    Selection.setSelection((Spannable) mText, 0, mText.length());
+                }
+
                 mTouchFocusSelected = true;
             }
 
@@ -6643,7 +6656,7 @@
                 // ExtractEditText goes out of focus.
                 mIsInTextSelectionMode = false;
             } else {
-                terminateTextSelectionMode();
+                stopTextSelectionMode();
             }
 
             if (mSelectionModifierCursorController != null) {
@@ -6745,29 +6758,23 @@
         final int end = getSelectionEnd();
 
         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
                 Selection.setSelection((Spannable)mText, prevStart, prevEnd);
 
-                if (hasSelectionController() && !getSelectionController().isShowing()) {
-                    // If the anchors aren't showing, revive them.
-                    getSelectionController().show();
-                } else {
-                    // Tapping inside the selection displays the cut/copy/paste context menu
-                    // as long as the anchors are already showing.
-                    showContextMenu();
-                }
-                return;
+                // Tapping inside the selection displays the cut/copy/paste context menu
+                showContextMenu();
             } else {
                 // Tapping outside stops selection mode, if any
                 stopTextSelectionMode();
 
-                if (hasInsertionController() && mText.length() > 0) {
+                boolean selectAllGotFocus = mSelectAllOnFocus && mTouchFocusSelected;
+                if (hasInsertionController() && !selectAllGotFocus) {
                     getInsertionController().show();
                 }
             }
-        } else if (hasSelection() && hasSelectionController()) {
-            getSelectionController().show();
         }
     }
 
@@ -6790,7 +6797,8 @@
                 int end = Math.min(len, mPrevEnd);
                 Selection.setSelection((Spannable)mText, start, end);
 
-                if (hasSelection()) {
+                boolean selectAllGotFocus = mSelectAllOnFocus && mTouchFocusSelected;
+                if (hasSelection() && !selectAllGotFocus) {
                     startTextSelectionMode();
                 }
             }
@@ -7147,7 +7155,7 @@
     }
 
     private boolean canSelectText() {
-        return textCanBeSelected() && mText.length() != 0;
+        return hasSelectionController() && mText.length() != 0;
     }
 
     private boolean textCanBeSelected() {
@@ -7317,7 +7325,7 @@
         }
 
         int selectionStart, selectionEnd;
-        
+
         long wordLimits = getWordLimitsAt(minOffset);
         if (wordLimits >= 0) {
             selectionStart = extractRangeStartFromLong(wordLimits);
@@ -7476,6 +7484,7 @@
         }
 
         if (added) {
+            hideControllers();
             menu.setHeaderTitle(com.android.internal.R.string.editTextMenuTitle);
         }
     }
@@ -7652,29 +7661,18 @@
                 return;
             }
 
-            if (!requestFocus()) {
+            if (!canSelectText() || !requestFocus()) {
                 return;
             }
 
             selectCurrentWord();
+            getSelectionController().show();
+            final InputMethodManager imm = (InputMethodManager)
+                    getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
+            imm.showSoftInput(this, 0, null);
             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() {
         if (mIsInTextSelectionMode) {
@@ -8052,14 +8050,6 @@
         // Whether selection anchors are active
         private boolean mIsShowing;
 
-        private static final int DELAY_BEFORE_FADE_OUT = 4100;
-
-        private final Runnable mHider = new Runnable() {
-            public void run() {
-                hide();
-            }
-        };
-
         SelectionModifierCursorController() {
             mStartHandle = new HandleView(this, HandleView.LEFT);
             mEndHandle = new HandleView(this, HandleView.RIGHT);
@@ -8076,29 +8066,18 @@
             mStartHandle.show();
             mEndHandle.show();
             hideInsertionPointCursorController();
-            hideDelayed(DELAY_BEFORE_FADE_OUT);
         }
 
         public void hide() {
             mStartHandle.hide();
             mEndHandle.hide();
             mIsShowing = false;
-            removeCallbacks(mHider);
-        }
-
-        private void hideDelayed(int delay) {
-            removeCallbacks(mHider);
-            postDelayed(mHider, delay);
         }
 
         public boolean isShowing() {
             return mIsShowing;
         }
 
-        public void cancelFadeOutAnimation() {
-            hide();
-        }
-
         public void updatePosition(HandleView handle, int x, int y) {
             int selectionStart = getSelectionStart();
             int selectionEnd = getSelectionEnd();
@@ -8150,7 +8129,6 @@
 
             mStartHandle.positionAtCursor(selectionStart, true);
             mEndHandle.positionAtCursor(selectionEnd, true);
-            hideDelayed(DELAY_BEFORE_FADE_OUT);
         }
 
         public boolean onTouchEvent(MotionEvent event) {
diff --git a/core/res/res/layout/search_bar.xml b/core/res/res/layout/search_bar.xml
index 7935e2a..c7c073c 100644
--- a/core/res/res/layout/search_bar.xml
+++ b/core/res/res/layout/search_bar.xml
@@ -75,6 +75,7 @@
                 android:drawablePadding="2dip"
                 android:singleLine="true"
                 android:ellipsize="end"
+                android:selectAllOnFocus="true"
                 android:inputType="text|textAutoComplete"
                 android:dropDownWidth="match_parent"
                 android:dropDownHeight="match_parent"