Merge "Import translations. DO NOT MERGE"
diff --git a/res/drawable/rounded_corner.xml b/res/drawable/rounded_corner.xml
new file mode 100644
index 0000000..fb8f4f5
--- /dev/null
+++ b/res/drawable/rounded_corner.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!--
+  ~ Copyright (C) 2014 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License
+  -->
+<shape xmlns:android="http://schemas.android.com/apk/res/android"
+    android:shape="rectangle" >
+    <solid android:color="@color/searchbox_background_color" />
+    <corners android:radius="2dp" />
+</shape>
diff --git a/res/layout/dialtacts_activity.xml b/res/layout/dialtacts_activity.xml
index e0049b5..c7a788d 100644
--- a/res/layout/dialtacts_activity.xml
+++ b/res/layout/dialtacts_activity.xml
@@ -98,6 +98,7 @@
     </FrameLayout>
     <!-- Host container for the contact tile drag shadow -->
     <FrameLayout
+        android:id="@+id/activity_overlay"
         android:layout_height="match_parent"
         android:layout_width="match_parent">
         <ImageView
diff --git a/res/layout/search_edittext.xml b/res/layout/search_edittext.xml
index 03c5106..1932228 100644
--- a/res/layout/search_edittext.xml
+++ b/res/layout/search_edittext.xml
@@ -11,55 +11,107 @@
     android:layout_marginRight="@dimen/search_margin_horizontal"
     android:paddingLeft="@dimen/search_box_left_padding"
     android:paddingRight="@dimen/search_box_right_padding"
-    android:background="@drawable/rounded_corner_bg"
-    android:gravity="center_vertical"
+    android:background="@drawable/rounded_corner"
     android:elevation="@dimen/search_box_elevation"
     >
