diff options
-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); + } + } |