2012-07-19 18:08:40 -07: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-19 18:08:40 -07:00
|
|
|
import android.app.AlertDialog;
|
2012-07-19 18:08:40 -07:00
|
|
|
import android.content.Context;
|
2012-07-19 18:08:40 -07:00
|
|
|
import android.content.res.Resources;
|
2012-07-19 18:08:40 -07:00
|
|
|
import android.preference.DialogPreference;
|
2012-07-19 18:08:40 -07:00
|
|
|
import android.text.method.ScrollingMovementMethod;
|
2012-07-19 18:08:40 -07:00
|
|
|
import android.util.AttributeSet;
|
2012-07-19 18:08:40 -07:00
|
|
|
import android.util.Log;
|
2012-07-19 18:08:40 -07:00
|
|
|
import android.util.TypedValue;
|
2012-07-19 18:08:40 -07:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.TextView;
|
2012-07-19 18:08:40 -07:00
|
|
|
|
|
|
|
class FontSizePreference extends DialogPreference {
|
2012-07-19 18:08:40 -07:00
|
|
|
private static final String LOGTAG = "FontSizePreference";
|
2012-07-19 18:08:40 -07: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-19 18:08:40 -07:00
|
|
|
|
|
|
|
private final Context mContext;
|
|
|
|
private TextView mPreviewFontView;
|
|
|
|
private Button mIncreaseFontButton;
|
|
|
|
private Button mDecreaseFontButton;
|
|
|
|
|
2012-07-19 18:08:40 -07:00
|
|
|
private final String[] mFontTwipValues;
|
|
|
|
/** 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-19 18:08:40 -07:00
|
|
|
public FontSizePreference(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
2012-07-19 18:08:40 -07:00
|
|
|
mContext = context;
|
2012-07-19 18:08:40 -07:00
|
|
|
|
|
|
|
final Resources res = mContext.getResources();
|
|
|
|
mFontTwipValues = res.getStringArray(R.array.pref_font_size_values);
|
2012-07-19 18:08:40 -07: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());
|
|
|
|
|
|
|
|
mDecreaseFontButton = (Button) dialogView.findViewById(R.id.decrease_preview_font_button);
|
|
|
|
mIncreaseFontButton = (Button) dialogView.findViewById(R.id.increase_preview_font_button);
|
2012-07-19 18:08:40 -07: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-19 18:08:40 -07:00
|
|
|
|
|
|
|
builder.setView(dialogView);
|
2012-07-19 18:08:40 -07:00
|
|
|
}
|
2012-07-19 18:08:40 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the mPreviewFontView to the given text size and forces redraw. Does not update the
|
|
|
|
* font indicies.
|
|
|
|
*/
|
|
|
|
private void updatePreviewFontSize(String twip) {
|
|
|
|
mPreviewFontView.setTextSize(PREVIEW_FONT_SIZE_UNIT, convertTwipStrToPT(twip));
|
|
|
|
mPreviewFontView.invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
private float convertTwipStrToPT(String twip) {
|
|
|
|
return Float.parseFloat(twip) / TWIP_TO_PT_RATIO;
|
|
|
|
}
|
2012-07-19 18:08:40 -07:00
|
|
|
}
|