MaterialSearchActionView.java revision a70ab69fd489a1f10dd75fc84214bd970096ae02
1/* 2 * Copyright (C) 2014 Google Inc. 3 * Licensed to The Android Open Source Project. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package com.android.mail.ui; 19 20import android.content.Context; 21import android.text.Editable; 22import android.text.TextWatcher; 23import android.util.AttributeSet; 24import android.view.KeyEvent; 25import android.view.View; 26import android.view.inputmethod.EditorInfo; 27import android.view.inputmethod.InputMethodManager; 28import android.widget.EditText; 29import android.widget.ImageView; 30import android.widget.LinearLayout; 31import android.widget.TextView; 32 33import com.android.mail.R; 34 35/** 36 * Custom view for the action bar when search is displayed. 37 */ 38public class MaterialSearchActionView extends LinearLayout implements TextWatcher, 39 View.OnClickListener, TextView.OnEditorActionListener { 40 private MaterialSearchViewController mController; 41 private InputMethodManager mImm; 42 private boolean mShowingClose; 43 private boolean mSupportVoice; 44 45 private View mBackButton; 46 private EditText mQueryText; 47 private ImageView mEndingButton; 48 49 public MaterialSearchActionView(Context context) { 50 super(context); 51 } 52 53 public MaterialSearchActionView(Context context, AttributeSet attrs) { 54 super(context, attrs); 55 } 56 57 // PUBLIC API 58 public void setController(MaterialSearchViewController controller, String initialQuery, 59 boolean supportVoice) { 60 mController = controller; 61 mQueryText.setText(initialQuery); 62 mSupportVoice = supportVoice; 63 } 64 65 public void clearSearchQuery() { 66 mQueryText.setText(""); 67 } 68 69 public void focusSearchBar(boolean hasFocus) { 70 if (hasFocus) { 71 mQueryText.requestFocus(); 72 mImm.showSoftInput(mQueryText, 0); 73 } else { 74 mImm.hideSoftInputFromWindow(mQueryText.getWindowToken(), 0); 75 } 76 } 77 78 @Override 79 protected void onFinishInflate() { 80 super.onFinishInflate(); 81 82 mImm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 83 mBackButton = findViewById(R.id.search_actionbar_back_button); 84 mBackButton.setOnClickListener(this); 85 mQueryText = (EditText) findViewById(R.id.search_actionbar_query_text); 86 mQueryText.addTextChangedListener(this); 87 mQueryText.setOnClickListener(this); 88 mQueryText.setOnEditorActionListener(this); 89 mEndingButton = (ImageView) findViewById(R.id.search_actionbar_ending_button); 90 mEndingButton.setOnClickListener(this); 91 } 92 93 @Override 94 public void setVisibility(int visibility) { 95 if (visibility != VISIBLE) { 96 mQueryText.setText(""); 97 } 98 super.setVisibility(visibility); 99 } 100 101 @Override 102 public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { 103 // Only care about onTextChanged 104 } 105 106 @Override 107 public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { 108 mController.onQueryTextChanged(charSequence.toString()); 109 if (!mSupportVoice || charSequence.length() > 0) { 110 mShowingClose = true; 111 mEndingButton.setImageResource(R.drawable.ic_close_24dp); 112 } else { 113 mShowingClose = false; 114 mEndingButton.setImageResource(R.drawable.ic_mic_24dp); 115 } 116 } 117 118 @Override 119 public void afterTextChanged(Editable editable) { 120 // Only care about onTextChanged 121 } 122 123 @Override 124 public void onClick(View view) { 125 if (view == mBackButton) { 126 mController.onSearchCanceled(); 127 mQueryText.setText(""); 128 } else if (view == mEndingButton) { 129 if (mShowingClose) { 130 mQueryText.setText(""); 131 mController.showSearchActionBar( 132 MaterialSearchViewController.SEARCH_VIEW_STATE_VISIBLE); 133 } else { 134 mController.onVoiceSearch(); 135 } 136 } else if (view == mQueryText) { 137 mController.showSearchActionBar(MaterialSearchViewController.SEARCH_VIEW_STATE_VISIBLE); 138 } 139 } 140 141 @Override 142 public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) { 143 if (actionId == EditorInfo.IME_ACTION_SEARCH) { 144 mController.onSearchPerformed(mQueryText.getText().toString()); 145 } 146 return false; 147 } 148} 149