SearchViewCompat.java revision fe32563fd610767a2d3eea8dbd96e6bae87739d5
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.v4.widget; 18 19import android.content.Context; 20import android.os.Build; 21import android.view.View; 22 23/** 24 * Helper for accessing newer features in SearchView. 25 */ 26public class SearchViewCompat { 27 28 interface SearchViewCompatImpl { 29 View newSearchView(Context context); 30 Object newOnQueryTextListener(OnQueryTextListenerCompat listener); 31 void setOnQueryTextListener(Object searchView, Object listener); 32 } 33 34 static class SearchViewCompatStubImpl implements SearchViewCompatImpl { 35 36 @Override 37 public View newSearchView(Context context) { 38 return null; 39 } 40 41 @Override 42 public Object newOnQueryTextListener(OnQueryTextListenerCompat listener) { 43 return null; 44 } 45 46 @Override 47 public void setOnQueryTextListener(Object searchView, Object listener) { 48 49 } 50 } 51 52 static class SearchViewCompatHoneycombImpl extends SearchViewCompatStubImpl { 53 54 @Override 55 public View newSearchView(Context context) { 56 return SearchViewCompatHoneycomb.newSearchView(context); 57 } 58 59 @Override 60 public Object newOnQueryTextListener(final OnQueryTextListenerCompat listener) { 61 return SearchViewCompatHoneycomb.newOnQueryTextListener( 62 new SearchViewCompatHoneycomb.OnQueryTextListenerCompatBridge() { 63 @Override 64 public boolean onQueryTextSubmit(String query) { 65 return listener.onQueryTextSubmit(query); 66 } 67 @Override 68 public boolean onQueryTextChange(String newText) { 69 return listener.onQueryTextChange(newText); 70 } 71 }); 72 } 73 74 @Override 75 public void setOnQueryTextListener(Object searchView, Object listener) { 76 SearchViewCompatHoneycomb.setOnQueryTextListener(searchView, listener); 77 } 78 } 79 80 private static final SearchViewCompatImpl IMPL; 81 82 static { 83 if (Build.VERSION.SDK_INT >= 11) { // Honeycomb 84 IMPL = new SearchViewCompatHoneycombImpl(); 85 } else { 86 IMPL = new SearchViewCompatStubImpl(); 87 } 88 } 89 90 private SearchViewCompat(Context context) { 91 /* Hide constructor */ 92 } 93 94 /** 95 * Creates a new SearchView. 96 * 97 * @param context The Context the view is running in. 98 * @return A SearchView instance if the class is present on the current 99 * platform, null otherwise. 100 */ 101 public static View newSearchView(Context context) { 102 return IMPL.newSearchView(context); 103 } 104 105 /** 106 * Sets a listener for user actions within the SearchView. 107 * 108 * @param searchView The SearchView in which to register the listener. 109 * @param listener the listener object that receives callbacks when the user performs 110 * actions in the SearchView such as clicking on buttons or typing a query. 111 */ 112 public static void setOnQueryTextListener(View searchView, OnQueryTextListenerCompat listener) { 113 IMPL.setOnQueryTextListener(searchView, listener.mListener); 114 } 115 116 /** 117 * Callbacks for changes to the query text. 118 */ 119 public static abstract class OnQueryTextListenerCompat { 120 final Object mListener; 121 122 public OnQueryTextListenerCompat() { 123 mListener = IMPL.newOnQueryTextListener(this); 124 } 125 126 /** 127 * Called when the user submits the query. This could be due to a key press on the 128 * keyboard or due to pressing a submit button. 129 * The listener can override the standard behavior by returning true 130 * to indicate that it has handled the submit request. Otherwise return false to 131 * let the SearchView handle the submission by launching any associated intent. 132 * 133 * @param query the query text that is to be submitted 134 * 135 * @return true if the query has been handled by the listener, false to let the 136 * SearchView perform the default action. 137 */ 138 public boolean onQueryTextSubmit(String query) { 139 return false; 140 } 141 142 /** 143 * Called when the query text is changed by the user. 144 * 145 * @param newText the new content of the query text field. 146 * 147 * @return false if the SearchView should perform the default action of showing any 148 * suggestions if available, true if the action was handled by the listener. 149 */ 150 public boolean onQueryTextChange(String newText) { 151 return false; 152 } 153 } 154} 155