@@ -49,6 +49,7 @@ public void run() {
4949 final InputMethodManager imm =
5050 (InputMethodManager ) getContext ().getSystemService (Context .INPUT_METHOD_SERVICE );
5151 if (imm != null ) {
52+ editText .setCursorVisible (true );
5253 HIDDEN_METHOD_INVOKER .showSoftInputUnchecked (imm , PeliasSearchView .this , 0 );
5354 }
5455 }
@@ -59,6 +60,7 @@ public void run() {
5960 final InputMethodManager imm =
6061 (InputMethodManager ) getContext ().getSystemService (Context .INPUT_METHOD_SERVICE );
6162 if (imm != null ) {
63+ editText .setCursorVisible (false );
6264 imm .hideSoftInputFromWindow (getWindowToken (), 0 );
6365 }
6466 }
@@ -70,6 +72,7 @@ public void run() {
7072 }
7173 };
7274
75+ private EditText editText ;
7376 private ListView autoCompleteListView ;
7477 private SavedSearch savedSearch ;
7578 private Pelias pelias ;
@@ -86,6 +89,7 @@ public void run() {
8689 private boolean cacheSearchResults = true ;
8790 private boolean autoKeyboardShow = true ;
8891 private SuggestFilter suggestFilter ;
92+ private boolean checkHideAutocompleteList = false ;
8993
9094 private Callback <Result > suggestCallback = new Callback <Result >() {
9195 @ Override public void onResponse (Call <Result > call , Response <Result > response ) {
@@ -114,6 +118,8 @@ public void run() {
114118 }
115119 };
116120
121+ private SearchSubmitListener searchSubmitListener ;
122+
117123 /**
118124 * Constructs a new search view given a context.
119125 */
@@ -135,6 +141,11 @@ private void setup() {
135141 disableDefaultSoftKeyboardBehaviour ();
136142 setOnQueryTextListener (this );
137143 setImeOptions (EditorInfo .IME_ACTION_SEARCH );
144+ setupEditText ();
145+ }
146+
147+ private void setupEditText () {
148+ editText = (EditText ) findViewById (R .id .search_src_text );
138149 }
139150
140151 /**
@@ -152,31 +163,7 @@ public void setAutoCompleteListView(final ListView listView) {
152163 autoCompleteListView = listView ;
153164 setOnQueryTextFocusChangeListener (new View .OnFocusChangeListener () {
154165 @ Override public void onFocusChange (View view , boolean hasFocus ) {
155- if (hasFocus ) {
156- final Animation slideIn = loadAnimation (getContext (), R .anim .slide_in );
157- setAutoCompleteAdapterIcon (recentSearchIconResourceId );
158- loadSavedSearches ();
159- listView .setVisibility (VISIBLE );
160- listView .setAnimation (slideIn );
161- if (autoKeyboardShow ) {
162- postDelayed (showImeRunnable , 300 );
163- }
164- setOnQueryTextListener (PeliasSearchView .this );
165- } else {
166- final Animation slideOut = loadAnimation (getContext (), R .anim .slide_out );
167- listView .setVisibility (GONE );
168- listView .setAnimation (slideOut );
169- postDelayed (hideImeRunnable , 300 );
170- setOnQueryTextListener (null );
171- }
172-
173- // Notify secondary listener
174- if (onPeliasFocusChangeListener != null ) {
175- onPeliasFocusChangeListener .onFocusChange (view , hasFocus );
176- }
177-
178- focusedViewHasFocus = hasFocus ;
179- postDelayed (backPressedRunnable , 300 );
166+ PeliasSearchView .this .onFocusChange (view , hasFocus );
180167 }
181168 });
182169
@@ -198,6 +185,63 @@ public void enableAutoKeyboardShow() {
198185 autoKeyboardShow = true ;
199186 }
200187
188+ public void setSearchSubmitListener (SearchSubmitListener listener ) {
189+ searchSubmitListener = listener ;
190+ }
191+
192+ private void handleSearchGainingFocus () {
193+ setAutoCompleteAdapterIcon (recentSearchIconResourceId );
194+ loadSavedSearches ();
195+ safeShowAutocompleteList ();
196+ setOnQueryTextListener (PeliasSearchView .this );
197+ }
198+
199+ private void handleSearchLosingFocus () {
200+ safeHideAutocompleteList ();
201+ postDelayed (hideImeRunnable , 300 );
202+ setOnQueryTextListener (null );
203+ }
204+
205+ private void safeShowAutocompleteList () {
206+ if (autoCompleteListView == null ) {
207+ return ;
208+ }
209+ if (autoCompleteListView .getVisibility () != VISIBLE ) {
210+ final Animation slideIn = loadAnimation (getContext (), R .anim .slide_in );
211+ autoCompleteListView .setVisibility (VISIBLE );
212+ autoCompleteListView .setAnimation (slideIn );
213+ }
214+ if (autoKeyboardShow ) {
215+ postDelayed (showImeRunnable , 300 );
216+ }
217+ }
218+
219+ /**
220+ * Checks whether autocomplete list should be hidden and if so, hides it.
221+ */
222+ private void safeHideAutocompleteList () {
223+ if (checkHideAutocompleteList ) {
224+ checkHideAutocompleteList = false ;
225+ if (searchSubmitListener .hideAutocompleteOnSearchSubmit ()) {
226+ hideAutocompleteList ();
227+ }
228+ } else {
229+ hideAutocompleteList ();
230+ }
231+ }
232+
233+ /**
234+ * Hides the autocomplete list with animation.
235+ */
236+ private void hideAutocompleteList () {
237+ if (autoCompleteListView == null ) {
238+ return ;
239+ }
240+ final Animation slideOut = loadAnimation (getContext (), R .anim .slide_out );
241+ autoCompleteListView .setVisibility (GONE );
242+ autoCompleteListView .setAnimation (slideOut );
243+ }
244+
201245 /**
202246 * Overrides default behavior for showing soft keyboard. Enables manual control by this class.
203247 */
@@ -219,16 +263,21 @@ private void disableDefaultSoftKeyboardBehaviour() {
219263
220264 @ Override public boolean onQueryTextSubmit (String query ) {
221265 if (pelias != null ) {
222- pelias .search (query , callback );
266+ if (searchSubmitListener == null || searchSubmitListener .searchOnSearchKeySubmit ()) {
267+ pelias .search (query , callback );
268+ }
223269 }
224270
225271 storeSavedSearch (query , null );
226272
227273 if (onSubmitListener != null ) {
228274 onSubmitListener .onSubmit ();
229275 }
276+ if (searchSubmitListener != null ) {
277+ checkHideAutocompleteList = true ;
278+ }
230279 textSubmitted = true ;
231- clearFocus ( );
280+ onFocusChange ( this , false );
232281 resetCursorPosition ();
233282 return false ;
234283 }
@@ -381,7 +430,6 @@ public interface OnSubmitListener {
381430 }
382431
383432 private void resetCursorPosition () {
384- EditText editText = (EditText ) findViewById (R .id .search_src_text );
385433 if (editText != null ) {
386434 editText .setSelection (0 );
387435 }
@@ -423,7 +471,11 @@ public AdapterView.OnItemClickListener invoke() {
423471 } else {
424472 final Result result = new Result ();
425473 final ArrayList <Feature > features = new ArrayList <>(1 );
426- clearFocus ();
474+ if (hasFocus ()) {
475+ clearFocus ();
476+ } else {
477+ onFocusChange (PeliasSearchView .this , false );
478+ }
427479 setQuery (item .getText (), false );
428480 resetCursorPosition ();
429481 features .add (item .getSimpleFeature ().toFeature ());
@@ -439,6 +491,22 @@ public AdapterView.OnItemClickListener invoke() {
439491 }
440492 }
441493
494+ private void onFocusChange (View view , boolean hasFocus ) {
495+ if (hasFocus ) {
496+ handleSearchGainingFocus ();
497+ } else {
498+ handleSearchLosingFocus ();
499+ }
500+
501+ // Notify secondary listener
502+ if (onPeliasFocusChangeListener != null ) {
503+ onPeliasFocusChangeListener .onFocusChange (view , hasFocus );
504+ }
505+ focusedViewHasFocus = hasFocus ;
506+
507+ postDelayed (backPressedRunnable , 300 );
508+ }
509+
442510 /**
443511 * Listener to simulate when the SearchBar gains focus and then loses it when the user presses
444512 * back without executing a search or clicking on an item in the autocomplete list view.
@@ -535,4 +603,5 @@ private void updateSavedSearch() {
535603 Callback <Result > getSuggestCallback () {
536604 return suggestCallback ;
537605 }
606+
538607}
0 commit comments