OSDN Git Service

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