SearchBar.java revision c9422fb7ac972aee1f05c5ec0f07c2ec24cc771b
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.inputmethod.EditorInfo; 23import android.view.KeyEvent; 24import android.view.MotionEvent; 25import android.view.View; 26import android.widget.RelativeLayout; 27import android.support.v17.leanback.R; 28import android.widget.TextView; 29 30/** 31 * SearchBar is a search widget. 32 */ 33public class SearchBar extends RelativeLayout { 34 private static final String TAG = SearchBar.class.getSimpleName(); 35 36 /** 37 * Listener for search query changes 38 */ 39 public interface SearchBarListener { 40 41 /** 42 * Method invoked when the search bar detects a change in the query. 43 * 44 * @param query The current full query. 45 */ 46 public void onSearchQueryChange(String query); 47 48 /** 49 * Method invoked when the search query is submitted. 50 * 51 * @param query The query being submitted. 52 */ 53 public void onSearchQuerySubmit(String query); 54 55 /** 56 * Method invoked when the IME is being dismissed. 57 * 58 * @param query The query set in the search bar at the time the IME is being dismissed. 59 */ 60 public void onKeyboardDismiss(String query); 61 } 62 63 private SearchBarListener mSearchBarListener; 64 private SearchEditText mSearchTextEditor; 65 private String mSearchQuery; 66 private final Handler mHandler = new Handler(); 67 68 public SearchBar(Context context) { 69 this(context, null); 70 } 71 72 public SearchBar(Context context, AttributeSet attrs) { 73 this(context, attrs, 0); 74 } 75 76 public SearchBar(Context context, AttributeSet attrs, int defStyle) { 77 super(context, attrs, defStyle); 78 mSearchQuery = ""; 79 } 80 81 @Override 82 protected void onFinishInflate() { 83 super.onFinishInflate(); 84 85 mSearchTextEditor = (SearchEditText)findViewById(R.id.lb_search_text_editor); 86 mSearchTextEditor.setOnFocusChangeListener(new OnFocusChangeListener() { 87 @Override 88 public void onFocusChange(View view, boolean hasFocus) { 89 if (hasFocus) { 90 showNativeKeyboard(); 91 } 92 } 93 }); 94 mSearchTextEditor.addTextChangedListener(new TextWatcher() { 95 @Override 96 public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { 97 98 } 99 100 @Override 101 public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { 102 setSearchQuery(charSequence.toString()); 103 } 104 105 @Override 106 public void afterTextChanged(Editable editable) { 107 108 } 109 }); 110 mSearchTextEditor.setOnKeyboardDismissListener( 111 new SearchEditText.OnKeyboardDismissListener() { 112 @Override 113 public void onKeyboardDismiss() { 114 if (null != mSearchBarListener) { 115 mSearchBarListener.onKeyboardDismiss(mSearchQuery); 116 } 117 } 118 }); 119 mSearchTextEditor.setOnEditorActionListener(new TextView.OnEditorActionListener() { 120 @Override 121 public boolean onEditorAction(TextView textView, int action, KeyEvent keyEvent) { 122 if (EditorInfo.IME_ACTION_SEARCH == action && null != mSearchBarListener) { 123 mSearchBarListener.onSearchQuerySubmit(mSearchQuery); 124 return true; 125 } 126 return false; 127 } 128 }); 129 mSearchTextEditor.setFocusable(true); 130 mSearchTextEditor.setVisibility(VISIBLE); 131 mSearchTextEditor.requestFocus(); 132 } 133 134 /** 135 * Set a listener for when the term search changes 136 * @param listener 137 */ 138 public void setSearchBarListener(SearchBarListener listener) { 139 mSearchBarListener = listener; 140 } 141 142 /** 143 * Set the search query 144 * @param query the search query to use 145 */ 146 public void setSearchQuery(String query) { 147 if (query.equals(mSearchQuery)) { 148 return; 149 } 150 mSearchQuery = query; 151 if (null != mSearchBarListener) { 152 mSearchBarListener.onSearchQueryChange(mSearchQuery); 153 } 154 } 155 156 /** 157 * Set the hint text shown in the search bar. 158 * @param hint The hint to use. 159 */ 160 public void setHint(String hint) { 161 mSearchTextEditor.setHint(hint); 162 } 163 164 protected void showNativeKeyboard() { 165 mHandler.post(new Runnable() { 166 @Override 167 public void run() { 168 mSearchTextEditor.requestFocusFromTouch(); 169 mSearchTextEditor.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), 170 SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 171 mSearchTextEditor.getWidth(), mSearchTextEditor.getHeight(), 0)); 172 mSearchTextEditor.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), 173 SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 174 mSearchTextEditor.getWidth(), mSearchTextEditor.getHeight(), 0)); 175 } 176 }); 177 } 178 179} 180