-    <ImageView
-        android:id="@+id/search_magnifying_glass"
-        android:layout_height="@dimen/search_box_icon_size"
-        android:layout_width="@dimen/search_box_icon_size"
-        android:layout_margin="@dimen/search_box_icon_margin"
-        android:padding="@dimen/search_box_search_icon_padding"
-        android:src="@drawable/ic_ab_search" />
-    <EditText
-        android:id="@+id/search_view"
-        android:layout_width="0dp"
-        android:layout_height="@dimen/search_box_icon_size"
-        android:layout_weight="1"
-        android:layout_marginLeft="@dimen/search_box_text_left_margin"
-        android:textSize="@dimen/search_text_size"
-        android:fontFamily="@string/search_font_family"
-        android:textColor="@color/searchbox_text_color"
-        android:textColorHint="@color/searchbox_hint_text_color"
-        android:hint="@string/dialer_hint_find_contact"
-        android:longClickable="false"
-        android:inputType="textFilter"/>
-    <ImageView
-        android:id="@+id/search_close_button"
-        android:layout_height="@dimen/search_box_icon_size"
-        android:layout_width="@dimen/search_box_icon_size"
-        android:layout_margin="@dimen/search_box_icon_margin"
-        android:src="@drawable/ic_close_dk"
-        android:clickable="true"
-        android:background="?android:attr/selectableItemBackground"
-        android:contentDescription="@string/description_clear_search"
-        android:visibility="gone" />
-    <ImageView
-        android:id="@+id/voice_search_button"
-        android:layout_height="@dimen/search_box_icon_size"
-        android:layout_width="@dimen/search_box_icon_size"
-        android:layout_margin="@dimen/search_box_icon_margin"
-        android:src="@drawable/ic_voice_search"
-        android:clickable="true"
-        android:contentDescription="@string/description_start_voice_search"
-        android:background="?android:attr/selectableItemBackground" />
-    <ImageButton
-        android:id="@+id/dialtacts_options_menu_button"
-        android:layout_width="@dimen/search_box_icon_size"
-        android:layout_height="@dimen/search_box_icon_size"
-        android:layout_margin="@dimen/search_box_icon_margin"
-        android:background="?android:attr/selectableItemBackground"
-        android:src="@drawable/ic_overflow_menu"
-        android:tint="@color/searchbox_options_menu_color" />
-</view>
+    <LinearLayout
+        android:id="@+id/search_box_collapsed"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:gravity="center_vertical"
+        android:orientation="horizontal" >
+
+        <!-- Portion of the search box that starts a search when clicked -->
+        <LinearLayout
+            android:id="@+id/search_box_start_search"
+            android:layout_height="wrap_content"
+            android:layout_width="0dp"
+            android:layout_weight="1"
+            android:gravity="center_vertical" >
+
+            <ImageView
+                android:id="@+id/search_magnifying_glass"
+                android:layout_height="@dimen/search_box_icon_size"
+                android:layout_width="@dimen/search_box_icon_size"
+                android:layout_margin="@dimen/search_box_icon_margin"
+                android:padding="@dimen/search_box_search_icon_padding"
+                android:src="@drawable/ic_ab_search"
+                android:importantForAccessibility="no" />
+
+            <TextView
+                android:layout_width="wrap_content"
+                android:layout_height="@dimen/search_box_icon_size"
+                android:layout_marginLeft="@dimen/search_box_text_left_margin"
+                android:textSize="@dimen/search_text_size"
+                android:fontFamily="@string/search_font_family"
+                android:textColor="@color/searchbox_text_color"
+                android:textColorHint="@color/searchbox_hint_text_color"
+                android:gravity="center_vertical"
+                android:hint="@string/dialer_hint_find_contact" />
+
+        </LinearLayout>
+
+        <ImageView
+            android:id="@+id/voice_search_button"
+            android:layout_height="@dimen/search_box_icon_size"
+            android:layout_width="@dimen/search_box_icon_size"
+            android:layout_margin="@dimen/search_box_icon_margin"
+            android:src="@drawable/ic_voice_search"
+            android:clickable="true"
+            android:contentDescription="@string/description_start_voice_search"
+            android:background="?android:attr/selectableItemBackground" />
+
+        <ImageButton
+            android:id="@+id/dialtacts_options_menu_button"
+            android:layout_width="@dimen/search_box_icon_size"
+            android:layout_height="@dimen/search_box_icon_size"
+            android:layout_margin="@dimen/search_box_icon_margin"
+            android:background="?android:attr/selectableItemBackground"
+            android:src="@drawable/ic_overflow_menu"
+            android:contentDescription="@string/action_menu_overflow_description"
+            android:tint="@color/searchbox_options_menu_color" />
+
+    </LinearLayout>
+
+    <LinearLayout
+        android:id="@+id/search_box_expanded"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:orientation="horizontal"
+        android:gravity="center_vertical"
+        android:visibility="gone" >
+
+        <ImageButton
+            android:id="@+id/search_back_button"
+            android:layout_width="@dimen/search_box_icon_size"
+            android:layout_height="@dimen/search_box_icon_size"
+            android:layout_margin="@dimen/search_box_icon_margin"
+            android:src="@drawable/ic_back_arrow"
+            android:background="?android:attr/selectableItemBackground"
+            android:contentDescription="@string/action_menu_overflow_description"
+            android:tint="@color/actionbar_background_color" />
+
+        <EditText
+            android:id="@+id/search_view"
+            android:layout_width="0dp"
+            android:layout_height="@dimen/search_box_icon_size"
+            android:layout_weight="1"
+            android:layout_marginLeft="@dimen/search_box_text_left_margin"
+            android:textSize="@dimen/search_text_size"
+            android:fontFamily="@string/search_font_family"
+            android:textColor="@color/searchbox_text_color"
+            android:inputType="textFilter" />
+
+        <ImageView
+            android:id="@+id/search_close_button"
+            android:layout_height="@dimen/search_box_icon_size"
+            android:layout_width="@dimen/search_box_icon_size"
+            android:layout_margin="@dimen/search_box_icon_margin"
+            android:src="@drawable/ic_close_dk"
+            android:clickable="true"
+            android:background="?android:attr/selectableItemBackground"
+            android:contentDescription="@string/description_clear_search" />
+
+    </LinearLayout>
+
+</view>
\ No newline at end of file
diff --git a/src/com/android/dialer/DialtactsActivity.java b/src/com/android/dialer/DialtactsActivity.java
index 3b68db9..c164294 100644
--- a/src/com/android/dialer/DialtactsActivity.java
+++ b/src/com/android/dialer/DialtactsActivity.java
@@ -48,6 +48,7 @@
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.View.OnDragListener;
+import android.view.View.OnTouchListener;
 import android.view.animation.AccelerateInterpolator;
 import android.view.animation.Animation;
 import android.view.animation.Animation.AnimationListener;
