SuggestionProvider.javaAPI DocAndroid 1.5 API8999Wed May 06 22:42:46 BST


public class SuggestionProvider extends android.content.ContentProvider
Use network-based Google Suggests to provide search suggestions. Future: Merge live suggestions with saved recent queries

Fields Summary
public static final
private static final String
private String
private static final int
private static final String
private static final String
private static final String[]
private HttpClient
Constructors Summary
Methods Summary
public intdelete( uri, java.lang.String selection, java.lang.String[] selectionArgs)

        throw new UnsupportedOperationException();
public java.lang.StringgetType( uri)
This will always return {@link SearchManager#SUGGEST_MIME_TYPE} as this provider is purely to provide suggestions.

        return SearchManager.SUGGEST_MIME_TYPE;
public uri, android.content.ContentValues values)

        throw new UnsupportedOperationException();
private static

        return new ArrayListCursor(COLUMNS, new ArrayList<ArrayList>());
public booleanonCreate()

        mHttpClient = new GoogleHttpClient(getContext().getContentResolver(),
                USER_AGENT, false /* not gzip capable */);
        HttpParams params = mHttpClient.getParams();
        params.setLongParameter(HTTP_TIMEOUT, HTTP_TIMEOUT_MS);

        // NOTE:  Do not look up the resource here;  Localization changes may not have completed
        // yet (e.g. we may still be reading the SIM card).
        mSuggestUri = null;
        return true;
public android.database.Cursorquery( uri, java.lang.String[] projection, java.lang.String selection, java.lang.String[] selectionArgs, java.lang.String sortOrder)
Queries for a given search term and returns a cursor containing suggestions ordered by best match.

        String query = selectionArgs[0];
        if (TextUtils.isEmpty(query)) {
            /* Can't pass back null, things blow up */
            return makeEmptyCursor();
        try {
            query = URLEncoder.encode(query, "UTF-8");
            // NOTE:  This code uses resources to optionally select the search Uri, based on the
            // MCC value from the SIM.  iThe default string will most likely be fine.  It is
            // paramerterized to accept info from the Locale, the language code is the first
            // parameter (%1$s) and the country code is the second (%2$s).  This code *must* 
            // function in the same way as a similar lookup in
            //  If you change
            // either of these functions, change them both.  (The same is true for the underlying
            // resource strings, which are stored in mcc-specific xml files.)
            if (mSuggestUri == null) {
                Locale l = Locale.getDefault();
                mSuggestUri = getContext().getResources().getString(R.string.google_search_base,
                        + "json=true&q=";

            HttpPost method = new HttpPost(mSuggestUri + query);
            StringEntity content = new StringEntity("");
            HttpResponse response = mHttpClient.execute(method);
            if (response.getStatusLine().getStatusCode() == 200) {
                /* Goto
                 * to see what the data format looks like. It's basically a json
                 * array containing 4 other arrays. We only care about the middle
                 * 2 which contain the suggestions and their popularity.
                JSONArray results = new JSONArray(EntityUtils.toString(response.getEntity()));
                JSONArray suggestions = results.getJSONArray(1);
                JSONArray popularity = results.getJSONArray(2);
                return new SuggestionsCursor(suggestions, popularity);
        } catch (UnsupportedEncodingException e) {
            Log.w(LOG_TAG, "Error", e);
        } catch (IOException e) {
            Log.w(LOG_TAG, "Error", e);
        } catch (JSONException e) {
            Log.w(LOG_TAG, "Error", e);
        return makeEmptyCursor();
public intupdate( uri, android.content.ContentValues values, java.lang.String selection, java.lang.String[] selectionArgs)

        throw new UnsupportedOperationException();