SearchView improvements per design.

- X is visible only if there is text, or we need a way to close a
search field that is iconified by default.
- Search dialog (legacy) has a back button to the left.
- Hitting X on a non-focused search view will bring it into focus
and show the keyboard if necessary.

Change-Id: I5a30bb08adcf84639a922a9e13be1d1562f714e6
diff --git a/core/java/android/app/SearchDialog.java b/core/java/android/app/SearchDialog.java
index 9cb57be..41eea2e 100644
--- a/core/java/android/app/SearchDialog.java
+++ b/core/java/android/app/SearchDialog.java
@@ -80,6 +80,7 @@
     private View mSearchPlate;
     private SearchView mSearchView;
     private Drawable mWorkingSpinner;
+    private View mCloseSearch;
 
     // interaction with searchable application
     private SearchableInfo mSearchable;
@@ -167,11 +168,18 @@
         SearchBar searchBar = (SearchBar) findViewById(com.android.internal.R.id.search_bar);
         searchBar.setSearchDialog(this);
         mSearchView = (SearchView) findViewById(com.android.internal.R.id.search_view);
-        mSearchView.setSubmitButtonEnabled(true);
         mSearchView.setOnCloseListener(mOnCloseListener);
         mSearchView.setOnQueryTextListener(mOnQueryChangeListener);
         mSearchView.setOnSuggestionListener(mOnSuggestionSelectionListener);
 
+        mCloseSearch = findViewById(com.android.internal.R.id.closeButton);
+        mCloseSearch.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                dismiss();
+            }
+        });
+
         // TODO: Move the badge logic to SearchView or move the badge to search_bar.xml
         mBadgeLabel = (TextView) mSearchView.findViewById(com.android.internal.R.id.search_badge);
         mSearchAutoComplete = (AutoCompleteTextView)
diff --git a/core/java/android/widget/SearchView.java b/core/java/android/widget/SearchView.java
index 586ece8..f3bda43 100644
--- a/core/java/android/widget/SearchView.java
+++ b/core/java/android/widget/SearchView.java
@@ -603,7 +603,7 @@
         final boolean hasText = !TextUtils.isEmpty(mQueryTextView.getText());
         // Should we show the close button? It is not shown if there's no focus,
         // field is not iconified by default and there is no text in it.
-        final boolean showClose = hasText || mIconifiedByDefault || mQueryTextView.hasFocus();
+        final boolean showClose = hasText || mIconifiedByDefault;
         mCloseButton.setVisibility(showClose ? VISIBLE : INVISIBLE);
         mCloseButton.getDrawable().setState(hasText ? ENABLED_STATE_SET : EMPTY_STATE_SET);
     }