@@ -85,6 +86,7 @@
 import com.android.dialer.list.SearchFragment;
 import com.android.dialer.list.SmartDialSearchFragment;
 import com.android.dialer.widget.SearchEditTextLayout;
+import com.android.dialer.widget.SearchEditTextLayout.OnBackButtonClickedListener;
 import com.android.dialerbind.DatabaseHelperManager;
 import com.android.internal.telephony.ITelephony;
 
@@ -190,9 +192,8 @@
     private String mPendingSearchViewQuery;
 
     private EditText mSearchView;
-    private View mSearchIcon;
-    private View mSearchViewCloseButton;
     private View mVoiceSearchButton;
+    private SearchEditTextLayout mSearchEditTextLayout;
 
     /**
      * View that contains the "Remove" dialog that shows up when the user long presses a contact.
@@ -277,14 +278,6 @@
             } else if (mRegularSearchFragment != null) {
                 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
             }
-
-            if (TextUtils.isEmpty(newText)) {
-                mSearchViewCloseButton.setVisibility(View.GONE);
-                mVoiceSearchButton.setVisibility(View.VISIBLE);
-            } else {
-                mSearchViewCloseButton.setVisibility(View.VISIBLE);
-                mVoiceSearchButton.setVisibility(View.GONE);
-            }
         }
 
         @Override
@@ -294,15 +287,14 @@
 
 
     /**
-     * Open the search UI when the user touches the search text view.
+     * Open the search UI when the user clicks on the search box.
      */
-    private final View.OnTouchListener mSearchViewOnTouchListener = new View.OnTouchListener() {
+    private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
         @Override
-        public boolean onTouch(View v, MotionEvent event) {
+        public void onClick(View v) {
             if (!isInSearchUi()) {
                 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString());
             }
-            return false;
         }
     };
 
@@ -314,7 +306,7 @@
         public boolean onKey(View v, int keyCode, KeyEvent event) {
             if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN &&
                     TextUtils.isEmpty(mSearchView.getText().toString())) {
-                onBackPressed();
+                maybeExitSearchUi();
             }
             return false;
         }
@@ -332,21 +324,23 @@
         actionBar.setCustomView(R.layout.search_edittext);
         actionBar.setDisplayShowCustomEnabled(true);
 
-        SearchEditTextLayout actionBarView = (SearchEditTextLayout) actionBar.getCustomView();
-        actionBarView.setPreImeKeyListener(mSearchEditTextLayoutListener);
+        mSearchEditTextLayout = (SearchEditTextLayout) actionBar.getCustomView();
+        mSearchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
 
-        mSearchIcon = actionBarView.findViewById(R.id.search_magnifying_glass);
-        mVoiceSearchButton = actionBarView.findViewById(R.id.voice_search_button);
-
-        mSearchView = (EditText) actionBarView.findViewById(R.id.search_view);
+        mSearchView = (EditText) mSearchEditTextLayout.findViewById(R.id.search_view);
         mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
-        mSearchView.setOnTouchListener(mSearchViewOnTouchListener);
+        mVoiceSearchButton = mSearchEditTextLayout.findViewById(R.id.voice_search_button);
+        mSearchEditTextLayout.findViewById(R.id.search_box_start_search).setOnClickListener(
+                mSearchViewOnClickListener);
+        mSearchEditTextLayout.setOnBackButtonClickedListener(new OnBackButtonClickedListener() {
+            @Override
+            public void onBackButtonClicked() {
+                onBackPressed();
+            }
+        });
 
-        mSearchViewCloseButton = actionBarView.findViewById(R.id.search_close_button);
-        mSearchViewCloseButton.setOnClickListener(this);
-
-        ImageButton optionsMenuButton =
-                (ImageButton) actionBarView.findViewById(R.id.dialtacts_options_menu_button);
+        ImageButton optionsMenuButton = (ImageButton) mSearchEditTextLayout.findViewById(
+                R.id.dialtacts_options_menu_button);
         optionsMenuButton.setOnClickListener(this);
         final OptionsPopupMenu optionsMenu = buildOptionsMenu(optionsMenuButton);
         optionsMenuButton.setOnTouchListener(optionsMenu.getDragToOpenListener());
