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