summaryrefslogtreecommitdiffstats
path: root/java/com/android/dialer/app/widget/SearchEditTextLayout.java
blob: be850f9a03a22bfafc7420573ed5b47dfe1ddc55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * 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
 */

package com.android.dialer.app.widget;

import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import com.android.dialer.animation.AnimUtils;
import com.android.dialer.app.R;
import com.android.dialer.util.DialerUtils;

public class SearchEditTextLayout extends FrameLayout {

  private static final float EXPAND_MARGIN_FRACTION_START = 0.8f;
  private static final int ANIMATION_DURATION = 200;
  /* Subclass-visible for testing */
  protected boolean mIsExpanded = false;
  protected boolean mIsFadedOut = false;
  private OnKeyListener mPreImeKeyListener;
  private int mTopMargin;
  private int mBottomMargin;
  private int mLeftMargin;
  private int mRightMargin;
  private float mCollapsedElevation;
  private View mCollapsed;
  private View mExpanded;
  private EditText mSearchView;
  private View mSearchIcon;
  private View mCollapsedSearchBox;
  private View mVoiceSearchButtonView;
  private View mOverflowButtonView;
  private View mBackButtonView;
  private View mExpandedSearchBox;
  private View mClearButtonView;

  private ValueAnimator mAnimator;

  private Callback mCallback;

  public SearchEditTextLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public void setPreImeKeyListener(OnKeyListener listener) {
    mPreImeKeyListener = listener;
  }

  public void setCallback(Callback listener) {
    mCallback = listener;
  }

