OSDN Git Service

Clear voice search mode when changing search engines.
[android-x86/packages-apps-Browser.git] / src / com / android / browser / BrowserSettings.java
index d7b091f..92b4cac 100644 (file)
 
 package com.android.browser;
 
-import com.google.android.providers.GoogleSettings.Partner;
+import com.android.browser.search.SearchEngine;
+import com.android.browser.search.SearchEngines;
 
+import android.app.ActivityManager;
+import android.content.ComponentName;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.pm.ActivityInfo;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
+import android.database.ContentObserver;
+import android.os.Handler;
 import android.preference.PreferenceActivity;
 import android.preference.PreferenceScreen;
+import android.provider.Settings;
+import android.util.Log;
 import android.webkit.CookieManager;
 import android.webkit.GeolocationPermissions;
 import android.webkit.ValueCallback;
@@ -61,27 +68,29 @@ class BrowserSettings extends Observable {
     // NOTE: these defaults need to be kept in sync with the XML
     // until the performance of PreferenceManager.setDefaultValues()
     // is improved.
-    private boolean loadsImagesAutomatically = true;
-    private boolean javaScriptEnabled = true;
-    private boolean pluginsEnabled = true;
-    private boolean javaScriptCanOpenWindowsAutomatically = false;
-    private boolean showSecurityWarnings = true;
-    private boolean rememberPasswords = true;
-    private boolean saveFormData = true;
-    private boolean openInBackground = false;
+    // Note: boolean variables are set inside reset function.
+    private boolean loadsImagesAutomatically;
+    private boolean javaScriptEnabled;
+    private WebSettings.PluginState pluginState;
+    private boolean javaScriptCanOpenWindowsAutomatically;
+    private boolean showSecurityWarnings;
+    private boolean rememberPasswords;
+    private boolean saveFormData;
+    private boolean openInBackground;
     private String defaultTextEncodingName;
     private String homeUrl = "";
-    private boolean loginInitialized = false;
-    private boolean autoFitPage = true;
-    private boolean landscapeOnly = false;
-    private boolean loadsPageInOverviewMode = true;
-    private boolean showDebugSettings = false;
+    private SearchEngine searchEngine;
+    private boolean showSearchSuggestions;
+    private boolean autoFitPage;
+    private boolean landscapeOnly;
+    private boolean loadsPageInOverviewMode;
+    private boolean showDebugSettings;
     // HTML5 API flags
-    private boolean appCacheEnabled = true;
-    private boolean databaseEnabled = true;
-    private boolean domStorageEnabled = true;
-    private boolean geolocationEnabled = true;
-    private boolean workersEnabled = true;  // only affects V8. JSC does not have a similar setting
+    private boolean appCacheEnabled;
+    private boolean databaseEnabled;
+    private boolean domStorageEnabled;
+    private boolean geolocationEnabled;
+    private boolean workersEnabled;  // only affects V8. JSC does not have a similar setting
     // HTML5 API configuration params
     private long appCacheMaxSize = Long.MAX_VALUE;
     private String appCachePath;  // default value set in loadFromDb().
@@ -115,12 +124,15 @@ class BrowserSettings extends Observable {
         WebSettings.TextSize.NORMAL;
     private static WebSettings.ZoomDensity zoomDensity =
         WebSettings.ZoomDensity.MEDIUM;
+    private static int pageCacheCapacity;
 
     // Preference keys that are used outside this class
     public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
     public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
     public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
     public final static String PREF_HOMEPAGE = "homepage";
+    public final static String PREF_SEARCH_ENGINE = "search_engine";
+    public final static String PREF_SHOW_SEARCH_SUGGESTIONS = "show_search_suggestions";
     public final static String PREF_CLEAR_FORM_DATA =
             "privacy_clear_form_data";
     public final static String PREF_CLEAR_PASSWORDS =
@@ -186,7 +198,7 @@ class BrowserSettings extends Observable {
             s.setUseWideViewPort(b.useWideViewPort);
             s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
             s.setJavaScriptEnabled(b.javaScriptEnabled);
-            s.setPluginsEnabled(b.pluginsEnabled);
+            s.setPluginState(b.pluginState);
             s.setJavaScriptCanOpenWindowsAutomatically(
                     b.javaScriptCanOpenWindowsAutomatically);
             s.setDefaultTextEncodingName(b.defaultTextEncodingName);
@@ -201,6 +213,7 @@ class BrowserSettings extends Observable {
             s.setSaveFormData(b.saveFormData);
             s.setSavePassword(b.rememberPasswords);
             s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
+            s.setPageCacheCapacity(pageCacheCapacity);
 
             // WebView inside Browser doesn't want initial focus to be set.
             s.setNeedInitialFocus(false);
@@ -220,9 +233,7 @@ class BrowserSettings extends Observable {
             s.setDatabasePath(b.databasePath);
             s.setGeolocationDatabasePath(b.geolocationDatabasePath);
 
-            // Enable/Disable the error console.
-            b.mTabControl.getBrowserActivity().setShouldShowErrorConsole(
-                    b.showDebugSettings && b.showConsole);
+            b.updateTabControlSettings();
         }
     }
 
@@ -235,7 +246,7 @@ class BrowserSettings extends Observable {
      *            stored in this BrowserSettings object. This will update all
      *            observers of this object.
      */
-    public void loadFromDb(Context ctx) {
+    public void loadFromDb(final Context ctx) {
         SharedPreferences p =
                 PreferenceManager.getDefaultSharedPreferences(ctx);
         // Set the default value for the Application Caches path.
@@ -251,26 +262,72 @@ class BrowserSettings extends Observable {
         // Set the default value for the Geolocation database path.
         geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
 
-        homeUrl = getFactoryResetHomeUrl(ctx);
+        if (p.getString(PREF_HOMEPAGE, "") == "") {
+            // No home page preferences is set, set it to default.
+            setHomePage(ctx, getFactoryResetHomeUrl(ctx));
+        }
+
+        // the cost of one cached page is ~3M (measured using nytimes.com). For
+        // low end devices, we only cache one page. For high end devices, we try
+        // to cache more pages, currently choose 5.
+        ActivityManager am = (ActivityManager) ctx
+                .getSystemService(Context.ACTIVITY_SERVICE);
+        if (am.getMemoryClass() > 16) {
+            pageCacheCapacity = 5;
+        } else {
+            pageCacheCapacity = 1;
+        }
 
-        // Load the defaults from the xml
+        final ContentResolver cr = ctx.getContentResolver();
+        cr.registerContentObserver(
+                Settings.System.getUriFor(Settings.System.SHOW_WEB_SUGGESTIONS), false,
+                new ContentObserver(new Handler()) {
+                        @Override
+                        public void onChange(boolean selfChange) {
+                            SharedPreferences p =
+                                    PreferenceManager.getDefaultSharedPreferences(ctx);
+                            updateShowWebSuggestions(cr, p);
+                        }
+                });
+        updateShowWebSuggestions(cr, p);
+
+    // Load the defaults from the xml
         // This call is TOO SLOW, need to manually keep the defaults
         // in sync
         //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
-        syncSharedPreferences(p);
+        syncSharedPreferences(ctx, p);
     }
 
-    /* package */ void syncSharedPreferences(SharedPreferences p) {
+    /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
 
         homeUrl =
             p.getString(PREF_HOMEPAGE, homeUrl);
+        String searchEngineName = p.getString(PREF_SEARCH_ENGINE, null);
+        if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) {
+            if (searchEngine != null) {
+                if (searchEngine.supportsVoiceSearch()) {
+                    // One or more tabs could have been in voice search mode.
+                    // Clear it, since the new SearchEngine may not support
+                    // it, or may handle it differently.
+                    for (int i = 0; i < mTabControl.getTabCount(); i++) {
+                        mTabControl.getTab(i).revertVoiceSearchMode();
+                    }
+                }
+                searchEngine.close();
+            }
+            searchEngine = SearchEngines.get(ctx, searchEngineName);
+        }
+        Log.i(TAG, "Selected search engine: " + searchEngine);
+        showSearchSuggestions = p.getBoolean(PREF_SHOW_SEARCH_SUGGESTIONS, true);
+        // Persist to system settings
+        saveShowWebSuggestions(ctx.getContentResolver());
 
         loadsImagesAutomatically = p.getBoolean("load_images",
                 loadsImagesAutomatically);
         javaScriptEnabled = p.getBoolean("enable_javascript",
                 javaScriptEnabled);
-        pluginsEnabled = p.getBoolean("enable_plugins",
-                pluginsEnabled);
+        pluginState = WebSettings.PluginState.valueOf(
+                p.getString("plugin_state", pluginState.name()));
         javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
             "block_popup_windows",
             !javaScriptCanOpenWindowsAutomatically);
@@ -284,7 +341,6 @@ class BrowserSettings extends Observable {
                 CookieManager.getInstance().acceptCookie());
         CookieManager.getInstance().setAcceptCookie(accept_cookies);
         openInBackground = p.getBoolean("open_in_background", openInBackground);
-        loginInitialized = p.getBoolean("login_initialized", loginInitialized);
         textSize = WebSettings.TextSize.valueOf(
                 p.getString(PREF_TEXT_SIZE, textSize.name()));
         zoomDensity = WebSettings.ZoomDensity.valueOf(
@@ -296,9 +352,6 @@ class BrowserSettings extends Observable {
                 p.getBoolean("landscape_only", landscapeOnly);
         if (landscapeOnlyTemp != landscapeOnly) {
             landscapeOnly = landscapeOnlyTemp;
-            mTabControl.getBrowserActivity().setRequestedOrientation(
-                    landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
-                    : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
         }
         useWideViewPort = true; // use wide view port for either setting
         if (autoFitPage) {
@@ -345,8 +398,6 @@ class BrowserSettings extends Observable {
         // regardless of the setting we read here. This setting is only used after debug
         // is enabled.
         showConsole = p.getBoolean("javascript_console", showConsole);
-        mTabControl.getBrowserActivity().setShouldShowErrorConsole(
-                showDebugSettings && showConsole);
 
         // HTML5 API flags
         appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
@@ -358,10 +409,30 @@ class BrowserSettings extends Observable {
         update();
     }
 
+    private void saveShowWebSuggestions(ContentResolver cr) {
+        int value = showSearchSuggestions ? 1 : 0;
+        Settings.System.putInt(cr, Settings.System.SHOW_WEB_SUGGESTIONS, value);
+    }
+
+    private void updateShowWebSuggestions(ContentResolver cr, SharedPreferences p) {
+        showSearchSuggestions =
+                Settings.System.getInt(cr,
+                        Settings.System.SHOW_WEB_SUGGESTIONS, 1) == 1;
+        p.edit().putBoolean(PREF_SHOW_SEARCH_SUGGESTIONS, showSearchSuggestions).commit();
+    }
+
     public String getHomePage() {
         return homeUrl;
     }
 
+    public SearchEngine getSearchEngine() {
+        return searchEngine;
+    }
+
+    public boolean getShowSearchSuggestions() {
+        return showSearchSuggestions;
+    }
+
     public String getJsFlags() {
         return jsFlags;
     }
@@ -378,18 +449,6 @@ class BrowserSettings extends Observable {
         homeUrl = url;
     }
 
-    public boolean isLoginInitialized() {
-        return loginInitialized;
-    }
-
-    public void setLoginInitialized(Context context) {
-        loginInitialized = true;
-        Editor ed = PreferenceManager.
-                getDefaultSharedPreferences(context).edit();
-        ed.putBoolean("login_initialized", loginInitialized);
-        ed.commit();
-    }
-
     public WebSettings.TextSize getTextSize() {
         return textSize;
     }
@@ -474,6 +533,7 @@ class BrowserSettings extends Observable {
      */
     /* package */void setTabControl(TabControl tabControl) {
         mTabControl = tabControl;
+        updateTabControlSettings();
     }
 
     /*
@@ -517,6 +577,15 @@ class BrowserSettings extends Observable {
         db.clearHttpAuthUsernamePassword();
     }
 
+    private void updateTabControlSettings() {
+        // Enable/disable the error console.
+        mTabControl.getBrowserActivity().setShouldShowErrorConsole(
+            showDebugSettings && showConsole);
+        mTabControl.getBrowserActivity().setRequestedOrientation(
+            landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
+            : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
+    }
+
     private void maybeDisableWebsiteSettings(Context context) {
         PreferenceActivity activity = (PreferenceActivity) context;
         final PreferenceScreen screen = (PreferenceScreen)
@@ -550,6 +619,7 @@ class BrowserSettings extends Observable {
     }
 
     /*package*/ void resetDefaultPreferences(Context ctx) {
+        reset();
         SharedPreferences p =
             PreferenceManager.getDefaultSharedPreferences(ctx);
         p.edit().clear().commit();
@@ -564,13 +634,39 @@ class BrowserSettings extends Observable {
     private String getFactoryResetHomeUrl(Context context) {
         String url = context.getResources().getString(R.string.homepage_base);
         if (url.indexOf("{CID}") != -1) {
-            url = url.replace("{CID}", Partner.getString(context
-                    .getContentResolver(), Partner.CLIENT_ID, "android-google"));
+            url = url.replace("{CID}",
+                    BrowserProvider.getClientId(context.getContentResolver()));
         }
         return url;
     }
 
     // Private constructor that does nothing.
     private BrowserSettings() {
+        reset();
+    }
+
+    private void reset() {
+        // Private variables for settings
+        // NOTE: these defaults need to be kept in sync with the XML
+        // until the performance of PreferenceManager.setDefaultValues()
+        // is improved.
+        loadsImagesAutomatically = true;
+        javaScriptEnabled = true;
+        pluginState = WebSettings.PluginState.ON;
+        javaScriptCanOpenWindowsAutomatically = false;
+        showSecurityWarnings = true;
+        rememberPasswords = true;
+        saveFormData = true;
+        openInBackground = false;
+        autoFitPage = true;
+        landscapeOnly = false;
+        loadsPageInOverviewMode = true;
+        showDebugSettings = false;
+        // HTML5 API flags
+        appCacheEnabled = true;
+        databaseEnabled = true;
+        domStorageEnabled = true;
+        geolocationEnabled = true;
+        workersEnabled = true;  // only affects V8. JSC does not have a similar setting
     }
 }