SearchViewCompat.java revision 0574ca37da4619afe4e26753f5a1b4de314b6565
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 features in {@link android.widget.SearchView}
25 * introduced after API level 4 in a backwards compatible fashion.
26 */
27public class SearchViewCompat {
28
29    interface SearchViewCompatImpl {
30        View newSearchView(Context context);
31        Object newOnQueryTextListener(OnQueryTextListenerCompat listener);
32        void setOnQueryTextListener(Object searchView, Object listener);
33    }
34
35    static class SearchViewCompatStubImpl implements SearchViewCompatImpl {
36
37        @Override
38        public View newSearchView(Context context) {
39            return null;
40        }
41
42        @Override
43        public Object newOnQueryTextListener(OnQueryTextListenerCompat listener) {
44            return null;
45        }
46
47        @Override
48        public void setOnQueryTextListener(Object searchView, Object listener) {
49
50        }
51    }
52
53    static class SearchViewCompatHoneycombImpl extends SearchViewCompatStubImpl {
54
55        @Override
56        public View newSearchView(Context context) {
57            return SearchViewCompatHoneycomb.newSearchView(context);
58        }
59
60        @Override
61        public Object newOnQueryTextListener(final OnQueryTextListenerCompat listener) {
62            return SearchViewCompatHoneycomb.newOnQueryTextListener(
63                    new SearchViewCompatHoneycomb.OnQueryTextListenerCompatBridge() {
64                        @Override
65                        public boolean onQueryTextSubmit(String query) {
66                            return listener.onQueryTextSubmit(query);
67                        }
68                        @Override
69                        public boolean onQueryTextChange(String newText) {
70                            return listener.onQueryTextChange(newText);
71                        }
72                    });
73        }
74
75        @Override
76        public void setOnQueryTextListener(Object searchView, Object listener) {
77            SearchViewCompatHoneycomb.setOnQueryTextListener(searchView, listener);
78        }
79    }
80
81    private static final SearchViewCompatImpl IMPL;
82
83    static {
84        if (Build.VERSION.SDK_INT >= 11) { // Honeycomb
85            IMPL = new SearchViewCompatHoneycombImpl();
86        } else {
87            IMPL = new SearchViewCompatStubImpl();
88        }
89    }
90
91    private SearchViewCompat(Context context) {
92        /* Hide constructor */
93    }
94
95    /**
96     * Creates a new SearchView.
97     *
98     * @param context The Context the view is running in.
99     * @return A SearchView instance if the class is present on the current
100     *         platform, null otherwise.
101     */
102    public static View newSearchView(Context context) {
103        return IMPL.newSearchView(context);
104    }
105
106    /**
107     * Sets a listener for user actions within the SearchView.
108     *
109     * @param searchView The SearchView in which to register the listener.
110     * @param listener the listener object that receives callbacks when the user performs
111     *     actions in the SearchView such as clicking on buttons or typing a query.
112     */
113    public static void setOnQueryTextListener(View searchView, OnQueryTextListenerCompat listener) {
114        IMPL.setOnQueryTextListener(searchView, listener.mListener);
115    }
116
117    /**
118     * Callbacks for changes to the query text.
119     */
120    public static abstract class OnQueryTextListenerCompat {
121        final Object mListener;
122
123        public OnQueryTextListenerCompat() {
124            mListener = IMPL.newOnQueryTextListener(this);
125        }
126
127        /**
128         * Called when the user submits the query. This could be due to a key press on the
129         * keyboard or due to pressing a submit button.
130         * The listener can override the standard behavior by returning true
131         * to indicate that it has handled the submit request. Otherwise return false to
132         * let the SearchView handle the submission by launching any associated intent.
133         *
134         * @param query the query text that is to be submitted
135         *
136         * @return true if the query has been handled by the listener, false to let the
137         * SearchView perform the default action.
138         */
139        public boolean onQueryTextSubmit(String query) {
140            return false;
141        }
142
143        /**
144         * Called when the query text is changed by the user.
145         *
146         * @param newText the new content of the query text field.
147         *
148         * @return false if the SearchView should perform the default action of showing any
149         * suggestions if available, true if the action was handled by the listener.
150         */
151        public boolean onQueryTextChange(String newText) {
152            return false;
153        }
154    }
155}
156