@@ -919,17 +919,22 @@
     }
 
     private void onCloseClicked() {
-        if (mOnCloseListener == null || !mOnCloseListener.onClose()) {
-            CharSequence text = mQueryTextView.getText();
-            if (TextUtils.isEmpty(text)) {
+        CharSequence text = mQueryTextView.getText();
+        if (TextUtils.isEmpty(text)) {
+            if (mIconifiedByDefault) {
                 // query field already empty, hide the keyboard and remove focus
                 clearFocus();
                 setImeVisibility(false);
-            } else {
-                mQueryTextView.setText("");
             }
+        } else {
+            mQueryTextView.setText("");
+            mQueryTextView.requestFocus();
+            setImeVisibility(true);
+        }
+
+        if (mIconifiedByDefault && (mOnCloseListener == null || !mOnCloseListener.onClose())) {
             updateViewsVisibility(mIconifiedByDefault);
-            if (mIconifiedByDefault) setImeVisibility(false);
+            setImeVisibility(false);
         }
     }
 
diff --git a/core/res/res/drawable-hdpi/title_bar_shadow.9.png b/core/res/res/drawable-hdpi/title_bar_shadow.9.png
new file mode 100644
index 0000000..e6dab63
--- /dev/null
+++ b/core/res/res/drawable-hdpi/title_bar_shadow.9.png
Binary files differ
diff --git a/core/res/res/drawable-mdpi/title_bar_shadow.9.png b/core/res/res/drawable-mdpi/title_bar_shadow.9.png
new file mode 100644
index 0000000..dbcefee
--- /dev/null
+++ b/core/res/res/drawable-mdpi/title_bar_shadow.9.png
Binary files differ
diff --git a/core/res/res/layout/search_bar.xml b/core/res/res/layout/search_bar.xml
index 7918a3f..790ac6b 100644
--- a/core/res/res/layout/search_bar.xml
+++ b/core/res/res/layout/search_bar.xml
@@ -23,36 +23,58 @@
     android:id="@+id/search_bar"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
-    android:orientation="horizontal"
+    android:orientation="vertical"
+    android:background="@color/transparent"
     android:focusable="true"
-    android:background="?android:attr/actionModeBackground"
     android:descendantFocusability="afterDescendants">
 
-    <RelativeLayout
+    <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:layout_gravity="center_vertical"
-        >
+        android:background="?android:attr/actionModeBackground"
+        android:orientation="horizontal">
 
-        <ImageView
-            android:id="@+id/search_app_icon"
-            android:layout_height="48dip"
-            android:layout_width="48dip"
-            android:layout_marginLeft="8dip"
-            android:layout_marginRight="8dip"
-            android:layout_gravity="center_vertical"
-            android:layout_alignParentLeft="true"
-        />
-        <SearchView
-            android:id="@+id/search_view"
-            android:layout_width="match_parent"
+        <!-- Grouped to allow tapping on either item to exit search mode -->
+        <LinearLayout
+            android:id="@id/closeButton"
+            android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:maxWidth="600dip"
-            android:iconifiedByDefault="false"
-            android:layout_alignParentRight="true"
-            android:layout_gravity="center_vertical|right"
+            android:orientation="horizontal"
+            android:focusable="true"
+            android:background="?android:attr/selectableItemBackground">
+
+            <ImageView
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:layout_gravity="center_vertical"
+                android:src="?android:attr/homeAsUpIndicator"/>
+
+            <ImageView
+                android:id="@+id/search_app_icon"
+                android:layout_height="48dip"
+                android:layout_width="48dip"
+                android:layout_gravity="center_vertical"
             />
 
-    </RelativeLayout>
+        </LinearLayout>
 
+        <!-- Actual search view with search icon, text field, close
+            and voice buttons -->
+        <SearchView
+            android:id="@+id/search_view"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:layout_weight="1"
+            android:maxWidth="600dip"
+            android:iconifiedByDefault="false"
+            android:layout_gravity="center_vertical"
+            />
+
+    </LinearLayout>
+
+    <ImageView
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:scaleType="fitXY"
+        android:src="@drawable/title_bar_shadow"/>
 </view>
diff --git a/core/res/res/layout/search_view.xml b/core/res/res/layout/search_view.xml
index 99fdf5b..475aa59 100644
--- a/core/res/res/layout/search_view.xml
+++ b/core/res/res/layout/search_view.xml
@@ -74,7 +74,6 @@
                 android:id="@+id/search_app_icon"
                 android:layout_width="wrap_content"
                 android:layout_height="match_parent"
-                android:layout_marginRight="7dip"
                 android:layout_gravity="center_vertical"
                 android:src="?android:attr/searchViewSearchIcon"
             />
diff --git a/core/res/res/values/themes.xml b/core/res/res/values/themes.xml
index 2ab2c04..1fe4804 100644
--- a/core/res/res/values/themes.xml
+++ b/core/res/res/values/themes.xml
@@ -693,17 +693,14 @@
 
     <!-- Theme for the search input bar. -->
     <style name="Theme.SearchBar" parent="Theme.Holo.Light.Panel">
-        <item name="windowContentOverlay">@null</item>        
         <item name="actionModeBackground">@android:drawable/cab_background_opaque_holo_light</item>
     </style>
 
     <style name="Theme.Holo.SearchBar" parent="Theme.Holo.Panel">
-        <item name="windowContentOverlay">@null</item>
         <item name="actionModeBackground">@android:drawable/cab_background_opaque_holo_dark</item>
     </style>
 
     <style name="Theme.Holo.Light.SearchBar" parent="Theme.Holo.Light.Panel">
-        <item name="windowContentOverlay">@null</item>
         <item name="actionModeBackground">@android:drawable/cab_background_opaque_holo_light</item>
     </style>