• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.content;
18 
19 import android.app.SearchManager;
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.database.sqlite.SQLiteOpenHelper;
23 import android.net.Uri;
24 import android.text.TextUtils;
25 import android.util.Log;
26 
27 /**
28  * This superclass can be used to create a simple search suggestions provider for your application.
29  * It creates suggestions (as the user types) based on recent queries and/or recent views.
30  *
31  * <p>In order to use this class, you must do the following.
32  *
33  * <ul>
34  * <li>Implement and test query search, as described in {@link android.app.SearchManager}.  (This
35  * provider will send any suggested queries via the standard
36  * {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} Intent, which you'll already
37  * support once you have implemented and tested basic searchability.)</li>
38  * <li>Create a Content Provider within your application by extending
39  * {@link android.content.SearchRecentSuggestionsProvider}.  The class you create will be
40  * very simple - typically, it will have only a constructor.  But the constructor has a very
41  * important responsibility:  When it calls {@link #setupSuggestions(String, int)}, it
42  * <i>configures</i> the provider to match the requirements of your searchable activity.</li>
43  * <li>Create a manifest entry describing your provider.  Typically this would be as simple
44  * as adding the following lines:
45  * <pre class="prettyprint">
46  *     &lt;!-- Content provider for search suggestions --&gt;
47  *     &lt;provider android:name="YourSuggestionProviderClass"
48  *               android:authorities="your.suggestion.authority" /&gt;</pre>
49  * </li>
50  * <li>Please note that you <i>do not</i> instantiate this content provider directly from within
51  * your code.  This is done automatically by the system Content Resolver, when the search dialog
52  * looks for suggestions.</li>
53  * <li>In order for the Content Resolver to do this, you must update your searchable activity's
54  * XML configuration file with information about your content provider.  The following additions
55  * are usually sufficient:
56  * <pre class="prettyprint">
57  *     android:searchSuggestAuthority="your.suggestion.authority"
58  *     android:searchSuggestSelection=" ? "</pre>
59  * </li>
60  * <li>In your searchable activities, capture any user-generated queries and record them
61  * for future searches by calling {@link android.provider.SearchRecentSuggestions#saveRecentQuery
62  * SearchRecentSuggestions.saveRecentQuery()}.</li>
63  * </ul>
64  *
65  * @see android.provider.SearchRecentSuggestions
66  */
67 public class SearchRecentSuggestionsProvider extends ContentProvider {
68     // debugging support
69     private static final String TAG = "SuggestionsProvider";
70 
71     // client-provided configuration values
72     private String mAuthority;
73     private int mMode;
74     private boolean mTwoLineDisplay;
75 
76     // general database configuration and tables
77     private SQLiteOpenHelper mOpenHelper;
78     private static final String sDatabaseName = "suggestions.db";
79     private static final String sSuggestions = "suggestions";
80     private static final String ORDER_BY = "date DESC";
81     private static final String NULL_COLUMN = "query";
82 
83     // Table of database versions.  Don't forget to update!
84     // NOTE:  These version values are shifted left 8 bits (x 256) in order to create space for
85     // a small set of mode bitflags in the version int.
86     //
87     // 1      original implementation with queries, and 1 or 2 display columns
88     // 1->2   added UNIQUE constraint to display1 column
89     private static final int DATABASE_VERSION = 2 * 256;
90 
91     /**
92      * This mode bit configures the database to record recent queries.  <i>required</i>
93      *
94      * @see #setupSuggestions(String, int)
95      */
96     public static final int DATABASE_MODE_QUERIES = 1;
97     /**
98      * This mode bit configures the database to include a 2nd annotation line with each entry.
99      * <i>optional</i>
100      *
101      * @see #setupSuggestions(String, int)
102      */
103     public static final int DATABASE_MODE_2LINES = 2;
104 
105     // Uri and query support
106     private static final int URI_MATCH_SUGGEST = 1;
107 
108     private Uri mSuggestionsUri;
109     private UriMatcher mUriMatcher;
110 
111     private String mSuggestSuggestionClause;
112     private String[] mSuggestionProjection;
113 
114     /**
115      * Builds the database.  This version has extra support for using the version field
116      * as a mode flags field, and configures the database columns depending on the mode bits
117      * (features) requested by the extending class.
118      *
119      * @hide
120      */
121     private static class DatabaseHelper extends SQLiteOpenHelper {
122 
123         private int mNewVersion;
124 
DatabaseHelper(Context context, int newVersion)125         public DatabaseHelper(Context context, int newVersion) {
126             super(context, sDatabaseName, null, newVersion);
127             mNewVersion = newVersion;
128         }
129 
130         @Override
onCreate(SQLiteDatabase db)131         public void onCreate(SQLiteDatabase db) {
132             StringBuilder builder = new StringBuilder();
133             builder.append("CREATE TABLE suggestions (" +
134                     "_id INTEGER PRIMARY KEY" +
135                     ",display1 TEXT UNIQUE ON CONFLICT REPLACE");
136             if (0 != (mNewVersion & DATABASE_MODE_2LINES)) {
137                 builder.append(",display2 TEXT");
138             }
139             builder.append(",query TEXT" +
140                     ",date LONG" +
141                     ");");
142             db.execSQL(builder.toString());
143         }
144 
145         @Override
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)146         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
147             Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
148                     + newVersion + ", which will destroy all old data");
149             db.execSQL("DROP TABLE IF EXISTS suggestions");
150             onCreate(db);
151         }
152     }
153 
154     /**
155      * In order to use this class, you must extend it, and call this setup function from your
156      * constructor.  In your application or activities, you must provide the same values when
157      * you create the {@link android.provider.SearchRecentSuggestions} helper.
158      *
159      * @param authority This must match the authority that you've declared in your manifest.
160      * @param mode You can use mode flags here to determine certain functional aspects of your
161      * database.  Note, this value should not change from run to run, because when it does change,
162      * your suggestions database may be wiped.
163      *
164      * @see #DATABASE_MODE_QUERIES
165      * @see #DATABASE_MODE_2LINES
166      */
setupSuggestions(String authority, int mode)167     protected void setupSuggestions(String authority, int mode) {
168         if (TextUtils.isEmpty(authority) ||
169                 ((mode & DATABASE_MODE_QUERIES) == 0)) {
170             throw new IllegalArgumentException();
171         }
172         // unpack mode flags
173         mTwoLineDisplay = (0 != (mode & DATABASE_MODE_2LINES));
174 
175         // saved values
176         mAuthority = new String(authority);
177         mMode = mode;
178 
179         // derived values
180         mSuggestionsUri = Uri.parse("content://" + mAuthority + "/suggestions");
181         mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
182         mUriMatcher.addURI(mAuthority, SearchManager.SUGGEST_URI_PATH_QUERY, URI_MATCH_SUGGEST);
183 
184         if (mTwoLineDisplay) {
185             mSuggestSuggestionClause = "display1 LIKE ? OR display2 LIKE ?";
186 
187             mSuggestionProjection = new String [] {
188                     "0 AS " + SearchManager.SUGGEST_COLUMN_FORMAT,
189                     "'android.resource://system/"
190                             + com.android.internal.R.drawable.ic_menu_recent_history + "' AS "
191                             + SearchManager.SUGGEST_COLUMN_ICON_1,
192                     "display1 AS " + SearchManager.SUGGEST_COLUMN_TEXT_1,
193                     "display2 AS " + SearchManager.SUGGEST_COLUMN_TEXT_2,
194                     "query AS " + SearchManager.SUGGEST_COLUMN_QUERY,
195                     "_id"
196             };
197         } else {
198             mSuggestSuggestionClause = "display1 LIKE ?";
199 
200             mSuggestionProjection = new String [] {
201                     "0 AS " + SearchManager.SUGGEST_COLUMN_FORMAT,
202                     "'android.resource://system/"
203                             + com.android.internal.R.drawable.ic_menu_recent_history + "' AS "
204                             + SearchManager.SUGGEST_COLUMN_ICON_1,
205                     "display1 AS " + SearchManager.SUGGEST_COLUMN_TEXT_1,
206                     "query AS " + SearchManager.SUGGEST_COLUMN_QUERY,
207                     "_id"
208             };
209         }
210 
211 
212     }
213 
214     /**
215      * This method is provided for use by the ContentResolver.  Do not override, or directly
216      * call from your own code.
217      */
218     @Override
delete(Uri uri, String selection, String[] selectionArgs)219     public int delete(Uri uri, String selection, String[] selectionArgs) {
220         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
221 
222         final int length = uri.getPathSegments().size();
223         if (length != 1) {
224             throw new IllegalArgumentException("Unknown Uri");
225         }
226 
227         final String base = uri.getPathSegments().get(0);
228         int count = 0;
229         if (base.equals(sSuggestions)) {
230             count = db.delete(sSuggestions, selection, selectionArgs);
231         } else {
232             throw new IllegalArgumentException("Unknown Uri");
233         }
234         getContext().getContentResolver().notifyChange(uri, null);
235         return count;
236     }
237 
238     /**
239      * This method is provided for use by the ContentResolver.  Do not override, or directly
240      * call from your own code.
241      */
242     @Override
getType(Uri uri)243     public String getType(Uri uri) {
244         if (mUriMatcher.match(uri) == URI_MATCH_SUGGEST) {
245             return SearchManager.SUGGEST_MIME_TYPE;
246         }
247         int length = uri.getPathSegments().size();
248         if (length >= 1) {
249             String base = uri.getPathSegments().get(0);
250             if (base.equals(sSuggestions)) {
251                 if (length == 1) {
252                     return "vnd.android.cursor.dir/suggestion";
253                 } else if (length == 2) {
254                     return "vnd.android.cursor.item/suggestion";
255                 }
256             }
257         }
258         throw new IllegalArgumentException("Unknown Uri");
259     }
260 
261     /**
262      * This method is provided for use by the ContentResolver.  Do not override, or directly
263      * call from your own code.
264      */
265     @Override
insert(Uri uri, ContentValues values)266     public Uri insert(Uri uri, ContentValues values) {
267         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
268 
269         int length = uri.getPathSegments().size();
270         if (length < 1) {
271             throw new IllegalArgumentException("Unknown Uri");
272         }
273         // Note:  This table has on-conflict-replace semantics, so insert() may actually replace()
274         long rowID = -1;
275         String base = uri.getPathSegments().get(0);
276         Uri newUri = null;
277         if (base.equals(sSuggestions)) {
278             if (length == 1) {
279                 rowID = db.insert(sSuggestions, NULL_COLUMN, values);
280                 if (rowID > 0) {
281                     newUri = Uri.withAppendedPath(mSuggestionsUri, String.valueOf(rowID));
282                 }
283             }
284         }
285         if (rowID < 0) {
286             throw new IllegalArgumentException("Unknown Uri");
287         }
288         getContext().getContentResolver().notifyChange(newUri, null);
289         return newUri;
290     }
291 
292     /**
293      * This method is provided for use by the ContentResolver.  Do not override, or directly
294      * call from your own code.
295      */
296     @Override
onCreate()297     public boolean onCreate() {
298         if (mAuthority == null || mMode == 0) {
299             throw new IllegalArgumentException("Provider not configured");
300         }
301         int mWorkingDbVersion = DATABASE_VERSION + mMode;
302         mOpenHelper = new DatabaseHelper(getContext(), mWorkingDbVersion);
303 
304         return true;
305     }
306 
307     /**
308      * This method is provided for use by the ContentResolver.  Do not override, or directly
309      * call from your own code.
310      */
311     // TODO: Confirm no injection attacks here, or rewrite.
312     @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)313     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
314             String sortOrder) {
315         SQLiteDatabase db = mOpenHelper.getReadableDatabase();
316 
317         // special case for actual suggestions (from search manager)
318         if (mUriMatcher.match(uri) == URI_MATCH_SUGGEST) {
319             String suggestSelection;
320             String[] myArgs;
321             if (TextUtils.isEmpty(selectionArgs[0])) {
322                 suggestSelection = null;
323                 myArgs = null;
324             } else {
325                 String like = "%" + selectionArgs[0] + "%";
326                 if (mTwoLineDisplay) {
327                     myArgs = new String [] { like, like };
328                 } else {
329                     myArgs = new String [] { like };
330                 }
331                 suggestSelection = mSuggestSuggestionClause;
332             }
333             // Suggestions are always performed with the default sort order
334             Cursor c = db.query(sSuggestions, mSuggestionProjection,
335                     suggestSelection, myArgs, null, null, ORDER_BY, null);
336             c.setNotificationUri(getContext().getContentResolver(), uri);
337             return c;
338         }
339 
340         // otherwise process arguments and perform a standard query
341         int length = uri.getPathSegments().size();
342         if (length != 1 && length != 2) {
343             throw new IllegalArgumentException("Unknown Uri");
344         }
345 
346         String base = uri.getPathSegments().get(0);
347         if (!base.equals(sSuggestions)) {
348             throw new IllegalArgumentException("Unknown Uri");
349         }
350 
351         String[] useProjection = null;
352         if (projection != null && projection.length > 0) {
353             useProjection = new String[projection.length + 1];
354             System.arraycopy(projection, 0, useProjection, 0, projection.length);
355             useProjection[projection.length] = "_id AS _id";
356         }
357 
358         StringBuilder whereClause = new StringBuilder(256);
359         if (length == 2) {
360             whereClause.append("(_id = ").append(uri.getPathSegments().get(1)).append(")");
361         }
362 
363         // Tack on the user's selection, if present
364         if (selection != null && selection.length() > 0) {
365             if (whereClause.length() > 0) {
366                 whereClause.append(" AND ");
367             }
368 
369             whereClause.append('(');
370             whereClause.append(selection);
371             whereClause.append(')');
372         }
373 
374         // And perform the generic query as requested
375         Cursor c = db.query(base, useProjection, whereClause.toString(),
376                 selectionArgs, null, null, sortOrder,
377                 null);
378         c.setNotificationUri(getContext().getContentResolver(), uri);
379         return c;
380     }
381 
382     /**
383      * This method is provided for use by the ContentResolver.  Do not override, or directly
384      * call from your own code.
385      */
386     @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)387     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
388         throw new UnsupportedOperationException("Not implemented");
389     }
390 
391 }
392