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