OSDN Git Service

Add user-selected search providers to browser
[android-x86/packages-apps-Browser.git] / src / com / android / browser / BrowserProvider.java
index 47c9bf9..96745e5 100644 (file)
 
 package com.android.browser;
 
+import com.android.browser.search.SearchEngine;
+
 import android.app.SearchManager;
-import android.app.SearchableInfo;
-import android.backup.BackupManager;
-import android.content.ComponentName;
+import android.app.backup.BackupManager;
 import android.content.ContentProvider;
 import android.content.ContentResolver;
 import android.content.ContentUris;
@@ -27,29 +27,21 @@ import android.content.ContentValues;
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
-import android.content.UriMatcher;
 import android.content.SharedPreferences.Editor;
-import android.content.pm.PackageManager;
-import android.content.pm.ResolveInfo;
+import android.content.UriMatcher;
 import android.database.AbstractCursor;
-import android.database.ContentObserver;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.net.Uri;
-import android.os.AsyncTask;
-import android.os.Handler;
+import android.os.Process;
 import android.preference.PreferenceManager;
 import android.provider.Browser;
-import android.provider.Settings;
 import android.provider.Browser.BookmarkColumns;
 import android.speech.RecognizerResultsIntent;
 import android.text.TextUtils;
 import android.util.Log;
 import android.util.Patterns;
-import android.util.TypedValue;
-import android.webkit.GeolocationPermissions;
-
 
 import java.io.File;
 import java.io.FilenameFilter;
