BrowserSettings.java revision 825fba7eb4dae7911f84d8a48a662c4cd6a4db1f
1/*
2 * Copyright (C) 2011 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.ActivityManager;
20import android.content.ContentResolver;
21import android.content.Context;
22import android.content.SharedPreferences;
23import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
24import android.os.Build;
25import android.os.Message;
26import android.preference.PreferenceManager;
27import android.provider.Browser;
28import android.provider.Settings;
29import android.util.DisplayMetrics;
30import android.webkit.CookieManager;
31import android.webkit.GeolocationPermissions;
32import android.webkit.WebIconDatabase;
33import android.webkit.WebSettings;
34import android.webkit.WebSettings.AutoFillProfile;
35import android.webkit.WebSettings.LayoutAlgorithm;
36import android.webkit.WebSettings.PluginState;
37import android.webkit.WebSettings.TextSize;
38import android.webkit.WebSettings.ZoomDensity;
39import android.webkit.WebStorage;
40import android.webkit.WebView;
41import android.webkit.WebViewDatabase;
42
43import com.android.browser.homepages.HomeProvider;
44import com.android.browser.provider.BrowserProvider;
45import com.android.browser.search.SearchEngine;
46import com.android.browser.search.SearchEngines;
47
48import java.lang.ref.WeakReference;
49import java.util.Iterator;
50import java.util.LinkedList;
51import java.util.WeakHashMap;
52
53/**
54 * Class for managing settings
55 */
56public class BrowserSettings implements OnSharedPreferenceChangeListener,
57        PreferenceKeys {
58
59    // TODO: Do something with this UserAgent stuff
60    private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " +
61        "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " +
62        "Chrome/11.0.696.34 Safari/534.24";
63
64    private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
65        "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
66        "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
67
68    private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
69        "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
70        "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
71
72    private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
73        "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
74        "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
75
76    private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " +
77        "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " +
78        "(KHTML, like Gecko) Version/4.0 Safari/534.13";
79
80    private static final String USER_AGENTS[] = { null,
81            DESKTOP_USERAGENT,
82            IPHONE_USERAGENT,
83            IPAD_USERAGENT,
84            FROYO_USERAGENT,
85            HONEYCOMB_USERAGENT,
86    };
87
88    // The minimum min font size
89    // Aka, the lower bounds for the min font size range
90    // which is 1:5..24
91    private static final int MIN_FONT_SIZE_OFFSET = 5;
92    // The initial value in the text zoom range
93    // This is what represents 100% in the SeekBarPreference range
94    private static final int TEXT_ZOOM_START_VAL = 10;
95    // The size of a single step in the text zoom range, in percent
96    private static final int TEXT_ZOOM_STEP = 5;
97
98    private static BrowserSettings sInstance;
99
100    private Context mContext;
101    private SharedPreferences mPrefs;
102    private LinkedList<WeakReference<WebSettings>> mManagedSettings;
103    private Controller mController;
104    private WebStorageSizeManager mWebStorageSizeManager;
105    private AutofillHandler mAutofillHandler;
106    private WeakHashMap<WebSettings, String> mCustomUserAgents;
107    private static boolean sInitialized = false;
108    private boolean mNeedsSharedSync = true;
109    private float mFontSizeMult = 1.0f;
110
111    // Cached values
112    private int mPageCacheCapacity = 1;
113    private String mAppCachePath;
114
115    // Cached settings
116    private SearchEngine mSearchEngine;
117
118    private static String sFactoryResetUrl;
119
120    public static void initialize(final Context context) {
121        sInstance = new BrowserSettings(context);
122    }
123
124    public static BrowserSettings getInstance() {
125        return sInstance;
126    }
127
128    private BrowserSettings(Context context) {
129        mContext = context.getApplicationContext();
130        mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
131        mAutofillHandler = new AutofillHandler(mContext);
132        mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
133        mCustomUserAgents = new WeakHashMap<WebSettings, String>();
134        mAutofillHandler.asyncLoadFromDb();
135        BackgroundHandler.execute(mSetup);
136    }
137
138    public void setController(Controller controller) {
139        mController = controller;
140        if (sInitialized) {
141            syncSharedSettings();
142        }
143
144        if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
145             ((InstantSearchEngine) mSearchEngine).setController(mController);
146        }
147    }
148
149    public void startManagingSettings(WebSettings settings) {
150        if (mNeedsSharedSync) {
151            syncSharedSettings();
152        }
153        synchronized (mManagedSettings) {
154            syncStaticSettings(settings);
155            syncSetting(settings);
156            mManagedSettings.add(new WeakReference<WebSettings>(settings));
157        }
158    }
159
160    private Runnable mSetup = new Runnable() {
161
162        @Override
163        public void run() {
164            DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
165            mFontSizeMult = metrics.scaledDensity / metrics.density;
166            // the cost of one cached page is ~3M (measured using nytimes.com). For
167            // low end devices, we only cache one page. For high end devices, we try
168            // to cache more pages, currently choose 5.
169            if (ActivityManager.staticGetMemoryClass() > 16) {
170                mPageCacheCapacity = 5;
171            }
172            mWebStorageSizeManager = new WebStorageSizeManager(mContext,
173                    new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
174                    new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
175            // Workaround b/5253777
176            CookieManager.getInstance().acceptCookie();
177            // Workaround b/5254577
178            mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
179            if (Build.VERSION.CODENAME.equals("REL")) {
180                // This is a release build, always startup with debug disabled
181                setDebugEnabled(false);
182            }
183            if (mPrefs.contains(PREF_TEXT_SIZE)) {
184                /*
185                 * Update from TextSize enum to zoom percent
186                 * SMALLEST is 50%
187                 * SMALLER is 75%
188                 * NORMAL is 100%
189                 * LARGER is 150%
190                 * LARGEST is 200%
191                 */
192                switch (getTextSize()) {
193                case SMALLEST:
194                    setTextZoom(50);
195                    break;
196                case SMALLER:
197                    setTextZoom(75);
198                    break;
199                case LARGER:
200                    setTextZoom(150);
201                    break;
202                case LARGEST:
203                    setTextZoom(200);
204                    break;
205                }
206                mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
207            }
208
209            sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
210            if (sFactoryResetUrl.indexOf("{CID}") != -1) {
211                sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
212                    BrowserProvider.getClientId(mContext.getContentResolver()));
213            }
214
215            synchronized (BrowserSettings.class) {
216                sInitialized = true;
217                BrowserSettings.class.notifyAll();
218            }
219        }
220    };
221
222    private static void requireInitialization() {
223        synchronized (BrowserSettings.class) {
224            while (!sInitialized) {
225                try {
226                    BrowserSettings.class.wait();
227                } catch (InterruptedException e) {
228                }
229            }
230        }
231    }
232
233    /**
234     * Syncs all the settings that have a Preference UI
235     */
236    private void syncSetting(WebSettings settings) {
237        settings.setGeolocationEnabled(enableGeolocation());
238        settings.setJavaScriptEnabled(enableJavascript());
239        settings.setLightTouchEnabled(enableLightTouch());
240        settings.setNavDump(enableNavDump());
241        settings.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
242        settings.setShowVisualIndicator(enableVisualIndicator());
243        settings.setDefaultTextEncodingName(getDefaultTextEncoding());
244        settings.setDefaultZoom(getDefaultZoom());
245        settings.setMinimumFontSize(getMinimumFontSize());
246        settings.setMinimumLogicalFontSize(getMinimumFontSize());
247        settings.setForceUserScalable(forceEnableUserScalable());
248        settings.setPluginState(getPluginState());
249        settings.setTextZoom(getTextZoom());
250        settings.setAutoFillEnabled(isAutofillEnabled());
251        settings.setLayoutAlgorithm(getLayoutAlgorithm());
252        settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
253        settings.setLoadsImagesAutomatically(loadImages());
254        settings.setLoadWithOverviewMode(loadPageInOverviewMode());
255        settings.setSavePassword(rememberPasswords());
256        settings.setSaveFormData(saveFormdata());
257        settings.setUseWideViewPort(isWideViewport());
258        settings.setAutoFillProfile(getAutoFillProfile());
259
260        String ua = mCustomUserAgents.get(settings);
261        if (ua != null) {
262            settings.setUserAgentString(ua);
263        } else {
264            settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
265        }
266
267        settings.setProperty(WebViewProperties.gfxInvertedScreen,
268                useInvertedRendering() ? "true" : "false");
269
270        settings.setProperty(WebViewProperties.gfxInvertedScreenContrast,
271                Float.toString(getInvertedContrast()));
272
273        settings.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
274                enableCpuUploadPath() ? "true" : "false");
275    }
276
277    /**
278     * Syncs all the settings that have no UI
279     * These cannot change, so we only need to set them once per WebSettings
280     */
281    private void syncStaticSettings(WebSettings settings) {
282        settings.setDefaultFontSize(16);
283        settings.setDefaultFixedFontSize(13);
284        settings.setPageCacheCapacity(getPageCacheCapacity());
285
286        // WebView inside Browser doesn't want initial focus to be set.
287        settings.setNeedInitialFocus(false);
288        // Browser supports multiple windows
289        settings.setSupportMultipleWindows(true);
290        // enable smooth transition for better performance during panning or
291        // zooming
292        settings.setEnableSmoothTransition(true);
293        // disable content url access
294        settings.setAllowContentAccess(false);
295
296        // HTML5 API flags
297        settings.setAppCacheEnabled(true);
298        settings.setDatabaseEnabled(true);
299        settings.setDomStorageEnabled(true);
300        settings.setWorkersEnabled(true);  // This only affects V8.
301
302        // HTML5 configuration parametersettings.
303        settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
304        settings.setAppCachePath(getAppCachePath());
305        settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
306        settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
307    }
308
309    private void syncSharedSettings() {
310        mNeedsSharedSync = false;
311        CookieManager.getInstance().setAcceptCookie(acceptCookies());
312        if (mController != null) {
313            mController.setShouldShowErrorConsole(enableJavascriptConsole());
314        }
315    }
316
317    private void syncManagedSettings() {
318        syncSharedSettings();
319        synchronized (mManagedSettings) {
320            Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
321            while (iter.hasNext()) {
322                WeakReference<WebSettings> ref = iter.next();
323                WebSettings settings = ref.get();
324                if (settings == null) {
325                    iter.remove();
326                    continue;
327                }
328                syncSetting(settings);
329            }
330        }
331    }
332
333    @Override
334    public void onSharedPreferenceChanged(
335            SharedPreferences sharedPreferences, String key) {
336        syncManagedSettings();
337        if (PREF_SEARCH_ENGINE.equals(key)) {
338            updateSearchEngine(false);
339        }
340        if (PREF_USE_INSTANT_SEARCH.equals(key)) {
341            updateSearchEngine(true);
342        }
343        if (PREF_FULLSCREEN.equals(key)) {
344            if (mController.getUi() != null) {
345                mController.getUi().setFullscreen(useFullscreen());
346            }
347        } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
348            if (mController.getUi() != null) {
349                mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
350            }
351        }
352    }
353
354    public static String getFactoryResetHomeUrl(Context context) {
355        requireInitialization();
356        return sFactoryResetUrl;
357    }
358
359    public LayoutAlgorithm getLayoutAlgorithm() {
360        LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
361        if (autofitPages()) {
362            layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
363        }
364        if (isDebugEnabled()) {
365            if (isSmallScreen()) {
366                layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
367            } else {
368                if (isNormalLayout()) {
369                    layoutAlgorithm = LayoutAlgorithm.NORMAL;
370                } else {
371                    layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
372                }
373            }
374        }
375        return layoutAlgorithm;
376    }
377
378    public int getPageCacheCapacity() {
379        requireInitialization();
380        return mPageCacheCapacity;
381    }
382
383    public WebStorageSizeManager getWebStorageSizeManager() {
384        requireInitialization();
385        return mWebStorageSizeManager;
386    }
387
388    private String getAppCachePath() {
389        if (mAppCachePath == null) {
390            mAppCachePath = mContext.getDir("appcache", 0).getPath();
391        }
392        return mAppCachePath;
393    }
394
395    private void updateSearchEngine(boolean force) {
396        String searchEngineName = getSearchEngineName();
397        if (force || mSearchEngine == null ||
398                !mSearchEngine.getName().equals(searchEngineName)) {
399            if (mSearchEngine != null) {
400                if (mSearchEngine.supportsVoiceSearch()) {
401                     // One or more tabs could have been in voice search mode.
402                     // Clear it, since the new SearchEngine may not support
403                     // it, or may handle it differently.
404                     for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
405                         mController.getTabControl().getTab(i).revertVoiceSearchMode();
406                     }
407                 }
408                mSearchEngine.close();
409             }
410            mSearchEngine = SearchEngines.get(mContext, searchEngineName);
411
412             if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
413                 ((InstantSearchEngine) mSearchEngine).setController(mController);
414             }
415         }
416    }
417
418    public SearchEngine getSearchEngine() {
419        if (mSearchEngine == null) {
420            updateSearchEngine(false);
421        }
422        return mSearchEngine;
423    }
424
425    public boolean isDebugEnabled() {
426        requireInitialization();
427        return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
428    }
429
430    public void setDebugEnabled(boolean value) {
431        mPrefs.edit().putBoolean(PREF_DEBUG_MENU, value).apply();
432    }
433
434    public void clearCache() {
435        WebIconDatabase.getInstance().removeAllIcons();
436        if (mController != null) {
437            WebView current = mController.getCurrentWebView();
438            if (current != null) {
439                current.clearCache(true);
440            }
441        }
442    }
443
444    public void clearCookies() {
445        CookieManager.getInstance().removeAllCookie();
446    }
447
448    public void clearHistory() {
449        ContentResolver resolver = mContext.getContentResolver();
450        Browser.clearHistory(resolver);
451        Browser.clearSearches(resolver);
452    }
453
454    public void clearFormData() {
455        WebViewDatabase.getInstance(mContext).clearFormData();
456        if (mController!= null) {
457            WebView currentTopView = mController.getCurrentTopWebView();
458            if (currentTopView != null) {
459                currentTopView.clearFormData();
460            }
461        }
462    }
463
464    public void clearPasswords() {
465        WebViewDatabase db = WebViewDatabase.getInstance(mContext);
466        db.clearUsernamePassword();
467        db.clearHttpAuthUsernamePassword();
468    }
469
470    public void clearDatabases() {
471        WebStorage.getInstance().deleteAllData();
472    }
473
474    public void clearLocationAccess() {
475        GeolocationPermissions.getInstance().clearAll();
476    }
477
478    public void resetDefaultPreferences() {
479        // Preserve autologin setting
480        long gal = mPrefs.getLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, -1);
481        mPrefs.edit()
482                .clear()
483                .putLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, gal)
484                .apply();
485        syncManagedSettings();
486    }
487
488    public AutoFillProfile getAutoFillProfile() {
489        mAutofillHandler.waitForLoad();
490        return mAutofillHandler.getAutoFillProfile();
491    }
492
493    public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
494        mAutofillHandler.waitForLoad();
495        mAutofillHandler.setAutoFillProfile(profile, msg);
496        // Auto-fill will reuse the same profile ID when making edits to the profile,
497        // so we need to force a settings sync (otherwise the SharedPreferences
498        // manager will optimise out the call to onSharedPreferenceChanged(), as
499        // it thinks nothing has changed).
500        syncManagedSettings();
501    }
502
503    public void toggleDebugSettings() {
504        setDebugEnabled(!isDebugEnabled());
505    }
506
507    public boolean hasDesktopUseragent(WebView view) {
508        return view != null && mCustomUserAgents.get(view.getSettings()) != null;
509    }
510
511    public void toggleDesktopUseragent(WebView view) {
512        if (view == null) {
513            return;
514        }
515        WebSettings settings = view.getSettings();
516        if (mCustomUserAgents.get(settings) != null) {
517            mCustomUserAgents.remove(settings);
518            settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
519        } else {
520            mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
521            settings.setUserAgentString(DESKTOP_USERAGENT);
522        }
523    }
524
525    public static int getAdjustedMinimumFontSize(int rawValue) {
526        rawValue++; // Preference starts at 0, min font at 1
527        if (rawValue > 1) {
528            rawValue += (MIN_FONT_SIZE_OFFSET - 2);
529        }
530        return rawValue;
531    }
532
533    public int getAdjustedTextZoom(int rawValue) {
534        rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
535        return (int) ((rawValue + 100) * mFontSizeMult);
536    }
537
538    static int getRawTextZoom(int percent) {
539        return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
540    }
541
542    public SharedPreferences getPreferences() {
543        return mPrefs;
544    }
545
546    // -----------------------------
547    // getter/setters for accessibility_preferences.xml
548    // -----------------------------
549
550    @Deprecated
551    private TextSize getTextSize() {
552        String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
553        return TextSize.valueOf(textSize);
554    }
555
556    public int getMinimumFontSize() {
557        int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
558        return getAdjustedMinimumFontSize(minFont);
559    }
560
561    public boolean forceEnableUserScalable() {
562        return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
563    }
564
565    public int getTextZoom() {
566        requireInitialization();
567        int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
568        return getAdjustedTextZoom(textZoom);
569    }
570
571    public void setTextZoom(int percent) {
572        mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
573    }
574
575    // -----------------------------
576    // getter/setters for advanced_preferences.xml
577    // -----------------------------
578
579    public String getSearchEngineName() {
580        return mPrefs.getString(PREF_SEARCH_ENGINE, SearchEngine.GOOGLE);
581    }
582
583    public boolean openInBackground() {
584        return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
585    }
586
587    public boolean enableJavascript() {
588        return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
589    }
590
591    // TODO: Cache
592    public PluginState getPluginState() {
593        String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
594        return PluginState.valueOf(state);
595    }
596
597    // TODO: Cache
598    public ZoomDensity getDefaultZoom() {
599        String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
600        return ZoomDensity.valueOf(zoom);
601    }
602
603    public boolean loadPageInOverviewMode() {
604        return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
605    }
606
607    public boolean autofitPages() {
608        return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
609    }
610
611    public boolean blockPopupWindows() {
612        return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
613    }
614
615    public boolean loadImages() {
616        return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
617    }
618
619    public String getDefaultTextEncoding() {
620        return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
621    }
622
623    // -----------------------------
624    // getter/setters for general_preferences.xml
625    // -----------------------------
626
627    public String getHomePage() {
628        return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
629    }
630
631    public void setHomePage(String value) {
632        mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
633    }
634
635    public boolean isAutofillEnabled() {
636        return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
637    }
638
639    public void setAutofillEnabled(boolean value) {
640        mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
641    }
642
643    // -----------------------------
644    // getter/setters for debug_preferences.xml
645    // -----------------------------
646
647    public boolean isHardwareAccelerated() {
648        if (!isDebugEnabled()) {
649            return true;
650        }
651        return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
652    }
653
654    public boolean isSkiaHardwareAccelerated() {
655        if (!isDebugEnabled()) {
656            return false;
657        }
658        return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
659    }
660
661    public int getUserAgent() {
662        if (!isDebugEnabled()) {
663            return 0;
664        }
665        return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
666    }
667
668    // -----------------------------
669    // getter/setters for hidden_debug_preferences.xml
670    // -----------------------------
671
672    public boolean enableVisualIndicator() {
673        if (!isDebugEnabled()) {
674            return false;
675        }
676        return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
677    }
678
679    public boolean enableCpuUploadPath() {
680        if (!isDebugEnabled()) {
681            return false;
682        }
683        return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
684    }
685
686    public boolean enableJavascriptConsole() {
687        if (!isDebugEnabled()) {
688            return false;
689        }
690        return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
691    }
692
693    public boolean isSmallScreen() {
694        if (!isDebugEnabled()) {
695            return false;
696        }
697        return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
698    }
699
700    public boolean isWideViewport() {
701        if (!isDebugEnabled()) {
702            return true;
703        }
704        return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
705    }
706
707    public boolean isNormalLayout() {
708        if (!isDebugEnabled()) {
709            return false;
710        }
711        return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
712    }
713
714    public boolean isTracing() {
715        if (!isDebugEnabled()) {
716            return false;
717        }
718        return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
719    }
720
721    public boolean enableLightTouch() {
722        if (!isDebugEnabled()) {
723            return false;
724        }
725        return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
726    }
727
728    public boolean enableNavDump() {
729        if (!isDebugEnabled()) {
730            return false;
731        }
732        return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
733    }
734
735    public String getJsEngineFlags() {
736        if (!isDebugEnabled()) {
737            return "";
738        }
739        return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
740    }
741
742    // -----------------------------
743    // getter/setters for lab_preferences.xml
744    // -----------------------------
745
746    public boolean useQuickControls() {
747        return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
748    }
749
750    public boolean useMostVisitedHomepage() {
751        return HomeProvider.MOST_VISITED.equals(getHomePage());
752    }
753
754    public boolean useInstantSearch() {
755        return mPrefs.getBoolean(PREF_USE_INSTANT_SEARCH, false);
756    }
757
758    public boolean useFullscreen() {
759        return mPrefs.getBoolean(PREF_FULLSCREEN, false);
760    }
761
762    public boolean useInvertedRendering() {
763        return mPrefs.getBoolean(PREF_INVERTED, false);
764    }
765
766    public float getInvertedContrast() {
767        return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
768    }
769
770    // -----------------------------
771    // getter/setters for privacy_security_preferences.xml
772    // -----------------------------
773
774    public boolean showSecurityWarnings() {
775        return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
776    }
777
778    public boolean acceptCookies() {
779        return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
780    }
781
782    public boolean saveFormdata() {
783        return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
784    }
785
786    public boolean enableGeolocation() {
787        return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
788    }
789
790    public boolean rememberPasswords() {
791        return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
792    }
793
794    // -----------------------------
795    // getter/setters for bandwidth_preferences.xml
796    // -----------------------------
797
798    public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
799        return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
800    }
801
802    public static String getPreloadAlwaysPreferenceString(Context context) {
803        return context.getResources().getString(R.string.pref_data_preload_value_always);
804    }
805
806    private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
807            "browser_default_preload_setting";
808
809    public String getDefaultPreloadSetting() {
810        String preload = Settings.Secure.getString(mContext.getContentResolver(),
811                DEAULT_PRELOAD_SECURE_SETTING_KEY);
812        if (preload == null) {
813            preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
814        }
815        return preload;
816    }
817
818    public String getPreloadEnabled() {
819        return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
820    }
821
822}
823