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