diff options
Diffstat (limited to 'src/com/android/mail/providers/SearchRecentSuggestionsProvider.java')
-rw-r--r-- | src/com/android/mail/providers/SearchRecentSuggestionsProvider.java | 175 |
1 files changed, 34 insertions, 141 deletions
diff --git a/src/com/android/mail/providers/SearchRecentSuggestionsProvider.java b/src/com/android/mail/providers/SearchRecentSuggestionsProvider.java index 99ac4dfa4..0581869ba 100644 --- a/src/com/android/mail/providers/SearchRecentSuggestionsProvider.java +++ b/src/com/android/mail/providers/SearchRecentSuggestionsProvider.java @@ -18,37 +18,30 @@ package com.android.mail.providers; import android.app.SearchManager; -import android.content.ContentProvider; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; -import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; -import android.net.Uri; +import android.os.SystemClock; import android.text.TextUtils; import com.android.mail.R; import java.util.ArrayList; -public class SearchRecentSuggestionsProvider extends ContentProvider { +public class SearchRecentSuggestionsProvider { /* * String used to delimit different parts of a query. */ public static final String QUERY_TOKEN_SEPARATOR = " "; - // client-provided configuration values - private String mAuthority; - private int mMode; - // 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 @@ -56,24 +49,28 @@ public class SearchRecentSuggestionsProvider extends ContentProvider { // // 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; + private static final int DATABASE_VERSION = 3 * 256; /** * This mode bit configures the database to record recent queries. <i>required</i> - * - * @see #setupSuggestions(String, int) + * @see #setupSuggestions(int) */ public static final int DATABASE_MODE_QUERIES = 1; - // Uri and query support - private static final int URI_MATCH_SUGGEST = 1; - - private Uri mSuggestionsUri; - private UriMatcher mUriMatcher; - private String mSuggestSuggestionClause; private String[] mSuggestionProjection; + protected final Context mContext; + + public SearchRecentSuggestionsProvider(Context context) { + mContext = context; + mOpenHelper = new DatabaseHelper(mContext, DATABASE_VERSION); + } + + public void cleanup() { + mOpenHelper.close(); + } + /** * 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 @@ -110,31 +107,20 @@ public class SearchRecentSuggestionsProvider extends ContentProvider { * 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 */ - protected void setupSuggestions(String authority, int mode) { - if (TextUtils.isEmpty(authority) || - ((mode & DATABASE_MODE_QUERIES) == 0)) { + protected void setupSuggestions(int mode) { + if ((mode & DATABASE_MODE_QUERIES) == 0) { throw new IllegalArgumentException(); } - // 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); - // The URI of the icon that we will include on every suggestion here. final String historicalIcon = ContentResolver.SCHEME_ANDROID_RESOURCE + "://" - + getContext().getPackageName() + "/" + R.drawable.ic_history_holo_light; + + mContext.getPackageName() + "/" + R.drawable.ic_history_24dp; mSuggestSuggestionClause = "display1 LIKE ?"; mSuggestionProjection = new String [] { @@ -145,99 +131,6 @@ public class SearchRecentSuggestionsProvider extends ContentProvider { }; } - /** - * 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; - } - private ArrayList<String> mFullQueryTerms; /** @@ -278,13 +171,8 @@ public class SearchRecentSuggestionsProvider extends ContentProvider { mFullQueryTerms = terms; } - /** - * 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, + public Cursor query(String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = mOpenHelper.getReadableDatabase(); @@ -300,22 +188,27 @@ public class SearchRecentSuggestionsProvider extends ContentProvider { suggestSelection = mSuggestSuggestionClause; } // Suggestions are always performed with the default sort order - // Add this to the query: - // "select 'real_query' as SearchManager.SUGGEST_COLUMN_QUERY. - // rest of query - // real query will then show up in the suggestion Cursor c = db.query(sSuggestions, createProjection(selectionArgs), suggestSelection, myArgs, null, null, ORDER_BY, 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. + * We are going to keep track of recent suggestions ourselves and not depend on the framework. + * Note that this writes to disk. DO NOT CALL FROM MAIN THREAD. */ - @Override - public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { - throw new UnsupportedOperationException("Not implemented"); + public void saveRecentQuery(String query) { + SQLiteDatabase db = mOpenHelper.getWritableDatabase(); + ContentValues values = new ContentValues(3); + values.put("display1", query); + values.put("query", query); + values.put("date", SystemClock.elapsedRealtime()); + // Note: This table has on-conflict-replace semantics, so insert() may actually replace() + db.insert(sSuggestions, null, values); + } + + public void clearHistory() { + SQLiteDatabase db = mOpenHelper.getReadableDatabase(); + db.delete(sSuggestions, null, null); } }
\ No newline at end of file |