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