mobile/android/base/preferences/FontSizePreference.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko.preferences;
michael@0 7
michael@0 8 import org.mozilla.gecko.R;
michael@0 9
michael@0 10 import android.app.AlertDialog;
michael@0 11 import android.content.Context;
michael@0 12 import android.content.res.Resources;
michael@0 13 import android.graphics.Color;
michael@0 14 import android.preference.DialogPreference;
michael@0 15 import android.util.AttributeSet;
michael@0 16 import android.util.Log;
michael@0 17 import android.util.TypedValue;
michael@0 18 import android.view.LayoutInflater;
michael@0 19 import android.view.View;
michael@0 20 import android.view.ViewGroup;
michael@0 21 import android.widget.Button;
michael@0 22 import android.widget.ScrollView;
michael@0 23 import android.widget.TextView;
michael@0 24
michael@0 25 import java.util.HashMap;
michael@0 26
michael@0 27 class FontSizePreference extends DialogPreference {
michael@0 28 private static final String LOGTAG = "FontSizePreference";
michael@0 29 private static final int TWIP_TO_PT_RATIO = 20; // 20 twip = 1 point.
michael@0 30 private static final int PREVIEW_FONT_SIZE_UNIT = TypedValue.COMPLEX_UNIT_PT;
michael@0 31 private static final int DEFAULT_FONT_INDEX = 2;
michael@0 32
michael@0 33 private final Context mContext;
michael@0 34 /** Container for mPreviewFontView to allow for scrollable padding at the top of the view. */
michael@0 35 private ScrollView mScrollingContainer;
michael@0 36 private TextView mPreviewFontView;
michael@0 37 private Button mIncreaseFontButton;
michael@0 38 private Button mDecreaseFontButton;
michael@0 39
michael@0 40 private final String[] mFontTwipValues;
michael@0 41 private final String[] mFontSizeNames; // Ex: "Small".
michael@0 42 /** Index into the above arrays for the saved preference value (from Gecko). */
michael@0 43 private int mSavedFontIndex = DEFAULT_FONT_INDEX;
michael@0 44 /** Index into the above arrays for the currently displayed font size (the preview). */
michael@0 45 private int mPreviewFontIndex = mSavedFontIndex;
michael@0 46 private final HashMap<String, Integer> mFontTwipToIndexMap;
michael@0 47
michael@0 48 public FontSizePreference(Context context, AttributeSet attrs) {
michael@0 49 super(context, attrs);
michael@0 50 mContext = context;
michael@0 51
michael@0 52 final Resources res = mContext.getResources();
michael@0 53 mFontTwipValues = res.getStringArray(R.array.pref_font_size_values);
michael@0 54 mFontSizeNames = res.getStringArray(R.array.pref_font_size_entries);
michael@0 55 mFontTwipToIndexMap = new HashMap<String, Integer>();
michael@0 56 for (int i = 0; i < mFontTwipValues.length; ++i) {
michael@0 57 mFontTwipToIndexMap.put(mFontTwipValues[i], i);
michael@0 58 }
michael@0 59 }
michael@0 60
michael@0 61 @Override
michael@0 62 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
michael@0 63 final LayoutInflater inflater =
michael@0 64 (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
michael@0 65 View dialogView = inflater.inflate(R.layout.font_size_preference, null);
michael@0 66 initInternalViews(dialogView);
michael@0 67 updatePreviewFontSize(mFontTwipValues[mPreviewFontIndex]);
michael@0 68
michael@0 69 builder.setTitle(null);
michael@0 70 builder.setView(dialogView);
michael@0 71 }
michael@0 72
michael@0 73 /** Saves relevant views to instance variables and initializes their settings. */
michael@0 74 private void initInternalViews(View dialogView) {
michael@0 75 mScrollingContainer = (ScrollView) dialogView.findViewById(R.id.scrolling_container);
michael@0 76 // Background cannot be set in XML (see bug 783597 - TODO: Change this to XML when bug is fixed).
michael@0 77 mScrollingContainer.setBackgroundColor(Color.WHITE);
michael@0 78 mPreviewFontView = (TextView) dialogView.findViewById(R.id.preview);
michael@0 79
michael@0 80 mDecreaseFontButton = (Button) dialogView.findViewById(R.id.decrease_preview_font_button);
michael@0 81 mIncreaseFontButton = (Button) dialogView.findViewById(R.id.increase_preview_font_button);
michael@0 82 setButtonState(mPreviewFontIndex);
michael@0 83 mDecreaseFontButton.setOnClickListener(new View.OnClickListener() {
michael@0 84 @Override
michael@0 85 public void onClick(View v) {
michael@0 86 mPreviewFontIndex = Math.max(mPreviewFontIndex - 1, 0);
michael@0 87 updatePreviewFontSize(mFontTwipValues[mPreviewFontIndex]);
michael@0 88 mIncreaseFontButton.setEnabled(true);
michael@0 89 // If we reached the minimum index, disable the button.
michael@0 90 if (mPreviewFontIndex == 0) {
michael@0 91 mDecreaseFontButton.setEnabled(false);
michael@0 92 }
michael@0 93 }
michael@0 94 });
michael@0 95 mIncreaseFontButton.setOnClickListener(new View.OnClickListener() {
michael@0 96 @Override
michael@0 97 public void onClick(View v) {
michael@0 98 mPreviewFontIndex = Math.min(mPreviewFontIndex + 1, mFontTwipValues.length - 1);
michael@0 99 updatePreviewFontSize(mFontTwipValues[mPreviewFontIndex]);
michael@0 100
michael@0 101 mDecreaseFontButton.setEnabled(true);
michael@0 102 // If we reached the maximum index, disable the button.
michael@0 103 if (mPreviewFontIndex == mFontTwipValues.length - 1) {
michael@0 104 mIncreaseFontButton.setEnabled(false);
michael@0 105 }
michael@0 106 }
michael@0 107 });
michael@0 108 }
michael@0 109
michael@0 110 @Override
michael@0 111 protected void onDialogClosed(boolean positiveResult) {
michael@0 112 super.onDialogClosed(positiveResult);
michael@0 113 if (!positiveResult) {
michael@0 114 mPreviewFontIndex = mSavedFontIndex;
michael@0 115 return;
michael@0 116 }
michael@0 117 mSavedFontIndex = mPreviewFontIndex;
michael@0 118 final String twipVal = mFontTwipValues[mSavedFontIndex];
michael@0 119 final OnPreferenceChangeListener prefChangeListener = getOnPreferenceChangeListener();
michael@0 120 if (prefChangeListener == null) {
michael@0 121 Log.e(LOGTAG, "PreferenceChangeListener is null. FontSizePreference will not be saved to Gecko.");
michael@0 122 return;
michael@0 123 }
michael@0 124 prefChangeListener.onPreferenceChange(this, twipVal);
michael@0 125 }
michael@0 126
michael@0 127 /**
michael@0 128 * Finds the index of the given twip value and sets it as the saved preference value. Also the
michael@0 129 * current preview text size to the given value. Does not update the mPreviewFontView text size.
michael@0 130 */
michael@0 131 protected void setSavedFontSize(String twip) {
michael@0 132 final Integer index = mFontTwipToIndexMap.get(twip);
michael@0 133 if (index != null) {
michael@0 134 mSavedFontIndex = index;
michael@0 135 mPreviewFontIndex = mSavedFontIndex;
michael@0 136 return;
michael@0 137 }
michael@0 138 resetSavedFontSizeToDefault();
michael@0 139 Log.e(LOGTAG, "setSavedFontSize: Given font size does not exist in twip values map. Reverted to default font size.");
michael@0 140 }
michael@0 141
michael@0 142 /**
michael@0 143 * Updates the mPreviewFontView to the given text size, resets the container's scroll to the top
michael@0 144 * left, and invalidates the view. Does not update the font indices.
michael@0 145 */
michael@0 146 private void updatePreviewFontSize(String twip) {
michael@0 147 float pt = convertTwipStrToPT(twip);
michael@0 148 // Android will not render a font size of 0 pt but for Gecko, 0 twip turns off font
michael@0 149 // inflation. Thus we special case 0 twip to display a renderable font size.
michael@0 150 if (pt == 0) {
michael@0 151 // Android adds an inexplicable extra margin on the smallest font size so to get around
michael@0 152 // this, we reinflate the view.
michael@0 153 ViewGroup parentView = (ViewGroup) mScrollingContainer.getParent();
michael@0 154 parentView.removeAllViews();
michael@0 155 final LayoutInflater inflater =
michael@0 156 (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
michael@0 157 View dialogView = inflater.inflate(R.layout.font_size_preference, parentView);
michael@0 158 initInternalViews(dialogView);
michael@0 159 mPreviewFontView.setTextSize(PREVIEW_FONT_SIZE_UNIT, 1);
michael@0 160 } else {
michael@0 161 mPreviewFontView.setTextSize(PREVIEW_FONT_SIZE_UNIT, pt);
michael@0 162 }
michael@0 163 mScrollingContainer.scrollTo(0, 0);
michael@0 164 }
michael@0 165
michael@0 166 /**
michael@0 167 * Resets the font indices to the default value. Does not update the mPreviewFontView text size.
michael@0 168 */
michael@0 169 private void resetSavedFontSizeToDefault() {
michael@0 170 mSavedFontIndex = DEFAULT_FONT_INDEX;
michael@0 171 mPreviewFontIndex = mSavedFontIndex;
michael@0 172 }
michael@0 173
michael@0 174 private void setButtonState(int index) {
michael@0 175 if (index == 0) {
michael@0 176 mDecreaseFontButton.setEnabled(false);
michael@0 177 } else if (index == mFontTwipValues.length - 1) {
michael@0 178 mIncreaseFontButton.setEnabled(false);
michael@0 179 }
michael@0 180 }
michael@0 181
michael@0 182 /**
michael@0 183 * Returns the name of the font size (ex: "Small") at the currently saved preference value.
michael@0 184 */
michael@0 185 protected String getSavedFontSizeName() {
michael@0 186 return mFontSizeNames[mSavedFontIndex];
michael@0 187 }
michael@0 188
michael@0 189 private float convertTwipStrToPT(String twip) {
michael@0 190 return Float.parseFloat(twip) / TWIP_TO_PT_RATIO;
michael@0 191 }
michael@0 192 }

mercurial