1b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee/*
2b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee * Copyright (C) 2014 The Android Open Source Project
3b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee *
4b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee * Licensed under the Apache License, Version 2.0 (the "License");
5b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee * you may not use this file except in compliance with the License.
6b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee * You may obtain a copy of the License at
7b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee *
8b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee *      http://www.apache.org/licenses/LICENSE-2.0
9b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee *
10b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee * Unless required by applicable law or agreed to in writing, software
11b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee * distributed under the License is distributed on an "AS IS" BASIS,
12b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee * See the License for the specific language governing permissions and
14b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee * limitations under the License
15b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee */
16b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee
17b19038436ce8e4683eb19af02255eda9969a6a94Andrew Leepackage com.android.dialer.widget;
18b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee
19ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Leeimport android.animation.ValueAnimator;
20ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Leeimport android.animation.ValueAnimator.AnimatorUpdateListener;
21b19038436ce8e4683eb19af02255eda9969a6a94Andrew Leeimport android.content.Context;
22b19038436ce8e4683eb19af02255eda9969a6a94Andrew Leeimport android.util.AttributeSet;
23b19038436ce8e4683eb19af02255eda9969a6a94Andrew Leeimport android.view.KeyEvent;
2411ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Leeimport android.view.View;
2511ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Leeimport android.widget.EditText;
2611ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Leeimport android.widget.FrameLayout;
27b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee
28ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Leeimport com.android.dialer.R;
29f543baadd1119bad7cd9ff7ed39e5598c6fc50d6Yorke Leeimport com.android.dialer.util.DialerUtils;
30fd723cae1dc2de0f83ec14639d4431674dd3b82eSai Cheemalapatiimport com.android.phone.common.animation.AnimUtils;
31ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee
3211ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Leepublic class SearchEditTextLayout extends FrameLayout {
3305b6267428450f190d04af187a206cc495da8995Andrew Lee    private static final float EXPAND_MARGIN_FRACTION_START = 0.8f;
3411ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee    private static final int ANIMATION_DURATION = 200;
3511ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee
36b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee    private OnKeyListener mPreImeKeyListener;
37ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee    private int mTopMargin;
38ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee    private int mBottomMargin;
39ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee    private int mLeftMargin;
40ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee    private int mRightMargin;
41ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee
42c7aaa28a4400fc9c8b24bb7437f9b23d29155669Yorke Lee    private float mCollapsedElevation;
43c7aaa28a4400fc9c8b24bb7437f9b23d29155669Yorke Lee
445253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    /* Subclass-visible for testing */
455253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    protected boolean mIsExpanded = false;
465253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    protected boolean mIsFadedOut = false;
47b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee
4811ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee    private View mCollapsed;
4911ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee    private View mExpanded;
5011ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee    private EditText mSearchView;
51bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee    private View mSearchIcon;
5205b6267428450f190d04af187a206cc495da8995Andrew Lee    private View mCollapsedSearchBox;
5305b6267428450f190d04af187a206cc495da8995Andrew Lee    private View mVoiceSearchButtonView;
5405b6267428450f190d04af187a206cc495da8995Andrew Lee    private View mOverflowButtonView;
5505b6267428450f190d04af187a206cc495da8995Andrew Lee    private View mBackButtonView;
56bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee    private View mExpandedSearchBox;
5705b6267428450f190d04af187a206cc495da8995Andrew Lee    private View mClearButtonView;
5811ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee
595253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    private ValueAnimator mAnimator;
605253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee
6111ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee    private OnBackButtonClickedListener mOnBackButtonClickedListener;
6211ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee
6311ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee    /**
6411ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee     * Listener for the back button next to the search view being pressed
6511ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee     */
6611ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee    public interface OnBackButtonClickedListener {
6711ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee        public void onBackButtonClicked();
6811ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee    }
6911ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee
70b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee    public SearchEditTextLayout(Context context, AttributeSet attrs) {
71b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee        super(context, attrs);
72b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee    }
73b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee
74b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee    public void setPreImeKeyListener(OnKeyListener listener) {
75b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee        mPreImeKeyListener = listener;
76b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee    }
77b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee
7811ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee    public void setOnBackButtonClickedListener(OnBackButtonClickedListener listener) {
7911ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee        mOnBackButtonClickedListener = listener;
8011ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee    }
8111ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee
82b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee    @Override
83ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee    protected void onFinishInflate() {
84ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee        MarginLayoutParams params = (MarginLayoutParams) getLayoutParams();
85ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee        mTopMargin = params.topMargin;
86ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee        mBottomMargin = params.bottomMargin;
87ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee        mLeftMargin = params.leftMargin;
88ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee        mRightMargin = params.rightMargin;
8911ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee
90c7aaa28a4400fc9c8b24bb7437f9b23d29155669Yorke Lee        mCollapsedElevation = getElevation();
91c7aaa28a4400fc9c8b24bb7437f9b23d29155669Yorke Lee
9211ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee        mCollapsed = findViewById(R.id.search_box_collapsed);
9311ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee        mExpanded = findViewById(R.id.search_box_expanded);
9411ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee        mSearchView = (EditText) mExpanded.findViewById(R.id.search_view);
9511ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee
96bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee        mSearchIcon = findViewById(R.id.search_magnifying_glass);
9705b6267428450f190d04af187a206cc495da8995Andrew Lee        mCollapsedSearchBox = findViewById(R.id.search_box_start_search);
9805b6267428450f190d04af187a206cc495da8995Andrew Lee        mVoiceSearchButtonView = findViewById(R.id.voice_search_button);
9905b6267428450f190d04af187a206cc495da8995Andrew Lee        mOverflowButtonView = findViewById(R.id.dialtacts_options_menu_button);
10005b6267428450f190d04af187a206cc495da8995Andrew Lee        mBackButtonView = findViewById(R.id.search_back_button);
101bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee        mExpandedSearchBox = findViewById(R.id.search_box_expanded);
10205b6267428450f190d04af187a206cc495da8995Andrew Lee        mClearButtonView = findViewById(R.id.search_close_button);
10305b6267428450f190d04af187a206cc495da8995Andrew Lee
10411ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee        mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
10511ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee            @Override
10611ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee            public void onFocusChange(View v, boolean hasFocus) {
10711ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee                if (hasFocus) {
108f543baadd1119bad7cd9ff7ed39e5598c6fc50d6Yorke Lee                    DialerUtils.showInputMethod(v);
109f543baadd1119bad7cd9ff7ed39e5598c6fc50d6Yorke Lee                } else {
110f543baadd1119bad7cd9ff7ed39e5598c6fc50d6Yorke Lee                    DialerUtils.hideInputMethod(v);
11111ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee                }
11211ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee            }
11311ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee        });
11411ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee
11511ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee        findViewById(R.id.search_close_button).setOnClickListener(new OnClickListener() {
11611ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee            @Override
11711ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee            public void onClick(View v) {
11811ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee                mSearchView.setText(null);
11911ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee            }
12011ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee        });
12111ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee
12211ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee        findViewById(R.id.search_back_button).setOnClickListener(new OnClickListener() {
12311ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee            @Override
12411ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee            public void onClick(View v) {
12511ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee                if (mOnBackButtonClickedListener != null) {
12611ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee                    mOnBackButtonClickedListener.onBackButtonClicked();
12711ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee                }
12811ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee            }
12911ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee        });
13011ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee
131ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee        super.onFinishInflate();
132ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee    }
133ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee
134ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee    @Override
135b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee    public boolean dispatchKeyEventPreIme(KeyEvent event) {
136b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee        if (mPreImeKeyListener != null) {
137b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee            if (mPreImeKeyListener.onKey(this, event.getKeyCode(), event)) {
138b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee                return true;
139b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee            }
140b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee        }
141b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee        return super.dispatchKeyEventPreIme(event);
142b19038436ce8e4683eb19af02255eda9969a6a94Andrew Lee    }
143ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee
1445253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    public void fadeOut() {
145710709da8389d401d3089bbf9836dfca527f15c3Yorke Lee        fadeOut(null);
146710709da8389d401d3089bbf9836dfca527f15c3Yorke Lee    }
147710709da8389d401d3089bbf9836dfca527f15c3Yorke Lee
148710709da8389d401d3089bbf9836dfca527f15c3Yorke Lee    public void fadeOut(AnimUtils.AnimationCallback callback) {
149710709da8389d401d3089bbf9836dfca527f15c3Yorke Lee        AnimUtils.fadeOut(this, ANIMATION_DURATION, callback);
1505253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        mIsFadedOut = true;
1515253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    }
1525253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee
1535253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    public void fadeIn() {
1545253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        AnimUtils.fadeIn(this, ANIMATION_DURATION);
1555253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        mIsFadedOut = false;
1565253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    }
1575253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee
1585253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    public void setVisible(boolean visible) {
1595253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        if (visible) {
1605253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            setAlpha(1);
1615253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            setVisibility(View.VISIBLE);
1625253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            mIsFadedOut = false;
1635253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        } else {
1645253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            setAlpha(0);
1655253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            setVisibility(View.GONE);
1665253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            mIsFadedOut = true;
1675253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        }
1685253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    }
1695253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    public void expand(boolean animate, boolean requestFocus) {
170bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee        updateVisibility(true /* isExpand */);
17105b6267428450f190d04af187a206cc495da8995Andrew Lee
1725253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        if (animate) {
1735253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            AnimUtils.crossFadeViews(mExpanded, mCollapsed, ANIMATION_DURATION);
17405b6267428450f190d04af187a206cc495da8995Andrew Lee            mAnimator = ValueAnimator.ofFloat(EXPAND_MARGIN_FRACTION_START, 0f);
17505b6267428450f190d04af187a206cc495da8995Andrew Lee            setMargins(EXPAND_MARGIN_FRACTION_START);
1765253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            prepareAnimator(true);
1775253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        } else {
1785253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            mExpanded.setVisibility(View.VISIBLE);
1795253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            mExpanded.setAlpha(1);
1805253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            setMargins(0f);
1815253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            mCollapsed.setVisibility(View.GONE);
1825253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        }
1835253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee
184bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee        // Set 9-patch background. This owns the padding, so we need to restore the original values.
185bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee        int paddingTop = this.getPaddingTop();
186bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee        int paddingStart = this.getPaddingStart();
187bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee        int paddingBottom = this.getPaddingBottom();
188bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee        int paddingEnd = this.getPaddingEnd();
1895253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        setBackgroundResource(R.drawable.search_shadow);
190c7aaa28a4400fc9c8b24bb7437f9b23d29155669Yorke Lee        setElevation(0);
191bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee        setPaddingRelative(paddingStart, paddingTop, paddingEnd, paddingBottom);
192bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee
193bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee        setElevation(0);
1945253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        if (requestFocus) {
19511ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee            mSearchView.requestFocus();
1965253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        }
1975253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        mIsExpanded = true;
1985253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    }
1995253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee
2005253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    public void collapse(boolean animate) {
201bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee        updateVisibility(false /* isExpand */);
20205b6267428450f190d04af187a206cc495da8995Andrew Lee
2035253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        if (animate) {
2045253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            AnimUtils.crossFadeViews(mCollapsed, mExpanded, ANIMATION_DURATION);
2055253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            mAnimator = ValueAnimator.ofFloat(0f, 1f);
2065253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            prepareAnimator(false);
207ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee        } else {
2085253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            mCollapsed.setVisibility(View.VISIBLE);
2095253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            mCollapsed.setAlpha(1);
2105253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            setMargins(1f);
2115253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            mExpanded.setVisibility(View.GONE);
212ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee        }
2135253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee
2145253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        mIsExpanded = false;
215c7aaa28a4400fc9c8b24bb7437f9b23d29155669Yorke Lee        setElevation(mCollapsedElevation);
2165253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        setBackgroundResource(R.drawable.rounded_corner);
2175253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    }
2185253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee
219bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee    /**
220bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee     * Updates the visibility of views depending on whether we will show the expanded or collapsed
221bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee     * search view. This helps prevent some jank with the crossfading if we are animating.
222bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee     *
223bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee     * @param isExpand Whether we are about to show the expanded search box.
224bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee     */
225bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee    private void updateVisibility(boolean isExpand) {
226bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee        int collapsedViewVisibility = isExpand ? View.GONE : View.VISIBLE;
227bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee        int expandedViewVisibility = isExpand ? View.VISIBLE : View.GONE;
228bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee
229bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee        mSearchIcon.setVisibility(collapsedViewVisibility);
230bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee        mCollapsedSearchBox.setVisibility(collapsedViewVisibility);
231bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee        mVoiceSearchButtonView.setVisibility(collapsedViewVisibility);
232bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee        mOverflowButtonView.setVisibility(collapsedViewVisibility);
233bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee        mBackButtonView.setVisibility(expandedViewVisibility);
2346841f192723c34d2680ac45a852c75aef251b3e4Sai Cheemalapati        // TODO: Prevents keyboard from jumping up in landscape mode after exiting the
2356841f192723c34d2680ac45a852c75aef251b3e4Sai Cheemalapati        // SearchFragment when the query string is empty. More elegant fix?
2366841f192723c34d2680ac45a852c75aef251b3e4Sai Cheemalapati        //mExpandedSearchBox.setVisibility(expandedViewVisibility);
237bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee        mClearButtonView.setVisibility(expandedViewVisibility);
238bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee    }
239bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee
2405253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    private void prepareAnimator(final boolean expand) {
2415253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        if (mAnimator != null) {
2425253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee            mAnimator.cancel();
2435253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        }
2445253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee
2455253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        mAnimator.addUpdateListener(new AnimatorUpdateListener() {
246ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee            @Override
247ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee            public void onAnimationUpdate(ValueAnimator animation) {
248ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee                final Float fraction = (Float) animation.getAnimatedValue();
2495253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee                setMargins(fraction);
250ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee            }
251ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee        });
2525253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee
2535253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        mAnimator.setDuration(ANIMATION_DURATION);
2545253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        mAnimator.start();
2555253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    }
2565253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee
2575253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    public boolean isExpanded() {
2585253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        return mIsExpanded;
2595253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    }
2605253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee
2615253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    public boolean isFadedOut() {
2625253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        return mIsFadedOut;
263ec489fb5d4bba57ccdfd24b48e26257294d29979Yorke Lee    }
26411ca39eae2bd8ed5c3e21e8c4fa09c9b2b6bda51Yorke Lee
2655253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    /**
2665253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee     * Assigns margins to the search box as a fraction of its maximum margin size
2675253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee     *
2685253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee     * @param fraction How large the margins should be as a fraction of their full size
2695253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee     */
2705253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    private void setMargins(float fraction) {
2715253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        MarginLayoutParams params = (MarginLayoutParams) getLayoutParams();
2725253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        params.topMargin = (int) (mTopMargin * fraction);
2735253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        params.bottomMargin = (int) (mBottomMargin * fraction);
2745253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        params.leftMargin = (int) (mLeftMargin * fraction);
2755253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        params.rightMargin = (int) (mRightMargin * fraction);
2765253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee        requestLayout();
2775253be0d57edd4cdf5fbc0a980188e13e009c083Yorke Lee    }
278bcbcd9c2db5035fa010f12c2a120fd4c35cae005Andrew Lee}
279