BrowserPreferencesPage.java revision ed217d91fb3f1a8f4e75ab36ef81d72ef9f4e6d6
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_TEXT_ENCODING);
55        e.setOnPreferenceChangeListener(this);
56
57        if (BrowserSettings.getInstance().showDebugSettings()) {
58            addPreferencesFromResource(R.xml.debug_preferences);
59        }
60
61        e = findPreference(BrowserSettings.PREF_GEARS_SETTINGS);
62        e.setOnPreferenceClickListener(this);
63    }
64
65    @Override
66    protected void onPause() {
67        super.onPause();
68
69        // sync the shared preferences back to BrowserSettings
70        BrowserSettings.getInstance().syncSharedPreferences(
71                getPreferenceScreen().getSharedPreferences());
72    }
73
74    public boolean onPreferenceChange(Preference pref, Object objValue) {
75        if (pref.getKey().equals(BrowserSettings.PREF_EXTRAS_RESET_DEFAULTS)) {
76            Boolean value = (Boolean) objValue;
77            if (value.booleanValue() == true) {
78                finish();
79            }
80        } else if (pref.getKey().equals(BrowserSettings.PREF_HOMEPAGE)) {
81            String value = (String) objValue;
82            boolean needUpdate = value.indexOf(' ') != -1;
83            if (needUpdate) {
84                value = value.trim().replace(" ", "%20");
85            }
86            Uri path = Uri.parse(value);
87            if (path.getScheme() == null) {
88                value = "http://" + value;
89                needUpdate = true;
90            }
91            // Set the summary value.
92            pref.setSummary(value);
93            if (needUpdate) {
94                // Update through the EditText control as it has a cached copy
95                // of the string and it will handle persisting the value
96                ((EditTextPreference) pref).setText(value);
97
98                // as we update the value above, we need to return false
99                // here so that setText() is not called by EditTextPref
100                // with the old value.
101                return false;
102            } else {
103                return true;
104            }
105        } else if (pref.getKey().equals(BrowserSettings.PREF_TEXT_SIZE)) {
106            pref.setSummary(getVisualTextSizeName((String) objValue));
107            return true;
108        } else if (pref.getKey().equals(
109                BrowserSettings.PREF_DEFAULT_TEXT_ENCODING)) {
110            pref.setSummary((String) objValue);
111            return true;
112        }
113
114        return false;
115    }
116
117    public boolean onPreferenceClick(Preference pref) {
118        if (pref.getKey().equals(BrowserSettings.PREF_GEARS_SETTINGS)) {
119            List<Plugin> loadedPlugins = WebView.getPluginList().getList();
120            for(Plugin p : loadedPlugins) {
121                if (p.getName().equals("gears")) {
122                    p.dispatchClickEvent(this);
123                    return true;
124                }
125            }
126
127        }
128        return true;
129    }
130
131    private CharSequence getVisualTextSizeName(String enumName) {
132        CharSequence[] visualNames =
133                getResources().getTextArray(R.array.pref_text_size_choices);
134        CharSequence[] enumNames =
135                getResources().getTextArray(R.array.pref_text_size_values);
136
137        // Sanity check
138        if (visualNames.length != enumNames.length) {
139            return "";
140        }
141
142        for (int i = 0; i < enumNames.length; i++) {
143            if (enumNames[i].equals(enumName)) {
144                return visualNames[i];
145            }
146        }
147
148        return "";
149    }
150}
151