1 /* 2 * Copyright (C) 2010 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 android.text.TextUtils; 20 import android.util.Log; 21 22 import java.util.HashMap; 23 import java.util.Iterator; 24 import java.util.Map; 25 26 /** 27 * Decides whether a given source should be queried for a given query, taking 28 * into account the source's query threshold and query after zero results flag. 29 * 30 * This class is thread safe. 31 */ 32 class ShouldQueryStrategy { 33 34 private static final boolean DBG = false; 35 private static final String TAG = "QSB.ShouldQueryStrategy"; 36 37 // The last query we've seen 38 private String mLastQuery = ""; 39 40 private final Config mConfig; 41 42 // The current implementation keeps a record of those corpora that have 43 // returned zero results for some prefix of the current query. mEmptyCorpora 44 // maps from corpus to the length of the query which returned 45 // zero results. When a query is shortened (e.g., by deleting characters) 46 // or changed entirely, mEmptyCorpora is pruned (in updateQuery) 47 private final HashMap<Corpus, Integer> mEmptyCorpora 48 = new HashMap<Corpus, Integer>(); 49 50 public ShouldQueryStrategy(Config config) { 51 mConfig = config; 52 } 53 54 /** 55 * Returns whether we should query the given source for the given query. 56 */ 57 public boolean shouldQueryCorpus(Corpus corpus, String query) { 58 updateQuery(query); 59 if (query.length() == 0 60 && !corpus.isWebCorpus() // always query web, to warm up connection 61 && !mConfig.showSuggestionsForZeroQuery()) { 62 return false; 63 } 64 if (query.length() >= corpus.getQueryThreshold()) { 65 if (!corpus.queryAfterZeroResults() && mEmptyCorpora.containsKey(corpus)) { 66 if (DBG) Log.i(TAG, "Not querying " + corpus + ", returned 0 after " 67 + mEmptyCorpora.get(corpus)); 68 return false; 69 } 70 return true; 71 } 72 if (DBG) Log.d(TAG, "Query too short for corpus " + corpus); 73 return false; 74 } 75 76 /** 77 * Called to notify ShouldQueryStrategy when a source reports no results for a query. 78 */ 79 public void onZeroResults(Corpus corpus, String query) { 80 // Make sure this result is actually for a prefix of the current query. 81 if (mLastQuery.startsWith(query) && !corpus.queryAfterZeroResults() 82 && !TextUtils.isEmpty(query)) { 83 if (DBG) Log.d(TAG, corpus + " returned 0 results for '" + query + "'"); 84 mEmptyCorpora.put(corpus, query.length()); 85 } 86 } 87 88 private void updateQuery(String query) { 89 if (query.startsWith(mLastQuery)) { 90 // This is a refinement of the last query, no changes to mEmptyCorpora needed 91 } else if (mLastQuery.startsWith(query)) { 92 // This is a widening of the last query: clear out any sources 93 // that reported zero results after this query. 94 Iterator<Map.Entry<Corpus, Integer>> iter = mEmptyCorpora.entrySet().iterator(); 95 while (iter.hasNext()) { 96 if (iter.next().getValue() > query.length()) { 97 iter.remove(); 98 } 99 } 100 } else { 101 // This is a completely different query, clear everything. 102 mEmptyCorpora.clear(); 103 } 104 mLastQuery = query; 105 } 106 } 107