1 /* 2 * Copyright (C) 2010 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 com.android.browser.BrowserActivity; 20 import com.android.browser.PreferenceKeys; 21 import com.android.browser.R; 22 23 import android.content.Intent; 24 import android.content.res.Resources; 25 import android.os.Bundle; 26 import android.preference.ListPreference; 27 import android.preference.Preference; 28 import android.preference.PreferenceFragment; 29 import android.preference.PreferenceScreen; 30 import android.util.Log; 31 import android.webkit.GeolocationPermissions; 32 import android.webkit.ValueCallback; 33 import android.webkit.WebStorage; 34 35 import java.util.Map; 36 import java.util.Set; 37 38 public class AdvancedPreferencesFragment extends PreferenceFragment 39 implements Preference.OnPreferenceChangeListener { 40 41 @Override onCreate(Bundle savedInstanceState)42 public void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 45 // Load the XML preferences file 46 addPreferencesFromResource(R.xml.advanced_preferences); 47 48 PreferenceScreen websiteSettings = (PreferenceScreen) findPreference( 49 PreferenceKeys.PREF_WEBSITE_SETTINGS); 50 websiteSettings.setFragment(WebsiteSettingsFragment.class.getName()); 51 52 Preference e = findPreference(PreferenceKeys.PREF_DEFAULT_ZOOM); 53 e.setOnPreferenceChangeListener(this); 54 e.setSummary(getVisualDefaultZoomName( 55 getPreferenceScreen().getSharedPreferences() 56 .getString(PreferenceKeys.PREF_DEFAULT_ZOOM, null)) ); 57 58 e = findPreference(PreferenceKeys.PREF_DEFAULT_TEXT_ENCODING); 59 e.setOnPreferenceChangeListener(this); 60 61 e = findPreference(PreferenceKeys.PREF_RESET_DEFAULT_PREFERENCES); 62 e.setOnPreferenceChangeListener(this); 63 64 e = findPreference(PreferenceKeys.PREF_SEARCH_ENGINE); 65 e.setOnPreferenceChangeListener(this); 66 updateListPreferenceSummary((ListPreference) e); 67 68 e = findPreference(PreferenceKeys.PREF_PLUGIN_STATE); 69 e.setOnPreferenceChangeListener(this); 70 updateListPreferenceSummary((ListPreference) e); 71 } 72 updateListPreferenceSummary(ListPreference e)73 void updateListPreferenceSummary(ListPreference e) { 74 e.setSummary(e.getEntry()); 75 } 76 77 /* 78 * We need to set the PreferenceScreen state in onResume(), as the number of 79 * origins with active features (WebStorage, Geolocation etc) could have 80 * changed after calling the WebsiteSettingsActivity. 81 */ 82 @Override onResume()83 public void onResume() { 84 super.onResume(); 85 final PreferenceScreen websiteSettings = (PreferenceScreen) findPreference( 86 PreferenceKeys.PREF_WEBSITE_SETTINGS); 87 websiteSettings.setEnabled(false); 88 WebStorage.getInstance().getOrigins(new ValueCallback<Map>() { 89 @Override 90 public void onReceiveValue(Map webStorageOrigins) { 91 if ((webStorageOrigins != null) && !webStorageOrigins.isEmpty()) { 92 websiteSettings.setEnabled(true); 93 } 94 } 95 }); 96 GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() { 97 @Override 98 public void onReceiveValue(Set<String> geolocationOrigins) { 99 if ((geolocationOrigins != null) && !geolocationOrigins.isEmpty()) { 100 websiteSettings.setEnabled(true); 101 } 102 } 103 }); 104 } 105 106 @Override onPreferenceChange(Preference pref, Object objValue)107 public boolean onPreferenceChange(Preference pref, Object objValue) { 108 if (getActivity() == null) { 109 // We aren't attached, so don't accept preferences changes from the 110 // invisible UI. 111 Log.w("PageContentPreferencesFragment", "onPreferenceChange called from detached fragment!"); 112 return false; 113 } 114 115 if (pref.getKey().equals(PreferenceKeys.PREF_DEFAULT_ZOOM)) { 116 pref.setSummary(getVisualDefaultZoomName((String) objValue)); 117 return true; 118 } else if (pref.getKey().equals(PreferenceKeys.PREF_DEFAULT_TEXT_ENCODING)) { 119 pref.setSummary((String) objValue); 120 return true; 121 } else if (pref.getKey().equals(PreferenceKeys.PREF_RESET_DEFAULT_PREFERENCES)) { 122 Boolean value = (Boolean) objValue; 123 if (value.booleanValue() == true) { 124 startActivity(new Intent(BrowserActivity.ACTION_RESTART, null, 125 getActivity(), BrowserActivity.class)); 126 return true; 127 } 128 } else if (pref.getKey().equals(PreferenceKeys.PREF_PLUGIN_STATE) 129 || pref.getKey().equals(PreferenceKeys.PREF_SEARCH_ENGINE)) { 130 ListPreference lp = (ListPreference) pref; 131 lp.setValue((String) objValue); 132 updateListPreferenceSummary(lp); 133 return false; 134 } 135 return false; 136 } 137 getVisualDefaultZoomName(String enumName)138 private CharSequence getVisualDefaultZoomName(String enumName) { 139 Resources res = getActivity().getResources(); 140 CharSequence[] visualNames = res.getTextArray(R.array.pref_default_zoom_choices); 141 CharSequence[] enumNames = res.getTextArray(R.array.pref_default_zoom_values); 142 143 // Sanity check 144 if (visualNames.length != enumNames.length) { 145 return ""; 146 } 147 148 int length = enumNames.length; 149 for (int i = 0; i < length; i++) { 150 if (enumNames[i].equals(enumName)) { 151 return visualNames[i]; 152 } 153 } 154 155 return ""; 156 } 157 }