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