• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.quicksearchbox;
18 
19 import com.android.quicksearchbox.util.Consumer;
20 import com.android.quicksearchbox.util.Consumers;
21 import com.android.quicksearchbox.util.NamedTask;
22 import com.android.quicksearchbox.util.NamedTaskExecutor;
23 
24 import android.os.Handler;
25 import android.util.Log;
26 
27 /**
28  * A task that gets suggestions from a corpus.
29  */
30 public 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 
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      */
QueryTask(String query, int queryLimit, SuggestionCursorProvider<C> provider, Handler handler, Consumer<C> consumer)50     public QueryTask(String query, int queryLimit, SuggestionCursorProvider<C> provider,
51             Handler handler, Consumer<C> consumer) {
52         mQuery = query;
53         mQueryLimit = queryLimit;
54         mProvider = provider;
55         mHandler = handler;
56         mConsumer = consumer;
57     }
58 
59     @Override
getName()60     public String getName() {
61         return mProvider.getName();
62     }
63 
64     @Override
run()65     public void run() {
66         final C cursor = mProvider.getSuggestions(mQuery, mQueryLimit);
67         if (DBG) Log.d(TAG, "Suggestions from " + mProvider + " = " + cursor);
68         Consumers.consumeCloseableAsync(mHandler, mConsumer, cursor);
69     }
70 
71     @Override
toString()72     public String toString() {
73         return mProvider + "[" + mQuery + "]";
74     }
75 
startQuery(String query, int maxResults, SuggestionCursorProvider<C> provider, NamedTaskExecutor executor, Handler handler, Consumer<C> consumer)76     public static <C extends SuggestionCursor> void startQuery(String query,
77             int maxResults,
78             SuggestionCursorProvider<C> provider,
79             NamedTaskExecutor executor, Handler handler,
80             Consumer<C> consumer) {
81 
82         QueryTask<C> task = new QueryTask<C>(query, maxResults, provider, handler,
83                 consumer);
84         executor.execute(task);
85     }
86 }
87