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 package com.android.quicksearchbox.google; 17 18 import android.content.ComponentName; 19 import android.content.Context; 20 import android.content.Intent; 21 import android.os.Bundle; 22 import android.os.Handler; 23 24 import com.android.quicksearchbox.AbstractInternalSource; 25 import com.android.quicksearchbox.CursorBackedSourceResult; 26 import com.android.quicksearchbox.R; 27 import com.android.quicksearchbox.SourceResult; 28 import com.android.quicksearchbox.SuggestionCursor; 29 import com.android.quicksearchbox.util.NamedTaskExecutor; 30 31 /** 32 * Special source implementation for Google suggestions. 33 */ 34 public abstract class AbstractGoogleSource extends AbstractInternalSource implements GoogleSource { 35 36 /* 37 * This name corresponds to what was used in previous version of quick search box. We use the 38 * same name so that shortcuts continue to work after an upgrade. (It also makes logging more 39 * consistent). 40 */ 41 private static final String GOOGLE_SOURCE_NAME = 42 "com.android.quicksearchbox/.google.GoogleSearch"; 43 AbstractGoogleSource(Context context, Handler uiThread, NamedTaskExecutor iconLoader)44 public AbstractGoogleSource(Context context, Handler uiThread, NamedTaskExecutor iconLoader) { 45 super(context, uiThread, iconLoader); 46 } 47 48 @Override getIntentComponent()49 public abstract ComponentName getIntentComponent(); 50 51 @Override refreshShortcut(String shortcutId, String extraData)52 public abstract SuggestionCursor refreshShortcut(String shortcutId, String extraData); 53 54 /** 55 * Called by QSB to get web suggestions for a query. 56 */ 57 @Override queryInternal(String query)58 public abstract SourceResult queryInternal(String query); 59 60 /** 61 * Called by external apps to get web suggestions for a query. 62 */ 63 @Override queryExternal(String query)64 public abstract SourceResult queryExternal(String query); 65 66 @Override createVoiceSearchIntent(Bundle appData)67 public Intent createVoiceSearchIntent(Bundle appData) { 68 return createVoiceWebSearchIntent(appData); 69 } 70 71 @Override getDefaultIntentAction()72 public String getDefaultIntentAction() { 73 return Intent.ACTION_WEB_SEARCH; 74 } 75 76 @Override getHint()77 public CharSequence getHint() { 78 return getContext().getString(R.string.google_search_hint); 79 } 80 81 @Override getLabel()82 public CharSequence getLabel() { 83 return getContext().getString(R.string.google_search_label); 84 } 85 86 @Override getName()87 public String getName() { 88 return GOOGLE_SOURCE_NAME; 89 } 90 91 @Override getSettingsDescription()92 public CharSequence getSettingsDescription() { 93 return getContext().getString(R.string.google_search_description); 94 } 95 96 @Override getSourceIconResource()97 protected int getSourceIconResource() { 98 return R.mipmap.google_icon; 99 } 100 101 @Override getSuggestions(String query, int queryLimit)102 public SourceResult getSuggestions(String query, int queryLimit) { 103 return emptyIfNull(queryInternal(query), query); 104 } 105 getSuggestionsExternal(String query)106 public SourceResult getSuggestionsExternal(String query) { 107 return emptyIfNull(queryExternal(query), query); 108 } 109 emptyIfNull(SourceResult result, String query)110 private SourceResult emptyIfNull(SourceResult result, String query) { 111 return result == null ? new CursorBackedSourceResult(this, query) : result; 112 } 113 114 @Override voiceSearchEnabled()115 public boolean voiceSearchEnabled() { 116 return true; 117 } 118 119 @Override includeInAll()120 public boolean includeInAll() { 121 return true; 122 } 123 124 } 125