2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.example.android.searchabledict;
19 import android.app.SearchManager;
20 import android.content.ContentProvider;
21 import android.content.ContentValues;
22 import android.content.UriMatcher;
23 import android.content.res.Resources;
24 import android.database.Cursor;
25 import android.database.MatrixCursor;
26 import android.net.Uri;
27 import android.text.TextUtils;
29 import java.util.List;
32 * Provides search suggestions for a list of words and their definitions.
34 public class DictionaryProvider extends ContentProvider {
36 public static String AUTHORITY = "dictionary";
38 private static final int SEARCH_SUGGEST = 0;
39 private static final int SHORTCUT_REFRESH = 1;
40 private static final UriMatcher sURIMatcher = buildUriMatcher();
43 * The columns we'll include in our search suggestions. There are others that could be used
44 * to further customize the suggestions, see the docs in {@link SearchManager} for the details
45 * on additional columns that are supported.
47 private static final String[] COLUMNS = {
48 "_id", // must include this column
49 SearchManager.SUGGEST_COLUMN_TEXT_1,
50 SearchManager.SUGGEST_COLUMN_TEXT_2,
51 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
56 * Sets up a uri matcher for search suggestion and shortcut refresh queries.
58 private static UriMatcher buildUriMatcher() {
59 UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
60 matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST);
61 matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST);
62 matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT, SHORTCUT_REFRESH);
63 matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT + "/*", SHORTCUT_REFRESH);
68 public boolean onCreate() {
69 Resources resources = getContext().getResources();
70 Dictionary.getInstance().ensureLoaded(resources);
75 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
77 if (!TextUtils.isEmpty(selection)) {
78 throw new IllegalArgumentException("selection not allowed for " + uri);
80 if (selectionArgs != null && selectionArgs.length != 0) {
81 throw new IllegalArgumentException("selectionArgs not allowed for " + uri);
83 if (!TextUtils.isEmpty(sortOrder)) {
84 throw new IllegalArgumentException("sortOrder not allowed for " + uri);
86 switch (sURIMatcher.match(uri)) {
89 if (uri.getPathSegments().size() > 1) {
90 query = uri.getLastPathSegment().toLowerCase();
92 return getSuggestions(query, projection);
93 case SHORTCUT_REFRESH:
94 String shortcutId = null;
95 if (uri.getPathSegments().size() > 1) {
96 shortcutId = uri.getLastPathSegment();
98 return refreshShortcut(shortcutId, projection);
100 throw new IllegalArgumentException("Unknown URL " + uri);
104 private Cursor getSuggestions(String query, String[] projection) {
105 String processedQuery = query == null ? "" : query.toLowerCase();
106 List<Dictionary.Word> words = Dictionary.getInstance().getMatches(processedQuery);
108 MatrixCursor cursor = new MatrixCursor(COLUMNS);
109 for (Dictionary.Word word : words) {
110 cursor.addRow(columnValuesOfWord(word));
116 private Object[] columnValuesOfWord(Dictionary.Word word) {
117 return new String[] {
120 word.definition, // text2
121 word.word, // intent_data (included when clicking on item)
126 * Note: this is unused as is, but if we included
127 * {@link SearchManager#SUGGEST_COLUMN_SHORTCUT_ID} as a column in our results, we
128 * could expect to receive refresh queries on this uri for the id provided, in which case we
129 * would return a cursor with a single item representing the refreshed suggestion data.
131 private Cursor refreshShortcut(String shortcutId, String[] projection) {
136 * All queries for this provider are for the search suggestion and shortcut refresh mime type.
138 public String getType(Uri uri) {
139 switch (sURIMatcher.match(uri)) {
141 return SearchManager.SUGGEST_MIME_TYPE;
142 case SHORTCUT_REFRESH:
143 return SearchManager.SHORTCUT_MIME_TYPE;
145 throw new IllegalArgumentException("Unknown URL " + uri);
149 public Uri insert(Uri uri, ContentValues values) {
150 throw new UnsupportedOperationException();
153 public int delete(Uri uri, String selection, String[] selectionArgs) {
154 throw new UnsupportedOperationException();
157 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
158 throw new UnsupportedOperationException();