FileDocCategorySizeDatePackage
SuggestionProvider.javaAPI DocAndroid 1.5 API8999Wed May 06 22:42:46 BST 2009com.android.googlesearch

SuggestionProvider

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 android.net.Uri
CONTENT_URI
private static final String
USER_AGENT
private String
mSuggestUri
private static final int
HTTP_TIMEOUT_MS
private static final String
HTTP_TIMEOUT
private static final String
LOG_TAG
private static final String[]
COLUMNS
private HttpClient
mHttpClient
Constructors Summary
Methods Summary
public intdelete(android.net.Uri uri, java.lang.String selection, java.lang.String[] selectionArgs)

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

        return SearchManager.SUGGEST_MIME_TYPE;
    
public android.net.Uriinsert(android.net.Uri uri, android.content.ContentValues values)

        throw new UnsupportedOperationException();
    
private static com.android.internal.database.ArrayListCursormakeEmptyCursor()

        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(android.net.Uri 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
            // com.android.browser.BrowserActivity#onCreate().  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,
                                                                    l.getLanguage(), 
                                                                    l.getCountry().toLowerCase()) 
                        + "json=true&q=";
            }

            HttpPost method = new HttpPost(mSuggestUri + query);
            StringEntity content = new StringEntity("");
            method.setEntity(content);
            HttpResponse response = mHttpClient.execute(method);
            if (response.getStatusLine().getStatusCode() == 200) {
                
                /* Goto http://www.google.com/complete/search?json=true&q=foo
                 * 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(android.net.Uri uri, android.content.ContentValues values, java.lang.String selection, java.lang.String[] selectionArgs)

        throw new UnsupportedOperationException();