AbstractGoogleSource.java revision e29d52aa72c96c3147fa91d83aeb8dafc6d1f578
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;
24
25import android.content.ComponentName;
26import android.content.Context;
27import android.content.Intent;
28import android.os.Bundle;
29import android.os.Handler;
30
31/**
32 * Special source implementation for Google suggestions.
33 */
34public 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
44    public AbstractGoogleSource(Context context, Handler uiThread) {
45        super(context, uiThread);
46    }
47
48    public abstract ComponentName getIntentComponent();
49
50    public abstract SuggestionCursor refreshShortcut(String shortcutId, String extraData);
51
52    /**
53     * Called by QSB to get web suggestions for a query.
54     */
55    public abstract SourceResult queryInternal(String query);
56
57    /**
58     * Called by external apps to get web suggestions for a query.
59     */
60    public abstract SourceResult queryExternal(String query);
61
62    public Intent createVoiceSearchIntent(Bundle appData) {
63        return createVoiceWebSearchIntent(appData);
64    }
65
66    public String getDefaultIntentAction() {
67        return Intent.ACTION_WEB_SEARCH;
68    }
69
70    public CharSequence getHint() {
71        return getContext().getString(R.string.google_search_hint);
72    }
73
74    public CharSequence getLabel() {
75        return getContext().getString(R.string.google_search_label);
76    }
77
78    public String getName() {
79        return GOOGLE_SOURCE_NAME;
80    }
81
82    public CharSequence getSettingsDescription() {
83        return getContext().getString(R.string.google_search_description);
84    }
85
86    @Override
87    protected int getSourceIconResource() {
88        return R.drawable.google_icon;
89    }
90
91    public SourceResult getSuggestions(String query, int queryLimit, boolean onlySource) {
92        return emptyIfNull(queryInternal(query), query);
93    }
94
95    public SourceResult getSuggestionsExternal(String query) {
96        return emptyIfNull(queryExternal(query), query);
97    }
98
99    private SourceResult emptyIfNull(SourceResult result, String query) {
100        return result == null ? new CursorBackedSourceResult(this, query) : result;
101    }
102
103    public boolean voiceSearchEnabled() {
104        return true;
105    }
106
107    public int getMaxShortcuts(Config config) {
108        return config.getMaxShortcutsPerWebSource();
109    }
110
111    public boolean includeInAll() {
112        return true;
113    }
114
115}
116