OSDN Git Service

Deprecate fill_parent and introduce match_parent.
[android-x86/packages-apps-Browser.git] / src / com / android / browser / BrowserSettings.java
index d2371cd..941b28b 100644 (file)
@@ -1,3 +1,4 @@
+
 /*
  * Copyright (C) 2007 The Android Open Source Project
  *
@@ -18,6 +19,7 @@ package com.android.browser;
 
 import com.google.android.providers.GoogleSettings.Partner;
 
+import android.app.ActivityManager;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.pm.ActivityInfo;
@@ -25,8 +27,9 @@ import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.preference.PreferenceActivity;
 import android.preference.PreferenceScreen;
-import android.os.StatFs;
 import android.webkit.CookieManager;
+import android.webkit.GeolocationPermissions;
+import android.webkit.ValueCallback;
 import android.webkit.WebView;
 import android.webkit.WebViewDatabase;
 import android.webkit.WebIconDatabase;
@@ -36,6 +39,8 @@ import android.preference.PreferenceManager;
 import android.provider.Browser;
 
 import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
 import java.util.Observable;
 
 /*
@@ -60,7 +65,6 @@ class BrowserSettings extends Observable {
     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;
@@ -71,15 +75,21 @@ class BrowserSettings extends Observable {
     private boolean loginInitialized = false;
     private boolean autoFitPage = true;
     private boolean landscapeOnly = false;
+    private boolean loadsPageInOverviewMode = true;
     private boolean showDebugSettings = false;
-    private String databasePath; // default value set in loadFromDb()
-    private boolean databaseEnabled = true;
-    private long webStorageDefaultQuota = 5 * 1024 * 1024;
-    // The Browser always enables Application Caches.
+    // HTML5 API flags
     private boolean appCacheEnabled = true;
-    private String appCachePath;  // default value set in loadFromDb().
-    private long appCacheMaxSize = Long.MAX_VALUE;
+    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
+    // 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 = "";
 
     private final static String TAG = "BrowserSettings";
@@ -97,9 +107,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;
@@ -109,6 +116,7 @@ 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";
@@ -119,17 +127,16 @@ class BrowserSettings extends Observable {
             "privacy_clear_form_data";
     public final static String PREF_CLEAR_PASSWORDS =
             "privacy_clear_passwords";
-    public final static String PREF_DEFAULT_QUOTA =
-            "webstorage_default_quota";
     public final static String PREF_EXTRAS_RESET_DEFAULTS =
             "reset_default_preferences";
     public final static String PREF_DEBUG_SETTINGS = "debug_menu";
-    public final static String PREF_GEARS_SETTINGS = "gears_settings";
     public final static String PREF_WEBSITE_SETTINGS = "website_settings";
     public final static String PREF_TEXT_SIZE = "text_size";
     public final static String PREF_DEFAULT_ZOOM = "default_zoom";
     public final static String PREF_DEFAULT_TEXT_ENCODING =
             "default_text_encoding";
+    public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
+            "privacy_clear_geolocation_access";
 
     private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
             "U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.17 (KHTML, " +
@@ -195,23 +202,26 @@ class BrowserSettings extends Observable {
             s.setLightTouchEnabled(b.lightTouch);
             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);
 
-            s.setDatabasePath(b.databasePath);
+            // HTML5 API flags
+            s.setAppCacheEnabled(b.appCacheEnabled);
             s.setDatabaseEnabled(b.databaseEnabled);
             s.setDomStorageEnabled(b.domStorageEnabled);
-            s.setWebStorageDefaultQuota(b.webStorageDefaultQuota);
+            s.setWorkersEnabled(b.workersEnabled);  // This only affects V8.
+            s.setGeolocationEnabled(b.geolocationEnabled);
 
-            // Turn on Application Caches.
-            s.setAppCachePath(b.appCachePath);
-            s.setAppCacheEnabled(b.appCacheEnabled);
+            // 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(
@@ -231,19 +241,32 @@ class BrowserSettings extends Observable {
     public void loadFromDb(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.
-        appCacheMaxSize = getAppCacheMaxSize();
+        webStorageSizeManager = new WebStorageSizeManager(
+                ctx,
+                new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
+                new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
+        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();
 
         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
         // This call is TOO SLOW, need to manually keep the defaults
         // in sync
@@ -262,16 +285,6 @@ class BrowserSettings extends Observable {
                 javaScriptEnabled);
         pluginsEnabled = p.getBoolean("enable_plugins",
                 pluginsEnabled);
-        pluginsPath = p.getString("plugins_path", pluginsPath);
-        databasePath = p.getString("database_path", databasePath);
-        databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
-        webStorageDefaultQuota = Long.parseLong(p.getString(PREF_DEFAULT_QUOTA,
-                String.valueOf(webStorageDefaultQuota)));
-        appCacheEnabled = p.getBoolean("enable_appcache",
-                appCacheEnabled);
-        domStorageEnabled = p.getBoolean("enable_domstorage",
-                domStorageEnabled);
-        appCachePath = p.getString("appcache_path", appCachePath);
         javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
             "block_popup_windows",
             !javaScriptCanOpenWindowsAutomatically);
@@ -291,6 +304,8 @@ class BrowserSettings extends Observable {
         zoomDensity = WebSettings.ZoomDensity.valueOf(
                 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
         autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
+        loadsPageInOverviewMode = p.getBoolean("load_page",
+                loadsPageInOverviewMode);
         boolean landscapeOnlyTemp =
                 p.getBoolean("landscape_only", landscapeOnly);
         if (landscapeOnlyTemp != landscapeOnly) {
@@ -332,7 +347,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,
@@ -348,11 +362,14 @@ class BrowserSettings extends Observable {
         mTabControl.getBrowserActivity().setShouldShowErrorConsole(
                 showDebugSettings && showConsole);
 
-        update();
-    }
+        // HTML5 API flags
+        appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
+        databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
+        domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
+        geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
+        workersEnabled = p.getBoolean("enable_workers", workersEnabled);
 
-    public String getPluginsPath() {
-        return pluginsPath;
+        update();
     }
 
     public String getHomePage() {
@@ -363,6 +380,10 @@ class BrowserSettings extends Observable {
         return jsFlags;
     }
 
+    public WebStorageSizeManager getWebStorageSizeManager() {
+        return webStorageSizeManager;
+    }
+
     public void setHomePage(Context context, String url) {
         Editor ed = PreferenceManager.
                 getDefaultSharedPreferences(context).edit();
@@ -411,10 +432,6 @@ class BrowserSettings extends Observable {
         return navDump;
     }
 
-    public boolean doFlick() {
-        return doFlick;
-    }
-
     public boolean showDebugSettings() {
         return showDebugSettings;
     }
@@ -514,14 +531,36 @@ class BrowserSettings extends Observable {
         db.clearHttpAuthUsernamePassword();
     }
 
-    /*package*/ void clearDatabases(Context context) {
-        WebStorage.getInstance().deleteAllData();
-        // Remove all listed databases from the preferences
+    private void maybeDisableWebsiteSettings(Context context) {
         PreferenceActivity activity = (PreferenceActivity) context;
-        PreferenceScreen screen = (PreferenceScreen)
+        final PreferenceScreen screen = (PreferenceScreen)
             activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
-        screen.removeAll();
         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) {
+        WebStorage.getInstance().deleteAllData();
+        maybeDisableWebsiteSettings(context);
+    }
+
+    /*package*/ void clearLocationAccess(Context context) {
+        GeolocationPermissions.getInstance().clearAll();
+        maybeDisableWebsiteSettings(context);
     }
 
     /*package*/ void resetDefaultPreferences(Context ctx) {
@@ -532,6 +571,8 @@ class BrowserSettings extends Observable {
                 true);
         // reset homeUrl
         setHomePage(ctx, getFactoryResetHomeUrl(ctx));
+        // reset appcache max size
+        appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
     }
 
     private String getFactoryResetHomeUrl(Context context) {
@@ -543,35 +584,6 @@ class BrowserSettings extends Observable {
         return url;
     }
 
-    private long getAppCacheMaxSize() {
-        StatFs dataPartition = new StatFs(appCachePath);
-        long freeSpace = dataPartition.getAvailableBlocks()
-            * dataPartition.getBlockSize();
-        long fileSystemSize = dataPartition.getBlockCount()
-            * dataPartition.getBlockSize();
-        return calculateAppCacheMaxSize(fileSystemSize, freeSpace);
-    }
-
-    /*package*/ static long calculateAppCacheMaxSize(long fileSystemSizeBytes,
-            long freeSpaceBytes) {
-        if (fileSystemSizeBytes <= 0
-                || freeSpaceBytes <= 0
-                || freeSpaceBytes > fileSystemSizeBytes) {
-            return 0;
-        }
-
-        long fileSystemSizeRatio =
-            4 << ((int) Math.floor(Math.log10(fileSystemSizeBytes / (1024 * 1024))));
-        long maxSizeBytes = (long) Math.min(Math.floor(fileSystemSizeBytes / fileSystemSizeRatio),
-                Math.floor(freeSpaceBytes / 4));
-        // Round maxSizeBytes up to a multiple of 512KB (except when freeSpaceBytes < 1MB).
-        long maxSizeStepBytes = 512 * 1024;
-        if (freeSpaceBytes < maxSizeStepBytes * 2) {
-            return 0;
-        }
-        return (maxSizeStepBytes * ((maxSizeBytes / maxSizeStepBytes) + 1));
-    }
-
     // Private constructor that does nothing.
     private BrowserSettings() {
     }