Bug 773535 - Use tablet style prefs on tablets. r=bnicholson

This commit is contained in:
Wes Johnston 2013-04-22 14:39:30 -07:00
parent a9c99fbd1c
commit 20a1e61eb3
9 changed files with 242 additions and 104 deletions

View File

@ -0,0 +1,61 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko;
import java.util.ArrayList;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.os.Bundle;
import android.util.Log;
/* A simple implementation of PreferenceFragment for large screen devices
* This will strip category headers (so that they aren't shown to the user twice)
* as well as initializing Gecko prefs when a fragment is shown.
*/
public class GeckoPreferenceFragment extends PreferenceFragment {
private static final String LOGTAG = "GeckoPreferenceFragment";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String resource = getArguments().getString("resource");
int res = getActivity().getResources().getIdentifier(resource,
"xml",
getActivity().getPackageName());
addPreferencesFromResource(res);
/* This is only hit when we're using the headers (i.e. on large screen devices).
Strip the first category it isn't shown twice */
PreferenceScreen screen = stripCategories(getPreferenceScreen());
setPreferenceScreen(screen);
((GeckoPreferences)getActivity()).setupPreferences(screen);
}
private PreferenceScreen stripCategories(PreferenceScreen preferenceScreen) {
PreferenceScreen newScreen = getPreferenceManager().createPreferenceScreen(preferenceScreen.getContext());
int order = 0;
if (preferenceScreen.getPreferenceCount() > 0 && preferenceScreen.getPreference(0) instanceof PreferenceCategory) {
PreferenceCategory cat = (PreferenceCategory) preferenceScreen.getPreference(0);
for (int i = 0; i < cat.getPreferenceCount(); i++) {
Preference pref = cat.getPreference(i);
pref.setOrder(order++);
newScreen.addPreference(pref);
}
}
for (int i = 1; i < preferenceScreen.getPreferenceCount(); i++) {
Preference pref = preferenceScreen.getPreference(i);
pref.setOrder(order++);
newScreen.addPreference(pref);
}
return newScreen;
}
}

View File

