QueryTask.java revision 848fa7a19abedc372452073abaf52780c7b6d78d
1/* 2 * Copyright (C) 2009 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 com.android.quicksearchbox; 18 19import com.android.quicksearchbox.util.Consumer; 20import com.android.quicksearchbox.util.NamedTask; 21import com.android.quicksearchbox.util.NamedTaskExecutor; 22 23import android.os.Handler; 24import android.util.Log; 25 26/** 27 * A task that gets suggestions from a corpus. 28 */ 29public class QueryTask<C extends SuggestionCursor> implements NamedTask { 30 private static final String TAG = "QSB.QueryTask"; 31 private static final boolean DBG = false; 32 33 private final String mQuery; 34 private final int mQueryLimit; 35 private final SuggestionCursorProvider<C> mProvider; 36 private final Handler mHandler; 37 private final Consumer<C> mConsumer; 38 private final boolean mTheOnlyOne; 39 40 /** 41 * Creates a new query task. 42 * 43 * @param query Query to run. 44 * @param queryLimit The number of suggestions to ask each provider for. 45 * @param provider The provider to ask for suggestions. 46 * @param handler Handler that {@link Consumer#consume} will 47 * get called on. If null, the method is called on the query thread. 48 * @param consumer Consumer to notify when the suggestions have been returned. 49 * @param onlyTask Indicates if this is the only task within a batch. 50 */ 51 public QueryTask(String query, int queryLimit, SuggestionCursorProvider<C> provider, 52 Handler handler, Consumer<C> consumer, boolean onlyTask) { 53 mQuery = query; 54 mQueryLimit = queryLimit; 55 mProvider = provider; 56 mHandler = handler; 57 mConsumer = consumer; 58 mTheOnlyOne = onlyTask; 59 } 60 61 public String getName() { 62 return mProvider.getName(); 63 } 64 65 public void run() { 66 final C cursor = mProvider.getSuggestions(mQuery, mQueryLimit, mTheOnlyOne); 67 if (DBG) Log.d(TAG, "Suggestions from " + mProvider + " = " + cursor); 68 if (mHandler == null) { 69 mConsumer.consume(cursor); 70 } else { 71 mHandler.post(new Runnable() { 72 public void run() { 73 boolean accepted = mConsumer.consume(cursor); 74 if (!accepted) { 75 cursor.close(); 76 } 77 } 78 }); 79 } 80 } 81 82 @Override 83 public String toString() { 84 return mProvider + "[" + mQuery + "]"; 85 } 86 87 public static <C extends SuggestionCursor> void startQueries(String query, 88 int maxResultsPerProvider, 89 Iterable<? extends SuggestionCursorProvider<C>> providers, 90 NamedTaskExecutor executor, Handler handler, 91 Consumer<C> consumer, boolean onlyOneProvider) { 92 93 for (SuggestionCursorProvider<C> provider : providers) { 94 QueryTask<C> task = new QueryTask<C>(query, maxResultsPerProvider, provider, handler, 95 consumer, onlyOneProvider); 96 executor.execute(task); 97 } 98 } 99 100} 101