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