OSDN Git Service

Clear voice search mode when changing search engines.
[android-x86/packages-apps-Browser.git] / src / com / android / browser / BrowserSettings.java
index fd35269..92b4cac 100644 (file)
@@ -1,3 +1,4 @@
+
 /*
  * Copyright (C) 2007 The Android Open Source Project
  *
 
 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;
 import android.webkit.WebView;
 import android.webkit.WebViewDatabase;
 import android.webkit.WebIconDatabase;
@@ -35,8 +44,9 @@ import android.webkit.WebStorage;
 import android.preference.PreferenceManager;
 import android.provider.Browser;
 
-import java.util.Set;
 import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
 import java.util.Observable;
 
 /*
@@ -58,32 +68,34 @@ 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 String pluginsPath;  // default value set in loadFromDb().
-    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().
     private String databasePath; // default value set in loadFromDb()
+    private String geolocationDatabasePath; // default value set in loadFromDb()
     private WebStorageSizeManager webStorageSizeManager;
 
     private String jsFlags = "";
@@ -103,9 +115,6 @@ class BrowserSettings extends Observable {
     // The setting can be then toggled from the settings menu.
     private boolean showConsole = true;
 
-    // Browser only settings
-    private boolean doFlick = false;
-
     // Private preconfigured values
     private static int minimumFontSize = 8;
     private static int minimumLogicalFontSize = 8;
@@ -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,32 +213,27 @@ 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);
             // Browser supports multiple windows
             s.setSupportMultipleWindows(true);
-            // Turn off file access
-            s.setAllowFileAccess(false);
 
             // HTML5 API flags
             s.setAppCacheEnabled(b.appCacheEnabled);
             s.setDatabaseEnabled(b.databaseEnabled);
             s.setDomStorageEnabled(b.domStorageEnabled);
             s.setWorkersEnabled(b.workersEnabled);  // This only affects V8.
+            s.setGeolocationEnabled(b.geolocationEnabled);
 
             // HTML5 configuration parameters.
             s.setAppCacheMaxSize(b.appCacheMaxSize);
             s.setAppCachePath(b.appCachePath);
             s.setDatabasePath(b.databasePath);
+            s.setGeolocationDatabasePath(b.geolocationDatabasePath);
 
-            // Enable/Disable the error console.
-            b.mTabControl.getBrowserActivity().setShouldShowErrorConsole(
-                    b.showDebugSettings && b.showConsole);
-
-            // Configure the Geolocation permissions manager to deny all
-            // permission requests if Geolocation is disabled in the browser.
-            // TODO(steveblock): Implement
+            b.updateTabControlSettings();
         }
     }
 
@@ -239,13 +246,9 @@ 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 plugins path to the application's
-        // local directory.
-        pluginsPath = ctx.getDir("plugins", 0).getPath();
         // Set the default value for the Application Caches path.
         appCachePath = ctx.getDir("appcache", 0).getPath();
         // Determine the maximum size of the application cache.
@@ -256,28 +259,75 @@ class BrowserSettings extends Observable {
         appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
         // Set the default value for the Database path.
         databasePath = ctx.getDir("databases", 0).getPath();
+        // Set the default value for the Geolocation database path.
+        geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
+
+        if (p.getString(PREF_HOMEPAGE, "") == "") {
+            // No home page preferences is set, set it to default.
+            setHomePage(ctx, getFactoryResetHomeUrl(ctx));
+        }
 
-        homeUrl = 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);
-        pluginsPath = p.getString("plugins_path", pluginsPath);
+        pluginState = WebSettings.PluginState.valueOf(
+                p.getString("plugin_state", pluginState.name()));
         javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
             "block_popup_windows",
             !javaScriptCanOpenWindowsAutomatically);
@@ -291,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(
@@ -303,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) {
@@ -340,7 +386,6 @@ class BrowserSettings extends Observable {
             tracing = p.getBoolean("enable_tracing", tracing);
             lightTouch = p.getBoolean("enable_light_touch", lightTouch);
             navDump = p.getBoolean("enable_nav_dump", navDump);
-            doFlick = p.getBoolean("enable_flick", doFlick);
             userAgent = Integer.parseInt(p.getString("user_agent", "0"));
         }
         // JS flags is loaded from DB even if showDebugSettings is false,
@@ -353,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);
@@ -362,21 +405,34 @@ class BrowserSettings extends Observable {
         domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
         geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
         workersEnabled = p.getBoolean("enable_workers", workersEnabled);
-        // HTML 5 configuration params
-        appCachePath = p.getString("appcache_path", appCachePath);
-        databasePath = p.getString("database_path", databasePath);
 
         update();
     }
 
-    public String getPluginsPath() {
-        return pluginsPath;
+    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;
     }
@@ -393,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;
     }
@@ -433,10 +477,6 @@ class BrowserSettings extends Observable {
         return navDump;
     }
 
-    public boolean doFlick() {
-        return doFlick;
-    }
-
     public boolean showDebugSettings() {
         return showDebugSettings;
     }
@@ -493,6 +533,7 @@ class BrowserSettings extends Observable {
      */
     /* package */void setTabControl(TabControl tabControl) {
         mTabControl = tabControl;
+        updateTabControlSettings();
     }
 
     /*
@@ -536,17 +577,35 @@ 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) {
-        Set webStorageOrigins = WebStorage.getInstance().getOrigins();
-        Set geolocationOrigins =
-                 GeolocationPermissions.getInstance().getOrigins();
-        if (((webStorageOrigins == null) || webStorageOrigins.isEmpty()) &&
-            ((geolocationOrigins == null) || geolocationOrigins.isEmpty())) {
-            PreferenceActivity activity = (PreferenceActivity) context;
-            PreferenceScreen screen = (PreferenceScreen)
-                activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
-            screen.setEnabled(false);
-        }
+        PreferenceActivity activity = (PreferenceActivity) context;
+        final PreferenceScreen screen = (PreferenceScreen)
+            activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
+        screen.setEnabled(false);
+        WebStorage.getInstance().getOrigins(new ValueCallback<Map>() {
+            public void onReceiveValue(Map webStorageOrigins) {
+                if ((webStorageOrigins != null) && !webStorageOrigins.isEmpty()) {
+                    screen.setEnabled(true);
+                }
+            }
+        });
+
+        GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() {
+            public void onReceiveValue(Set<String> geolocationOrigins) {
+                if ((geolocationOrigins != null) && !geolocationOrigins.isEmpty()) {
+                    screen.setEnabled(true);
+                }
+            }
+        });
     }
 
     /*package*/ void clearDatabases(Context context) {
@@ -560,6 +619,7 @@ class BrowserSettings extends Observable {
     }
 
     /*package*/ void resetDefaultPreferences(Context ctx) {
+        reset();
         SharedPreferences p =
             PreferenceManager.getDefaultSharedPreferences(ctx);
         p.edit().clear().commit();
@@ -574,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
     }
 }