blob: a4f0f044b2aaccd6d0adee9ae1c3864ce6589a6c [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;
John Reckf57c0292011-07-21 18:15:39 -070025import android.os.Looper;
Ben Murdoch23da30e2010-10-26 15:18:44 +010026import android.os.Message;
Ben Murdoch0cb81892010-10-08 12:41:33 +010027import android.preference.PreferenceManager;
Ben Murdoch0cb81892010-10-08 12:41:33 +010028import android.provider.Browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010030import android.webkit.GeolocationPermissions;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.webkit.WebIconDatabase;
32import android.webkit.WebSettings;
Ben Murdoch0cb81892010-10-08 12:41:33 +010033import android.webkit.WebSettings.AutoFillProfile;
John Reck35e9dd62011-04-25 09:01:54 -070034import android.webkit.WebSettings.LayoutAlgorithm;
35import android.webkit.WebSettings.PluginState;
36import android.webkit.WebSettings.TextSize;
37import android.webkit.WebSettings.ZoomDensity;
Nicolas Roard78a98e42009-05-11 13:34:17 +010038import android.webkit.WebStorage;
John Reck812d2d62011-01-18 14:16:15 -080039import android.webkit.WebView;
40import android.webkit.WebViewDatabase;
The Android Open Source Project0c908882009-03-03 19:32:16 -080041
John Reck46500332011-06-07 14:36:10 -070042import com.android.browser.homepages.HomeProvider;
43import com.android.browser.provider.BrowserProvider;
44import com.android.browser.search.SearchEngine;
45import com.android.browser.search.SearchEngines;
46
John Reck35e9dd62011-04-25 09:01:54 -070047import java.lang.ref.WeakReference;
48import java.util.Iterator;
49import java.util.LinkedList;
John Reckb8b2af82011-05-20 15:58:33 -070050import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051
John Reck35e9dd62011-04-25 09:01:54 -070052/**
53 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080054 */
John Reck35e9dd62011-04-25 09:01:54 -070055public class BrowserSettings implements OnSharedPreferenceChangeListener,
56 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010057
John Reck35e9dd62011-04-25 09:01:54 -070058 // TODO: Do something with this UserAgent stuff
John Reck7fd1e8f2011-04-27 18:22:57 -070059 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " +
60 "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " +
61 "Chrome/11.0.696.34 Safari/534.24";
The Android Open Source Project0c908882009-03-03 19:32:16 -080062
63 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070064 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
65 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
Bart Searsf6915fb2010-07-08 19:58:22 -070066
67 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070068 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
69 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
Bart Searsf6915fb2010-07-08 19:58:22 -070070
71 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070072 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
73 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -080074
John Reck7fd1e8f2011-04-27 18:22:57 -070075 private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " +
76 "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " +
77 "(KHTML, like Gecko) Version/4.0 Safari/534.13";
78
John Reck35e9dd62011-04-25 09:01:54 -070079 private static final String USER_AGENTS[] = { null,
80 DESKTOP_USERAGENT,
81 IPHONE_USERAGENT,
82 IPAD_USERAGENT,
John Reck7fd1e8f2011-04-27 18:22:57 -070083 FROYO_USERAGENT,
84 HONEYCOMB_USERAGENT,
John Reck35e9dd62011-04-25 09:01:54 -070085 };
The Android Open Source Project0c908882009-03-03 19:32:16 -080086
John Reck8fc22a12011-06-16 14:57:41 -070087 // The minimum min font size
88 // Aka, the lower bounds for the min font size range
89 // which is 1:5..24
90 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -070091 // The initial value in the text zoom range
92 // This is what represents 100% in the SeekBarPreference range
93 private static final int TEXT_ZOOM_START_VAL = 10;
94 // The size of a single step in the text zoom range, in percent
95 private static final int TEXT_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -070096
John Reck35e9dd62011-04-25 09:01:54 -070097 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -070098
John Reck35e9dd62011-04-25 09:01:54 -070099 private Context mContext;
100 private SharedPreferences mPrefs;
101 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700102 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700103 private WebStorageSizeManager mWebStorageSizeManager;
104 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700105 private WeakHashMap<WebSettings, String> mCustomUserAgents;
John Reck78a6a1d2011-07-21 13:54:03 -0700106 private boolean mInitialized = false;
John Reckf57c0292011-07-21 18:15:39 -0700107 // Looper shared between some lightweight background operations
108 // Specifically, this is created on the thread that initializes browser settings
109 // and is then reused by CrashRecoveryHandler
110 private Looper mBackgroundLooper;
John Reck78a6a1d2011-07-21 13:54:03 -0700111
112 // Cached values
113 private int mPageCacheCapacity = 1;
114 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800115
John Reck35e9dd62011-04-25 09:01:54 -0700116 // Cached settings
117 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118
John Reck35e9dd62011-04-25 09:01:54 -0700119 public static void initialize(final Context context) {
120 sInstance = new BrowserSettings(context);
121 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800122
John Reck35e9dd62011-04-25 09:01:54 -0700123 public static BrowserSettings getInstance() {
124 return sInstance;
125 }
Ben Murdochef671652010-11-25 17:17:58 +0000126
John Reck35e9dd62011-04-25 09:01:54 -0700127 private BrowserSettings(Context context) {
128 mContext = context;
129 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
130 mAutofillHandler = new AutofillHandler(mContext);
131 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700132 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
John Reck35e9dd62011-04-25 09:01:54 -0700133 mPrefs.registerOnSharedPreferenceChangeListener(this);
134 mAutofillHandler.asyncLoadFromDb();
John Reckf57c0292011-07-21 18:15:39 -0700135 new Thread(mSetupAndLoop, "BackgroundLooper").start();
John Reck35e9dd62011-04-25 09:01:54 -0700136 }
137
138 public void setController(Controller controller) {
139 mController = controller;
140 syncSharedSettings();
141
142 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
143 ((InstantSearchEngine) mSearchEngine).setController(mController);
Ben Murdochef671652010-11-25 17:17:58 +0000144 }
145 }
146
John Reckf57c0292011-07-21 18:15:39 -0700147 public Looper getBackgroundLooper() {
148 requireInitialization();
149 return mBackgroundLooper;
150 }
151
John Reck35e9dd62011-04-25 09:01:54 -0700152 public void startManagingSettings(WebSettings settings) {
153 synchronized (mManagedSettings) {
154 syncStaticSettings(settings);
155 syncSetting(settings);
156 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800157 }
158 }
159
John Reckf57c0292011-07-21 18:15:39 -0700160 private Runnable mSetupAndLoop = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700161
162 @Override
163 public void run() {
164 // the cost of one cached page is ~3M (measured using nytimes.com). For
165 // low end devices, we only cache one page. For high end devices, we try
166 // to cache more pages, currently choose 5.
167 if (ActivityManager.staticGetMemoryClass() > 16) {
168 mPageCacheCapacity = 5;
169 }
170 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
171 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
172 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
173 if (Build.VERSION.CODENAME.equals("REL")) {
174 // This is a release build, always startup with debug disabled
175 setDebugEnabled(false);
176 }
177 if (mPrefs.contains(PREF_TEXT_SIZE)) {
178 /*
179 * Update from TextSize enum to zoom percent
180 * SMALLEST is 50%
181 * SMALLER is 75%
182 * NORMAL is 100%
183 * LARGER is 150%
184 * LARGEST is 200%
185 */
186 switch (getTextSize()) {
187 case SMALLEST:
188 setTextZoom(50);
189 break;
190 case SMALLER:
191 setTextZoom(75);
192 break;
193 case LARGER:
194 setTextZoom(150);
195 break;
196 case LARGEST:
197 setTextZoom(200);
198 break;
199 }
200 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
201 }
John Reckf57c0292011-07-21 18:15:39 -0700202 Looper.prepare();
203 mBackgroundLooper = Looper.myLooper();
John Reck78a6a1d2011-07-21 13:54:03 -0700204 synchronized (BrowserSettings.this) {
205 mInitialized = true;
206 BrowserSettings.this.notifyAll();
207 }
John Reckf57c0292011-07-21 18:15:39 -0700208 Looper.loop();
John Reck78a6a1d2011-07-21 13:54:03 -0700209 }
210 };
211
212 void requireInitialization() {
213 synchronized (BrowserSettings.this) {
214 while (!mInitialized) {
215 try {
216 BrowserSettings.this.wait();
217 } catch (InterruptedException e) {
218 }
219 }
220 }
221 }
222
The Android Open Source Project0c908882009-03-03 19:32:16 -0800223 /**
John Reck35e9dd62011-04-25 09:01:54 -0700224 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800225 */
John Reck35e9dd62011-04-25 09:01:54 -0700226 private void syncSetting(WebSettings settings) {
227 settings.setGeolocationEnabled(enableGeolocation());
228 settings.setJavaScriptEnabled(enableJavascript());
229 settings.setLightTouchEnabled(enableLightTouch());
230 settings.setNavDump(enableNavDump());
231 settings.setShowVisualIndicator(enableVisualIndicator());
232 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
233 settings.setDefaultZoom(getDefaultZoom());
234 settings.setMinimumFontSize(getMinimumFontSize());
235 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck92f25f82011-04-26 16:57:10 -0700236 settings.setForceUserScalable(forceEnableUserScalable());
John Reck35e9dd62011-04-25 09:01:54 -0700237 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700238 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700239 settings.setAutoFillEnabled(isAutofillEnabled());
240 settings.setLayoutAlgorithm(getLayoutAlgorithm());
241 settings.setJavaScriptCanOpenWindowsAutomatically(blockPopupWindows());
242 settings.setLoadsImagesAutomatically(loadImages());
243 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
244 settings.setSavePassword(rememberPasswords());
245 settings.setSaveFormData(saveFormdata());
246 settings.setUseWideViewPort(isWideViewport());
247 settings.setAutoFillProfile(getAutoFillProfile());
John Reckb8b2af82011-05-20 15:58:33 -0700248
249 String ua = mCustomUserAgents.get(settings);
John Reck46500332011-06-07 14:36:10 -0700250 if (ua != null) {
John Reckb8b2af82011-05-20 15:58:33 -0700251 settings.setUserAgentString(ua);
252 } else {
253 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
254 }
John Reck2fd9d0e2011-07-15 11:13:48 -0700255
256 settings.setProperty("gfxInvertedScreen",
257 useInvertedRendering() ? "true" : "false");
Ben Murdochef671652010-11-25 17:17:58 +0000258 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800259
John Reck35e9dd62011-04-25 09:01:54 -0700260 /**
261 * Syncs all the settings that have no UI
262 * These cannot change, so we only need to set them once per WebSettings
263 */
264 private void syncStaticSettings(WebSettings settings) {
265 settings.setDefaultFontSize(16);
266 settings.setDefaultFixedFontSize(13);
267 settings.setPageCacheCapacity(getPageCacheCapacity());
Ben Murdochef671652010-11-25 17:17:58 +0000268
John Reck35e9dd62011-04-25 09:01:54 -0700269 // WebView inside Browser doesn't want initial focus to be set.
270 settings.setNeedInitialFocus(false);
271 // Browser supports multiple windows
272 settings.setSupportMultipleWindows(true);
273 // enable smooth transition for better performance during panning or
274 // zooming
275 settings.setEnableSmoothTransition(true);
276 // disable content url access
277 settings.setAllowContentAccess(false);
278
279 // HTML5 API flags
280 settings.setAppCacheEnabled(true);
281 settings.setDatabaseEnabled(true);
282 settings.setDomStorageEnabled(true);
283 settings.setWorkersEnabled(true); // This only affects V8.
284
285 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700286 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700287 settings.setAppCachePath(getAppCachePath());
288 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
289 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
290 }
291
292 private void syncSharedSettings() {
293 CookieManager.getInstance().setAcceptCookie(acceptCookies());
294 if (mController != null) {
295 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700296 }
John Reck35e9dd62011-04-25 09:01:54 -0700297 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700298
John Reck35e9dd62011-04-25 09:01:54 -0700299 private void syncManagedSettings() {
300 syncSharedSettings();
301 synchronized (mManagedSettings) {
302 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
303 while (iter.hasNext()) {
304 WeakReference<WebSettings> ref = iter.next();
305 WebSettings settings = ref.get();
306 if (settings == null) {
307 iter.remove();
308 continue;
309 }
310 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000311 }
John Reck35e9dd62011-04-25 09:01:54 -0700312 }
313 }
Ben Murdochef671652010-11-25 17:17:58 +0000314
John Reck35e9dd62011-04-25 09:01:54 -0700315 @Override
316 public void onSharedPreferenceChanged(
317 SharedPreferences sharedPreferences, String key) {
318 syncManagedSettings();
319 if (PREF_SEARCH_ENGINE.equals(key)) {
320 updateSearchEngine(false);
321 }
322 if (PREF_USE_INSTANT_SEARCH.equals(key)) {
323 updateSearchEngine(true);
324 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700325 if (PREF_FULLSCREEN.equals(key)) {
326 if (mController.getUi() != null) {
327 mController.getUi().setFullscreen(useFullscreen());
328 }
Michael Kolb0241e752011-07-07 14:58:50 -0700329 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
330 if (mController.getUi() != null) {
331 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
332 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700333 }
John Reck35e9dd62011-04-25 09:01:54 -0700334 }
335
John Reck961d35d2011-06-23 09:45:54 -0700336 public static String getFactoryResetHomeUrl(Context context) {
John Reck35e9dd62011-04-25 09:01:54 -0700337 String url = context.getResources().getString(R.string.homepage_base);
338 if (url.indexOf("{CID}") != -1) {
339 url = url.replace("{CID}",
340 BrowserProvider.getClientId(context.getContentResolver()));
341 }
342 return url;
343 }
344
345 public LayoutAlgorithm getLayoutAlgorithm() {
346 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
347 if (autofitPages()) {
348 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
349 }
350 if (isDebugEnabled()) {
351 if (isSmallScreen()) {
352 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000353 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700354 if (isNormalLayout()) {
355 layoutAlgorithm = LayoutAlgorithm.NORMAL;
356 } else {
357 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
358 }
Ben Murdochef671652010-11-25 17:17:58 +0000359 }
John Reck35e9dd62011-04-25 09:01:54 -0700360 }
361 return layoutAlgorithm;
362 }
Ben Murdochef671652010-11-25 17:17:58 +0000363
John Reck35e9dd62011-04-25 09:01:54 -0700364 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700365 requireInitialization();
366 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800367 }
368
John Reck35e9dd62011-04-25 09:01:54 -0700369 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700370 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700371 return mWebStorageSizeManager;
372 }
373
John Reck35e9dd62011-04-25 09:01:54 -0700374 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700375 if (mAppCachePath == null) {
376 mAppCachePath = mContext.getDir("appcache", 0).getPath();
377 }
378 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700379 }
380
381 private void updateSearchEngine(boolean force) {
382 String searchEngineName = getSearchEngineName();
383 if (force || mSearchEngine == null ||
384 !mSearchEngine.getName().equals(searchEngineName)) {
385 if (mSearchEngine != null) {
386 if (mSearchEngine.supportsVoiceSearch()) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000387 // One or more tabs could have been in voice search mode.
388 // Clear it, since the new SearchEngine may not support
389 // it, or may handle it differently.
390 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
391 mController.getTabControl().getTab(i).revertVoiceSearchMode();
392 }
393 }
John Reck35e9dd62011-04-25 09:01:54 -0700394 mSearchEngine.close();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000395 }
John Reck35e9dd62011-04-25 09:01:54 -0700396 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000397
John Reck35e9dd62011-04-25 09:01:54 -0700398 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
399 ((InstantSearchEngine) mSearchEngine).setController(mController);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000400 }
401 }
402 }
403
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100404 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700405 if (mSearchEngine == null) {
406 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100407 }
John Reck35e9dd62011-04-25 09:01:54 -0700408 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100409 }
410
John Reck35e9dd62011-04-25 09:01:54 -0700411 public boolean isDebugEnabled() {
John Reck78a6a1d2011-07-21 13:54:03 -0700412 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700413 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100414 }
415
John Reck35e9dd62011-04-25 09:01:54 -0700416 public void setDebugEnabled(boolean value) {
417 mPrefs.edit().putBoolean(PREF_DEBUG_MENU, value).apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100418 }
419
John Reck35e9dd62011-04-25 09:01:54 -0700420 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800421 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700422 if (mController != null) {
423 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800424 if (current != null) {
425 current.clearCache(true);
426 }
427 }
428 }
429
John Reck35e9dd62011-04-25 09:01:54 -0700430 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800431 CookieManager.getInstance().removeAllCookie();
432 }
433
John Reck35e9dd62011-04-25 09:01:54 -0700434 public void clearHistory() {
435 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800436 Browser.clearHistory(resolver);
437 Browser.clearSearches(resolver);
438 }
439
John Reck35e9dd62011-04-25 09:01:54 -0700440 public void clearFormData() {
441 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700442 if (mController!= null) {
443 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100444 if (currentTopView != null) {
445 currentTopView.clearFormData();
446 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800447 }
448 }
449
John Reck35e9dd62011-04-25 09:01:54 -0700450 public void clearPasswords() {
451 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800452 db.clearUsernamePassword();
453 db.clearHttpAuthUsernamePassword();
454 }
455
John Reck35e9dd62011-04-25 09:01:54 -0700456 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100457 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100458 }
459
John Reck35e9dd62011-04-25 09:01:54 -0700460 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100461 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100462 }
463
John Reck35e9dd62011-04-25 09:01:54 -0700464 public void resetDefaultPreferences() {
465 mPrefs.edit().clear().apply();
466 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700467 }
468
John Reck35e9dd62011-04-25 09:01:54 -0700469 public AutoFillProfile getAutoFillProfile() {
470 mAutofillHandler.waitForLoad();
471 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800472 }
473
John Reck35e9dd62011-04-25 09:01:54 -0700474 public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
475 mAutofillHandler.waitForLoad();
476 mAutofillHandler.setAutoFillProfile(profile, msg);
Ben Murdoch273330a2011-07-08 18:37:22 +0100477 // Auto-fill will reuse the same profile ID when making edits to the profile,
478 // so we need to force a settings sync (otherwise the SharedPreferences
479 // manager will optimise out the call to onSharedPreferenceChanged(), as
480 // it thinks nothing has changed).
481 syncManagedSettings();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800482 }
483
John Reck35e9dd62011-04-25 09:01:54 -0700484 public void toggleDebugSettings() {
485 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800486 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100487
John Reckb8b2af82011-05-20 15:58:33 -0700488 public boolean hasDesktopUseragent(WebView view) {
489 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
490 }
491
492 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700493 if (view == null) {
494 return;
495 }
John Reckb8b2af82011-05-20 15:58:33 -0700496 WebSettings settings = view.getSettings();
497 if (mCustomUserAgents.get(settings) != null) {
498 mCustomUserAgents.remove(settings);
499 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
500 } else {
501 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
502 settings.setUserAgentString(DESKTOP_USERAGENT);
503 }
504 }
505
John Reck7dc444b2011-06-16 17:44:29 -0700506 public static int getAdjustedMinimumFontSize(int rawValue) {
507 rawValue++; // Preference starts at 0, min font at 1
508 if (rawValue > 1) {
509 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
510 }
511 return rawValue;
512 }
513
514 public static int getAdjustedTextZoom(int rawValue) {
515 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
516 return rawValue + 100;
517 }
518
519 static int getRawTextZoom(int percent) {
520 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
521 }
522
John Reck35e9dd62011-04-25 09:01:54 -0700523 // -----------------------------
524 // getter/setters for accessibility_preferences.xml
525 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100526
John Reck7dc444b2011-06-16 17:44:29 -0700527 @Deprecated
528 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700529 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
530 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100531 }
532
John Reck35e9dd62011-04-25 09:01:54 -0700533 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700534 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700535 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100536 }
537
John Reck92f25f82011-04-26 16:57:10 -0700538 public boolean forceEnableUserScalable() {
539 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
540 }
541
John Reck7dc444b2011-06-16 17:44:29 -0700542 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700543 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700544 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
545 return getAdjustedTextZoom(textZoom);
546 }
547
548 public void setTextZoom(int percent) {
549 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
550 }
551
John Reck35e9dd62011-04-25 09:01:54 -0700552 // -----------------------------
553 // getter/setters for advanced_preferences.xml
554 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100555
John Reck35e9dd62011-04-25 09:01:54 -0700556 public String getSearchEngineName() {
557 return mPrefs.getString(PREF_SEARCH_ENGINE, SearchEngine.GOOGLE);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100558 }
John Reck63bb6872010-12-01 19:29:32 -0800559
John Reck35e9dd62011-04-25 09:01:54 -0700560 public boolean openInBackground() {
561 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800562 }
John Reck35e9dd62011-04-25 09:01:54 -0700563
564 public boolean enableJavascript() {
565 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
566 }
567
568 // TODO: Cache
569 public PluginState getPluginState() {
570 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
571 return PluginState.valueOf(state);
572 }
573
574 // TODO: Cache
575 public ZoomDensity getDefaultZoom() {
576 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
577 return ZoomDensity.valueOf(zoom);
578 }
579
580 public boolean loadPageInOverviewMode() {
581 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
582 }
583
584 public boolean autofitPages() {
585 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
586 }
587
588 public boolean blockPopupWindows() {
589 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
590 }
591
592 public boolean loadImages() {
593 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
594 }
595
596 public String getDefaultTextEncoding() {
597 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
598 }
599
600 // -----------------------------
601 // getter/setters for general_preferences.xml
602 // -----------------------------
603
604 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700605 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
606 }
607
608 public void setHomePage(String value) {
609 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
610 }
611
612 public boolean isAutofillEnabled() {
613 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
614 }
615
616 public void setAutofillEnabled(boolean value) {
617 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
618 }
619
620 // -----------------------------
621 // getter/setters for debug_preferences.xml
622 // -----------------------------
623
624 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700625 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700626 return true;
627 }
628 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
629 }
630
631 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700632 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700633 return 0;
634 }
635 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
636 }
637
638 // -----------------------------
639 // getter/setters for hidden_debug_preferences.xml
640 // -----------------------------
641
642 public boolean enableVisualIndicator() {
643 if (!isDebugEnabled()) {
644 return false;
645 }
646 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
647 }
648
649 public boolean enableJavascriptConsole() {
650 if (!isDebugEnabled()) {
651 return false;
652 }
653 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
654 }
655
656 public boolean isSmallScreen() {
657 if (!isDebugEnabled()) {
658 return false;
659 }
660 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
661 }
662
663 public boolean isWideViewport() {
664 if (!isDebugEnabled()) {
665 return true;
666 }
667 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
668 }
669
670 public boolean isNormalLayout() {
671 if (!isDebugEnabled()) {
672 return false;
673 }
674 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
675 }
676
677 public boolean isTracing() {
678 if (!isDebugEnabled()) {
679 return false;
680 }
681 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
682 }
683
684 public boolean enableLightTouch() {
685 if (!isDebugEnabled()) {
686 return false;
687 }
688 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
689 }
690
691 public boolean enableNavDump() {
692 if (!isDebugEnabled()) {
693 return false;
694 }
695 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
696 }
697
698 public String getJsEngineFlags() {
699 if (!isDebugEnabled()) {
700 return "";
701 }
702 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
703 }
704
705 // -----------------------------
706 // getter/setters for lab_preferences.xml
707 // -----------------------------
708
709 public boolean useQuickControls() {
710 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
711 }
712
713 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700714 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700715 }
716
717 public boolean useInstantSearch() {
718 return mPrefs.getBoolean(PREF_USE_INSTANT_SEARCH, false);
719 }
720
Michael Kolbc38c6042011-04-27 10:46:06 -0700721 public boolean useFullscreen() {
722 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
723 }
724
John Reck2fd9d0e2011-07-15 11:13:48 -0700725 public boolean useInvertedRendering() {
726 return mPrefs.getBoolean(PREF_INVERTED, false);
727 }
728
John Reck35e9dd62011-04-25 09:01:54 -0700729 // -----------------------------
730 // getter/setters for privacy_security_preferences.xml
731 // -----------------------------
732
733 public boolean showSecurityWarnings() {
734 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
735 }
736
737 public boolean acceptCookies() {
738 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
739 }
740
741 public boolean saveFormdata() {
742 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
743 }
744
745 public boolean enableGeolocation() {
746 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
747 }
748
749 public boolean rememberPasswords() {
750 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
751 }
752
Michael Kolb14612442011-06-24 13:06:29 -0700753 // -----------------------------
754 // getter/setters for bandwidth_preferences.xml
755 // -----------------------------
756
757 public boolean isPreloadEnabled() {
758 return mPrefs.getBoolean(PREF_DATA_PRELOAD, false);
759 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800760}