Home | History | Annotate | Download | only in google
      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 com.android.quicksearchbox.AbstractSource;
     19 import com.android.quicksearchbox.CursorBackedSourceResult;
     20 import com.android.quicksearchbox.QsbApplication;
     21 import com.android.quicksearchbox.R;
     22 import com.android.quicksearchbox.SourceResult;
     23 import com.android.quicksearchbox.SuggestionCursor;
     24 
     25 import android.content.ComponentName;
     26 import android.content.Context;
     27 import android.content.Intent;
     28 import android.graphics.drawable.Drawable;
     29 import android.net.Uri;
     30 import android.os.Bundle;
     31 
     32 /**
     33  * Special source implementation for Google suggestions.
     34  */
     35 public abstract class GoogleSource extends AbstractSource {
     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 GoogleSource(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     protected 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