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