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