Bug 715179 - (6 of 8) Set font sizes from the FontSizePreference dialog are saved into GeckoPreferences. r=bnicholson

This commit is contained in:
Michael Comella 2012-07-19 21:08:40 -04:00
parent cdbebc6e5e
commit 300714841c
2 changed files with 74 additions and 2 deletions

View File

@ -9,6 +9,7 @@ import android.app.AlertDialog;
import android.content.Context;
import android.content.res.Resources;
import android.preference.DialogPreference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.text.method.ScrollingMovementMethod;
import android.util.AttributeSet;
import android.util.Log;
@ -18,6 +19,8 @@ import android.view.View;
import android.widget.Button;
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.
@ -30,16 +33,24 @@ class FontSizePreference extends DialogPreference {
private Button mDecreaseFontButton;
private final String[] 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 HashMap<String, Integer> mFontTwipToIndexMap;
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);
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);
}
}
@Override
@ -77,6 +88,38 @@ class FontSizePreference extends DialogPreference {
builder.setView(dialogView);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (!positiveResult) {
mPreviewFontIndex = mSavedFontIndex;
return;
}
mSavedFontIndex = mPreviewFontIndex;
final String twipVal = 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);
}
/**
* Finds the index of the given twip 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);
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.");
}
/**
* Updates the mPreviewFontView to the given text size and forces redraw. Does not update the
* font indicies.
@ -86,6 +129,21 @@ class FontSizePreference extends DialogPreference {
mPreviewFontView.invalidate();
}
/**
* Resets the font indicies to the default value. Does not update the mPreviewFontView text size.
*/
private void resetSavedFontSizeToDefault() {
mSavedFontIndex = DEFAULT_FONT_INDEX;
mPreviewFontIndex = mSavedFontIndex;
}
/**
* 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;
}

View File

@ -160,9 +160,12 @@ public class GeckoPreferences
int newIndex = ((ListPreference)preference).findIndexOfValue((String) newValue);
CharSequence newEntry = ((ListPreference)preference).getEntries()[newIndex];
((ListPreference)preference).setSummary(newEntry);
}
if (preference instanceof LinkPreference)
} else if (preference instanceof LinkPreference) {
finish();
} else if (preference instanceof FontSizePreference) {
final FontSizePreference fontSizePref = (FontSizePreference) preference;
fontSizePref.setSummary(fontSizePref.getSavedFontSizeName());
}
return true;
}
@ -336,7 +339,18 @@ public class GeckoPreferences
((ListPreference)pref).setSummary(selectedEntry);
}
});
} else if (pref instanceof FontSizePreference && "string".equals(prefType)) {
final FontSizePreference fontSizePref = (FontSizePreference) pref;
final String twipValue = jPref.getString("value");
fontSizePref.setSavedFontSize(twipValue);
final String fontSizeName = fontSizePref.getSavedFontSizeName();
GeckoAppShell.getMainHandler().post(new Runnable() {
public void run() {
pref.setSummary(fontSizeName); // Ex: "Small".
}
});
}
}
} catch (JSONException e) {
Log.e(LOGTAG, "Problem parsing preferences response: ", e);