Home | History | Annotate | Download | only in google
      1 /*
      2  * Copyright (C) 2008 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 
     17 package com.android.quicksearchbox.google;
     18 
     19 import com.android.quicksearchbox.CursorBackedSourceResult;
     20 import com.android.quicksearchbox.QsbApplication;
     21 import com.android.quicksearchbox.Source;
     22 import com.android.quicksearchbox.SourceResult;
     23 import com.android.quicksearchbox.SuggestionCursorBackedCursor;
     24 
     25 import android.app.SearchManager;
     26 import android.content.ContentProvider;
     27 import android.content.ContentValues;
     28 import android.content.Context;
     29 import android.content.UriMatcher;
     30 import android.database.Cursor;
     31 import android.net.Uri;
     32 import android.util.Log;
     33 
     34 /**
     35  * A suggestion provider which provides content from Genie, a service that offers
     36  * a superset of the content provided by Google Suggest.
     37  */
     38 public class GoogleSuggestionProvider extends ContentProvider {
     39     private static final boolean DBG = false;
     40     private static final String TAG = "QSB.GoogleSuggestionProvider";
     41 
     42     // UriMatcher constants
     43     private static final int SEARCH_SUGGEST = 0;
     44     private static final int SEARCH_SHORTCUT = 1;
     45 
     46     private UriMatcher mUriMatcher;
     47 
     48     private GoogleSource mSource;
     49 
     50     @Override
     51     public boolean onCreate() {
     52         mSource = QsbApplication.get(getContext()).getGoogleSource();
     53         mUriMatcher = buildUriMatcher(getContext());
     54         return true;
     55     }
     56 
     57     /**
     58      * This will always return {@link SearchManager#SUGGEST_MIME_TYPE} as this
     59      * provider is purely to provide suggestions.
     60      */
     61     @Override
     62     public String getType(Uri uri) {
     63         return SearchManager.SUGGEST_MIME_TYPE;
     64     }
     65 
     66     private SourceResult emptyIfNull(SourceResult result, GoogleSource source, String query) {
     67         return result == null ? new CursorBackedSourceResult(source, query) : result;
     68     }
     69 
     70     @Override
     71     public Cursor query(Uri uri, String[] projection, String selection,
     72             String[] selectionArgs, String sortOrder) {
     73 
     74         if (DBG) Log.d(TAG, "query uri=" + uri);
     75         int match = mUriMatcher.match(uri);
     76 
     77         if (match == SEARCH_SUGGEST) {
     78             String query = getQuery(uri);
     79             return new SuggestionCursorBackedCursor(
     80                     emptyIfNull(mSource.queryExternal(query), mSource, query));
     81         } else if (match == SEARCH_SHORTCUT) {
     82             String shortcutId = getQuery(uri);
     83             String extraData =
     84                 uri.getQueryParameter(SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA);
     85             return new SuggestionCursorBackedCursor(mSource.refreshShortcut(shortcutId, extraData));
     86         } else {
     87             throw new IllegalArgumentException("Unknown URI " + uri);
     88         }
     89     }
     90 
     91     /**
     92      * Gets the search text from a uri.
     93      */
     94     private String getQuery(Uri uri) {
     95         if (uri.getPathSegments().size() > 1) {
     96             return uri.getLastPathSegment();
     97         } else {
     98             return "";
     99         }
    100     }
    101 
    102     @Override
    103     public Uri insert(Uri uri, ContentValues values) {
    104         throw new UnsupportedOperationException();
    105     }
    106 
    107     @Override
    108     public int update(Uri uri, ContentValues values, String selection,
    109             String[] selectionArgs) {
    110         throw new UnsupportedOperationException();
    111     }
    112 
    113     @Override
    114     public int delete(Uri uri, String selection, String[] selectionArgs) {
    115         throw new UnsupportedOperationException();
    116     }
    117 
    118     private UriMatcher buildUriMatcher(Context context) {
    119         String authority = getAuthority(context);
    120         UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
    121         matcher.addURI(authority, SearchManager.SUGGEST_URI_PATH_QUERY,
    122                 SEARCH_SUGGEST);
    123         matcher.addURI(authority, SearchManager.SUGGEST_URI_PATH_QUERY + "/*",
    124                 SEARCH_SUGGEST);
    125         matcher.addURI(authority, SearchManager.SUGGEST_URI_PATH_SHORTCUT,
    126                 SEARCH_SHORTCUT);
    127         matcher.addURI(authority, SearchManager.SUGGEST_URI_PATH_SHORTCUT + "/*",
    128                 SEARCH_SHORTCUT);
    129         return matcher;
    130     }
    131 
    132     protected String getAuthority(Context context) {
    133         return context.getPackageName() + ".google";
    134     }
    135 
    136 }
    137