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