Make searchbox expand when entering search UI

Bug: 14900155
Change-Id: Ib6e500ed55db90ad1fa13ba6dd0e141721331ba4
diff --git a/src/com/android/dialer/DialtactsActivity.java b/src/com/android/dialer/DialtactsActivity.java
index 3b68db9..4149f39 100644
--- a/src/com/android/dialer/DialtactsActivity.java
+++ b/src/com/android/dialer/DialtactsActivity.java
@@ -193,6 +193,7 @@
     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.
@@ -332,21 +333,21 @@
         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);
+        mSearchIcon = mSearchEditTextLayout.findViewById(R.id.search_magnifying_glass);
+        mVoiceSearchButton = mSearchEditTextLayout.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);
 
-        mSearchViewCloseButton = actionBarView.findViewById(R.id.search_close_button);
+        mSearchViewCloseButton = mSearchEditTextLayout.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());
@@ -816,6 +817,7 @@
         transaction.commit();
 
         mListsFragment.getView().animate().alpha(0).withLayer();
+        mSearchEditTextLayout.animateExpandOrCollapse(true);
 
         if (!mIsDialpadShown) {
             mSearchIcon.setVisibility(View.GONE);
@@ -847,6 +849,7 @@
         transaction.commit();
 
         mListsFragment.getView().animate().alpha(1).withLayer();
+        mSearchEditTextLayout.animateExpandOrCollapse(false);
         mSearchIcon.setVisibility(View.VISIBLE);
     }
 
diff --git a/src/com/android/dialer/widget/SearchEditTextLayout.java b/src/com/android/dialer/widget/SearchEditTextLayout.java
index 40a4e43..e2cbcb3 100644
--- a/src/com/android/dialer/widget/SearchEditTextLayout.java
+++ b/src/com/android/dialer/widget/SearchEditTextLayout.java
@@ -16,17 +16,27 @@
 
 package com.android.dialer.widget;
 
-
+import android.animation.ValueAnimator;
+import android.animation.ValueAnimator.AnimatorUpdateListener;
 import android.content.Context;
 import android.util.AttributeSet;
 import android.view.KeyEvent;
 import android.widget.LinearLayout;
 
+import com.android.dialer.R;
+
 public class SearchEditTextLayout extends LinearLayout {
     private OnKeyListener mPreImeKeyListener;
+    private int mTopMargin;
+    private int mBottomMargin;
+    private int mLeftMargin;
+    private int mRightMargin;
+
+    private int mBackgroundColor;
 
     public SearchEditTextLayout(Context context, AttributeSet attrs) {
         super(context, attrs);
+        mBackgroundColor = getResources().getColor(R.color.searchbox_background_color);
     }
 
     public void setPreImeKeyListener(OnKeyListener listener) {
@@ -34,6 +44,16 @@
     }
 
     @Override
+    protected void onFinishInflate() {
+        MarginLayoutParams params = (MarginLayoutParams) getLayoutParams();
+        mTopMargin = params.topMargin;
+        mBottomMargin = params.bottomMargin;
+        mLeftMargin = params.leftMargin;
+        mRightMargin = params.rightMargin;
+        super.onFinishInflate();
+    }
+
+    @Override
     public boolean dispatchKeyEventPreIme(KeyEvent event) {
         if (mPreImeKeyListener != null) {
             if (mPreImeKeyListener.onKey(this, event.getKeyCode(), event)) {
@@ -42,4 +62,28 @@
         }
         return super.dispatchKeyEventPreIme(event);
     }
+
+    public void animateExpandOrCollapse(boolean expand) {
+        final ValueAnimator animator;
+        if (expand) {
+            animator = ValueAnimator.ofFloat(1f, 0f);
+            setBackgroundColor(mBackgroundColor);
+        } else {
+            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.start();
+    }
 }
\ No newline at end of file