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 android.content.ComponentName;
19import android.content.Context;
20import android.content.Intent;
21import android.os.Bundle;
22import android.os.Handler;
23
24import com.android.quicksearchbox.AbstractInternalSource;
25import com.android.quicksearchbox.CursorBackedSourceResult;
26import com.android.quicksearchbox.R;
27import com.android.quicksearchbox.SourceResult;
28import com.android.quicksearchbox.SuggestionCursor;
29import com.android.quicksearchbox.util.NamedTaskExecutor;
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, NamedTaskExecutor iconLoader) {
45        super(context, uiThread, iconLoader);
46    }
47
48    @Override
49    public abstract ComponentName getIntentComponent();
50
51    @Override
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
58    public abstract SourceResult queryInternal(String query);
59
60    /**
61     * Called by external apps to get web suggestions for a query.
62     */
63    @Override
64    public abstract SourceResult queryExternal(String query);
65
66    @Override
67    public Intent createVoiceSearchIntent(Bundle appData) {
68        return createVoiceWebSearchIntent(appData);
69    }
70
71    @Override
72    public String getDefaultIntentAction() {
73        return Intent.ACTION_WEB_SEARCH;
74    }
75
76    @Override
77    public CharSequence getHint() {
78        return getContext().getString(R.string.google_search_hint);
79    }
80
81    @Override
82    public CharSequence getLabel() {
83        return getContext().getString(R.string.google_search_label);
84    }
85
86    @Override
87    public String getName() {
88        return GOOGLE_SOURCE_NAME;
89    }
90
91    @Override
92    public CharSequence getSettingsDescription() {
93        return getContext().getString(R.string.google_search_description);
94    }
95
96    @Override
97    protected int getSourceIconResource() {
98        return R.mipmap.google_icon;
99    }
100
101    @Override
102    public SourceResult getSuggestions(String query, int queryLimit) {
103        return emptyIfNull(queryInternal(query), query);
104    }
105
106    public SourceResult getSuggestionsExternal(String query) {
107        return emptyIfNull(queryExternal(query), query);
108    }
109
110    private SourceResult emptyIfNull(SourceResult result, String query) {
111        return result == null ? new CursorBackedSourceResult(this, query) : result;
112    }
113
114    @Override
115    public boolean voiceSearchEnabled() {
116        return true;
117    }
118
119    @Override
120    public boolean includeInAll() {
121        return true;
122    }
123
124}
125