Add user-selected search providers to browser

The lists of search providers are taken from Chrome.

Change-Id: I320f43f06d3a79a3b7a065c73859853dedf0a6b5
diff --git a/src/com/android/browser/BrowserProvider.java b/src/com/android/browser/BrowserProvider.java
index 33f3006..1320e83 100644
--- a/src/com/android/browser/BrowserProvider.java
+++ b/src/com/android/browser/BrowserProvider.java
@@ -16,10 +16,10 @@
 
 package com.android.browser;
 
+import com.android.browser.search.SearchEngine;
+
 import android.app.SearchManager;
-import android.app.SearchableInfo;
 import android.app.backup.BackupManager;
-import android.content.ComponentName;
 import android.content.ContentProvider;
 import android.content.ContentResolver;
 import android.content.ContentUris;
@@ -31,13 +31,11 @@
 import android.content.UriMatcher;
 import android.content.res.Configuration;
 import android.database.AbstractCursor;
-import android.database.ContentObserver;
 import android.database.Cursor;
 import android.database.DatabaseUtils;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.net.Uri;
-import android.os.Handler;
 import android.os.Process;
 import android.preference.PreferenceManager;
 import android.provider.Browser;
@@ -170,7 +168,7 @@
     // optionally a trailing slash, all matched as separate groups.
     private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
 
-    private SearchManager mSearchManager;
+    private BrowserSettings mSettings;
 
     private int mMaxSuggestionShortSize;
     private int mMaxSuggestionLongSize;
@@ -388,59 +386,10 @@
                 ed.apply();
             }
         }
-        mSearchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
-        mShowWebSuggestionsSettingChangeObserver
-            = new ShowWebSuggestionsSettingChangeObserver();
-        context.getContentResolver().registerContentObserver(
-                Settings.System.getUriFor(
-                        Settings.System.SHOW_WEB_SUGGESTIONS),
-                true, mShowWebSuggestionsSettingChangeObserver);
-        updateShowWebSuggestions();
+        mSettings = BrowserSettings.getInstance();
         return true;
     }
 
-    /**
-     * This Observer will ensure that if the user changes the system
-     * setting of whether to display web suggestions, we will
-     * change accordingly.
-     */
-    /* package */ class ShowWebSuggestionsSettingChangeObserver
-            extends ContentObserver {
-        public ShowWebSuggestionsSettingChangeObserver() {
-            super(new Handler());
-        }
-
-        @Override
-        public void onChange(boolean selfChange) {
-            updateShowWebSuggestions();
-        }
-    }
-
-    private ShowWebSuggestionsSettingChangeObserver
-            mShowWebSuggestionsSettingChangeObserver;
-
-    // If non-null, then the system is set to show web suggestions,
-    // and this is the SearchableInfo to use to get them.
-    private SearchableInfo mSearchableInfo;
-
-    /**
-     * Check the system settings to see whether web suggestions are
-     * allowed.  If so, store the SearchableInfo to grab suggestions
-     * while the user is typing.
-     */
-    private void updateShowWebSuggestions() {
-        mSearchableInfo = null;
-        Context context = getContext();
-        if (Settings.System.getInt(context.getContentResolver(),
-                Settings.System.SHOW_WEB_SUGGESTIONS,
-                1 /* default on */) == 1) {
-            ComponentName webSearchComponent = mSearchManager.getWebSearchActivity();
-            if (webSearchComponent != null) {
-                mSearchableInfo = mSearchManager.getSearchableInfo(webSearchComponent);
-            }
-        }
-    }
-
     private void fixPicasaBookmark() {
         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
         Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
@@ -939,11 +888,15 @@
         if (bookmarksOnly || Patterns.WEB_URL.matcher(selectionArgs[0]).matches()) {
             return new MySuggestionCursor(c, null, "");
         } else {
-            // get Google suggest if there is still space in the list
-            if (myArgs != null && myArgs.length > 1 && mSearchableInfo != null
-                    && c.getCount() < (mMaxSuggestionShortSize - 1)) {
-                Cursor sc = mSearchManager.getSuggestions(mSearchableInfo, selectionArgs[0]);
-                return new MySuggestionCursor(c, sc, selectionArgs[0]);
+            // get search suggestions if there is still space in the list
+            if (myArgs != null && myArgs.length > 1
+                    && mSettings.getShowSearchSuggestions()
+                    && c.getCount() < (MAX_SUGGEST_SHORT_SMALL - 1)) {
+                SearchEngine searchEngine = mSettings.getSearchEngine();
+                if (searchEngine != null && searchEngine.supportsSuggestions()) {
+                    Cursor sc = searchEngine.getSuggestions(getContext(), selectionArgs[0]);
+                    return new MySuggestionCursor(c, sc, selectionArgs[0]);
+                }
             }
             return new MySuggestionCursor(c, null, selectionArgs[0]);
         }