1 /* 2 * Copyright (C) 2007 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 17 package android.widget; 18 19 import android.database.Cursor; 20 21 /** 22 * <p>The CursorFilter delegates most of the work to the CursorAdapter. 23 * Subclasses should override these delegate methods to run the queries 24 * and convert the results into String that can be used by auto-completion 25 * widgets.</p> 26 */ 27 class CursorFilter extends Filter { 28 29 CursorFilterClient mClient; 30 31 interface CursorFilterClient { 32 CharSequence convertToString(Cursor cursor); 33 Cursor runQueryOnBackgroundThread(CharSequence constraint); 34 Cursor getCursor(); 35 void changeCursor(Cursor cursor); 36 } 37 38 CursorFilter(CursorFilterClient client) { 39 mClient = client; 40 } 41 42 @Override 43 public CharSequence convertResultToString(Object resultValue) { 44 return mClient.convertToString((Cursor) resultValue); 45 } 46 47 @Override 48 protected FilterResults performFiltering(CharSequence constraint) { 49 Cursor cursor = mClient.runQueryOnBackgroundThread(constraint); 50 51 FilterResults results = new FilterResults(); 52 if (cursor != null) { 53 results.count = cursor.getCount(); 54 results.values = cursor; 55 } else { 56 results.count = 0; 57 results.values = null; 58 } 59 return results; 60 } 61 62 @Override 63 protected void publishResults(CharSequence constraint, FilterResults results) { 64 Cursor oldCursor = mClient.getCursor(); 65 66 if (results.values != null && results.values != oldCursor) { 67 mClient.changeCursor((Cursor) results.values); 68 } 69 } 70 } 71