1262529 - update preferences UI to handle new font size settings
MozReview-Commit-ID: 5aMmw7MVdSD
--- a/mobile/android/base/java/org/mozilla/gecko/preferences/FontSizePreference.java
+++ b/mobile/android/base/java/org/mozilla/gecko/preferences/FontSizePreference.java
@@ -6,16 +6,17 @@
package org.mozilla.gecko.preferences;
import org.mozilla.gecko.R;
import android.app.AlertDialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
+import android.os.Bundle;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
@@ -25,41 +26,46 @@ import android.widget.TextView;
import java.util.HashMap;
class FontSizePreference extends DialogPreference {
private static final String LOGTAG = "FontSizePreference";
private static final int TWIP_TO_PT_RATIO = 20; // 20 twip = 1 point.
private static final int PREVIEW_FONT_SIZE_UNIT = TypedValue.COMPLEX_UNIT_PT;
private static final int DEFAULT_FONT_INDEX = 2;
+ public static final String PREFSBUNDLE_SIZE = "size";
+ public static final String PREFSBUNDLE_TWIPS = "twips";
+
private final Context mContext;
/** Container for mPreviewFontView to allow for scrollable padding at the top of the view. */
private ScrollView mScrollingContainer;
private TextView mPreviewFontView;
private Button mIncreaseFontButton;
private Button mDecreaseFontButton;
- private final String[] mFontTwipValues;
+ private final String[] mFontSizeValues;
+ private final int[] mFontTwipValues;
private final String[] mFontSizeNames; // Ex: "Small".
/** Index into the above arrays for the saved preference value (from Gecko). */
private int mSavedFontIndex = DEFAULT_FONT_INDEX;
/** Index into the above arrays for the currently displayed font size (the preview). */
private int mPreviewFontIndex = mSavedFontIndex;
- private final HashMap<String, Integer> mFontTwipToIndexMap;
+ private final HashMap<String, Integer> mFontSizeToIndexMap;
public FontSizePreference(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
final Resources res = mContext.getResources();
- mFontTwipValues = res.getStringArray(R.array.pref_font_size_values);
+ mFontSizeValues = res.getStringArray(R.array.pref_font_size_values);
+ mFontTwipValues = res.getIntArray(R.array.pref_font_twip_values);
mFontSizeNames = res.getStringArray(R.array.pref_font_size_entries);
- mFontTwipToIndexMap = new HashMap<String, Integer>();
- for (int i = 0; i < mFontTwipValues.length; ++i) {
- mFontTwipToIndexMap.put(mFontTwipValues[i], i);
+ mFontSizeToIndexMap = new HashMap<>();
+ for (int i = 0; i < mFontSizeValues.length; ++i) {
+ mFontSizeToIndexMap.put(mFontSizeValues[i], i);
}
}
@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
final LayoutInflater inflater =
(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = inflater.inflate(R.layout.font_size_preference, null);
@@ -90,66 +96,68 @@ class FontSizePreference extends DialogP
if (mPreviewFontIndex == 0) {
mDecreaseFontButton.setEnabled(false);
}
}
});
mIncreaseFontButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
- mPreviewFontIndex = Math.min(mPreviewFontIndex + 1, mFontTwipValues.length - 1);
+ mPreviewFontIndex = Math.min(mPreviewFontIndex + 1, mFontSizeValues.length - 1);
updatePreviewFontSize(mFontTwipValues[mPreviewFontIndex]);
mDecreaseFontButton.setEnabled(true);
// If we reached the maximum index, disable the button.
- if (mPreviewFontIndex == mFontTwipValues.length - 1) {
+ if (mPreviewFontIndex == mFontSizeValues.length - 1) {
mIncreaseFontButton.setEnabled(false);
}
}
});
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (!positiveResult) {
mPreviewFontIndex = mSavedFontIndex;
return;
}
mSavedFontIndex = mPreviewFontIndex;
- final String twipVal = mFontTwipValues[mSavedFontIndex];
+ final Bundle prefsVal = new Bundle(2);
+ prefsVal.putString(PREFSBUNDLE_SIZE, mFontSizeValues[mSavedFontIndex]);
+ prefsVal.putInt(PREFSBUNDLE_TWIPS, mFontTwipValues[mSavedFontIndex]);
final OnPreferenceChangeListener prefChangeListener = getOnPreferenceChangeListener();
if (prefChangeListener == null) {
Log.e(LOGTAG, "PreferenceChangeListener is null. FontSizePreference will not be saved to Gecko.");
return;
}
- prefChangeListener.onPreferenceChange(this, twipVal);
+ prefChangeListener.onPreferenceChange(this, prefsVal);
}
/**
- * Finds the index of the given twip value and sets it as the saved preference value. Also the
+ * Finds the index of the given size value and sets it as the saved preference value. Also the
* current preview text size to the given value. Does not update the mPreviewFontView text size.
*/
- protected void setSavedFontSize(String twip) {
- final Integer index = mFontTwipToIndexMap.get(twip);
+ protected void setSavedFontSize(String size) {
+ final Integer index = mFontSizeToIndexMap.get(size);
if (index != null) {
mSavedFontIndex = index;
mPreviewFontIndex = mSavedFontIndex;
return;
}
resetSavedFontSizeToDefault();
- Log.e(LOGTAG, "setSavedFontSize: Given font size does not exist in twip values map. Reverted to default font size.");
+ Log.e(LOGTAG, "setSavedFontSize: Given font size does not exist in size values map. Reverted to default font size.");
}
/**
* Updates the mPreviewFontView to the given text size, resets the container's scroll to the top
* left, and invalidates the view. Does not update the font indices.
*/
- private void updatePreviewFontSize(String twip) {
- float pt = convertTwipStrToPT(twip);
+ private void updatePreviewFontSize(int twip) {
+ float pt = (float) twip / TWIP_TO_PT_RATIO;
// Android will not render a font size of 0 pt but for Gecko, 0 twip turns off font
// inflation. Thus we special case 0 twip to display a renderable font size.
if (pt == 0) {
// Android adds an inexplicable extra margin on the smallest font size so to get around
// this, we reinflate the view.
ViewGroup parentView = (ViewGroup) mScrollingContainer.getParent();
parentView.removeAllViews();
final LayoutInflater inflater =
@@ -169,24 +177,20 @@ class FontSizePreference extends DialogP
private void resetSavedFontSizeToDefault() {
mSavedFontIndex = DEFAULT_FONT_INDEX;
mPreviewFontIndex = mSavedFontIndex;
}
private void setButtonState(int index) {
if (index == 0) {
mDecreaseFontButton.setEnabled(false);
- } else if (index == mFontTwipValues.length - 1) {
+ } else if (index == mFontSizeValues.length - 1) {
mIncreaseFontButton.setEnabled(false);
}
}
/**
* Returns the name of the font size (ex: "Small") at the currently saved preference value.
*/
protected String getSavedFontSizeName() {
return mFontSizeNames[mSavedFontIndex];
}
-
- private float convertTwipStrToPT(String twip) {
- return Float.parseFloat(twip) / TWIP_TO_PT_RATIO;
- }
}
--- a/mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferences.java
+++ b/mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferences.java
@@ -165,16 +165,17 @@ public class GeckoPreferences
public static final String PREFS_NOTIFICATIONS_WHATS_NEW = NON_PREF_PREFIX + "notifications.whats_new";
public static final String PREFS_APP_UPDATE_LAST_BUILD_ID = "app.update.last_build_id";
public static final String PREFS_READ_PARTNER_CUSTOMIZATIONS_PROVIDER = NON_PREF_PREFIX + "distribution.read_partner_customizations_provider";
public static final String PREFS_READ_PARTNER_BOOKMARKS_PROVIDER = NON_PREF_PREFIX + "distribution.read_partner_bookmarks_provider";
public static final String PREFS_CUSTOM_TABS = NON_PREF_PREFIX + "customtabs";
public static final String PREFS_ACTIVITY_STREAM = NON_PREF_PREFIX + "activitystream";
public static final String PREFS_CATEGORY_EXPERIMENTAL_FEATURES = NON_PREF_PREFIX + "category_experimental";
public static final String PREFS_COMPACT_TABS = NON_PREF_PREFIX + "compact_tabs";
+ public static final String PREFS_FONT_SIZE = "browser.zoom.globalZoom";
private static final String ACTION_STUMBLER_UPLOAD_PREF = "STUMBLER_PREF";
// This isn't a Gecko pref, even if it looks like one.
private static final String PREFS_BROWSER_LOCALE = "locale";
public static final String PREFS_RESTORE_SESSION = NON_PREF_PREFIX + "restoreSession3";
@@ -1217,17 +1218,23 @@ public class GeckoPreferences
}
}, 1000);
} else if (HANDLERS.containsKey(prefName)) {
PrefHandler handler = HANDLERS.get(prefName);
handler.onChange(this, preference, newValue);
}
// Send Gecko-side pref changes to Gecko
- if (isGeckoPref(prefName)) {
+ if (prefName.equals(PREFS_FONT_SIZE)) {
+ String size = ((Bundle) newValue).getString(FontSizePreference.PREFSBUNDLE_SIZE);
+ PrefsHelper.setPref(prefName, size, false /* flush */);
+
+ int twips = ((Bundle) newValue).getInt(FontSizePreference.PREFSBUNDLE_TWIPS);
+ PrefsHelper.setPref("font.size.inflation.minTwips", twips, true /* flush */);
+ } else if (isGeckoPref(prefName)) {
PrefsHelper.setPref(prefName, newValue, true /* flush */);
}
if (preference instanceof ListPreference) {
// We need to find the entry for the new value
int newIndex = ((ListPreference) preference).findIndexOfValue((String) newValue);
CharSequence newEntry = ((ListPreference) preference).getEntries()[newIndex];
((ListPreference) preference).setSummary(newEntry);
--- a/mobile/android/base/resources/values/arrays.xml
+++ b/mobile/android/base/resources/values/arrays.xml
@@ -26,22 +26,29 @@
<string-array name="pref_font_size_entries">
<item>@string/pref_font_size_tiny</item>
<item>@string/pref_font_size_small</item>
<item>@string/pref_font_size_medium</item>
<item>@string/pref_font_size_large</item>
<item>@string/pref_font_size_xlarge</item>
</string-array>
<string-array name="pref_font_size_values">
+ <item>50</item>
+ <item>75</item>
+ <item>100</item>
+ <item>125</item>
+ <item>150</item>
+ </string-array>
+ <integer-array name="pref_font_twip_values">
<item>0</item>
<item>80</item>
<item>120</item>
<item>160</item>
<item>240</item>
- </string-array>
+ </integer-array>
<string-array name="pref_char_encoding_entries">
<item>@string/pref_char_encoding_on</item>
<item>@string/pref_char_encoding_off</item>
</string-array>
<string-array name="pref_char_encoding_values">
<item>true</item>
<item>false</item>
</string-array>
--- a/mobile/android/base/resources/xml/preferences_accessibility.xml
+++ b/mobile/android/base/resources/xml/preferences_accessibility.xml
@@ -3,17 +3,17 @@
- 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:title="@string/pref_category_accessibility"
android:enabled="false">
<org.mozilla.gecko.preferences.FontSizePreference
- android:key="font.size.inflation.minTwips"
+ android:key="browser.zoom.globalZoom"
android:title="@string/pref_text_size"
android:positiveButtonText="@string/pref_font_size_set"
android:negativeButtonText="@string/button_cancel"
android:persistent="false" />
<SwitchPreference android:key="browser.ui.zoom.force-user-scalable"
android:title="@string/pref_zoom_force_enabled"
android:summary="@string/pref_zoom_force_enabled_summary" />
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/PrefsHelper.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/PrefsHelper.java
@@ -26,17 +26,17 @@ public final class PrefsHelper {
// Map pref name to ArrayList for multiple observers or PrefHandler for single observer.
private static final SimpleArrayMap<String, Object> OBSERVERS = new SimpleArrayMap<>();
private static final HashSet<String> INT_TO_STRING_PREFS = new HashSet<>(8);
private static final HashSet<String> INT_TO_BOOL_PREFS = new HashSet<>(2);
static {
INT_TO_STRING_PREFS.add("browser.chrome.titlebarMode");
INT_TO_STRING_PREFS.add("network.cookie.cookieBehavior");
- INT_TO_STRING_PREFS.add("font.size.inflation.minTwips");
+ INT_TO_STRING_PREFS.add("browser.zoom.globalZoom");
INT_TO_STRING_PREFS.add("home.sync.updateMode");
INT_TO_STRING_PREFS.add("browser.image_blocking");
INT_TO_BOOL_PREFS.add("browser.display.use_document_fonts");
}
@WrapForJNI
private static final int PREF_INVALID = -1;
@WrapForJNI