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 17 package com.android.browser.preferences; 18 19 import android.content.Context; 20 import android.os.Bundle; 21 import android.preference.Preference; 22 import android.preference.PreferenceFragment; 23 import android.webkit.WebView; 24 25 import com.android.browser.BrowserSettings; 26 import com.android.browser.PreferenceKeys; 27 import com.android.browser.R; 28 29 import java.text.NumberFormat; 30 31 public class AccessibilityPreferencesFragment extends PreferenceFragment 32 implements Preference.OnPreferenceChangeListener { 33 34 NumberFormat mFormat; 35 // Used to pause/resume timers, which are required for WebViewPreview 36 WebView mControlWebView; 37 38 @Override 39 public void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 mControlWebView = new WebView(getActivity()); 42 addPreferencesFromResource(R.xml.accessibility_preferences); 43 BrowserSettings settings = BrowserSettings.getInstance(); 44 mFormat = NumberFormat.getPercentInstance(); 45 46 Preference e = findPreference(PreferenceKeys.PREF_MIN_FONT_SIZE); 47 e.setOnPreferenceChangeListener(this); 48 updateMinFontSummary(e, settings.getMinimumFontSize()); 49 e = findPreference(PreferenceKeys.PREF_TEXT_ZOOM); 50 e.setOnPreferenceChangeListener(this); 51 updateTextZoomSummary(e, settings.getTextZoom()); 52 e = findPreference(PreferenceKeys.PREF_DOUBLE_TAP_ZOOM); 53 e.setOnPreferenceChangeListener(this); 54 updateDoubleTapZoomSummary(e, settings.getDoubleTapZoom()); 55 e = findPreference(PreferenceKeys.PREF_INVERTED_CONTRAST); 56 e.setOnPreferenceChangeListener(this); 57 updateInvertedContrastSummary(e, (int) (settings.getInvertedContrast() * 100)); 58 } 59 60 @Override 61 public void onResume() { 62 super.onResume(); 63 mControlWebView.resumeTimers(); 64 } 65 66 @Override 67 public void onPause() { 68 super.onPause(); 69 mControlWebView.pauseTimers(); 70 } 71 72 @Override 73 public void onDestroy() { 74 super.onDestroy(); 75 mControlWebView.destroy(); 76 mControlWebView = null; 77 } 78 79 void updateMinFontSummary(Preference pref, int minFontSize) { 80 Context c = getActivity(); 81 pref.setSummary(c.getString(R.string.pref_min_font_size_value, minFontSize)); 82 } 83 84 void updateTextZoomSummary(Preference pref, int textZoom) { 85 pref.setSummary(mFormat.format(textZoom / 100.0)); 86 } 87 88 void updateDoubleTapZoomSummary(Preference pref, int doubleTapZoom) { 89 pref.setSummary(mFormat.format(doubleTapZoom / 100.0)); 90 } 91 92 void updateInvertedContrastSummary(Preference pref, int contrast) { 93 pref.setSummary(mFormat.format(contrast / 100.0)); 94 } 95 96 @Override 97 public boolean onPreferenceChange(Preference pref, Object objValue) { 98 if (getActivity() == null) { 99 // We aren't attached, so don't accept preferences changes from the 100 // invisible UI. 101 return false; 102 } 103 104 if (PreferenceKeys.PREF_MIN_FONT_SIZE.equals(pref.getKey())) { 105 updateMinFontSummary(pref, BrowserSettings 106 .getAdjustedMinimumFontSize((Integer) objValue)); 107 } 108 if (PreferenceKeys.PREF_TEXT_ZOOM.equals(pref.getKey())) { 109 BrowserSettings settings = BrowserSettings.getInstance(); 110 updateTextZoomSummary(pref, settings 111 .getAdjustedTextZoom((Integer) objValue)); 112 } 113 if (PreferenceKeys.PREF_DOUBLE_TAP_ZOOM.equals(pref.getKey())) { 114 BrowserSettings settings = BrowserSettings.getInstance(); 115 updateDoubleTapZoomSummary(pref, settings 116 .getAdjustedDoubleTapZoom((Integer) objValue)); 117 } 118 if (PreferenceKeys.PREF_INVERTED_CONTRAST.equals(pref.getKey())) { 119 updateInvertedContrastSummary(pref, 120 (int) ((10 + (Integer) objValue) * 10)); 121 } 122 return true; 123 } 124 125 } 126