11935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov/* 21935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * Copyright (C) 2011 The Android Open Source Project 31935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * 41935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * Licensed under the Apache License, Version 2.0 (the "License"); 51935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * you may not use this file except in compliance with the License. 61935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * You may obtain a copy of the License at 71935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * 81935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * http://www.apache.org/licenses/LICENSE-2.0 91935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * 101935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * Unless required by applicable law or agreed to in writing, software 111935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * distributed under the License is distributed on an "AS IS" BASIS, 121935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * See the License for the specific language governing permissions and 141935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * limitations under the License. 151935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov */ 161935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 171935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganovpackage android.support.v4.widget; 181935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 191935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganovimport android.content.Context; 201935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganovimport android.os.Build; 211935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganovimport android.view.View; 221935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 231935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov/** 240574ca37da4619afe4e26753f5a1b4de314b6565Svetoslav Ganov * Helper for accessing features in {@link android.widget.SearchView} 250574ca37da4619afe4e26753f5a1b4de314b6565Svetoslav Ganov * introduced after API level 4 in a backwards compatible fashion. 261935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov */ 271935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganovpublic class SearchViewCompat { 281935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 291935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov interface SearchViewCompatImpl { 301935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov View newSearchView(Context context); 311935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov Object newOnQueryTextListener(OnQueryTextListenerCompat listener); 321935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov void setOnQueryTextListener(Object searchView, Object listener); 331935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 341935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 351935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov static class SearchViewCompatStubImpl implements SearchViewCompatImpl { 361935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 371935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov @Override 381935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov public View newSearchView(Context context) { 391935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov return null; 401935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 411935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 421935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov @Override 431935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov public Object newOnQueryTextListener(OnQueryTextListenerCompat listener) { 441935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov return null; 451935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 461935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 471935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov @Override 481935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov public void setOnQueryTextListener(Object searchView, Object listener) { 491935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 501935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 511935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 521935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 531935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov static class SearchViewCompatHoneycombImpl extends SearchViewCompatStubImpl { 541935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 551935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov @Override 561935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov public View newSearchView(Context context) { 571935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov return SearchViewCompatHoneycomb.newSearchView(context); 581935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 591935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 601935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov @Override 611935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov public Object newOnQueryTextListener(final OnQueryTextListenerCompat listener) { 621935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov return SearchViewCompatHoneycomb.newOnQueryTextListener( 631935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov new SearchViewCompatHoneycomb.OnQueryTextListenerCompatBridge() { 641935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov @Override 651935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov public boolean onQueryTextSubmit(String query) { 661935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov return listener.onQueryTextSubmit(query); 671935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 681935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov @Override 691935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov public boolean onQueryTextChange(String newText) { 701935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov return listener.onQueryTextChange(newText); 711935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 721935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov }); 731935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 741935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 751935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov @Override 761935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov public void setOnQueryTextListener(Object searchView, Object listener) { 771935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov SearchViewCompatHoneycomb.setOnQueryTextListener(searchView, listener); 781935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 791935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 801935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 811935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov private static final SearchViewCompatImpl IMPL; 821935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 831935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov static { 841935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov if (Build.VERSION.SDK_INT >= 11) { // Honeycomb 851935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov IMPL = new SearchViewCompatHoneycombImpl(); 861935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } else { 871935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov IMPL = new SearchViewCompatStubImpl(); 881935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 891935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 901935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 911935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov private SearchViewCompat(Context context) { 921935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov /* Hide constructor */ 931935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 941935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 951935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov /** 96fe32563fd610767a2d3eea8dbd96e6bae87739d5Svetoslav Ganov * Creates a new SearchView. 971935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * 981935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * @param context The Context the view is running in. 991935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * @return A SearchView instance if the class is present on the current 1001935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * platform, null otherwise. 1011935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov */ 1021935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov public static View newSearchView(Context context) { 1031935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov return IMPL.newSearchView(context); 1041935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 1051935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 1061935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov /** 1071935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * Sets a listener for user actions within the SearchView. 1081935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * 1091935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * @param searchView The SearchView in which to register the listener. 1101935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * @param listener the listener object that receives callbacks when the user performs 1111935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * actions in the SearchView such as clicking on buttons or typing a query. 1121935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov */ 1131935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov public static void setOnQueryTextListener(View searchView, OnQueryTextListenerCompat listener) { 1141935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov IMPL.setOnQueryTextListener(searchView, listener.mListener); 1151935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 1161935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 1171935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov /** 1181935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * Callbacks for changes to the query text. 1191935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov */ 1201935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov public static abstract class OnQueryTextListenerCompat { 1211935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov final Object mListener; 1221935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 1231935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov public OnQueryTextListenerCompat() { 1241935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov mListener = IMPL.newOnQueryTextListener(this); 1251935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 1261935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 1271935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov /** 1281935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * Called when the user submits the query. This could be due to a key press on the 1291935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * keyboard or due to pressing a submit button. 1301935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * The listener can override the standard behavior by returning true 1311935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * to indicate that it has handled the submit request. Otherwise return false to 1321935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * let the SearchView handle the submission by launching any associated intent. 1331935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * 1341935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * @param query the query text that is to be submitted 1351935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * 1361935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * @return true if the query has been handled by the listener, false to let the 1371935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * SearchView perform the default action. 1381935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov */ 1391935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov public boolean onQueryTextSubmit(String query) { 1401935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov return false; 1411935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 1421935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov 1431935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov /** 1441935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * Called when the query text is changed by the user. 1451935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * 1461935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * @param newText the new content of the query text field. 1471935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * 1481935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * @return false if the SearchView should perform the default action of showing any 1491935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * suggestions if available, true if the action was handled by the listener. 1501935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov */ 1511935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov public boolean onQueryTextChange(String newText) { 1521935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov return false; 1531935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 1541935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov } 1551935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov} 156