AbstractGoogleSource.java revision 13b4f2dc4b339790c2b9b0220be47c8e77fd61ea
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.AbstractSource;
19import com.android.quicksearchbox.CursorBackedSourceResult;
20import com.android.quicksearchbox.QsbApplication;
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.graphics.drawable.Drawable;
29import android.net.Uri;
30import android.os.Bundle;
31
32/**
33 * Special source implementation for Google suggestions.
34 */
35public abstract class AbstractGoogleSource extends AbstractSource 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) {
46        super(context);
47    }
48
49    public abstract ComponentName getIntentComponent();
50
51    public abstract SuggestionCursor refreshShortcut(String shortcutId, String extraData);
52
53    public abstract boolean isLocationAware();
54
55    /**
56     * Called by QSB to get web suggestions for a query.
57     */
58    public abstract SourceResult queryInternal(String query);
59
60    /**
61     * Called by external apps to get web suggestions for a query.
62     */
63    public abstract SourceResult queryExternal(String query);
64
65    public boolean canRead() {
66        return true;
67    }
68
69    public Intent createVoiceSearchIntent(Bundle appData) {
70        return createVoiceWebSearchIntent(appData);
71    }
72
73    public String getDefaultIntentAction() {
74        return Intent.ACTION_WEB_SEARCH;
75    }
76
77    public String getDefaultIntentData() {
78        return null;
79    }
80
81    public CharSequence getHint() {
82        return getContext().getString(R.string.google_search_hint);
83    }
84
85    @Override
86    protected String getIconPackage() {
87        return getContext().getPackageName();
88    }
89
90    public CharSequence getLabel() {
91        return getContext().getString(R.string.google_search_label);
92    }
93
94    public String getName() {
95        return GOOGLE_SOURCE_NAME;
96    }
97
98    public int getQueryThreshold() {
99        return 0;
100    }
101
102    public CharSequence getSettingsDescription() {
103        return getContext().getString(R.string.google_search_description);
104    }
105
106    public Drawable getSourceIcon() {
107        return getContext().getResources().getDrawable(getSourceIconResource());
108    }
109
110    public Uri getSourceIconUri() {
111        return Uri.parse("android.resource://" + getContext().getPackageName()
112                + "/" +  getSourceIconResource());
113    }
114
115    private int getSourceIconResource() {
116        return R.drawable.google_icon;
117    }
118
119    public SourceResult getSuggestions(String query, int queryLimit, boolean onlySource) {
120        return emptyIfNull(queryInternal(query), query);
121    }
122
123    public SourceResult getSuggestionsExternal(String query) {
124        return emptyIfNull(queryExternal(query), query);
125    }
126
127    private SourceResult emptyIfNull(SourceResult result, String query) {
128        return result == null ? new CursorBackedSourceResult(this, query) : result;
129    }
130
131    public int getVersionCode() {
132        return QsbApplication.get(getContext()).getVersionCode();
133    }
134
135    /**
136     * Shortcuts from previous version are compatible with shortcuts from this version, so we just
137     * return true. If shortcuts become incompatible during an upgrade, some examination of the
138     * version code should be added here.
139     */
140    @Override
141    public boolean isVersionCodeCompatible(int version) {
142        return true;
143    }
144
145    public boolean queryAfterZeroResults() {
146        return true;
147    }
148
149    public boolean voiceSearchEnabled() {
150        return true;
151    }
152
153    public boolean isWebSuggestionSource() {
154        return true;
155    }
156
157}
158