OSDN Git Service

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