summaryrefslogtreecommitdiffstats
path: root/java/com/android/dialer/main/impl/toolbar/SearchBarView.java
blob: 78cabf7338b1085bbf1f8aa76c3a8843d434581e (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
/*
 * Copyright (C) 2018 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.main.impl.toolbar;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.android.dialer.animation.AnimUtils;
import com.android.dialer.common.UiUtil;
import com.android.dialer.util.DialerUtils;
import com.google.common.base.Optional;

/** Search bar for {@link MainToolbar}. Mostly used to handle expand and collapse animation. */
final class SearchBarView extends FrameLayout {

  private static final int ANIMATION_DURATION = 200;
  private static final float EXPAND_MARGIN_FRACTION_START = 0.8f;

  private final float margin;
  private final float animationEndHeight;
  private final float animationStartHeight;

  private SearchBarListener listener;
  private EditText searchBox;
  private TextView searchBoxTextView;
  // This useful for when the query didn't actually change. We want to avoid making excessive calls
  // where we can since IPCs can take a long time on slow networks.
  private boolean skipLatestTextChange;

  private boolean isExpanded;
  private View searchBoxCollapsed;
  private View searchBoxExpanded;
  private View clearButton;

  public SearchBarView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    margin = getContext().getResources().getDimension(R.dimen.search_bar_margin);
    animationEndHeight =
        getContext().getResources().getDimension(R.dimen.expanded_search_bar_height);
    animationStartHeight =
        getContext().getResources().getDimension(R.dimen.collapsed_search_bar_height);
  }

  @Override
  protected void onFinishInflate() {
    super.onFinishInflate();
    clearButton = findViewById(R.id.search_clear_button);
    searchBox = findViewById(R.id.search_view);
    searchBoxTextView = findViewById(R.id.search_box_start_search);
    searchBoxCollapsed = findViewById(R.id.search_box_collapsed);
    searchBoxExpanded = findViewById(R.id.search_box_expanded);

    setOnClickListener(v -> listener.onSearchBarClicked());
    findViewById(R.id.voice_search_button).setOnClickListener(v -> voiceSearchClicked());
    findViewById(R.id.search_back_button).setOnClickListener(v -> onSearchBackButtonClicked());
    clearButton.setOnClickListener(v -> onSearchClearButtonClicked());
    searchBox.addTextChangedListener(new SearchBoxTextWatcher());
  }

  private void onSearchClearButtonClicked() {
    searchBox.setText("");
  }

  private void onSearchBackButtonClicked() {
    if (!isExpanded) {
      return;
    }

    listener.onSearchBackButtonClicked();
    collapse(true);
  }

  private void voiceSearchClicked() {
    listener.onVoiceButtonClicked(
        result -> {
          if (!TextUtils.isEmpty(result)) {
            expand(true, Optional.of(result));
          }
        });
  }

  /** Expand the search bar and populate it with text if any exists. */
  /* package-private */ void expand(boolean animate, Optional<String> text) {
    if (isExpanded) {
      return;
    }

    int duration = animate ? ANIMATION_DURATION : 0;
    searchBoxExpanded.setVisibility(VISIBLE);
    AnimUtils.crossFadeViews(searchBoxExpanded, searchBoxCollapsed, duration);
    ValueAnimator animator = ValueAnimator.ofFloat(EXPAND_MARGIN_FRACTION_START, 0f);
    animator.addUpdateListener(animation -> setMargins((Float) animation.getAnimatedValue()));
    animator.setDuration(duration);
    animator.addListener(
        new AnimatorListenerAdapter() {
          @Override
          public void onAnimationStart(Animator animation) {
            super.onAnimationStart(animation);
            DialerUtils.showInputMethod(searchBox);
            isExpanded = true;
          }

          @Override
          public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            if (text.isPresent()) {
              searchBox.setText(text.get());
            }
            searchBox.requestFocus();
            setBackgroundResource(R.drawable.search_bar_background);
          }
        });
    animator.start();
  }

  /** Collapse the search bar and clear it's text. */
  /* package-private */ void collapse(boolean animate) {
    if (!isExpanded) {
      return;
    }

    int duration = animate ? ANIMATION_DURATION : 0;
    AnimUtils.crossFadeViews(searchBoxCollapsed, searchBoxExpanded, duration);
    ValueAnimator animator = ValueAnimator.ofFloat(0f, EXPAND_MARGIN_FRACTION_START);
    animator.addUpdateListener(animation -> setMargins((Float) animation.getAnimatedValue()));
    animator.setDuration(duration);

    animator.addListener(
        new AnimatorListenerAdapter() {
          @Override
          public void onAnimationStart(Animator animation) {
            super.onAnimationStart(animation);
            DialerUtils.hideInputMethod(searchBox);
            isExpanded = false;
          }

          @Override
          public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            searchBox.setText("");
            searchBoxExpanded.setVisibility(INVISIBLE);
            setBackgroundResource(R.drawable.search_bar_background_rounded_corners);
          }
        });
    animator.start();
  }

  /**
   * 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) {
    int margin = (int) (this.margin * fraction);
    MarginLayoutParams params = (MarginLayoutParams) getLayoutParams();
    params.topMargin = margin;
    params.bottomMargin = margin;
    params.leftMargin = margin;
    params.rightMargin = margin;
    searchBoxExpanded.getLayoutParams().height =
        (int) (animationEndHeight - (animationEndHeight - animationStartHeight) * fraction);
    requestLayout();
  }

  /* package-private */ void setSearchBarListener(SearchBarListener listener) {
    this.listener = listener;
  }

  public String getQuery() {
    return searchBox.getText().toString();
  }

  public boolean isExpanded() {
    return isExpanded;
  }

  public void setQueryWithoutUpdate(String query) {
    skipLatestTextChange = true;
    searchBox.setText(query);
    searchBox.setSelection(searchBox.getText().length());
  }

  public void hideKeyboard() {
    UiUtil.hideKeyboardFrom(getContext(), searchBox);
  }

  public void showKeyboard() {
    UiUtil.openKeyboardFrom(getContext(), searchBox);
  }

  public void setHint(@StringRes int hint) {
    searchBox.setHint(hint);
    searchBoxTextView.setText(hint);
  }

  /** Handles logic for text changes in the search box. */
  private class SearchBoxTextWatcher implements 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) {}

    @Override
    public void afterTextChanged(Editable s) {
      clearButton.setVisibility(TextUtils.isEmpty(s) ? GONE : VISIBLE);
      if (skipLatestTextChange) {
        skipLatestTextChange = false;
        return;
      }

      listener.onSearchQueryUpdated(s.toString());
    }
  }
}