BrowserPreferencesPage.java revision 2f83068b9e8835f97010bc2ee1d77f3a13827ae4
1/* 2 * Copyright (C) 2008 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 java.util.List; 20 21import android.net.Uri; 22import android.os.Bundle; 23import android.preference.EditTextPreference; 24import android.preference.Preference; 25import android.preference.PreferenceActivity; 26import android.webkit.WebView; 27import android.webkit.Plugin; 28 29public class BrowserPreferencesPage extends PreferenceActivity 30 implements Preference.OnPreferenceChangeListener, 31 Preference.OnPreferenceClickListener { 32 33 @Override 34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 37 // Load the XML preferences file 38 addPreferencesFromResource(R.xml.browser_preferences); 39 40 Preference e = findPreference(BrowserSettings.PREF_HOMEPAGE); 41 e.setOnPreferenceChangeListener(this); 42 e.setSummary(getPreferenceScreen().getSharedPreferences() 43 .getString(BrowserSettings.PREF_HOMEPAGE, null)); 44 45 e = findPreference(BrowserSettings.PREF_EXTRAS_RESET_DEFAULTS); 46 e.setOnPreferenceChangeListener(this); 47 48 e = findPreference(BrowserSettings.PREF_TEXT_SIZE); 49 e.setOnPreferenceChangeListener(this); 50 e.setSummary(getVisualTextSizeName( 51 getPreferenceScreen().getSharedPreferences() 52 .getString(BrowserSettings.PREF_TEXT_SIZE, null)) ); 53 54 e = findPreference(BrowserSettings.PREF_DEFAULT_ZOOM); 55 e.setOnPreferenceChangeListener(this); 56 e.setSummary(getVisualDefaultZoomName( 57 getPreferenceScreen().getSharedPreferences() 58 .getString(BrowserSettings.PREF_DEFAULT_ZOOM, null)) ); 59 60 e = findPreference(BrowserSettings.PREF_DEFAULT_TEXT_ENCODING); 61 e.setOnPreferenceChangeListener(this); 62 63 if (BrowserSettings.getInstance().showDebugSettings()) { 64 addPreferencesFromResource(R.xml.debug_preferences); 65 } 66 67 e = findPreference(BrowserSettings.PREF_GEARS_SETTINGS); 68 e.setOnPreferenceClickListener(this); 69 } 70 71 @Override 72 protected void onPause() { 73 super.onPause(); 74 75 // sync the shared preferences back to BrowserSettings 76 BrowserSettings.getInstance().syncSharedPreferences( 77 getPreferenceScreen().getSharedPreferences()); 78 } 79 80 public boolean onPreferenceChange(Preference pref, Object objValue) { 81 if (pref.getKey().equals(BrowserSettings.PREF_EXTRAS_RESET_DEFAULTS)) { 82 Boolean value = (Boolean) objValue; 83 if (value.booleanValue() == true) { 84 finish(); 85 } 86 } else if (pref.getKey().equals(BrowserSettings.PREF_HOMEPAGE)) { 87 String value = (String) objValue; 88 boolean needUpdate = value.indexOf(' ') != -1; 89 if (needUpdate) { 90 value = value.trim().replace(" ", "%20"); 91 } 92 if (value.length() != 0 && Uri.parse(value).getScheme() == null) { 93 value = "http://" + value; 94 needUpdate = true; 95 } 96 // Set the summary value. 97 pref.setSummary(value); 98 if (needUpdate) { 99 // Update through the EditText control as it has a cached copy 100 // of the string and it will handle persisting the value 101 ((EditTextPreference) pref).setText(value); 102 103 // as we update the value above, we need to return false 104 // here so that setText() is not called by EditTextPref 105 // with the old value. 106 return false; 107 } else { 108 return true; 109 } 110 } else if (pref.getKey().equals(BrowserSettings.PREF_TEXT_SIZE)) { 111 pref.setSummary(getVisualTextSizeName((String) objValue)); 112 return true; 113 } else if (pref.getKey().equals(BrowserSettings.PREF_DEFAULT_ZOOM)) { 114 pref.setSummary(getVisualDefaultZoomName((String) objValue)); 115 return true; 116 } else if (pref.getKey().equals( 117 BrowserSettings.PREF_DEFAULT_TEXT_ENCODING)) { 118 pref.setSummary((String) objValue); 119 return true; 120 } 121 122 return false; 123 } 124 125 public boolean onPreferenceClick(Preference pref) { 126 if (pref.getKey().equals(BrowserSettings.PREF_GEARS_SETTINGS)) { 127 List<Plugin> loadedPlugins = WebView.getPluginList().getList(); 128 for(Plugin p : loadedPlugins) { 129 if (p.getName().equals("gears")) { 130 p.dispatchClickEvent(this); 131 return true; 132 } 133 } 134 135 } 136 return true; 137 } 138 139 private CharSequence getVisualTextSizeName(String enumName) { 140 CharSequence[] visualNames = getResources().getTextArray( 141 R.array.pref_text_size_choices); 142 CharSequence[] enumNames = getResources().getTextArray( 143 R.array.pref_text_size_values); 144 145 // Sanity check 146 if (visualNames.length != enumNames.length) { 147 return ""; 148 } 149 150 for (int i = 0; i < enumNames.length; i++) { 151 if (enumNames[i].equals(enumName)) { 152 return visualNames[i]; 153 } 154 } 155 156 return ""; 157 } 158 159 private CharSequence getVisualDefaultZoomName(String enumName) { 160 CharSequence[] visualNames = getResources().getTextArray( 161 R.array.pref_default_zoom_choices); 162 CharSequence[] enumNames = getResources().getTextArray( 163 R.array.pref_default_zoom_values); 164 165 // Sanity check 166 if (visualNames.length != enumNames.length) { 167 return ""; 168 } 169 170 for (int i = 0; i < enumNames.length; i++) { 171 if (enumNames[i].equals(enumName)) { 172 return visualNames[i]; 173 } 174 } 175 176 return ""; 177 } 178} 179