SearchBar.java revision 3c2998d502e4e2586d0bfc52ff5d1f2725fa9e6d
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package android.support.v17.leanback.widget;
15
16import android.content.Context;
17import android.os.Handler;
18import android.os.SystemClock;
19import android.text.Editable;
20import android.text.TextWatcher;
21import android.util.AttributeSet;
22import android.view.MotionEvent;
23import android.view.View;
24import android.widget.RelativeLayout;
25import android.support.v17.leanback.R;
26
27/**
28 * SearchBar is a search widget.
29 */
30public class SearchBar extends RelativeLayout {
31    private static final String TAG = SearchBar.class.getSimpleName();
32
33    /**
34     * Listener for search query changes
35     */
36    public interface SearchBarListener {
37
38        /**
39         * Method invoked when the search bar detects a change in the query.
40         *
41         * @param query The current full query.
42         */
43        public void onSearchQueryChange(String query);
44
45        /**
46         * Method invoked when the search query is submitted.
47         *
48         * @param query The query being submitted.
49         */
50        public void onSearchQuerySubmit(String query);
51
52        /**
53         * Method invoked when the IME is being dismissed.
54         *
55         * @param query The query set in the search bar at the time the IME is being dismissed.
56         */
57        public void onKeyboardDismiss(String query);
58    }
59
60    private SearchBarListener mSearchBarListener;
61    private SearchEditText mSearchTextEditor;
62    private String mSearchQuery;
63    private final Handler mHandler = new Handler();
64
65    public SearchBar(Context context) {
66        this(context, null);
67    }
68
69    public SearchBar(Context context, AttributeSet attrs) {
70        this(context, attrs, 0);
71    }
72
73    public SearchBar(Context context, AttributeSet attrs, int defStyle) {
74        super(context, attrs, defStyle);
75        mSearchQuery = "";
76    }
77
78    @Override
79    protected void onFinishInflate() {
80        super.onFinishInflate();
81
82        mSearchTextEditor = (SearchEditText)findViewById(R.id.lb_search_text_editor);
83        mSearchTextEditor.setOnFocusChangeListener(new OnFocusChangeListener() {
84            @Override
85            public void onFocusChange(View view, boolean hasFocus) {
86                if (hasFocus) {
87                    showNativeKeyboard();
88                }
89            }
90        });
91        mSearchTextEditor.addTextChangedListener(new TextWatcher() {
92            @Override
93            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
94
95            }
96
97            @Override
98            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
99                setSearchQuery(charSequence.toString());
100            }
101
102            @Override
103            public void afterTextChanged(Editable editable) {
104
105            }
106        });
107        mSearchTextEditor.setOnKeyboardDismissListener(
108            new SearchEditText.OnKeyboardDismissListener() {
109                @Override
110                public void onKeyboardDismiss() {
111                    if (null != mSearchBarListener) {
112                        mSearchBarListener.onKeyboardDismiss(mSearchQuery);
113                    }
114                }
115        });
116        mSearchTextEditor.setFocusable(true);
117        mSearchTextEditor.setVisibility(VISIBLE);
118        mSearchTextEditor.requestFocus();
119    }
120
121    /**
122     * Set a listener for when the term search changes
123     * @param listener
124     */
125    public void setSearchBarListener(SearchBarListener listener) {
126        mSearchBarListener = listener;
127    }
128
129    /**
130     * Set the search query
131     * @param query the search query to use
132     */
133    public void setSearchQuery(String query) {
134        if (query.equals(mSearchQuery)) {
135            return;
136        }
137        mSearchQuery = query;
138        if (null != mSearchBarListener) {
139            mSearchBarListener.onSearchQueryChange(mSearchQuery);
140        }
141    }
142
143    /**
144     * Set the hint text shown in the search bar.
145     * @param hint The hint to use.
146     */
147    public void setHint(String hint) {
148        mSearchTextEditor.setHint(hint);
149    }
150
151    protected void showNativeKeyboard() {
152        mHandler.post(new Runnable() {
153            @Override
154            public void run() {
155                mSearchTextEditor.requestFocusFromTouch();
156                mSearchTextEditor.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),
157                        SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN,
158                        mSearchTextEditor.getWidth(), mSearchTextEditor.getHeight(), 0));
159                mSearchTextEditor.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),
160                        SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,
161                        mSearchTextEditor.getWidth(), mSearchTextEditor.getHeight(), 0));
162            }
163        });
164    }
165
166}
167