diff options
author | Bjorn Bringert <bringert@android.com> | 2012-03-01 19:30:11 +0000 |
---|---|---|
committer | Bjorn Bringert <bringert@android.com> | 2012-03-02 10:26:17 +0000 |
commit | 430286ba80ebaa23ffa591cbbd6951d5f9fe69fa (patch) | |
tree | 37a93d671fa511c5c45fe693051820ad57086ba2 /common/java/com/android/common/Search.java | |
parent | 698c55f3a36fe972b381ad216dc5de3760936858 (diff) | |
download | android_frameworks_ex-430286ba80ebaa23ffa591cbbd6951d5f9fe69fa.tar.gz android_frameworks_ex-430286ba80ebaa23ffa591cbbd6951d5f9fe69fa.tar.bz2 android_frameworks_ex-430286ba80ebaa23ffa591cbbd6951d5f9fe69fa.zip |
Add getSuggestions() to Search
This is a copy of the hidden SearchManager.getSuggestions(),
which I plan to remove. It doesn't depend on any hidden APIs
and is purely a helper method.
Change-Id: I7915cdf327cca9f701e56bcedbf3f5388acf3a66
Diffstat (limited to 'common/java/com/android/common/Search.java')
-rw-r--r-- | common/java/com/android/common/Search.java | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/common/java/com/android/common/Search.java b/common/java/com/android/common/Search.java index b2f3f56..6a442d4 100644 --- a/common/java/com/android/common/Search.java +++ b/common/java/com/android/common/Search.java @@ -16,6 +16,13 @@ package com.android.common; +import android.app.SearchManager; +import android.app.SearchableInfo; +import android.content.ContentResolver; +import android.content.Context; +import android.database.Cursor; +import android.net.Uri; + /** * Utilities for search implementations. * @@ -46,4 +53,72 @@ public class Search { public final static String SUGGEST_COLUMN_LAST_ACCESS_HINT = "suggest_last_access_hint"; private Search() { } // don't instantiate + + /** + * Gets a cursor with search suggestions. + * + * @param searchable Information about how to get the suggestions. + * @param query The search text entered (so far). + * @return a cursor with suggestions, or <code>null</null> the suggestion query failed. + */ + public static Cursor getSuggestions(Context context, SearchableInfo searchable, String query) { + return getSuggestions(context, searchable, query, -1); + } + + /** + * Gets a cursor with search suggestions. + * + * @param searchable Information about how to get the suggestions. + * @param query The search text entered (so far). + * @param limit The query limit to pass to the suggestion provider. This is advisory, + * the returned cursor may contain more rows. Pass {@code -1} for no limit. + * @return a cursor with suggestions, or <code>null</null> the suggestion query failed. + */ + public static Cursor getSuggestions(Context context, SearchableInfo searchable, + String query, int limit) { + if (searchable == null) { + return null; + } + + String authority = searchable.getSuggestAuthority(); + if (authority == null) { + return null; + } + + Uri.Builder uriBuilder = new Uri.Builder() + .scheme(ContentResolver.SCHEME_CONTENT) + .authority(authority) + .query("") // TODO: Remove, workaround for a bug in Uri.writeToParcel() + .fragment(""); // TODO: Remove, workaround for a bug in Uri.writeToParcel() + + // if content path provided, insert it now + final String contentPath = searchable.getSuggestPath(); + if (contentPath != null) { + uriBuilder.appendEncodedPath(contentPath); + } + + // append standard suggestion query path + uriBuilder.appendPath(SearchManager.SUGGEST_URI_PATH_QUERY); + + // get the query selection, may be null + String selection = searchable.getSuggestSelection(); + // inject query, either as selection args or inline + String[] selArgs = null; + if (selection != null) { + selArgs = new String[] { query }; + } else { // no selection, use REST pattern + uriBuilder.appendPath(query); + } + + if (limit > 0) { + uriBuilder.appendQueryParameter(SearchManager.SUGGEST_PARAMETER_LIMIT, + String.valueOf(limit)); + } + + Uri uri = uriBuilder.build(); + + // finally, make the query + return context.getContentResolver().query(uri, null, selection, selArgs, null); + } + } |