@@ -374,6 +368,8 @@
         parentLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
         parentLayout.setOnDragListener(new LayoutOnDragListener());
 
+        setupActivityOverlay();
+
         mFloatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
         ViewUtil.setupFloatingActionButton(mFloatingActionButtonContainer, getResources());
 
@@ -386,6 +382,19 @@
         SmartDialPrefix.initializeNanpSettings(this);
     }
 
+    private void setupActivityOverlay() {
+        final View activityOverlay = findViewById(R.id.activity_overlay);
+        activityOverlay.setOnTouchListener(new OnTouchListener() {
+            @Override
+            public boolean onTouch(View v, MotionEvent event) {
+                if (!mIsDialpadShown) {
+                    maybeExitSearchUi();
+                }
+                return false;
+            }
+        });
+    }
+
     @Override
     protected void onResume() {
         super.onResume();
@@ -608,8 +617,11 @@
         }
 
         mListsFragment.maybeShowActionBar();
+
         if (isInSearchUi()) {
-            mSearchIcon.setVisibility(View.GONE);
+            if (TextUtils.isEmpty(mSearchQuery)) {
+                exitSearchUi();
+            }
         }
     }
 
@@ -816,10 +828,7 @@
         transaction.commit();
 
         mListsFragment.getView().animate().alpha(0).withLayer();
-
-        if (!mIsDialpadShown) {
-            mSearchIcon.setVisibility(View.GONE);
-        }
+        mSearchEditTextLayout.animateExpandOrCollapse(true);
     }
 
     /**
@@ -837,7 +846,6 @@
 
         final FragmentTransaction transaction = getFragmentManager().beginTransaction();
         transaction.setCustomAnimations(0, android.R.animator.fade_out);
-
         if (mSmartDialSearchFragment != null) {
             transaction.remove(mSmartDialSearchFragment);
         }
@@ -847,7 +855,7 @@
         transaction.commit();
 
         mListsFragment.getView().animate().alpha(1).withLayer();
-        mSearchIcon.setVisibility(View.VISIBLE);
+        mSearchEditTextLayout.animateExpandOrCollapse(false);
     }
 
     /** Returns an Intent to launch Call Settings screen */
@@ -867,11 +875,24 @@
             hideDialpadFragment(true, false);
         } else if (isInSearchUi()) {
             exitSearchUi();
+            hideInputMethod(parentLayout);
         } else {
             super.onBackPressed();
         }
     }
 
