BrowserSettings.java revision b81735a96d5e44ff9acf275619ad797ec0467670
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
19import com.google.android.providers.GoogleSettings.Partner;
20
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.pm.ActivityInfo;
24import android.content.SharedPreferences;
25import android.content.SharedPreferences.Editor;
26import android.preference.PreferenceActivity;
27import android.preference.PreferenceScreen;
28import android.webkit.CookieManager;
29import android.webkit.GeolocationPermissions;
30import android.webkit.WebView;
31import android.webkit.WebViewDatabase;
32import android.webkit.WebIconDatabase;
33import android.webkit.WebSettings;
34import android.webkit.WebStorage;
35import android.preference.PreferenceManager;
36import android.provider.Browser;
37
38import java.util.Set;
39import java.util.HashMap;
40import java.util.Observable;
41
42/*
43 * Package level class for storing various WebView and Browser settings. To use
44 * this class:
45 * BrowserSettings s = BrowserSettings.getInstance();
46 * s.addObserver(webView.getSettings());
47 * s.loadFromDb(context); // Only needed on app startup
48 * s.javaScriptEnabled = true;
49 * ... // set any other settings
50 * s.update(); // this will update all the observers
51 *
52 * To remove an observer:
53 * s.deleteObserver(webView.getSettings());
54 */
55class BrowserSettings extends Observable {
56
57    // Private variables for settings
58    // NOTE: these defaults need to be kept in sync with the XML
59    // until the performance of PreferenceManager.setDefaultValues()
60    // is improved.
61    private boolean loadsImagesAutomatically = true;
62    private boolean javaScriptEnabled = true;
63    private boolean pluginsEnabled = true;
64    private String pluginsPath;  // default value set in loadFromDb().
65    private boolean javaScriptCanOpenWindowsAutomatically = false;
66    private boolean showSecurityWarnings = true;
67    private boolean rememberPasswords = true;
68    private boolean saveFormData = true;
69    private boolean openInBackground = false;
70    private String defaultTextEncodingName;
71    private String homeUrl = "";
72    private boolean loginInitialized = false;
73    private boolean autoFitPage = true;
74    private boolean landscapeOnly = false;
75    private boolean loadsPageInOverviewMode = true;
76    private boolean showDebugSettings = false;
77    // HTML5 API flags
78    private boolean appCacheEnabled = true;
79    private boolean databaseEnabled = true;
80    private boolean domStorageEnabled = true;
81    private boolean geolocationEnabled = true;
82    private boolean workersEnabled = true;  // only affects V8. JSC does not have a similar setting
83    // HTML5 API configuration params
84    private long appCacheMaxSize = Long.MAX_VALUE;
85    private String appCachePath;  // default value set in loadFromDb().
86    private String databasePath; // default value set in loadFromDb()
87    private WebStorageSizeManager webStorageSizeManager;
88
89    private String jsFlags = "";
90
91    private final static String TAG = "BrowserSettings";
92
93    // Development settings
94    public WebSettings.LayoutAlgorithm layoutAlgorithm =
95        WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
96    private boolean useWideViewPort = true;
97    private int userAgent = 0;
98    private boolean tracing = false;
99    private boolean lightTouch = false;
100    private boolean navDump = false;
101
102    // By default the error console is shown once the user navigates to about:debug.
103    // The setting can be then toggled from the settings menu.
104    private boolean showConsole = true;
105
106    // Browser only settings
107    private boolean doFlick = false;
108
109    // Private preconfigured values
110    private static int minimumFontSize = 8;
111    private static int minimumLogicalFontSize = 8;
112    private static int defaultFontSize = 16;
113    private static int defaultFixedFontSize = 13;
114    private static WebSettings.TextSize textSize =
115        WebSettings.TextSize.NORMAL;
116    private static WebSettings.ZoomDensity zoomDensity =
117        WebSettings.ZoomDensity.MEDIUM;
118
119    // Preference keys that are used outside this class
120    public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
121    public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
122    public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
123    public final static String PREF_HOMEPAGE = "homepage";
124    public final static String PREF_CLEAR_FORM_DATA =
125            "privacy_clear_form_data";
126    public final static String PREF_CLEAR_PASSWORDS =
127            "privacy_clear_passwords";
128    public final static String PREF_EXTRAS_RESET_DEFAULTS =
129            "reset_default_preferences";
130    public final static String PREF_DEBUG_SETTINGS = "debug_menu";
131    public final static String PREF_WEBSITE_SETTINGS = "website_settings";
132    public final static String PREF_TEXT_SIZE = "text_size";
133    public final static String PREF_DEFAULT_ZOOM = "default_zoom";
134    public final static String PREF_DEFAULT_TEXT_ENCODING =
135            "default_text_encoding";
136    public final static String PREF_CLEAR_LOCATION_ACCESS =
137            "privacy_clear_location_access";
138
139    private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
140            "U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.17 (KHTML, " +
141            "like Gecko) Version/4.0 Safari/530.17";
142
143    private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
144            "CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 " +
145            "(KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16";
146
147    // Value to truncate strings when adding them to a TextView within
148    // a ListView
149    public final static int MAX_TEXTVIEW_LEN = 80;
150
151    private TabControl mTabControl;
152
153    // Single instance of the BrowserSettings for use in the Browser app.
154    private static BrowserSettings sSingleton;
155
156    // Private map of WebSettings to Observer objects used when deleting an
157    // observer.
158    private HashMap<WebSettings,Observer> mWebSettingsToObservers =
159        new HashMap<WebSettings,Observer>();
160
161    /*
162     * An observer wrapper for updating a WebSettings object with the new
163     * settings after a call to BrowserSettings.update().
164     */
165    static class Observer implements java.util.Observer {
166        // Private WebSettings object that will be updated.
167        private WebSettings mSettings;
168
169        Observer(WebSettings w) {
170            mSettings = w;
171        }
172
173        public void update(Observable o, Object arg) {
174            BrowserSettings b = (BrowserSettings)o;
175            WebSettings s = mSettings;
176
177            s.setLayoutAlgorithm(b.layoutAlgorithm);
178            if (b.userAgent == 0) {
179                // use the default ua string
180                s.setUserAgentString(null);
181            } else if (b.userAgent == 1) {
182                s.setUserAgentString(DESKTOP_USERAGENT);
183            } else if (b.userAgent == 2) {
184                s.setUserAgentString(IPHONE_USERAGENT);
185            }
186            s.setUseWideViewPort(b.useWideViewPort);
187            s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
188            s.setJavaScriptEnabled(b.javaScriptEnabled);
189            s.setPluginsEnabled(b.pluginsEnabled);
190            s.setJavaScriptCanOpenWindowsAutomatically(
191                    b.javaScriptCanOpenWindowsAutomatically);
192            s.setDefaultTextEncodingName(b.defaultTextEncodingName);
193            s.setMinimumFontSize(b.minimumFontSize);
194            s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
195            s.setDefaultFontSize(b.defaultFontSize);
196            s.setDefaultFixedFontSize(b.defaultFixedFontSize);
197            s.setNavDump(b.navDump);
198            s.setTextSize(b.textSize);
199            s.setDefaultZoom(b.zoomDensity);
200            s.setLightTouchEnabled(b.lightTouch);
201            s.setSaveFormData(b.saveFormData);
202            s.setSavePassword(b.rememberPasswords);
203            s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
204
205            // WebView inside Browser doesn't want initial focus to be set.
206            s.setNeedInitialFocus(false);
207            // Browser supports multiple windows
208            s.setSupportMultipleWindows(true);
209            // Turn off file access
210            s.setAllowFileAccess(false);
211
212            // HTML5 API flags
213            s.setAppCacheEnabled(b.appCacheEnabled);
214            s.setDatabaseEnabled(b.databaseEnabled);
215            s.setDomStorageEnabled(b.domStorageEnabled);
216            s.setWorkersEnabled(b.workersEnabled);  // This only affects V8.
217
218            // HTML5 configuration parameters.
219            s.setAppCacheMaxSize(b.appCacheMaxSize);
220            s.setAppCachePath(b.appCachePath);
221            s.setDatabasePath(b.databasePath);
222
223            // Enable/Disable the error console.
224            b.mTabControl.getBrowserActivity().setShouldShowErrorConsole(
225                    b.showDebugSettings && b.showConsole);
226
227            // Configure the Geolocation permissions manager to deny all
228            // permission requests if Geolocation is disabled in the browser.
229            // TODO(steveblock): Implement
230        }
231    }
232
233    /**
234     * Load settings from the browser app's database.
235     * NOTE: Strings used for the preferences must match those specified
236     * in the browser_preferences.xml
237     * @param ctx A Context object used to query the browser's settings
238     *            database. If the database exists, the saved settings will be
239     *            stored in this BrowserSettings object. This will update all
240     *            observers of this object.
241     */
242    public void loadFromDb(Context ctx) {
243        SharedPreferences p =
244                PreferenceManager.getDefaultSharedPreferences(ctx);
245
246        // Set the default value for the plugins path to the application's
247        // local directory.
248        pluginsPath = ctx.getDir("plugins", 0).getPath();
249        // Set the default value for the Application Caches path.
250        appCachePath = ctx.getDir("appcache", 0).getPath();
251        // Determine the maximum size of the application cache.
252        webStorageSizeManager = new WebStorageSizeManager(
253                ctx,
254                new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
255                new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
256        appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
257        // Set the default value for the Database path.
258        databasePath = ctx.getDir("databases", 0).getPath();
259
260        homeUrl = getFactoryResetHomeUrl(ctx);
261
262        // Load the defaults from the xml
263        // This call is TOO SLOW, need to manually keep the defaults
264        // in sync
265        //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
266        syncSharedPreferences(p);
267    }
268
269    /* package */ void syncSharedPreferences(SharedPreferences p) {
270
271        homeUrl =
272            p.getString(PREF_HOMEPAGE, homeUrl);
273
274        loadsImagesAutomatically = p.getBoolean("load_images",
275                loadsImagesAutomatically);
276        javaScriptEnabled = p.getBoolean("enable_javascript",
277                javaScriptEnabled);
278        pluginsEnabled = p.getBoolean("enable_plugins",
279                pluginsEnabled);
280        pluginsPath = p.getString("plugins_path", pluginsPath);
281        javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
282            "block_popup_windows",
283            !javaScriptCanOpenWindowsAutomatically);
284        showSecurityWarnings = p.getBoolean("show_security_warnings",
285                showSecurityWarnings);
286        rememberPasswords = p.getBoolean("remember_passwords",
287                rememberPasswords);
288        saveFormData = p.getBoolean("save_formdata",
289                saveFormData);
290        boolean accept_cookies = p.getBoolean("accept_cookies",
291                CookieManager.getInstance().acceptCookie());
292        CookieManager.getInstance().setAcceptCookie(accept_cookies);
293        openInBackground = p.getBoolean("open_in_background", openInBackground);
294        loginInitialized = p.getBoolean("login_initialized", loginInitialized);
295        textSize = WebSettings.TextSize.valueOf(
296                p.getString(PREF_TEXT_SIZE, textSize.name()));
297        zoomDensity = WebSettings.ZoomDensity.valueOf(
298                p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
299        autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
300        loadsPageInOverviewMode = p.getBoolean("load_page",
301                loadsPageInOverviewMode);
302        boolean landscapeOnlyTemp =
303                p.getBoolean("landscape_only", landscapeOnly);
304        if (landscapeOnlyTemp != landscapeOnly) {
305            landscapeOnly = landscapeOnlyTemp;
306            mTabControl.getBrowserActivity().setRequestedOrientation(
307                    landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
308                    : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
309        }
310        useWideViewPort = true; // use wide view port for either setting
311        if (autoFitPage) {
312            layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
313        } else {
314            layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
315        }
316        defaultTextEncodingName =
317                p.getString(PREF_DEFAULT_TEXT_ENCODING,
318                        defaultTextEncodingName);
319
320        showDebugSettings =
321                p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
322        // Debug menu items have precidence if the menu is visible
323        if (showDebugSettings) {
324            boolean small_screen = p.getBoolean("small_screen",
325                    layoutAlgorithm ==
326                    WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
327            if (small_screen) {
328                layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
329            } else {
330                boolean normal_layout = p.getBoolean("normal_layout",
331                        layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
332                if (normal_layout) {
333                    layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
334                } else {
335                    layoutAlgorithm =
336                            WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
337                }
338            }
339            useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
340            tracing = p.getBoolean("enable_tracing", tracing);
341            lightTouch = p.getBoolean("enable_light_touch", lightTouch);
342            navDump = p.getBoolean("enable_nav_dump", navDump);
343            doFlick = p.getBoolean("enable_flick", doFlick);
344            userAgent = Integer.parseInt(p.getString("user_agent", "0"));
345        }
346        // JS flags is loaded from DB even if showDebugSettings is false,
347        // so that it can be set once and be effective all the time.
348        jsFlags = p.getString("js_engine_flags", "");
349
350        // Read the setting for showing/hiding the JS Console always so that should the
351        // user enable debug settings, we already know if we should show the console.
352        // The user will never see the console unless they navigate to about:debug,
353        // regardless of the setting we read here. This setting is only used after debug
354        // is enabled.
355        showConsole = p.getBoolean("javascript_console", showConsole);
356        mTabControl.getBrowserActivity().setShouldShowErrorConsole(
357                showDebugSettings && showConsole);
358
359        // HTML5 API flags
360        appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
361        databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
362        domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
363        geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
364        workersEnabled = p.getBoolean("enable_workers", workersEnabled);
365        // HTML 5 configuration params
366        appCachePath = p.getString("appcache_path", appCachePath);
367        databasePath = p.getString("database_path", databasePath);
368
369        update();
370    }
371
372    public String getPluginsPath() {
373        return pluginsPath;
374    }
375
376    public String getHomePage() {
377        return homeUrl;
378    }
379
380    public String getJsFlags() {
381        return jsFlags;
382    }
383
384    public WebStorageSizeManager getWebStorageSizeManager() {
385        return webStorageSizeManager;
386    }
387
388    public void setHomePage(Context context, String url) {
389        Editor ed = PreferenceManager.
390                getDefaultSharedPreferences(context).edit();
391        ed.putString(PREF_HOMEPAGE, url);
392        ed.commit();
393        homeUrl = url;
394    }
395
396    public boolean isLoginInitialized() {
397        return loginInitialized;
398    }
399
400    public void setLoginInitialized(Context context) {
401        loginInitialized = true;
402        Editor ed = PreferenceManager.
403                getDefaultSharedPreferences(context).edit();
404        ed.putBoolean("login_initialized", loginInitialized);
405        ed.commit();
406    }
407
408    public WebSettings.TextSize getTextSize() {
409        return textSize;
410    }
411
412    public WebSettings.ZoomDensity getDefaultZoom() {
413        return zoomDensity;
414    }
415
416    public boolean openInBackground() {
417        return openInBackground;
418    }
419
420    public boolean showSecurityWarnings() {
421        return showSecurityWarnings;
422    }
423
424    public boolean isTracing() {
425        return tracing;
426    }
427
428    public boolean isLightTouch() {
429        return lightTouch;
430    }
431
432    public boolean isNavDump() {
433        return navDump;
434    }
435
436    public boolean doFlick() {
437        return doFlick;
438    }
439
440    public boolean showDebugSettings() {
441        return showDebugSettings;
442    }
443
444    public void toggleDebugSettings() {
445        showDebugSettings = !showDebugSettings;
446        navDump = showDebugSettings;
447        update();
448    }
449
450    /**
451     * Add a WebSettings object to the list of observers that will be updated
452     * when update() is called.
453     *
454     * @param s A WebSettings object that is strictly tied to the life of a
455     *            WebView.
456     */
457    public Observer addObserver(WebSettings s) {
458        Observer old = mWebSettingsToObservers.get(s);
459        if (old != null) {
460            super.deleteObserver(old);
461        }
462        Observer o = new Observer(s);
463        mWebSettingsToObservers.put(s, o);
464        super.addObserver(o);
465        return o;
466    }
467
468    /**
469     * Delete the given WebSettings observer from the list of observers.
470     * @param s The WebSettings object to be deleted.
471     */
472    public void deleteObserver(WebSettings s) {
473        Observer o = mWebSettingsToObservers.get(s);
474        if (o != null) {
475            mWebSettingsToObservers.remove(s);
476            super.deleteObserver(o);
477        }
478    }
479
480    /*
481     * Package level method for obtaining a single app instance of the
482     * BrowserSettings.
483     */
484    /*package*/ static BrowserSettings getInstance() {
485        if (sSingleton == null ) {
486            sSingleton = new BrowserSettings();
487        }
488        return sSingleton;
489    }
490
491    /*
492     * Package level method for associating the BrowserSettings with TabControl
493     */
494    /* package */void setTabControl(TabControl tabControl) {
495        mTabControl = tabControl;
496    }
497
498    /*
499     * Update all the observers of the object.
500     */
501    /*package*/ void update() {
502        setChanged();
503        notifyObservers();
504    }
505
506    /*package*/ void clearCache(Context context) {
507        WebIconDatabase.getInstance().removeAllIcons();
508        if (mTabControl != null) {
509            WebView current = mTabControl.getCurrentWebView();
510            if (current != null) {
511                current.clearCache(true);
512            }
513        }
514    }
515
516    /*package*/ void clearCookies(Context context) {
517        CookieManager.getInstance().removeAllCookie();
518    }
519
520    /* package */void clearHistory(Context context) {
521        ContentResolver resolver = context.getContentResolver();
522        Browser.clearHistory(resolver);
523        Browser.clearSearches(resolver);
524    }
525
526    /* package */ void clearFormData(Context context) {
527        WebViewDatabase.getInstance(context).clearFormData();
528        if (mTabControl != null) {
529            mTabControl.getCurrentTopWebView().clearFormData();
530        }
531    }
532
533    /*package*/ void clearPasswords(Context context) {
534        WebViewDatabase db = WebViewDatabase.getInstance(context);
535        db.clearUsernamePassword();
536        db.clearHttpAuthUsernamePassword();
537    }
538
539    private void maybeDisableWebsiteSettings(Context context) {
540        Set webStorageOrigins = WebStorage.getInstance().getOrigins();
541        Set geolocationOrigins =
542                 GeolocationPermissions.getInstance().getOrigins();
543        if (((webStorageOrigins == null) || webStorageOrigins.isEmpty()) &&
544            ((geolocationOrigins == null) || geolocationOrigins.isEmpty())) {
545            PreferenceActivity activity = (PreferenceActivity) context;
546            PreferenceScreen screen = (PreferenceScreen)
547                activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
548            screen.setEnabled(false);
549        }
550    }
551
552    /*package*/ void clearDatabases(Context context) {
553        WebStorage.getInstance().deleteAllData();
554        maybeDisableWebsiteSettings(context);
555    }
556
557    /*package*/ void clearLocationAccess(Context context) {
558        GeolocationPermissions.getInstance().clearAll();
559        maybeDisableWebsiteSettings(context);
560    }
561
562    /*package*/ void resetDefaultPreferences(Context ctx) {
563        SharedPreferences p =
564            PreferenceManager.getDefaultSharedPreferences(ctx);
565        p.edit().clear().commit();
566        PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
567                true);
568        // reset homeUrl
569        setHomePage(ctx, getFactoryResetHomeUrl(ctx));
570        // reset appcache max size
571        appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
572    }
573
574    private String getFactoryResetHomeUrl(Context context) {
575        String url = context.getResources().getString(R.string.homepage_base);
576        if (url.indexOf("{CID}") != -1) {
577            url = url.replace("{CID}", Partner.getString(context
578                    .getContentResolver(), Partner.CLIENT_ID, "android-google"));
579        }
580        return url;
581    }
582
583    // Private constructor that does nothing.
584    private BrowserSettings() {
585    }
586}
587