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