+    /**
+     * @return True if the search UI was exited, false otherwise
+     */
+    private boolean maybeExitSearchUi() {
+        if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
+            exitSearchUi();
+            hideInputMethod(parentLayout);
+            return true;
+        }
+        return false;
+    }
+
     @Override
     public void onDialpadQueryChanged(String query) {
         if (mSmartDialSearchFragment != null) {
diff --git a/src/com/android/dialer/widget/SearchEditTextLayout.java b/src/com/android/dialer/widget/SearchEditTextLayout.java
index 40a4e43..33bf09f 100644
--- a/src/com/android/dialer/widget/SearchEditTextLayout.java
+++ b/src/com/android/dialer/widget/SearchEditTextLayout.java
@@ -16,23 +16,97 @@
 
 package com.android.dialer.widget;
 
-
+import android.animation.ValueAnimator;
+import android.animation.ValueAnimator.AnimatorUpdateListener;
 import android.content.Context;
 import android.util.AttributeSet;
+import android.util.Log;
 import android.view.KeyEvent;
-import android.widget.LinearLayout;
+import android.view.View;
+import android.view.inputmethod.InputMethodManager;
+import android.widget.EditText;
+import android.widget.FrameLayout;
 
-public class SearchEditTextLayout extends LinearLayout {
+import com.android.contacts.common.animation.AnimationUtils;
+import com.android.dialer.R;
+
+public class SearchEditTextLayout extends FrameLayout {
+    private static final int ANIMATION_DURATION = 200;
+
     private OnKeyListener mPreImeKeyListener;
+    private int mTopMargin;
+    private int mBottomMargin;
+    private int mLeftMargin;
+    private int mRightMargin;
+
+    private int mBackgroundColor;
+
+    private View mCollapsed;
+    private View mExpanded;
+    private EditText mSearchView;
+
+    private OnBackButtonClickedListener mOnBackButtonClickedListener;
+
+    /**
+     * Listener for the back button next to the search view being pressed
+     */
+    public interface OnBackButtonClickedListener {
+        public void onBackButtonClicked();
+    }
 
     public SearchEditTextLayout(Context context, AttributeSet attrs) {
         super(context, attrs);
+        mBackgroundColor = getResources().getColor(R.color.searchbox_background_color);
     }
 
     public void setPreImeKeyListener(OnKeyListener listener) {
         mPreImeKeyListener = listener;
     }
 
+    public void setOnBackButtonClickedListener(OnBackButtonClickedListener listener) {
+        mOnBackButtonClickedListener = listener;
+    }
+
+    @Override
+    protected void onFinishInflate() {
+        MarginLayoutParams params = (MarginLayoutParams) getLayoutParams();
+        mTopMargin = params.topMargin;
+        mBottomMargin = params.bottomMargin;
+        mLeftMargin = params.leftMargin;
+        mRightMargin = params.rightMargin;
+
+        mCollapsed = findViewById(R.id.search_box_collapsed);
+        mExpanded = findViewById(R.id.search_box_expanded);
+        mSearchView = (EditText) mExpanded.findViewById(R.id.search_view);
+
+        mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
+            @Override
+            public void onFocusChange(View v, boolean hasFocus) {
+                if (hasFocus) {
+                    showInputMethod(v);
+                }
+            }
+        });
+
+        findViewById(R.id.search_close_button).setOnClickListener(new OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                mSearchView.setText(null);
+            }
+        });
+
+        findViewById(R.id.search_back_button).setOnClickListener(new OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                if (mOnBackButtonClickedListener != null) {
+                    mOnBackButtonClickedListener.onBackButtonClicked();
+                }
+            }
+        });
+
+        super.onFinishInflate();
+    }
+
     @Override
     public boolean dispatchKeyEventPreIme(KeyEvent event) {
         if (mPreImeKeyListener != null) {
@@ -42,4 +116,40 @@
         }
         return super.dispatchKeyEventPreIme(event);
     }
+
+    public void animateExpandOrCollapse(boolean expand) {
+        final ValueAnimator animator;
+        if (expand) {
+            AnimationUtils.crossFadeViews(mExpanded, mCollapsed, ANIMATION_DURATION);
+            animator = ValueAnimator.ofFloat(1f, 0f);
+            setBackgroundColor(mBackgroundColor);
+            mSearchView.requestFocus();
+        } else {
+            AnimationUtils.crossFadeViews(mCollapsed, mExpanded, ANIMATION_DURATION);
+            animator = ValueAnimator.ofFloat(0f, 1f);
+            setBackgroundResource(R.drawable.rounded_corner);
+        }
+        animator.addUpdateListener(new AnimatorUpdateListener() {
+            @Override
+            public void onAnimationUpdate(ValueAnimator animation) {
+                final Float fraction = (Float) animation.getAnimatedValue();
+                MarginLayoutParams params = (MarginLayoutParams) getLayoutParams();
+                params.topMargin = (int) (mTopMargin * fraction);
+                params.bottomMargin = (int) (mBottomMargin * fraction);
+                params.leftMargin = (int) (mLeftMargin * fraction);
+                params.rightMargin = (int) (mRightMargin * fraction);
+                requestLayout();
+            }
+        });
+        animator.setDuration(ANIMATION_DURATION);
+        animator.start();
+    }
+
+    private void showInputMethod(View view) {
+        final InputMethodManager imm = (InputMethodManager) getContext().getSystemService(
+                Context.INPUT_METHOD_SERVICE);
+        if (imm != null) {
+            imm.showSoftInput(view, 0);
+        }
+    }
 }
\ No newline at end of file