@@ -71,7 +63,7 @@ public class BrowserProvider extends ContentProvider {
             "viewer?source=androidclient";
 
     private static final String[] TABLE_NAMES = new String[] {
-        "bookmarks", "searches", "geolocation"
+        "bookmarks", "searches"
     };
     private static final String[] SUGGEST_PROJECTION = new String[] {
             "_id", "url", "title", "bookmark", "user_entered"
@@ -86,10 +78,10 @@ public class BrowserProvider extends ContentProvider {
     private static final int SUGGEST_COLUMN_INTENT_DATA_ID = 2;
     private static final int SUGGEST_COLUMN_TEXT_1_ID = 3;
     private static final int SUGGEST_COLUMN_TEXT_2_ID = 4;
-    private static final int SUGGEST_COLUMN_ICON_1_ID = 5;
-    private static final int SUGGEST_COLUMN_ICON_2_ID = 6;
-    private static final int SUGGEST_COLUMN_QUERY_ID = 7;
-    private static final int SUGGEST_COLUMN_FORMAT = 8;
+    private static final int SUGGEST_COLUMN_TEXT_2_URL_ID = 5;
+    private static final int SUGGEST_COLUMN_ICON_1_ID = 6;
+    private static final int SUGGEST_COLUMN_ICON_2_ID = 7;
+    private static final int SUGGEST_COLUMN_QUERY_ID = 8;
     private static final int SUGGEST_COLUMN_INTENT_EXTRA_DATA = 9;
 
     // shared suggestion columns
@@ -99,10 +91,10 @@ public class BrowserProvider extends ContentProvider {
             SearchManager.SUGGEST_COLUMN_INTENT_DATA,
             SearchManager.SUGGEST_COLUMN_TEXT_1,
             SearchManager.SUGGEST_COLUMN_TEXT_2,
+            SearchManager.SUGGEST_COLUMN_TEXT_2_URL,
             SearchManager.SUGGEST_COLUMN_ICON_1,
             SearchManager.SUGGEST_COLUMN_ICON_2,
             SearchManager.SUGGEST_COLUMN_QUERY,
-            SearchManager.SUGGEST_COLUMN_FORMAT,
             SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA};
 
     private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3;
@@ -113,7 +105,6 @@ public class BrowserProvider extends ContentProvider {
     // make sure that these match the index of TABLE_NAMES
     private static final int URI_MATCH_BOOKMARKS = 0;
     private static final int URI_MATCH_SEARCHES = 1;
-    private static final int URI_MATCH_GEOLOCATION = 2;
     // (id % 10) should match the table name index
     private static final int URI_MATCH_BOOKMARKS_ID = 10;
     private static final int URI_MATCH_SEARCHES_ID = 11;
@@ -138,8 +129,6 @@ public class BrowserProvider extends ContentProvider {
         URI_MATCHER.addURI("browser",
                 TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY,
                 URI_MATCH_BOOKMARKS_SUGGEST);
-        URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_GEOLOCATION],
-                URI_MATCH_GEOLOCATION);
     }
 
     // 1 -> 2 add cache table
@@ -169,12 +158,7 @@ public class BrowserProvider extends ContentProvider {
     // optionally a trailing slash, all matched as separate groups.
     private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
 
-    private SearchManager mSearchManager;
-
-    // The ID of the ColorStateList to be applied to urls of website suggestions, as derived from
-    // the current theme. This is not set until/unless beautifyUrl is called, at which point
-    // this variable caches the color value.
-    private static String mSearchUrlColorId;
+    private BrowserSettings mSettings;
 
     public BrowserProvider() {
     }
@@ -308,15 +292,17 @@ public class BrowserProvider extends ContentProvider {
         }
 
         private void removeGears() {
-            AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
-                public Void doInBackground(Void... unused) {
+            new Thread() {
+                @Override
+                public void run() {
+                    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
                     String browserDataDirString = mContext.getApplicationInfo().dataDir;
                     final String appPluginsDirString = "app_plugins";
                     final String gearsPrefix = "gears";
                     File appPluginsDir = new File(browserDataDirString + File.separator
                             + appPluginsDirString);
                     if (!appPluginsDir.exists()) {
-                        return null;
+                        return;
                     }
                     // Delete the Gears plugin files
                     File[] gearsFiles = appPluginsDir.listFiles(new FilenameFilter() {
@@ -335,10 +321,9 @@ public class BrowserProvider extends ContentProvider {
                     File gearsDataDir = new File(browserDataDirString + File.separator
                             + gearsPrefix);
                     if (!gearsDataDir.exists()) {
-                        return null;
+                        return;
                     }
                     deleteDirectory(gearsDataDir);
-                    return null;
                 }
 
                 private void deleteDirectory(File currentDir) {
@@ -351,9 +336,7 @@ public class BrowserProvider extends ContentProvider {
                     }
                     currentDir.delete();
                 }
-            };
-
-            task.execute();
+            }.start();
         }
     }
 
@@ -376,59 +359,10 @@ public class BrowserProvider extends ContentProvider {
                 ed.commit();
             }
         }
-        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 " +
@@ -468,6 +402,7 @@ public class BrowserProvider extends ContentProvider {
         private String  mString;
         private int     mSuggestText1Id;
         private int     mSuggestText2Id;
+        private int     mSuggestText2UrlId;
         private int     mSuggestQueryId;
         private int     mSuggestIntentExtraDataId;
 
@@ -488,6 +423,7 @@ public class BrowserProvider extends ContentProvider {
             if (mSuggestCursor == null) {
                 mSuggestText1Id = -1;
                 mSuggestText2Id = -1;
+                mSuggestText2UrlId = -1;
                 mSuggestQueryId = -1;
                 mSuggestIntentExtraDataId = -1;
             } else {
@@ -495,6 +431,8 @@ public class BrowserProvider extends ContentProvider {
                                 SearchManager.SUGGEST_COLUMN_TEXT_1);
                 mSuggestText2Id = mSuggestCursor.getColumnIndex(
                                 SearchManager.SUGGEST_COLUMN_TEXT_2);
+                mSuggestText2UrlId = mSuggestCursor.getColumnIndex(
+                        SearchManager.SUGGEST_COLUMN_TEXT_2_URL);
                 mSuggestQueryId = mSuggestCursor.getColumnIndex(
                                 SearchManager.SUGGEST_COLUMN_QUERY);
                 mSuggestIntentExtraDataId = mSuggestCursor.getColumnIndex(
@@ -590,12 +528,22 @@ public class BrowserProvider extends ContentProvider {
                         if (type == 0) {
                             return getContext().getString(R.string.search_the_web);
                         } else if (type == 1) {
-                            return getHistorySubtitle();
+                            return null;  // Use TEXT_2_URL instead
                         } else {
                             if (mSuggestText2Id == -1) return null;
                             return mSuggestCursor.getString(mSuggestText2Id);
                         }
 
+                    case SUGGEST_COLUMN_TEXT_2_URL_ID:
+                        if (type == 0) {
+                            return null;
+                        } else if (type == 1) {
+                            return getHistoryUrl();
+                        } else {
+                            if (mSuggestText2UrlId == -1) return null;
+                            return mSuggestCursor.getString(mSuggestText2UrlId);
+                        }
+
                     case SUGGEST_COLUMN_ICON_1_ID:
                         if (type == 1) {
                             if (mHistoryCursor.getInt(3) == 1) {
@@ -630,9 +578,6 @@ public class BrowserProvider extends ContentProvider {
                             return mSuggestCursor.getString(mSuggestQueryId);
                         }
 
-                    case SUGGEST_COLUMN_FORMAT:
-                        return "html";
-
                     case SUGGEST_COLUMN_INTENT_EXTRA_DATA:
                         if (type == 0) {
                             return null;
@@ -718,7 +663,7 @@ public class BrowserProvider extends ContentProvider {
         private String getHistoryTitle() {
             String title = mHistoryCursor.getString(2 /* webpage title */);
             if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
-                title = beautifyUrl(mHistoryCursor.getString(1 /* url */));
+                title = stripUrl(mHistoryCursor.getString(1 /* url */));
             }
             return title;
         }
@@ -730,30 +675,15 @@ public class BrowserProvider extends ContentProvider {
          *
          * @return the subtitle string to use, or null if none
          */
-        private String getHistorySubtitle() {
+        private String getHistoryUrl() {
             String title = mHistoryCursor.getString(2 /* webpage title */);
             if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
                 return null;
             } else {
-                return beautifyUrl(mHistoryCursor.getString(1 /* url */));
+                return stripUrl(mHistoryCursor.getString(1 /* url */));
             }
         }
 
-        /**
-         * Strips "http://" from the beginning of a url and "/" from the end,
-         * and adds html formatting to make it green.
-         */
-        private String beautifyUrl(String url) {
-            if (mSearchUrlColorId == null) {
-                // Get the color used for this purpose from the current theme.
-                TypedValue colorValue = new TypedValue();
-                getContext().getTheme().resolveAttribute(
-                        com.android.internal.R.attr.textColorSearchUrl, colorValue, true);
-                mSearchUrlColorId = Integer.toString(colorValue.resourceId);
-            }
-
-            return "<font color=\"@" + mSearchUrlColorId + "\">" + stripUrl(url) + "</font>";
-        }
     }
 
     private static class ResultsCursor extends AbstractCursor {
@@ -849,9 +779,6 @@ public class BrowserProvider extends ContentProvider {
         if (match == -1) {
             throw new IllegalArgumentException("Unknown URL");
         }
-        if (match == URI_MATCH_GEOLOCATION) {
-            throw new UnsupportedOperationException("query() not supported for geolocation");
-        }
         if (match == URI_MATCH_SUGGEST && mResultsCursor != null) {
             Cursor results = mResultsCursor;
             mResultsCursor = null;
@@ -892,12 +819,15 @@ public class BrowserProvider extends ContentProvider {
                     || Patterns.WEB_URL.matcher(selectionArgs[0]).matches()) {
                 return new MySuggestionCursor(c, null, "");
             } else {
-                // get Google suggest if there is still space in the list
+                // get search suggestions if there is still space in the list
                 if (myArgs != null && myArgs.length > 1
-                        && mSearchableInfo != null
+                        && mSettings.getShowSearchSuggestions()
                         && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
-                    Cursor sc = mSearchManager.getSuggestions(mSearchableInfo, selectionArgs[0]);
-                    return new MySuggestionCursor(c, sc, selectionArgs[0]);
+                    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]);
             }
@@ -952,9 +882,6 @@ public class BrowserProvider extends ContentProvider {
             case URI_MATCH_SUGGEST:
                 return SearchManager.SUGGEST_MIME_TYPE;
 
-            case URI_MATCH_GEOLOCATION:
-                return "vnd.android.cursor.dir/geolocation";
-
             default:
                 throw new IllegalArgumentException("Unknown URL");
         }
@@ -991,16 +918,6 @@ public class BrowserProvider extends ContentProvider {
                 break;
             }
 
-            case URI_MATCH_GEOLOCATION:
-                String origin = initialValues.getAsString(Browser.GeolocationColumns.ORIGIN);
-                if (TextUtils.isEmpty(origin)) {
-                    throw new IllegalArgumentException("Empty origin");
-                }
-                GeolocationPermissions.getInstance().allow(origin);
-                // TODO: Should we have one URI per permission?
-                uri = Browser.GEOLOCATION_URI;
-                break;
-
             default:
                 throw new IllegalArgumentException("Unknown URL");
         }
@@ -1030,10 +947,6 @@ public class BrowserProvider extends ContentProvider {
             throw new IllegalArgumentException("Unknown URL");
         }
 
-        if (match == URI_MATCH_GEOLOCATION) {
-            return deleteGeolocation(url, where, whereArgs);
-        }
-
         // need to know whether it's the bookmarks table for a couple of reasons
         boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID);
         String id = null;
@@ -1072,19 +985,6 @@ public class BrowserProvider extends ContentProvider {
         return count;
     }
 
-    private int deleteGeolocation(Uri uri, String where, String[] whereArgs) {
-        if (whereArgs.length != 1) {
-            throw new IllegalArgumentException("Bad where arguments");
-        }
-        String origin = whereArgs[0];
-        if (TextUtils.isEmpty(origin)) {
-            throw new IllegalArgumentException("Empty origin");
-        }
-        GeolocationPermissions.getInstance().clear(origin);
-        getContext().getContentResolver().notifyChange(Browser.GEOLOCATION_URI, null);
-        return 1;  // We always return 1, to avoid having to check whether anything was actually removed
-    }
-
     @Override
     public int update(Uri url, ContentValues values, String where,
             String[] whereArgs) {