@ -41,6 +41,7 @@ import android.widget.LinearLayout;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class GeckoPreferences
extends PreferenceActivity
@ -52,6 +53,7 @@ public class GeckoPreferences
private PreferenceScreen mPreferenceScreen;
private static boolean sIsCharEncodingEnabled = false;
private static final String NON_PREF_PREFIX = "android.not_a_preference.";
private boolean mInitialized = false;
// These match keys in resources/xml/preferences.xml.in.
private static String PREFS_ANNOUNCEMENTS_ENABLED = NON_PREF_PREFIX + "privacy.announcements.enabled";
@ -66,38 +68,36 @@ public class GeckoPreferences
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
// If this is a smaller screen
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB || !onIsMultiPane()) {
addPreferencesFromResource(R.xml.preferences_general);
addPreferencesFromResource(R.xml.preferences_content);
addPreferencesFromResource(R.xml.preferences_privacy);
}
registerEventListener("Sanitize:Finished");
if (Build.VERSION.SDK_INT >= 14)
getActionBar().setHomeButtonEnabled(true);
}
mPreferenceScreen = getPreferenceScreen();
if (!AppConstants.MOZ_UPDATER) {
((PreferenceGroup) mPreferenceScreen.findPreference(PREFS_CATEGORY_GENERAL))
.removePreference(findPreference(PREFS_UPDATER_AUTODOWNLOAD));
}
Preference telemetryPref = findPreference(PREFS_TELEMETRY_ENABLED);
if (AppConstants.MOZ_TELEMETRY_REPORTING) {
if (AppConstants.MOZ_TELEMETRY_ON_BY_DEFAULT) {
telemetryPref.setKey(PREFS_TELEMETRY_ENABLED_PRERELEASE);
}
} else {
((PreferenceGroup) mPreferenceScreen.findPreference(PREFS_CATEGORY_PRIVACY))
.removePreference(telemetryPref);
}
@Override
public void onBuildHeaders(List<Header> target) {
if (onIsMultiPane())
loadHeadersFromResource(R.xml.preference_headers, target);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if (!hasFocus)
if (!hasFocus || mInitialized)
return;
mPreferencesList = new ArrayList<String>();
initGroups(mPreferenceScreen);
initValues();
mInitialized = true;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB || !onIsMultiPane()) {
PreferenceScreen screen = getPreferenceScreen();
setupPreferences(screen);
}
}
@Override
@ -143,14 +143,37 @@ public class GeckoPreferences
}
}
private void initGroups(PreferenceGroup preferences) {
final int count = preferences.getPreferenceCount();
for (int i = 0; i < count; i++) {
public void setupPreferences(PreferenceGroup prefs) {
ArrayList<String> list = new ArrayList<String>();
setupPreferences(prefs, list);
getGeckoPreferences(prefs, list);
}
private void setupPreferences(PreferenceGroup preferences, ArrayList<String> prefs) {
for (int i = 0; i < preferences.getPreferenceCount(); i++) {
Preference pref = preferences.getPreference(i);
if (pref instanceof PreferenceGroup)
initGroups((PreferenceGroup)pref);
else {
if (pref instanceof PreferenceGroup) {
setupPreferences((PreferenceGroup)pref, prefs);
} else {
String key = pref.getKey();
pref.setOnPreferenceChangeListener(this);
if (PREFS_UPDATER_AUTODOWNLOAD.equals(key) && !AppConstants.MOZ_UPDATER) {
preferences.removePreference(pref);
i--;
continue;
} else if (PREFS_TELEMETRY_ENABLED.equals(key)) {
if (AppConstants.MOZ_TELEMETRY_REPORTING) {
if (AppConstants.MOZ_TELEMETRY_ON_BY_DEFAULT) {
pref.setKey(PREFS_TELEMETRY_ENABLED_PRERELEASE);
key = PREFS_TELEMETRY_ENABLED_PRERELEASE;
}
} else {
preferences.removePreference(pref);
i--;
continue;
}
}
// Some Preference UI elements are not actually preferences,
// but they require a key to work correctly. For example,
@ -158,9 +181,8 @@ public class GeckoPreferences
// saved when the orientation changes. It uses the
// "android.not_a_preference.privacy.clear" key - which doesn't
// exist in Gecko - to satisfy this requirement.
String key = pref.getKey();
if (key != null && !key.startsWith(NON_PREF_PREFIX)) {
mPreferencesList.add(pref.getKey());
prefs.add(key);
}
}
}
@ -246,16 +268,16 @@ public class GeckoPreferences
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String prefName = preference.getKey();
if (prefName != null && prefName.equals(PREFS_MP_ENABLED)) {
if (PREFS_MP_ENABLED.equals(prefName)) {
showDialog((Boolean) newValue ? DIALOG_CREATE_MASTER_PASSWORD : DIALOG_REMOVE_MASTER_PASSWORD);
return false;
} else if (prefName != null && prefName.equals(PREFS_MENU_CHAR_ENCODING)) {
} else if (PREFS_MENU_CHAR_ENCODING.equals(prefName)) {
setCharEncodingState(((String) newValue).equals("true"));
} else if (prefName != null && prefName.equals(PREFS_ANNOUNCEMENTS_ENABLED)) {
} else if (PREFS_ANNOUNCEMENTS_ENABLED.equals(prefName)) {
// Send a broadcast intent to the product announcements service, either to start or
// to stop the repeated background checks.
broadcastAnnouncementsPref(GeckoApp.mAppContext, ((Boolean) newValue).booleanValue());
} else if (prefName != null && prefName.equals(PREFS_UPDATER_AUTODOWNLOAD)) {
} else if (PREFS_UPDATER_AUTODOWNLOAD.equals(prefName)) {
org.mozilla.gecko.updater.UpdateServiceHelper.registerForUpdates(GeckoApp.mAppContext, (String)newValue);
}
@ -434,14 +456,16 @@ public class GeckoPreferences
}
// Initialize preferences by requesting the preference values from Gecko
private void initValues() {
JSONArray jsonPrefs = new JSONArray(mPreferencesList);
private void getGeckoPreferences(final PreferenceGroup screen, ArrayList<String> prefs) {
JSONArray jsonPrefs = new JSONArray(prefs);
PrefsHelper.getPrefs(jsonPrefs, new PrefsHelper.PrefHandlerBase() {
private Preference getField(String prefName) {
return (mPreferenceScreen == null ? null : mPreferenceScreen.findPreference(prefName));
return screen.findPreference(prefName);
}
@Override public void prefValue(String prefName, final boolean value) {
@Override
public void prefValue(String prefName, final boolean value) {
final Preference pref = getField(prefName);
if (pref instanceof CheckBoxPreference) {
ThreadUtils.postToUiThread(new Runnable() {
@ -454,7 +478,8 @@ public class GeckoPreferences
}
}
@Override public void prefValue(String prefName, final String value) {
@Override
public void prefValue(String prefName, final String value) {
final Preference pref = getField(prefName);
if (pref instanceof EditTextPreference) {
ThreadUtils.postToUiThread(new Runnable() {
@ -486,12 +511,13 @@ public class GeckoPreferences
}
}
@Override public void finish() {
@Override
public void finish() {
// enable all preferences once we have them from gecko
ThreadUtils.postToUiThread(new Runnable() {
@Override
public void run() {
mPreferenceScreen.setEnabled(true);
screen.setEnabled(true);
}
});
}

View File

@ -102,6 +102,7 @@ FENNEC_JAVA_FILES = \
GeckoMessageReceiver.java \
GeckoSubMenu.java \
GeckoPreferences.java \
GeckoPreferenceFragment.java \
GeckoProfile.java \
GeckoPopupMenu.java \
GeckoSmsManager.java \
@ -523,7 +524,10 @@ RES_VALUES_V14 = \
$(NULL)
RES_XML = \
res/xml/preferences.xml \
res/xml/preference_headers.xml \
res/xml/preferences_general.xml \
res/xml/preferences_privacy.xml \
res/xml/preferences_content.xml \
$(SYNC_RES_XML) \
$(NULL)

View File

@ -65,12 +65,16 @@
<!ENTITY pref_category_general "General">
<!ENTITY pref_category_privacy "Privacy &amp; Security">
<!ENTITY pref_category_content "Content">
<!ENTITY pref_category_importexport "Import &amp; Export">
<!ENTITY pref_about_firefox "About &brandShortName;">
<!ENTITY pref_do_not_track "Tell sites not to track me">
<!ENTITY pref_telemetry "Send performance data">
<!ENTITY pref_remember_signons "Remember passwords">
<!-- Localization note: These are shown in the left sidebar on tablets -->
<!ENTITY pref_header_general "General">
<!ENTITY pref_header_privacy "Privacy &amp; Security">
<!ENTITY pref_header_content "Content">
<!ENTITY pref_cookies_menu "Cookies">
<!ENTITY pref_cookies_accept_all "Enabled">
<!ENTITY pref_cookies_not_accept_foreign "Enabled, excluding 3rd party">

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
<header android:fragment="org.mozilla.gecko.GeckoPreferenceFragment"
android:title="@string/pref_header_general">
<extra android:name="resource"
android:value="preferences_general"/>
</header>
<header android:fragment="org.mozilla.gecko.GeckoPreferenceFragment"
android:title="@string/pref_header_content">
<extra android:name="resource"
android:value="preferences_content"/>
</header>
<header android:fragment="org.mozilla.gecko.GeckoPreferenceFragment"
android:title="@string/pref_header_privacy">
<extra android:name="resource"
android:value="preferences_privacy"/>
</header>
</preference-headers>

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:enabled="false">
<PreferenceCategory android:title="@string/pref_category_content">
<ListPreference android:key="browser.menu.showCharacterEncoding"
android:title="@string/pref_char_encoding"
android:entries="@array/pref_char_encoding_entries"
android:entryValues="@array/pref_char_encoding_values"
android:persistent="false" />
<ListPreference android:key="plugin.enable"
android:title="@string/pref_plugins"
android:entries="@array/pref_plugins_entries"
android:entryValues="@array/pref_plugins_values"
android:persistent="false" />
<org.mozilla.gecko.FontSizePreference
android:key="font.size.inflation.minTwips"
android:title="@string/pref_text_size"
android:positiveButtonText="@string/pref_font_size_set"
android:negativeButtonText="@string/button_cancel"
android:persistent="false" />
<CheckBoxPreference
android:key="browser.zoom.reflowOnZoom"
android:title="@string/pref_reflow_on_zoom"
android:defaultValue="false"
android:persistent="false" />
</PreferenceCategory>
</PreferenceScreen>

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:gecko="http://schemas.android.com/apk/res-auto"
android:enabled="false">
<PreferenceCategory android:title="@string/pref_category_general">
<org.mozilla.gecko.LinkPreference android:title="@string/pref_about_firefox"
url="about:" />
<org.mozilla.gecko.SyncPreference android:title="@string/pref_sync"
android:persistent="false" />
<ListPreference android:key="app.update.autodownload"
android:title="@string/pref_update_autodownload"
android:entries="@array/pref_update_autodownload_entries"
android:entryValues="@array/pref_update_autodownload_values"
android:persistent="false" />
<org.mozilla.gecko.AndroidImportPreference
android:key="android.not_a_preference.import_android"
gecko:entries="@array/pref_import_android_entries"
gecko:entryKeys="@array/pref_import_android_keys"
gecko:initialValues="@array/pref_import_android_values"
android:title="@string/pref_import_android"
android:positiveButtonText="@string/bookmarkhistory_button_import"
android:negativeButtonText="@string/button_cancel"
android:persistent="false" />
</PreferenceCategory>
</PreferenceScreen>

View File

@ -3,58 +3,11 @@
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:gecko="http://schemas.android.com/apk/res-auto"
android:enabled="false">
<PreferenceCategory android:title="@string/pref_category_general"
android:key="category_general">
<org.mozilla.gecko.LinkPreference android:title="@string/pref_about_firefox"
url="about:" />
<org.mozilla.gecko.SyncPreference android:title="@string/pref_sync"
android:persistent="false" />
<ListPreference android:key="app.update.autodownload"
android:title="@string/pref_update_autodownload"
android:entries="@array/pref_update_autodownload_entries"
android:entryValues="@array/pref_update_autodownload_values"
android:persistent="false" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_category_content">
<ListPreference android:key="browser.menu.showCharacterEncoding"
android:title="@string/pref_char_encoding"
android:entries="@array/pref_char_encoding_entries"
android:entryValues="@array/pref_char_encoding_values"
android:persistent="false" />
<ListPreference android:key="plugin.enable"
android:title="@string/pref_plugins"
android:entries="@array/pref_plugins_entries"
android:entryValues="@array/pref_plugins_values"
android:persistent="false" />
<org.mozilla.gecko.FontSizePreference
android:key="font.size.inflation.minTwips"
android:title="@string/pref_text_size"
android:positiveButtonText="@string/pref_font_size_set"
android:negativeButtonText="@string/button_cancel"
android:persistent="false" />
<CheckBoxPreference
android:key="browser.zoom.reflowOnZoom"
android:title="@string/pref_reflow_on_zoom"
android:defaultValue="false"
android:persistent="false" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_category_privacy"
android:key="category_privacy">
<PreferenceCategory android:title="@string/pref_category_privacy">
<ListPreference android:key="network.cookie.cookieBehavior"
android:title="@string/pref_cookies_menu"
@ -103,17 +56,4 @@
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_category_importexport">
<org.mozilla.gecko.AndroidImportPreference
android:key="android.not_a_preference.import_android"
gecko:entries="@array/pref_import_android_entries"
gecko:entryKeys="@array/pref_import_android_keys"
gecko:initialValues="@array/pref_import_android_values"
android:title="@string/pref_import_android"
android:positiveButtonText="@string/bookmarkhistory_button_import"
android:negativeButtonText="@string/button_cancel"
android:persistent="false" />
</PreferenceCategory>
</PreferenceScreen>

View File

@ -77,7 +77,9 @@
<string name="pref_category_general">&pref_category_general;</string>
<string name="pref_category_privacy">&pref_category_privacy;</string>
<string name="pref_category_content">&pref_category_content;</string>
<string name="pref_category_importexport">&pref_category_importexport;</string>
<string name="pref_header_general">&pref_header_general;</string>
<string name="pref_header_privacy">&pref_header_privacy;</string>
<string name="pref_header_content">&pref_header_content;</string>
<string name="pref_about_firefox">&pref_about_firefox;</string>
<string name="pref_do_not_track">&pref_do_not_track;</string>
<string name="pref_telemetry">&pref_telemetry;</string>