2012-07-20 01:08:40 +00:00
|
|
|
/* -*- 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;
|
|
|
|
|
2012-07-20 01:08:40 +00:00
|
|
|
import android.app.AlertDialog;
|
2012-07-20 01:08:40 +00:00
|
|
|
import android.content.Context;
|
2012-07-20 01:08:40 +00:00
|
|
|
import android.content.res.Configuration;
|
2012-07-20 01:08:40 +00:00
|
|
|
import android.content.res.Resources;
|
2012-07-20 01:08:40 +00:00
|
|
|
import android.preference.DialogPreference;
|
2012-07-20 01:08:40 +00:00
|
|
|
import android.preference.Preference.OnPreferenceChangeListener;
|
2012-07-20 01:08:40 +00:00
|
|
|
import android.text.method.ScrollingMovementMethod;
|
2012-07-20 01:08:40 +00:00
|
|
|
import android.util.AttributeSet;
|
2012-07-20 01:08:40 +00:00
|
|
|
import android.util.DisplayMetrics;
|
2012-07-20 01:08:40 +00:00
|
|
|
import android.util.Log;
|
2012-07-20 01:08:40 +00:00
|
|
|
import android.util.TypedValue;
|
2012-07-20 01:08:40 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
2012-07-20 01:08:40 +00:00
|
|
|
import android.view.ViewTreeObserver;
|
|
|
|
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
|
2012-07-20 01:08:40 +00:00
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.TextView;
|
2012-07-20 01:08:40 +00:00
|
|
|
|
2012-07-20 01:08:40 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
2012-07-20 01:08:40 +00:00
|
|
|
class FontSizePreference extends DialogPreference {
|
2012-07-20 01:08:40 +00:00
|
|
|
private static final String LOGTAG = "FontSizePreference";
|
2012-07-20 01:08:40 +00:00
|
|
|
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;
|
2012-07-20 01:08:40 +00:00
|
|
|
|
2012-07-20 01:08:40 +00:00
|
|
|
// Final line height = line height * line_mult + line_mult;
|
|
|
|
private static final float LINE_SPACING_ADD = 0f; // Units unknown.
|
|
|
|
private static final float LINE_SPACING_MULT = 1.0f;
|
|
|
|
|
|
|
|
private static final float MIN_TEXTVIEW_WIDTH_DIP = 360; // Width of the Galaxy Nexus (portrait).
|
|
|
|
/** The dialog will encompass the minimum width + (1 / scaleFactor) of the remaining space. */
|
|
|
|
private static final float TEXTVIEW_WIDTH_SCALE_FACTOR = 3;
|
|
|
|
|
2012-07-20 01:08:40 +00:00
|
|
|
private final Context mContext;
|
2012-07-20 01:08:40 +00:00
|
|
|
private int mCurrentOrientation;
|
2012-07-20 01:08:40 +00:00
|
|
|
private TextView mPreviewFontView;
|
|
|
|
private Button mIncreaseFontButton;
|
|
|
|
private Button mDecreaseFontButton;
|
|
|
|
|
2012-07-20 01:08:40 +00:00
|
|
|
private final String[] mFontTwipValues;
|
2012-07-20 01:08:40 +00:00
|
|
|
private final String[] mFontSizeNames; // Ex: "Small".
|
2012-07-20 01:08:40 +00:00
|
|
|
/** 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;
|
2012-07-20 01:08:40 +00:00
|
|
|
private final HashMap<String, Integer> mFontTwipToIndexMap;
|
|
|
|
|
|
|
|
private boolean mPreviewFontViewHeightSet;
|
2012-07-20 01:08:40 +00:00
|
|
|
|
2012-07-20 01:08:40 +00:00
|
|
|
public FontSizePreference(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
2012-07-20 01:08:40 +00:00
|
|
|
mContext = context;
|
2012-07-20 01:08:40 +00:00
|
|
|
|
|
|
|
final Resources res = mContext.getResources();
|
|
|
|
mFontTwipValues = res.getStringArray(R.array.pref_font_size_values);
|
2012-07-20 01:08:40 +00:00
|
|
|
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);
|
|
|
|
}
|
2012-07-20 01:08:40 +00:00
|
|
|
mCurrentOrientation = res.getConfiguration().orientation;
|
2012-07-20 01:08:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
mPreviewFontView = (TextView) dialogView.findViewById(R.id.preview);
|
|
|
|
mPreviewFontView.setMovementMethod(new ScrollingMovementMethod());
|
2012-07-20 01:08:40 +00:00
|
|
|
// There is no way to get the value for this padding so we turn it off.
|
|
|
|
mPreviewFontView.setIncludeFontPadding(false);
|
|
|
|
// Retrieving line spacing is not available until API 16 so we override the values instead.
|
|
|
|
mPreviewFontView.setLineSpacing(LINE_SPACING_ADD, LINE_SPACING_MULT);
|
2012-07-20 01:08:40 +00:00
|
|
|
|
|
|
|
mDecreaseFontButton = (Button) dialogView.findViewById(R.id.decrease_preview_font_button);
|
|
|
|
mIncreaseFontButton = (Button) dialogView.findViewById(R.id.increase_preview_font_button);
|
2012-07-20 01:08:40 +00:00
|
|
|
mDecreaseFontButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
updatePreviewFontSize(mFontTwipValues[--mPreviewFontIndex]);
|
|
|
|
mIncreaseFontButton.setEnabled(true);
|
|
|
|
// If we reached the minimum index, disable the button.
|
|
|
|
if (mPreviewFontIndex == 0) {
|
|
|
|
mDecreaseFontButton.setEnabled(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
mIncreaseFontButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
updatePreviewFontSize(mFontTwipValues[++mPreviewFontIndex]);
|
|
|
|
|
|
|
|
mDecreaseFontButton.setEnabled(true);
|
|
|
|
// If we reached the maximum index, disable the button.
|
|
|
|
if (mPreviewFontIndex == mFontTwipValues.length - 1) {
|
|
|
|
mIncreaseFontButton.setEnabled(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2012-07-20 01:08:40 +00:00
|
|
|
|
2012-07-20 01:08:40 +00:00
|
|
|
// Set mPreviewFontView dimensions.
|
|
|
|
setPreviewFontViewWidth();
|
|
|
|
setPreviewFontViewHeightListener();
|
|
|
|
setFontSizeToMaximum(); // Expects onGlobalLayout() to be called.
|
|
|
|
|
2012-07-20 01:08:40 +00:00
|
|
|
builder.setView(dialogView);
|
2012-07-20 01:08:40 +00:00
|
|
|
}
|
2012-07-20 01:08:40 +00:00
|
|
|
|
2012-07-20 01:08:40 +00:00
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
2012-07-20 01:08:40 +00:00
|
|
|
protected void onConfigurationChanged(Configuration newConfig) {
|
|
|
|
if (mCurrentOrientation != newConfig.orientation) {
|
|
|
|
mCurrentOrientation = newConfig.orientation;
|
|
|
|
|
2012-07-24 00:02:21 +00:00
|
|
|
// mPreviewFontView will be null if the dialog has not yet been shown.
|
|
|
|
if (mPreviewFontView != null) {
|
|
|
|
// Recalculate the mPreviewFontView dimensions since we have new screen dimensions.
|
|
|
|
setPreviewFontViewWidth();
|
|
|
|
mPreviewFontViewHeightSet = false;
|
|
|
|
setFontSizeToMaximum(); // Expects onGlobalLayout() to be called.
|
|
|
|
}
|
2012-07-20 01:08:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the preview font size to the maximum given size.
|
|
|
|
*/
|
|
|
|
private void setFontSizeToMaximum() {
|
|
|
|
updatePreviewFontSize(mFontTwipValues[mFontTwipValues.length - 1]);
|
|
|
|
// NOTE: If this method is being used with onGlobalLayout() to set mFontPreviewView height,
|
|
|
|
// the font size cannot be changed past this point or the dialog height will not calculate
|
|
|
|
// correctly. We must wait for the global layout state to change, calculate the height in
|
|
|
|
// onGlobalLayout(), and only then can changes be made.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a global layout listener that will calculate the maximum required height of
|
|
|
|
* mPreviewFontView based upon the current font values and then reset the TextView to the saved
|
|
|
|
* preference values. This listener will only run once. mPreviewFontViewHeightSet must be set to
|
|
|
|
* false before being used again.
|
|
|
|
*/
|
|
|
|
private void setPreviewFontViewHeightListener() {
|
|
|
|
mPreviewFontViewHeightSet = false;
|
|
|
|
final ViewTreeObserver vto = mPreviewFontView.getViewTreeObserver();
|
|
|
|
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
|
|
|
|
@Override
|
|
|
|
public void onGlobalLayout() {
|
|
|
|
if (!mPreviewFontViewHeightSet) {
|
|
|
|
mPreviewFontViewHeightSet = true;
|
|
|
|
final int desiredHeight = (int) (mPreviewFontView.getLineCount() *
|
|
|
|
mPreviewFontView.getLineHeight() * LINE_SPACING_MULT + LINE_SPACING_ADD +
|
|
|
|
mPreviewFontView.getPaddingTop() + mPreviewFontView.getPaddingBottom());
|
|
|
|
mPreviewFontView.setHeight(desiredHeight);
|
|
|
|
|
|
|
|
// Set the dialog state to the saved preference values.
|
|
|
|
setButtonState(mPreviewFontIndex);
|
|
|
|
updatePreviewFontSize(mFontTwipValues[mPreviewFontIndex]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the width of the mPreviewFontView TextView. The width is calculated by adding a constant
|
|
|
|
* minimum width to a fraction of the remaining space on screen (if any), which is determined by
|
|
|
|
* the given scale factor. Note that in ICS, it appears that the dialog will not wrap content
|
|
|
|
* and will set the view size itself. In Gingerbread, this code executes and sets the dialog
|
|
|
|
* width dynamically.
|
|
|
|
*/
|
|
|
|
private void setPreviewFontViewWidth() {
|
2012-07-28 06:13:34 +00:00
|
|
|
final DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
|
2012-07-20 01:08:40 +00:00
|
|
|
final float density = metrics.density;
|
|
|
|
|
|
|
|
final float actualWidthDip = metrics.widthPixels / density;
|
|
|
|
float scaleExtraDip = (actualWidthDip - MIN_TEXTVIEW_WIDTH_DIP) / TEXTVIEW_WIDTH_SCALE_FACTOR;
|
|
|
|
scaleExtraDip = scaleExtraDip >= 0 ? scaleExtraDip : 0;
|
|
|
|
final float desiredWidthDip = MIN_TEXTVIEW_WIDTH_DIP + scaleExtraDip;
|
|
|
|
final int desiredWidthPx = Math.round(desiredWidthDip * density);
|
|
|
|
mPreviewFontView.setWidth(desiredWidthPx);
|
|
|
|
}
|
|
|
|
|
2012-07-20 01:08:40 +00:00
|
|
|
/**
|
|
|
|
* 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.");
|
|
|
|
}
|
|
|
|
|
2012-07-20 01:08:40 +00:00
|
|
|
/**
|
2012-07-20 01:08:40 +00:00
|
|
|
* Updates the mPreviewFontView to the given text size, resets the scroll to the top left, and
|
|
|
|
* invalidates the view. Does not update the font indices.
|
2012-07-20 01:08:40 +00:00
|
|
|
*/
|
|
|
|
private void updatePreviewFontSize(String twip) {
|
2012-07-20 01:08:40 +00:00
|
|
|
float pt = convertTwipStrToPT(twip);
|
|
|
|
// 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) {
|
|
|
|
mPreviewFontView.setTextSize(PREVIEW_FONT_SIZE_UNIT, 1);
|
|
|
|
} else {
|
|
|
|
mPreviewFontView.setTextSize(PREVIEW_FONT_SIZE_UNIT, pt);
|
|
|
|
}
|
2012-07-20 01:08:40 +00:00
|
|
|
mPreviewFontView.scrollTo(0, 0);
|
2012-07-20 01:08:40 +00:00
|
|
|
}
|
|
|
|
|
2012-07-20 01:08:40 +00:00
|
|
|
/**
|
2012-07-20 01:08:40 +00:00
|
|
|
* Resets the font indices to the default value. Does not update the mPreviewFontView text size.
|
2012-07-20 01:08:40 +00:00
|
|
|
*/
|
|
|
|
private void resetSavedFontSizeToDefault() {
|
|
|
|
mSavedFontIndex = DEFAULT_FONT_INDEX;
|
|
|
|
mPreviewFontIndex = mSavedFontIndex;
|
|
|
|
}
|
|
|
|
|
2012-07-20 01:08:40 +00:00
|
|
|
private void setButtonState(int index) {
|
|
|
|
if (index == 0) {
|
|
|
|
mDecreaseFontButton.setEnabled(false);
|
|
|
|
} else if (index == mFontTwipValues.length - 1) {
|
|
|
|
mIncreaseFontButton.setEnabled(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-20 01:08:40 +00:00
|
|
|
/**
|
|
|
|
* Returns the name of the font size (ex: "Small") at the currently saved preference value.
|
|
|
|
*/
|
|
|
|
protected String getSavedFontSizeName() {
|
|
|
|
return mFontSizeNames[mSavedFontIndex];
|
|
|
|
}
|
|
|
|
|
2012-07-20 01:08:40 +00:00
|
|
|
private float convertTwipStrToPT(String twip) {
|
|
|
|
return Float.parseFloat(twip) / TWIP_TO_PT_RATIO;
|
|
|
|
}
|
2012-07-20 01:08:40 +00:00
|
|
|
}
|