OSDN Git Service

Add test definition for cts-gesture.
[android-x86/development.git] / samples / SearchableDictionary / src / com / example / android / searchabledict / DictionaryProvider.java
1 /*
2  * Copyright (C) 2009 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.example.android.searchabledict;
18
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;
28
29 import java.util.List;
30
31 /**
32  * Provides search suggestions for a list of words and their definitions.
33  */
34 public class DictionaryProvider extends ContentProvider {
35
36     public static String AUTHORITY = "dictionary";
37
38     private static final int SEARCH_SUGGEST = 0;
39     private static final int SHORTCUT_REFRESH = 1;
40     private static final UriMatcher sURIMatcher = buildUriMatcher();
41
42     /**
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.
46      */
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,
52             };
53
54
55     /**
56      * Sets up a uri matcher for search suggestion and shortcut refresh queries.
57      */
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);
64         return matcher;
65     }
66
67     @Override
68     public boolean onCreate() {
69         Resources resources = getContext().getResources();
70         Dictionary.getInstance().ensureLoaded(resources);
71         return true;
72     }
73
74     @Override
75     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
76             String sortOrder) {
77         if (!TextUtils.isEmpty(selection)) {
78             throw new IllegalArgumentException("selection not allowed for " + uri);
79         }
80         if (selectionArgs != null && selectionArgs.length != 0) {
81             throw new IllegalArgumentException("selectionArgs not allowed for " + uri);
82         }
83         if (!TextUtils.isEmpty(sortOrder)) {
84             throw new IllegalArgumentException("sortOrder not allowed for " + uri);
85         }
86         switch (sURIMatcher.match(uri)) {
87             case SEARCH_SUGGEST:
88                 String query = null;
89                 if (uri.getPathSegments().size() > 1) {
90                     query = uri.getLastPathSegment().toLowerCase();
91                 }
92                 return getSuggestions(query, projection);
93             case SHORTCUT_REFRESH:
94                 String shortcutId = null;
95                 if (uri.getPathSegments().size() > 1) {
96                     shortcutId = uri.getLastPathSegment();
97                 }
98                 return refreshShortcut(shortcutId, projection);
99             default:
100                 throw new IllegalArgumentException("Unknown URL " + uri);
101         }
102     }
103
104     private Cursor getSuggestions(String query, String[] projection) {
105         String processedQuery = query == null ? "" : query.toLowerCase();
106         List<Dictionary.Word> words = Dictionary.getInstance().getMatches(processedQuery);
107
108         MatrixCursor cursor = new MatrixCursor(COLUMNS);
109         for (Dictionary.Word word : words) {
110             cursor.addRow(columnValuesOfWord(word));
111         }
112
113         return cursor;
114     }
115
116     private Object[] columnValuesOfWord(Dictionary.Word word) {
117         return new String[] {
118                 word.word,           // _id
119                 word.word,           // text1
120                 word.definition,     // text2
121                 word.word,           // intent_data (included when clicking on item)
122         };
123     }
124
125     /**
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.
130      */
131     private Cursor refreshShortcut(String shortcutId, String[] projection) {
132         return null;
133     }
134
135     /**
136      * All queries for this provider are for the search suggestion and shortcut refresh mime type.
137      */
138     public String getType(Uri uri) {
139         switch (sURIMatcher.match(uri)) {
140             case SEARCH_SUGGEST:
141                 return SearchManager.SUGGEST_MIME_TYPE;
142             case SHORTCUT_REFRESH:
143                 return SearchManager.SHORTCUT_MIME_TYPE;
144             default:
145                 throw new IllegalArgumentException("Unknown URL " + uri);
146         }
147     }
148
149     public Uri insert(Uri uri, ContentValues values) {
150         throw new UnsupportedOperationException();
151     }
152
153     public int delete(Uri uri, String selection, String[] selectionArgs) {
154         throw new UnsupportedOperationException();
155     }
156
157     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
158         throw new UnsupportedOperationException();
159     }
160 }