Swap out search box contents when expanding

* Replace search box layout with two main child views - one for the
collapsed state and one for the expanded state.

* Add back button to the expanded state

* Replaced OnTouchListener on the searchbox with an OnClickedListener
Bug: 14900155
Change-Id: I4f07650ddf7e265ee5c4d1054c0bf0ff7494ab6b
diff --git a/src/com/android/dialer/DialtactsActivity.java b/src/com/android/dialer/DialtactsActivity.java
index 39f611e..d898600 100644
--- a/src/com/android/dialer/DialtactsActivity.java
+++ b/src/com/android/dialer/DialtactsActivity.java
@@ -85,6 +85,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,8 +191,6 @@
     private String mPendingSearchViewQuery;
 
     private EditText mSearchView;
-    private View mSearchIcon;
-    private View mSearchViewCloseButton;
     private View mVoiceSearchButton;
     private SearchEditTextLayout mSearchEditTextLayout;
 
@@ -278,14 +277,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
@@ -295,15 +286,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;
         }
     };
 
@@ -336,15 +326,17 @@
         mSearchEditTextLayout = (SearchEditTextLayout) actionBar.getCustomView();
         mSearchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
 
-        mSearchIcon = mSearchEditTextLayout.findViewById(R.id.search_magnifying_glass);
-        mVoiceSearchButton = mSearchEditTextLayout.findViewById(R.id.voice_search_button);
-
         mSearchView = (EditText) mSearchEditTextLayout.findViewById(R.id.search_view);
         mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
-        mSearchView.setOnTouchListener(mSearchViewOnTouchListener);
-
-        mSearchViewCloseButton = mSearchEditTextLayout.findViewById(R.id.search_close_button);
-        mSearchViewCloseButton.setOnClickListener(this);
+        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();
+            }
+        });
 
         ImageButton optionsMenuButton = (ImageButton) mSearchEditTextLayout.findViewById(
                 R.id.dialtacts_options_menu_button);
@@ -609,11 +601,10 @@
         }
 
         mListsFragment.maybeShowActionBar();
+
         if (isInSearchUi()) {
             if (TextUtils.isEmpty(mSearchQuery)) {
                 exitSearchUi();
-            } else {
-                mSearchIcon.setVisibility(View.GONE);
             }
         }
     }
@@ -822,10 +813,6 @@
 
         mListsFragment.getView().animate().alpha(0).withLayer();
         mSearchEditTextLayout.animateExpandOrCollapse(true);
-
-        if (!mIsDialpadShown) {
-            mSearchIcon.setVisibility(View.GONE);
-        }
     }
 
     /**
@@ -843,7 +830,6 @@
 
         final FragmentTransaction transaction = getFragmentManager().beginTransaction();
         transaction.setCustomAnimations(0, android.R.animator.fade_out);
-
         if (mSmartDialSearchFragment != null) {
             transaction.remove(mSmartDialSearchFragment);
         }
@@ -854,7 +840,6 @@
 
         mListsFragment.getView().animate().alpha(1).withLayer();
         mSearchEditTextLayout.animateExpandOrCollapse(false);
-        mSearchIcon.setVisibility(View.VISIBLE);
     }
 
     /** Returns an Intent to launch Call Settings screen */
@@ -874,6 +859,7 @@
             hideDialpadFragment(true, false);
         } else if (isInSearchUi()) {
             exitSearchUi();
+            hideInputMethod(parentLayout);
         } else {
             super.onBackPressed();
         }
diff --git a/src/com/android/dialer/widget/SearchEditTextLayout.java b/src/com/android/dialer/widget/SearchEditTextLayout.java
index e2cbcb3..33bf09f 100644
--- a/src/com/android/dialer/widget/SearchEditTextLayout.java
+++ b/src/com/android/dialer/widget/SearchEditTextLayout.java
@@ -20,12 +20,19 @@
 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;
 
+import com.android.contacts.common.animation.AnimationUtils;
 import com.android.dialer.R;
 
-public class SearchEditTextLayout extends LinearLayout {
+public class SearchEditTextLayout extends FrameLayout {
+    private static final int ANIMATION_DURATION = 200;
+
     private OnKeyListener mPreImeKeyListener;
     private int mTopMargin;
     private int mBottomMargin;
@@ -34,6 +41,19 @@
 
     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);
@@ -43,6 +63,10 @@
         mPreImeKeyListener = listener;
     }
 
+    public void setOnBackButtonClickedListener(OnBackButtonClickedListener listener) {
+        mOnBackButtonClickedListener = listener;
+    }
+
     @Override
     protected void onFinishInflate() {
         MarginLayoutParams params = (MarginLayoutParams) getLayoutParams();
@@ -50,6 +74,36 @@
         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();
     }
 
@@ -66,9 +120,12 @@
     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);
         }
@@ -84,6 +141,15 @@
                 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