blob: dcc3269c763d5f0ad1bb1e040c34065fd4494148 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
John Reck35e9dd62011-04-25 09:01:54 -07002 * Copyright (C) 2011 The Android Open Source Project
The Android Open Source Project0c908882009-03-03 19:32:16 -08003 *
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
Grace Kloba9804c432009-12-02 11:07:40 -080019import android.app.ActivityManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080020import android.content.ContentResolver;
21import android.content.Context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080022import android.content.SharedPreferences;
John Reck812d2d62011-01-18 14:16:15 -080023import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
John Reckf48314f2011-04-27 17:52:17 -070024import android.os.Build;
Ben Murdoch23da30e2010-10-26 15:18:44 +010025import android.os.Message;
Ben Murdoch0cb81892010-10-08 12:41:33 +010026import android.preference.PreferenceManager;
Ben Murdoch0cb81892010-10-08 12:41:33 +010027import android.provider.Browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010029import android.webkit.GeolocationPermissions;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.webkit.WebIconDatabase;
31import android.webkit.WebSettings;
Ben Murdoch0cb81892010-10-08 12:41:33 +010032import android.webkit.WebSettings.AutoFillProfile;
John Reck35e9dd62011-04-25 09:01:54 -070033import android.webkit.WebSettings.LayoutAlgorithm;
34import android.webkit.WebSettings.PluginState;
35import android.webkit.WebSettings.TextSize;
36import android.webkit.WebSettings.ZoomDensity;
Nicolas Roard78a98e42009-05-11 13:34:17 +010037import android.webkit.WebStorage;
John Reck812d2d62011-01-18 14:16:15 -080038import android.webkit.WebView;
39import android.webkit.WebViewDatabase;
The Android Open Source Project0c908882009-03-03 19:32:16 -080040
John Reck46500332011-06-07 14:36:10 -070041import com.android.browser.homepages.HomeProvider;
42import com.android.browser.provider.BrowserProvider;
43import com.android.browser.search.SearchEngine;
44import com.android.browser.search.SearchEngines;
45
John Reck35e9dd62011-04-25 09:01:54 -070046import java.lang.ref.WeakReference;
47import java.util.Iterator;
48import java.util.LinkedList;
John Reckb8b2af82011-05-20 15:58:33 -070049import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080050
John Reck35e9dd62011-04-25 09:01:54 -070051/**
52 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080053 */
John Reck35e9dd62011-04-25 09:01:54 -070054public class BrowserSettings implements OnSharedPreferenceChangeListener,
55 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010056
John Reck35e9dd62011-04-25 09:01:54 -070057 // TODO: Do something with this UserAgent stuff
John Reck7fd1e8f2011-04-27 18:22:57 -070058 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " +
59 "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " +
60 "Chrome/11.0.696.34 Safari/534.24";
The Android Open Source Project0c908882009-03-03 19:32:16 -080061
62 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070063 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
64 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
Bart Searsf6915fb2010-07-08 19:58:22 -070065
66 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070067 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
68 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
Bart Searsf6915fb2010-07-08 19:58:22 -070069
70 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070071 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
72 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -080073
John Reck7fd1e8f2011-04-27 18:22:57 -070074 private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " +
75 "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " +
76 "(KHTML, like Gecko) Version/4.0 Safari/534.13";
77
John Reck35e9dd62011-04-25 09:01:54 -070078 private static final String USER_AGENTS[] = { null,
79 DESKTOP_USERAGENT,
80 IPHONE_USERAGENT,
81 IPAD_USERAGENT,
John Reck7fd1e8f2011-04-27 18:22:57 -070082 FROYO_USERAGENT,
83 HONEYCOMB_USERAGENT,
John Reck35e9dd62011-04-25 09:01:54 -070084 };
The Android Open Source Project0c908882009-03-03 19:32:16 -080085
John Reck8fc22a12011-06-16 14:57:41 -070086 // The minimum min font size
87 // Aka, the lower bounds for the min font size range
88 // which is 1:5..24
89 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -070090 // The initial value in the text zoom range
91 // This is what represents 100% in the SeekBarPreference range
92 private static final int TEXT_ZOOM_START_VAL = 10;
93 // The size of a single step in the text zoom range, in percent
94 private static final int TEXT_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -070095
John Reck35e9dd62011-04-25 09:01:54 -070096 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -070097
John Reck35e9dd62011-04-25 09:01:54 -070098 private Context mContext;
99 private SharedPreferences mPrefs;
100 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700101 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700102 private WebStorageSizeManager mWebStorageSizeManager;
103 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700104 private WeakHashMap<WebSettings, String> mCustomUserAgents;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800105
John Reck35e9dd62011-04-25 09:01:54 -0700106 // Cached settings
107 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800108
John Reck35e9dd62011-04-25 09:01:54 -0700109 public static void initialize(final Context context) {
110 sInstance = new BrowserSettings(context);
111 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800112
John Reck35e9dd62011-04-25 09:01:54 -0700113 public static BrowserSettings getInstance() {
114 return sInstance;
115 }
Ben Murdochef671652010-11-25 17:17:58 +0000116
John Reck35e9dd62011-04-25 09:01:54 -0700117 private BrowserSettings(Context context) {
118 mContext = context;
119 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
John Reckf48314f2011-04-27 17:52:17 -0700120 if (Build.VERSION.CODENAME.equals("REL")) {
121 // This is a release build, always startup with debug disabled
122 setDebugEnabled(false);
123 }
John Reck7dc444b2011-06-16 17:44:29 -0700124 if (mPrefs.contains(PREF_TEXT_SIZE)) {
125 /*
126 * Update from TextSize enum to zoom percent
127 * SMALLEST is 50%
128 * SMALLER is 75%
129 * NORMAL is 100%
130 * LARGER is 150%
131 * LARGEST is 200%
132 */
133 switch (getTextSize()) {
134 case SMALLEST:
135 setTextZoom(50);
136 break;
137 case SMALLER:
138 setTextZoom(75);
139 break;
140 case LARGER:
141 setTextZoom(150);
142 break;
143 case LARGEST:
144 setTextZoom(200);
145 break;
146 }
147 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
148 }
John Reck35e9dd62011-04-25 09:01:54 -0700149 mAutofillHandler = new AutofillHandler(mContext);
150 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700151 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
John Reck35e9dd62011-04-25 09:01:54 -0700152 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
153 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
154 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
155 mPrefs.registerOnSharedPreferenceChangeListener(this);
156 mAutofillHandler.asyncLoadFromDb();
157 }
158
159 public void setController(Controller controller) {
160 mController = controller;
161 syncSharedSettings();
162
163 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
164 ((InstantSearchEngine) mSearchEngine).setController(mController);
Ben Murdochef671652010-11-25 17:17:58 +0000165 }
166 }
167
John Reck35e9dd62011-04-25 09:01:54 -0700168 public void startManagingSettings(WebSettings settings) {
169 synchronized (mManagedSettings) {
170 syncStaticSettings(settings);
171 syncSetting(settings);
172 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800173 }
174 }
175
176 /**
John Reck35e9dd62011-04-25 09:01:54 -0700177 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800178 */
John Reck35e9dd62011-04-25 09:01:54 -0700179 private void syncSetting(WebSettings settings) {
180 settings.setGeolocationEnabled(enableGeolocation());
181 settings.setJavaScriptEnabled(enableJavascript());
182 settings.setLightTouchEnabled(enableLightTouch());
183 settings.setNavDump(enableNavDump());
184 settings.setShowVisualIndicator(enableVisualIndicator());
185 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
186 settings.setDefaultZoom(getDefaultZoom());
187 settings.setMinimumFontSize(getMinimumFontSize());
188 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck92f25f82011-04-26 16:57:10 -0700189 settings.setForceUserScalable(forceEnableUserScalable());
John Reck35e9dd62011-04-25 09:01:54 -0700190 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700191 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700192 settings.setAutoFillEnabled(isAutofillEnabled());
193 settings.setLayoutAlgorithm(getLayoutAlgorithm());
194 settings.setJavaScriptCanOpenWindowsAutomatically(blockPopupWindows());
195 settings.setLoadsImagesAutomatically(loadImages());
196 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
197 settings.setSavePassword(rememberPasswords());
198 settings.setSaveFormData(saveFormdata());
199 settings.setUseWideViewPort(isWideViewport());
200 settings.setAutoFillProfile(getAutoFillProfile());
John Reckb8b2af82011-05-20 15:58:33 -0700201
202 String ua = mCustomUserAgents.get(settings);
John Reck46500332011-06-07 14:36:10 -0700203 if (ua != null) {
John Reckb8b2af82011-05-20 15:58:33 -0700204 settings.setUserAgentString(ua);
205 } else {
206 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
207 }
John Reck2fd9d0e2011-07-15 11:13:48 -0700208
209 settings.setProperty("gfxInvertedScreen",
210 useInvertedRendering() ? "true" : "false");
Ben Murdochef671652010-11-25 17:17:58 +0000211 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800212
John Reck35e9dd62011-04-25 09:01:54 -0700213 /**
214 * Syncs all the settings that have no UI
215 * These cannot change, so we only need to set them once per WebSettings
216 */
217 private void syncStaticSettings(WebSettings settings) {
218 settings.setDefaultFontSize(16);
219 settings.setDefaultFixedFontSize(13);
220 settings.setPageCacheCapacity(getPageCacheCapacity());
Ben Murdochef671652010-11-25 17:17:58 +0000221
John Reck35e9dd62011-04-25 09:01:54 -0700222 // WebView inside Browser doesn't want initial focus to be set.
223 settings.setNeedInitialFocus(false);
224 // Browser supports multiple windows
225 settings.setSupportMultipleWindows(true);
226 // enable smooth transition for better performance during panning or
227 // zooming
228 settings.setEnableSmoothTransition(true);
229 // disable content url access
230 settings.setAllowContentAccess(false);
231
232 // HTML5 API flags
233 settings.setAppCacheEnabled(true);
234 settings.setDatabaseEnabled(true);
235 settings.setDomStorageEnabled(true);
236 settings.setWorkersEnabled(true); // This only affects V8.
237
238 // HTML5 configuration parametersettings.
239 settings.setAppCacheMaxSize(mWebStorageSizeManager.getAppCacheMaxSize());
240 settings.setAppCachePath(getAppCachePath());
241 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
242 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
243 }
244
245 private void syncSharedSettings() {
246 CookieManager.getInstance().setAcceptCookie(acceptCookies());
247 if (mController != null) {
248 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700249 }
John Reck35e9dd62011-04-25 09:01:54 -0700250 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700251
John Reck35e9dd62011-04-25 09:01:54 -0700252 private void syncManagedSettings() {
253 syncSharedSettings();
254 synchronized (mManagedSettings) {
255 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
256 while (iter.hasNext()) {
257 WeakReference<WebSettings> ref = iter.next();
258 WebSettings settings = ref.get();
259 if (settings == null) {
260 iter.remove();
261 continue;
262 }
263 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000264 }
John Reck35e9dd62011-04-25 09:01:54 -0700265 }
266 }
Ben Murdochef671652010-11-25 17:17:58 +0000267
John Reck35e9dd62011-04-25 09:01:54 -0700268 @Override
269 public void onSharedPreferenceChanged(
270 SharedPreferences sharedPreferences, String key) {
271 syncManagedSettings();
272 if (PREF_SEARCH_ENGINE.equals(key)) {
273 updateSearchEngine(false);
274 }
275 if (PREF_USE_INSTANT_SEARCH.equals(key)) {
276 updateSearchEngine(true);
277 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700278 if (PREF_FULLSCREEN.equals(key)) {
279 if (mController.getUi() != null) {
280 mController.getUi().setFullscreen(useFullscreen());
281 }
Michael Kolb0241e752011-07-07 14:58:50 -0700282 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
283 if (mController.getUi() != null) {
284 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
285 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700286 }
John Reck35e9dd62011-04-25 09:01:54 -0700287 }
288
John Reck961d35d2011-06-23 09:45:54 -0700289 public static String getFactoryResetHomeUrl(Context context) {
John Reck35e9dd62011-04-25 09:01:54 -0700290 String url = context.getResources().getString(R.string.homepage_base);
291 if (url.indexOf("{CID}") != -1) {
292 url = url.replace("{CID}",
293 BrowserProvider.getClientId(context.getContentResolver()));
294 }
295 return url;
296 }
297
298 public LayoutAlgorithm getLayoutAlgorithm() {
299 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
300 if (autofitPages()) {
301 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
302 }
303 if (isDebugEnabled()) {
304 if (isSmallScreen()) {
305 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000306 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700307 if (isNormalLayout()) {
308 layoutAlgorithm = LayoutAlgorithm.NORMAL;
309 } else {
310 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
311 }
Ben Murdochef671652010-11-25 17:17:58 +0000312 }
John Reck35e9dd62011-04-25 09:01:54 -0700313 }
314 return layoutAlgorithm;
315 }
Ben Murdochef671652010-11-25 17:17:58 +0000316
John Reck35e9dd62011-04-25 09:01:54 -0700317 // TODO: Cache
318 public int getPageCacheCapacity() {
319 // the cost of one cached page is ~3M (measured using nytimes.com). For
320 // low end devices, we only cache one page. For high end devices, we try
321 // to cache more pages, currently choose 5.
322 if (ActivityManager.staticGetMemoryClass() > 16) {
323 return 5;
324 } else {
325 return 1;
Grace Kloba9804c432009-12-02 11:07:40 -0800326 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800327 }
328
John Reck35e9dd62011-04-25 09:01:54 -0700329 public WebStorageSizeManager getWebStorageSizeManager() {
330 return mWebStorageSizeManager;
331 }
332
333 // TODO: Cache
334 private String getAppCachePath() {
335 return mContext.getDir("appcache", 0).getPath();
336 }
337
338 private void updateSearchEngine(boolean force) {
339 String searchEngineName = getSearchEngineName();
340 if (force || mSearchEngine == null ||
341 !mSearchEngine.getName().equals(searchEngineName)) {
342 if (mSearchEngine != null) {
343 if (mSearchEngine.supportsVoiceSearch()) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000344 // One or more tabs could have been in voice search mode.
345 // Clear it, since the new SearchEngine may not support
346 // it, or may handle it differently.
347 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
348 mController.getTabControl().getTab(i).revertVoiceSearchMode();
349 }
350 }
John Reck35e9dd62011-04-25 09:01:54 -0700351 mSearchEngine.close();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000352 }
John Reck35e9dd62011-04-25 09:01:54 -0700353 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000354
John Reck35e9dd62011-04-25 09:01:54 -0700355 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
356 ((InstantSearchEngine) mSearchEngine).setController(mController);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000357 }
358 }
359 }
360
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100361 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700362 if (mSearchEngine == null) {
363 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100364 }
John Reck35e9dd62011-04-25 09:01:54 -0700365 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100366 }
367
John Reck35e9dd62011-04-25 09:01:54 -0700368 public boolean isDebugEnabled() {
369 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100370 }
371
John Reck35e9dd62011-04-25 09:01:54 -0700372 public void setDebugEnabled(boolean value) {
373 mPrefs.edit().putBoolean(PREF_DEBUG_MENU, value).apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100374 }
375
John Reck35e9dd62011-04-25 09:01:54 -0700376 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800377 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700378 if (mController != null) {
379 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800380 if (current != null) {
381 current.clearCache(true);
382 }
383 }
384 }
385
John Reck35e9dd62011-04-25 09:01:54 -0700386 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800387 CookieManager.getInstance().removeAllCookie();
388 }
389
John Reck35e9dd62011-04-25 09:01:54 -0700390 public void clearHistory() {
391 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800392 Browser.clearHistory(resolver);
393 Browser.clearSearches(resolver);
394 }
395
John Reck35e9dd62011-04-25 09:01:54 -0700396 public void clearFormData() {
397 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700398 if (mController!= null) {
399 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100400 if (currentTopView != null) {
401 currentTopView.clearFormData();
402 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800403 }
404 }
405
John Reck35e9dd62011-04-25 09:01:54 -0700406 public void clearPasswords() {
407 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800408 db.clearUsernamePassword();
409 db.clearHttpAuthUsernamePassword();
410 }
411
John Reck35e9dd62011-04-25 09:01:54 -0700412 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100413 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100414 }
415
John Reck35e9dd62011-04-25 09:01:54 -0700416 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100417 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100418 }
419
John Reck35e9dd62011-04-25 09:01:54 -0700420 public void resetDefaultPreferences() {
421 mPrefs.edit().clear().apply();
422 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700423 }
424
John Reck35e9dd62011-04-25 09:01:54 -0700425 public AutoFillProfile getAutoFillProfile() {
426 mAutofillHandler.waitForLoad();
427 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800428 }
429
John Reck35e9dd62011-04-25 09:01:54 -0700430 public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
431 mAutofillHandler.waitForLoad();
432 mAutofillHandler.setAutoFillProfile(profile, msg);
Ben Murdoch273330a2011-07-08 18:37:22 +0100433 // Auto-fill will reuse the same profile ID when making edits to the profile,
434 // so we need to force a settings sync (otherwise the SharedPreferences
435 // manager will optimise out the call to onSharedPreferenceChanged(), as
436 // it thinks nothing has changed).
437 syncManagedSettings();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800438 }
439
John Reck35e9dd62011-04-25 09:01:54 -0700440 public void toggleDebugSettings() {
441 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800442 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100443
John Reckb8b2af82011-05-20 15:58:33 -0700444 public boolean hasDesktopUseragent(WebView view) {
445 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
446 }
447
448 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700449 if (view == null) {
450 return;
451 }
John Reckb8b2af82011-05-20 15:58:33 -0700452 WebSettings settings = view.getSettings();
453 if (mCustomUserAgents.get(settings) != null) {
454 mCustomUserAgents.remove(settings);
455 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
456 } else {
457 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
458 settings.setUserAgentString(DESKTOP_USERAGENT);
459 }
460 }
461
John Reck7dc444b2011-06-16 17:44:29 -0700462 public static int getAdjustedMinimumFontSize(int rawValue) {
463 rawValue++; // Preference starts at 0, min font at 1
464 if (rawValue > 1) {
465 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
466 }
467 return rawValue;
468 }
469
470 public static int getAdjustedTextZoom(int rawValue) {
471 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
472 return rawValue + 100;
473 }
474
475 static int getRawTextZoom(int percent) {
476 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
477 }
478
John Reck35e9dd62011-04-25 09:01:54 -0700479 // -----------------------------
480 // getter/setters for accessibility_preferences.xml
481 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100482
John Reck7dc444b2011-06-16 17:44:29 -0700483 @Deprecated
484 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700485 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
486 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100487 }
488
John Reck35e9dd62011-04-25 09:01:54 -0700489 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700490 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700491 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100492 }
493
John Reck92f25f82011-04-26 16:57:10 -0700494 public boolean forceEnableUserScalable() {
495 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
496 }
497
John Reck7dc444b2011-06-16 17:44:29 -0700498 public int getTextZoom() {
499 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
500 return getAdjustedTextZoom(textZoom);
501 }
502
503 public void setTextZoom(int percent) {
504 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
505 }
506
John Reck35e9dd62011-04-25 09:01:54 -0700507 // -----------------------------
508 // getter/setters for advanced_preferences.xml
509 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100510
John Reck35e9dd62011-04-25 09:01:54 -0700511 public String getSearchEngineName() {
512 return mPrefs.getString(PREF_SEARCH_ENGINE, SearchEngine.GOOGLE);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100513 }
John Reck63bb6872010-12-01 19:29:32 -0800514
John Reck35e9dd62011-04-25 09:01:54 -0700515 public boolean openInBackground() {
516 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800517 }
John Reck35e9dd62011-04-25 09:01:54 -0700518
519 public boolean enableJavascript() {
520 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
521 }
522
523 // TODO: Cache
524 public PluginState getPluginState() {
525 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
526 return PluginState.valueOf(state);
527 }
528
529 // TODO: Cache
530 public ZoomDensity getDefaultZoom() {
531 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
532 return ZoomDensity.valueOf(zoom);
533 }
534
535 public boolean loadPageInOverviewMode() {
536 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
537 }
538
539 public boolean autofitPages() {
540 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
541 }
542
543 public boolean blockPopupWindows() {
544 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
545 }
546
547 public boolean loadImages() {
548 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
549 }
550
551 public String getDefaultTextEncoding() {
552 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
553 }
554
555 // -----------------------------
556 // getter/setters for general_preferences.xml
557 // -----------------------------
558
559 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700560 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
561 }
562
563 public void setHomePage(String value) {
564 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
565 }
566
567 public boolean isAutofillEnabled() {
568 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
569 }
570
571 public void setAutofillEnabled(boolean value) {
572 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
573 }
574
575 // -----------------------------
576 // getter/setters for debug_preferences.xml
577 // -----------------------------
578
579 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700580 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700581 return true;
582 }
583 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
584 }
585
586 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700587 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700588 return 0;
589 }
590 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
591 }
592
593 // -----------------------------
594 // getter/setters for hidden_debug_preferences.xml
595 // -----------------------------
596
597 public boolean enableVisualIndicator() {
598 if (!isDebugEnabled()) {
599 return false;
600 }
601 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
602 }
603
604 public boolean enableJavascriptConsole() {
605 if (!isDebugEnabled()) {
606 return false;
607 }
608 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
609 }
610
611 public boolean isSmallScreen() {
612 if (!isDebugEnabled()) {
613 return false;
614 }
615 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
616 }
617
618 public boolean isWideViewport() {
619 if (!isDebugEnabled()) {
620 return true;
621 }
622 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
623 }
624
625 public boolean isNormalLayout() {
626 if (!isDebugEnabled()) {
627 return false;
628 }
629 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
630 }
631
632 public boolean isTracing() {
633 if (!isDebugEnabled()) {
634 return false;
635 }
636 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
637 }
638
639 public boolean enableLightTouch() {
640 if (!isDebugEnabled()) {
641 return false;
642 }
643 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
644 }
645
646 public boolean enableNavDump() {
647 if (!isDebugEnabled()) {
648 return false;
649 }
650 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
651 }
652
653 public String getJsEngineFlags() {
654 if (!isDebugEnabled()) {
655 return "";
656 }
657 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
658 }
659
660 // -----------------------------
661 // getter/setters for lab_preferences.xml
662 // -----------------------------
663
664 public boolean useQuickControls() {
665 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
666 }
667
668 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700669 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700670 }
671
672 public boolean useInstantSearch() {
673 return mPrefs.getBoolean(PREF_USE_INSTANT_SEARCH, false);
674 }
675
Michael Kolbc38c6042011-04-27 10:46:06 -0700676 public boolean useFullscreen() {
677 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
678 }
679
John Reck2fd9d0e2011-07-15 11:13:48 -0700680 public boolean useInvertedRendering() {
681 return mPrefs.getBoolean(PREF_INVERTED, false);
682 }
683
John Reck35e9dd62011-04-25 09:01:54 -0700684 // -----------------------------
685 // getter/setters for privacy_security_preferences.xml
686 // -----------------------------
687
688 public boolean showSecurityWarnings() {
689 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
690 }
691
692 public boolean acceptCookies() {
693 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
694 }
695
696 public boolean saveFormdata() {
697 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
698 }
699
700 public boolean enableGeolocation() {
701 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
702 }
703
704 public boolean rememberPasswords() {
705 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
706 }
707
Michael Kolb14612442011-06-24 13:06:29 -0700708 // -----------------------------
709 // getter/setters for bandwidth_preferences.xml
710 // -----------------------------
711
712 public boolean isPreloadEnabled() {
713 return mPrefs.getBoolean(PREF_DATA_PRELOAD, false);
714 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800715}