OSDN Git Service

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