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