MaterialSearchActionView.java revision 64168c6e3a4b770f3d60ef4b7d7a330046e5185e
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, View.OnKeyListener {
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        mQueryText.setOnKeyListener(this);
90        mEndingButton = (ImageView) findViewById(R.id.search_actionbar_ending_button);
91        mEndingButton.setOnClickListener(this);
92    }
93
94    @Override
95    public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
96        // Only care about onTextChanged
97    }
98
99    @Override
100    public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
101        mController.onQueryTextChanged(charSequence.toString());
102        if (!mSupportVoice || charSequence.length() > 0) {
103            mShowingClose = true;
104            mEndingButton.setImageResource(R.drawable.ic_close_24dp);
105        } else {
106            mShowingClose = false;
107            mEndingButton.setImageResource(R.drawable.ic_mic_24dp);
108        }
109    }
110
111    @Override
112    public void afterTextChanged(Editable editable) {
113        // Only care about onTextChanged
114    }
115
116    @Override
117    public void onClick(View view) {
118        if (view == mBackButton) {
119            mController.onSearchCanceled();
120            mQueryText.setText("");
121        } else if (view == mEndingButton) {
122            if (mShowingClose) {
123                mQueryText.setText("");
124                mController.showSearchActionBar(
125                        MaterialSearchViewController.SEARCH_VIEW_STATE_VISIBLE);
126            } else {
127                mController.onVoiceSearch();
128            }
129        } else if (view == mQueryText) {
130            mController.showSearchActionBar(MaterialSearchViewController.SEARCH_VIEW_STATE_VISIBLE);
131        }
132    }
133
134    @Override
135    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
136        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
137            mController.onSearchPerformed(mQueryText.getText().toString());
138        }
139        return false;
140    }
141
142    @Override
143    public boolean onKey(View v, int keyCode, KeyEvent event) {
144        // Hardware keyboard doesn't represent Enter as Search through imeOptions, so we need to
145        // capture them manually here.
146        if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_ENTER) {
147            mController.onSearchPerformed(mQueryText.getText().toString());
148        }
149        return false;
150    }
151}
152