1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.dialer.app.widget; 18 19 import android.animation.ValueAnimator; 20 import android.animation.ValueAnimator.AnimatorUpdateListener; 21 import android.content.Context; 22 import android.text.Editable; 23 import android.text.TextUtils; 24 import android.text.TextWatcher; 25 import android.util.AttributeSet; 26 import android.view.KeyEvent; 27 import android.view.View; 28 import android.widget.EditText; 29 import android.widget.FrameLayout; 30 import com.android.dialer.animation.AnimUtils; 31 import com.android.dialer.app.R; 32 import com.android.dialer.util.DialerUtils; 33 34 public class SearchEditTextLayout extends FrameLayout { 35 36 private static final float EXPAND_MARGIN_FRACTION_START = 0.8f; 37 private static final int ANIMATION_DURATION = 200; 38 /* Subclass-visible for testing */ 39 protected boolean mIsExpanded = false; 40 protected boolean mIsFadedOut = false; 41 private OnKeyListener mPreImeKeyListener; 42 private int mTopMargin; 43 private int mBottomMargin; 44 private int mLeftMargin; 45 private int mRightMargin; 46 private float mCollapsedElevation; 47 private View mCollapsed; 48 private View mExpanded; 49 private EditText mSearchView; 50 private View mSearchIcon; 51 private View mCollapsedSearchBox; 52 private View mVoiceSearchButtonView; 53 private View mOverflowButtonView; 54 private View mBackButtonView; 55 private View mClearButtonView; 56 57 private ValueAnimator mAnimator; 58 59 private Callback mCallback; 60 61 public SearchEditTextLayout(Context context, AttributeSet attrs) { 62 super(context, attrs); 63 } 64 65 public void setPreImeKeyListener(OnKeyListener listener) { 66 mPreImeKeyListener = listener; 67 } 68 69 public void setCallback(Callback listener) { 70 mCallback = listener; 71 } 72 73 @Override 74 protected void onFinishInflate() { 75 MarginLayoutParams params = (MarginLayoutParams) getLayoutParams(); 76 mTopMargin = params.topMargin; 77 mBottomMargin = params.bottomMargin; 78 mLeftMargin = params.leftMargin; 79 mRightMargin = params.rightMargin; 80 81 mCollapsedElevation = getElevation(); 82 83 mCollapsed = findViewById(R.id.search_box_collapsed); 84 mExpanded = findViewById(R.id.search_box_expanded); 85 mSearchView = (EditText) mExpanded.findViewById(R.id.search_view); 86 87 mSearchIcon = findViewById(R.id.search_magnifying_glass); 88 mCollapsedSearchBox = findViewById(R.id.search_box_start_search); 89 mVoiceSearchButtonView = findViewById(R.id.voice_search_button); 90 mOverflowButtonView = findViewById(R.id.dialtacts_options_menu_button); 91 mBackButtonView = findViewById(R.id.search_back_button); 92 mBackButtonView 93 .getResources() 94 .getDrawable(R.drawable.quantum_ic_arrow_back_vd_theme_24, null) 95 .setAutoMirrored(true); 96 mClearButtonView = findViewById(R.id.search_close_button); 97 98 // Convert a long click into a click to expand the search box. Touch events are also 99 // forwarded to the searchView. This accelerates the long-press scenario for copy/paste. 100 mCollapsed.setOnLongClickListener( 101 new OnLongClickListener() { 102 @Override 103 public boolean onLongClick(View view) { 104 mCollapsed.performClick(); 105 return false; 106 } 107 }); 108 mCollapsed.setOnTouchListener( 109 (v, event) -> { 110 mSearchView.onTouchEvent(event); 111 return false; 112 }); 113 114 mSearchView.setOnFocusChangeListener( 115 new OnFocusChangeListener() { 116 @Override 117 public void onFocusChange(View v, boolean hasFocus) { 118 if (hasFocus) { 119 DialerUtils.showInputMethod(v); 120 } else { 121 DialerUtils.hideInputMethod(v); 122 } 123 } 124 }); 125 126 mSearchView.setOnClickListener( 127 new View.OnClickListener() { 128 @Override 129 public void onClick(View v) { 130 if (mCallback != null) { 131 mCallback.onSearchViewClicked(); 132 } 133 } 134 }); 135 136 mSearchView.addTextChangedListener( 137 new TextWatcher() { 138 @Override 139 public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 140 141 @Override 142 public void onTextChanged(CharSequence s, int start, int before, int count) { 143 mClearButtonView.setVisibility(TextUtils.isEmpty(s) ? View.GONE : View.VISIBLE); 144 } 145 146 @Override 147 public void afterTextChanged(Editable s) {} 148 }); 149 150 findViewById(R.id.search_close_button) 151 .setOnClickListener( 152 new OnClickListener() { 153 @Override 154 public void onClick(View v) { 155 mSearchView.setText(null); 156 } 157 }); 158 159 findViewById(R.id.search_back_button) 160 .setOnClickListener( 161 new OnClickListener() { 162 @Override 163 public void onClick(View v) { 164 if (mCallback != null) { 165 mCallback.onBackButtonClicked(); 166 } 167 } 168 }); 169 170 super.onFinishInflate(); 171 } 172 173 @Override 174 public boolean dispatchKeyEventPreIme(KeyEvent event) { 175 if (mPreImeKeyListener != null) { 176 if (mPreImeKeyListener.onKey(this, event.getKeyCode(), event)) { 177 return true; 178 } 179 } 180 return super.dispatchKeyEventPreIme(event); 181 } 182 183 public void fadeOut() { 184 fadeOut(null); 185 } 186 187 public void fadeOut(AnimUtils.AnimationCallback callback) { 188 AnimUtils.fadeOut(this, ANIMATION_DURATION, callback); 189 mIsFadedOut = true; 190 } 191 192 public void fadeIn() { 193 AnimUtils.fadeIn(this, ANIMATION_DURATION); 194 mIsFadedOut = false; 195 } 196 197 public void fadeIn(AnimUtils.AnimationCallback callback) { 198 AnimUtils.fadeIn(this, ANIMATION_DURATION, AnimUtils.NO_DELAY, callback); 199 mIsFadedOut = false; 200 } 201 202 public void setVisible(boolean visible) { 203 if (visible) { 204 setAlpha(1); 205 setVisibility(View.VISIBLE); 206 mIsFadedOut = false; 207 } else { 208 setAlpha(0); 209 setVisibility(View.GONE); 210 mIsFadedOut = true; 211 } 212 } 213 214 public void expand(boolean animate, boolean requestFocus) { 215 updateVisibility(true /* isExpand */); 216 217 if (animate) { 218 AnimUtils.crossFadeViews(mExpanded, mCollapsed, ANIMATION_DURATION); 219 mAnimator = ValueAnimator.ofFloat(EXPAND_MARGIN_FRACTION_START, 0f); 220 setMargins(EXPAND_MARGIN_FRACTION_START); 221 prepareAnimator(); 222 } else { 223 mExpanded.setVisibility(View.VISIBLE); 224 mExpanded.setAlpha(1); 225 setMargins(0f); 226 mCollapsed.setVisibility(View.GONE); 227 } 228 229 // Set 9-patch background. This owns the padding, so we need to restore the original values. 230 int paddingTop = this.getPaddingTop(); 231 int paddingStart = this.getPaddingStart(); 232 int paddingBottom = this.getPaddingBottom(); 233 int paddingEnd = this.getPaddingEnd(); 234 setBackgroundResource(R.drawable.search_shadow); 235 setElevation(0); 236 setPaddingRelative(paddingStart, paddingTop, paddingEnd, paddingBottom); 237 238 if (requestFocus) { 239 mSearchView.requestFocus(); 240 } 241 mIsExpanded = true; 242 } 243 244 public void collapse(boolean animate) { 245 updateVisibility(false /* isExpand */); 246 247 if (animate) { 248 AnimUtils.crossFadeViews(mCollapsed, mExpanded, ANIMATION_DURATION); 249 mAnimator = ValueAnimator.ofFloat(0f, 1f); 250 prepareAnimator(); 251 } else { 252 mCollapsed.setVisibility(View.VISIBLE); 253 mCollapsed.setAlpha(1); 254 setMargins(1f); 255 mExpanded.setVisibility(View.GONE); 256 } 257 258 mIsExpanded = false; 259 setElevation(mCollapsedElevation); 260 setBackgroundResource(R.drawable.rounded_corner); 261 } 262 263 /** 264 * Updates the visibility of views depending on whether we will show the expanded or collapsed 265 * search view. This helps prevent some jank with the crossfading if we are animating. 266 * 267 * @param isExpand Whether we are about to show the expanded search box. 268 */ 269 private void updateVisibility(boolean isExpand) { 270 int collapsedViewVisibility = isExpand ? View.GONE : View.VISIBLE; 271 int expandedViewVisibility = isExpand ? View.VISIBLE : View.GONE; 272 273 mSearchIcon.setVisibility(collapsedViewVisibility); 274 mCollapsedSearchBox.setVisibility(collapsedViewVisibility); 275 mVoiceSearchButtonView.setVisibility(collapsedViewVisibility); 276 mOverflowButtonView.setVisibility(collapsedViewVisibility); 277 mBackButtonView.setVisibility(expandedViewVisibility); 278 // TODO: Prevents keyboard from jumping up in landscape mode after exiting the 279 // SearchFragment when the query string is empty. More elegant fix? 280 //mExpandedSearchBox.setVisibility(expandedViewVisibility); 281 if (TextUtils.isEmpty(mSearchView.getText())) { 282 mClearButtonView.setVisibility(View.GONE); 283 } else { 284 mClearButtonView.setVisibility(expandedViewVisibility); 285 } 286 } 287 288 private void prepareAnimator() { 289 if (mAnimator != null) { 290 mAnimator.cancel(); 291 } 292 293 mAnimator.addUpdateListener( 294 new AnimatorUpdateListener() { 295 @Override 296 public void onAnimationUpdate(ValueAnimator animation) { 297 final Float fraction = (Float) animation.getAnimatedValue(); 298 setMargins(fraction); 299 } 300 }); 301 302 mAnimator.setDuration(ANIMATION_DURATION); 303 mAnimator.start(); 304 } 305 306 public boolean isExpanded() { 307 return mIsExpanded; 308 } 309 310 public boolean isFadedOut() { 311 return mIsFadedOut; 312 } 313 314 /** 315 * Assigns margins to the search box as a fraction of its maximum margin size 316 * 317 * @param fraction How large the margins should be as a fraction of their full size 318 */ 319 private void setMargins(float fraction) { 320 MarginLayoutParams params = (MarginLayoutParams) getLayoutParams(); 321 params.topMargin = (int) (mTopMargin * fraction); 322 params.bottomMargin = (int) (mBottomMargin * fraction); 323 params.leftMargin = (int) (mLeftMargin * fraction); 324 params.rightMargin = (int) (mRightMargin * fraction); 325 requestLayout(); 326 } 327 328 /** Listener for the back button next to the search view being pressed */ 329 public interface Callback { 330 331 void onBackButtonClicked(); 332 333 void onSearchViewClicked(); 334 } 335 } 336