  @Override
  protected void onFinishInflate() {
    MarginLayoutParams params = (MarginLayoutParams) getLayoutParams();
    mTopMargin = params.topMargin;
    mBottomMargin = params.bottomMargin;
    mLeftMargin = params.leftMargin;
    mRightMargin = params.rightMargin;

    mCollapsedElevation = getElevation();

    mCollapsed = findViewById(R.id.search_box_collapsed);
    mExpanded = findViewById(R.id.search_box_expanded);
    mSearchView = (EditText) mExpanded.findViewById(R.id.search_view);

    mSearchIcon = findViewById(R.id.search_magnifying_glass);
    mCollapsedSearchBox = findViewById(R.id.search_box_start_search);
    mVoiceSearchButtonView = findViewById(R.id.voice_search_button);
    mOverflowButtonView = findViewById(R.id.dialtacts_options_menu_button);
    mBackButtonView = findViewById(R.id.search_back_button);
    mExpandedSearchBox = findViewById(R.id.search_box_expanded);
    mClearButtonView = findViewById(R.id.search_close_button);

    // Convert a long click into a click to expand the search box, and then long click on the
    // search view. This accelerates the long-press scenario for copy/paste.
    mCollapsedSearchBox.setOnLongClickListener(
        new OnLongClickListener() {
          @Override
          public boolean onLongClick(View view) {
            mCollapsedSearchBox.performClick();
            mSearchView.performLongClick();
            return false;
          }
        });

    mSearchView.setOnFocusChangeListener(
        new OnFocusChangeListener() {
          @Override
          public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
              DialerUtils.showInputMethod(v);
            } else {
              DialerUtils.hideInputMethod(v);
            }
          }
        });

    mSearchView.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            if (mCallback != null) {
              mCallback.onSearchViewClicked();
            }
          }
        });

    mSearchView.addTextChangedListener(
        new TextWatcher() {
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
            mClearButtonView.setVisibility(TextUtils.isEmpty(s) ? View.GONE : View.VISIBLE);
          }

          @Override
          public void afterTextChanged(Editable s) {}
        });

    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 (mCallback != null) {
                  mCallback.onBackButtonClicked();
                }
              }
            });

    super.onFinishInflate();
  }

  @Override
  public boolean dispatchKeyEventPreIme(KeyEvent event) {
    if (mPreImeKeyListener != null) {
      if (mPreImeKeyListener.onKey(this, event.getKeyCode(), event)) {
        return true;
      }
    }
    return super.dispatchKeyEventPreIme(event);
  }

  public void fadeOut() {
    fadeOut(null);
  }

  public void fadeOut(AnimUtils.AnimationCallback callback) {
    AnimUtils.fadeOut(this, ANIMATION_DURATION, callback);
    mIsFadedOut = true;
  }

  public void fadeIn() {
    AnimUtils.fadeIn(this, ANIMATION_DURATION);
    mIsFadedOut = false;
  }

  public void setVisible(boolean visible) {
    if (visible) {
      setAlpha(1);
      setVisibility(View.VISIBLE);
      mIsFadedOut = false;
    } else {
      setAlpha(0);
      setVisibility(View.GONE);
      mIsFadedOut = true;
    }
  }

  public void expand(boolean animate, boolean requestFocus) {
    updateVisibility(true /* isExpand */);

    if (animate) {
      AnimUtils.crossFadeViews(mExpanded, mCollapsed, ANIMATION_DURATION);
      mAnimator = ValueAnimator.ofFloat(EXPAND_MARGIN_FRACTION_START, 0f);
      setMargins(EXPAND_MARGIN_FRACTION_START);
      prepareAnimator(true);
    } else {
      mExpanded.setVisibility(View.VISIBLE);
      mExpanded.setAlpha(1);
      setMargins(0f);
      mCollapsed.setVisibility(View.GONE);
    }

    // Set 9-patch background. This owns the padding, so we need to restore the original values.
    int paddingTop = this.getPaddingTop();
    int paddingStart = this.getPaddingStart();
    int paddingBottom = this.getPaddingBottom();
    int paddingEnd = this.getPaddingEnd();
    setBackgroundResource(R.drawable.search_shadow);
    setElevation(0);
    setPaddingRelative(paddingStart, paddingTop, paddingEnd, paddingBottom);

    if (requestFocus) {
      mSearchView.requestFocus();
    }
    mIsExpanded = true;
  }

  public void collapse(boolean animate) {
    updateVisibility(false /* isExpand */);

    if (animate) {
      AnimUtils.crossFadeViews(mCollapsed, mExpanded, ANIMATION_DURATION);
      mAnimator = ValueAnimator.ofFloat(0f, 1f);
      prepareAnimator(false);
    } else {
      mCollapsed.setVisibility(View.VISIBLE);
      mCollapsed.setAlpha(1);
      setMargins(1f);
      mExpanded.setVisibility(View.GONE);
    }

    mIsExpanded = false;
    setElevation(mCollapsedElevation);
    setBackgroundResource(R.drawable.rounded_corner);
  }

  /**
   * Updates the visibility of views depending on whether we will show the expanded or collapsed
   * search view. This helps prevent some jank with the crossfading if we are animating.
   *
   * @param isExpand Whether we are about to show the expanded search box.
   */
  private void updateVisibility(boolean isExpand) {
    int collapsedViewVisibility = isExpand ? View.GONE : View.VISIBLE;
    int expandedViewVisibility = isExpand ? View.VISIBLE : View.GONE;

    mSearchIcon.setVisibility(collapsedViewVisibility);
    mCollapsedSearchBox.setVisibility(collapsedViewVisibility);
    mVoiceSearchButtonView.setVisibility(collapsedViewVisibility);
    mOverflowButtonView.setVisibility(collapsedViewVisibility);
    mBackButtonView.setVisibility(expandedViewVisibility);
    // TODO: Prevents keyboard from jumping up in landscape mode after exiting the
    // SearchFragment when the query string is empty. More elegant fix?
    //mExpandedSearchBox.setVisibility(expandedViewVisibility);
    if (TextUtils.isEmpty(mSearchView.getText())) {
      mClearButtonView.setVisibility(View.GONE);
    } else {
      mClearButtonView.setVisibility(expandedViewVisibility);
    }
  }

  private void prepareAnimator(final boolean expand) {
    if (mAnimator != null) {
      mAnimator.cancel();
    }

    mAnimator.addUpdateListener(
        new AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator animation) {
            final Float fraction = (Float) animation.getAnimatedValue();
            setMargins(fraction);
          }
        });

    mAnimator.setDuration(ANIMATION_DURATION);
    mAnimator.start();
  }

  public boolean isExpanded() {
    return mIsExpanded;
  }

  public boolean isFadedOut() {
    return mIsFadedOut;
  }

  /**
   * Assigns margins to the search box as a fraction of its maximum margin size
   *
   * @param fraction How large the margins should be as a fraction of their full size
   */
  private void setMargins(float fraction) {
    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();
  }

  /** Listener for the back button next to the search view being pressed */
  public interface Callback {

    void onBackButtonClicked();

    void onSearchViewClicked();
  }
}