OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.dom_distiller; | 5 package org.chromium.chrome.browser.dom_distiller; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.graphics.Typeface; | 8 import android.graphics.Typeface; |
9 import android.support.v7.app.AlertDialog; | 9 import android.support.v7.app.AlertDialog; |
10 import android.util.AttributeSet; | 10 import android.util.AttributeSet; |
11 import android.view.LayoutInflater; | 11 import android.view.LayoutInflater; |
12 import android.view.View; | 12 import android.view.View; |
13 import android.view.ViewGroup; | 13 import android.view.ViewGroup; |
14 import android.widget.AdapterView; | 14 import android.widget.AdapterView; |
15 import android.widget.AdapterView.OnItemSelectedListener; | 15 import android.widget.AdapterView.OnItemSelectedListener; |
16 import android.widget.ArrayAdapter; | 16 import android.widget.ArrayAdapter; |
17 import android.widget.LinearLayout; | 17 import android.widget.LinearLayout; |
18 import android.widget.RadioButton; | 18 import android.widget.RadioButton; |
19 import android.widget.RadioGroup; | 19 import android.widget.RadioGroup; |
20 import android.widget.SeekBar; | 20 import android.widget.SeekBar; |
21 import android.widget.Spinner; | 21 import android.widget.Spinner; |
22 import android.widget.TextView; | 22 import android.widget.TextView; |
23 | 23 |
24 import org.chromium.chrome.R; | 24 import org.chromium.chrome.R; |
25 import org.chromium.chrome.browser.accessibility.FontSizePrefs; | |
26 import org.chromium.chrome.browser.profiles.Profile; | 25 import org.chromium.chrome.browser.profiles.Profile; |
27 import org.chromium.components.dom_distiller.core.DistilledPagePrefs; | 26 import org.chromium.components.dom_distiller.core.DistilledPagePrefs; |
28 import org.chromium.components.dom_distiller.core.FontFamily; | 27 import org.chromium.components.dom_distiller.core.FontFamily; |
29 import org.chromium.components.dom_distiller.core.Theme; | 28 import org.chromium.components.dom_distiller.core.Theme; |
30 | 29 |
31 import java.text.NumberFormat; | 30 import java.text.NumberFormat; |
32 import java.util.EnumMap; | 31 import java.util.EnumMap; |
33 import java.util.Locale; | 32 import java.util.Locale; |
34 import java.util.Map; | 33 import java.util.Map; |
35 | 34 |
36 /** | 35 /** |
37 * A view which displays preferences for distilled pages. This allows users | 36 * A view which displays preferences for distilled pages. This allows users |
38 * to change the theme, font size, etc. of distilled pages. | 37 * to change the theme, font size, etc. of distilled pages. |
39 */ | 38 */ |
40 public class DistilledPagePrefsView extends LinearLayout | 39 public class DistilledPagePrefsView extends LinearLayout |
41 implements DistilledPagePrefs.Observer, SeekBar.OnSeekBarChangeListener, | 40 implements DistilledPagePrefs.Observer, SeekBar.OnSeekBarChangeListener
{ |
42 FontSizePrefs.Observer { | |
43 // XML layout for View. | 41 // XML layout for View. |
44 private static final int VIEW_LAYOUT = R.layout.distilled_page_prefs_view; | 42 private static final int VIEW_LAYOUT = R.layout.distilled_page_prefs_view; |
45 | 43 |
46 // RadioGroup for color mode buttons. | 44 // RadioGroup for color mode buttons. |
47 private RadioGroup mRadioGroup; | 45 private RadioGroup mRadioGroup; |
48 | 46 |
49 // Buttons for color mode. | 47 // Buttons for color mode. |
50 private final Map<Theme, RadioButton> mColorModeButtons; | 48 private final Map<Theme, RadioButton> mColorModeButtons; |
51 | 49 |
52 private final DistilledPagePrefs mDistilledPagePrefs; | 50 private final DistilledPagePrefs mDistilledPagePrefs; |
53 private final FontSizePrefs mFontSizePrefs; | |
54 | 51 |
55 // Text field showing font scale percentage. | 52 // Text field showing font scale percentage. |
56 private TextView mFontScaleTextView; | 53 private TextView mFontScaleTextView; |
57 | 54 |
58 // SeekBar for font scale. Has range of [0, 30]. | 55 // SeekBar for font scale. Has range of [0, 30]. |
59 private SeekBar mFontScaleSeekBar; | 56 private SeekBar mFontScaleSeekBar; |
60 | 57 |
61 // Spinner for choosing a font family. | 58 // Spinner for choosing a font family. |
62 private Spinner mFontFamilySpinner; | 59 private Spinner mFontFamilySpinner; |
63 | 60 |
64 private final NumberFormat mPercentageFormatter; | 61 private final NumberFormat mPercentageFormatter; |
65 | 62 |
66 /** | 63 /** |
67 * Creates a DistilledPagePrefsView. | 64 * Creates a DistilledPagePrefsView. |
68 * | 65 * |
69 * @param context Context for acquiring resources. | 66 * @param context Context for acquiring resources. |
70 * @param attrs Attributes from the XML layout inflation. | 67 * @param attrs Attributes from the XML layout inflation. |
71 */ | 68 */ |
72 public DistilledPagePrefsView(Context context, AttributeSet attrs) { | 69 public DistilledPagePrefsView(Context context, AttributeSet attrs) { |
73 super(context, attrs); | 70 super(context, attrs); |
74 mDistilledPagePrefs = DomDistillerServiceFactory.getForProfile( | 71 mDistilledPagePrefs = DomDistillerServiceFactory.getForProfile( |
75 Profile.getLastUsedProfile()).getDistilledPagePrefs(); | 72 Profile.getLastUsedProfile()).getDistilledPagePrefs(); |
76 mFontSizePrefs = FontSizePrefs.getInstance(getContext()); | |
77 mColorModeButtons = new EnumMap<Theme, RadioButton>(Theme.class); | 73 mColorModeButtons = new EnumMap<Theme, RadioButton>(Theme.class); |
78 mPercentageFormatter = NumberFormat.getPercentInstance(Locale.getDefault
()); | 74 mPercentageFormatter = NumberFormat.getPercentInstance(Locale.getDefault
()); |
79 } | 75 } |
80 | 76 |
81 public static DistilledPagePrefsView create(Context context) { | 77 public static DistilledPagePrefsView create(Context context) { |
82 return (DistilledPagePrefsView) LayoutInflater.from(context) | 78 return (DistilledPagePrefsView) LayoutInflater.from(context) |
83 .inflate(VIEW_LAYOUT, null); | 79 .inflate(VIEW_LAYOUT, null); |
84 } | 80 } |
85 | 81 |
86 public static void showDialog(Context context) { | 82 public static void showDialog(Context context) { |
(...skipping 14 matching lines...) Expand all Loading... |
101 initializeAndGetButton(R.id.sepia_mode, Theme.SEPIA)); | 97 initializeAndGetButton(R.id.sepia_mode, Theme.SEPIA)); |
102 mColorModeButtons.get(mDistilledPagePrefs.getTheme()).setChecked(true); | 98 mColorModeButtons.get(mDistilledPagePrefs.getTheme()).setChecked(true); |
103 | 99 |
104 mFontScaleSeekBar = (SeekBar) findViewById(R.id.font_size); | 100 mFontScaleSeekBar = (SeekBar) findViewById(R.id.font_size); |
105 mFontScaleTextView = (TextView) findViewById(R.id.font_size_percentage); | 101 mFontScaleTextView = (TextView) findViewById(R.id.font_size_percentage); |
106 | 102 |
107 mFontFamilySpinner = (Spinner) findViewById(R.id.font_family); | 103 mFontFamilySpinner = (Spinner) findViewById(R.id.font_family); |
108 initFontFamilySpinner(); | 104 initFontFamilySpinner(); |
109 | 105 |
110 // Setting initial progress on font scale seekbar. | 106 // Setting initial progress on font scale seekbar. |
111 onChangeFontSize(mFontSizePrefs.getFontScaleFactor()); | 107 onChangeFontScaling(mDistilledPagePrefs.getFontScaling()); |
112 mFontScaleSeekBar.setOnSeekBarChangeListener(this); | 108 mFontScaleSeekBar.setOnSeekBarChangeListener(this); |
113 } | 109 } |
114 | 110 |
115 private void initFontFamilySpinner() { | 111 private void initFontFamilySpinner() { |
116 ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getC
ontext(), | 112 ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getC
ontext(), |
117 android.R.layout.simple_spinner_item, getResources().getStringAr
ray( | 113 android.R.layout.simple_spinner_item, getResources().getStringAr
ray( |
118 R.array.distiller_mode_font_family_values)) { | 114 R.array.distiller_mode_font_family_values)) { |
119 @Override | 115 @Override |
120 public View getView(int position, View convertView, ViewGroup parent
) { | 116 public View getView(int position, View convertView, ViewGroup parent
) { |
121 View view = super.getView(position, convertView, parent); | 117 View view = super.getView(position, convertView, parent); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 } | 206 } |
211 | 207 |
212 /** | 208 /** |
213 * Changes which button is selected if the theme is changed in another tab. | 209 * Changes which button is selected if the theme is changed in another tab. |
214 */ | 210 */ |
215 @Override | 211 @Override |
216 public void onChangeTheme(Theme theme) { | 212 public void onChangeTheme(Theme theme) { |
217 mColorModeButtons.get(theme).setChecked(true); | 213 mColorModeButtons.get(theme).setChecked(true); |
218 } | 214 } |
219 | 215 |
| 216 @Override |
| 217 public void onChangeFontScaling(float scaling) { |
| 218 setFontScaleTextView(scaling); |
| 219 setFontScaleProgress(scaling); |
| 220 } |
| 221 |
220 // SeekBar.OnSeekBarChangeListener | 222 // SeekBar.OnSeekBarChangeListener |
221 | 223 |
222 @Override | 224 @Override |
223 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUse
r) { | 225 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUse
r) { |
224 // progress = [0, 30] | 226 // progress = [0, 30] |
225 // newValue = .50, .55, .60, ..., 1.95, 2.00 (supported font scales) | 227 // newValue = .50, .55, .60, ..., 1.95, 2.00 (supported font scales) |
226 float newValue = (progress / 20f + .5f); | 228 float newValue = (progress / 20f + .5f); |
227 setFontScaleTextView(newValue); | 229 setFontScaleTextView(newValue); |
228 mFontSizePrefs.setFontScaleFactor(newValue); | 230 if (fromUser) { |
| 231 mDistilledPagePrefs.setFontScaling(newValue); |
| 232 } |
229 } | 233 } |
230 | 234 |
231 @Override | 235 @Override |
232 public void onStartTrackingTouch(SeekBar seekBar) {} | 236 public void onStartTrackingTouch(SeekBar seekBar) {} |
233 | 237 |
234 @Override | 238 @Override |
235 public void onStopTrackingTouch(SeekBar seekBar) {} | 239 public void onStopTrackingTouch(SeekBar seekBar) {} |
236 | 240 |
237 // FontSizePrefs.Observer | |
238 | |
239 @Override | |
240 public void onChangeFontSize(float newFontSize) { | |
241 setFontScaleTextView(newFontSize); | |
242 setFontScaleProgress(newFontSize); | |
243 } | |
244 | |
245 @Override | |
246 public void onChangeForceEnableZoom(boolean enabled) {} | |
247 | |
248 @Override | |
249 public void onChangeUserSetForceEnableZoom(boolean enabled) {} | |
250 | |
251 /** | 241 /** |
252 * Initiatializes a Button and selects it if it corresponds to the current | 242 * Initiatializes a Button and selects it if it corresponds to the current |
253 * theme. | 243 * theme. |
254 */ | 244 */ |
255 private RadioButton initializeAndGetButton(int id, final Theme theme) { | 245 private RadioButton initializeAndGetButton(int id, final Theme theme) { |
256 final RadioButton button = (RadioButton) findViewById(id); | 246 final RadioButton button = (RadioButton) findViewById(id); |
257 button.setOnClickListener(new View.OnClickListener() { | 247 button.setOnClickListener(new View.OnClickListener() { |
258 @Override | 248 @Override |
259 public void onClick(View v) { | 249 public void onClick(View v) { |
260 mDistilledPagePrefs.setTheme(theme); | 250 mDistilledPagePrefs.setTheme(theme); |
261 } | 251 } |
262 }); | 252 }); |
263 return button; | 253 return button; |
264 } | 254 } |
265 | 255 |
266 /** | 256 /** |
267 * Sets the progress of mFontScaleSeekBar. | 257 * Sets the progress of mFontScaleSeekBar. |
268 */ | 258 */ |
269 private void setFontScaleProgress(float newValue) { | 259 private void setFontScaleProgress(float newValue) { |
270 // newValue = .50, .55, .60, ..., 1.95, 2.00 (supported font scales) | 260 // newValue = .50, .55, .60, ..., 1.95, 2.00 (supported font scales) |
271 // progress = [0, 30] | 261 // progress = [0, 30] |
272 int progress = (int) ((newValue - .5) * 20); | 262 int progress = (int) Math.round((newValue - .5) * 20); |
273 mFontScaleSeekBar.setProgress(progress); | 263 mFontScaleSeekBar.setProgress(progress); |
274 } | 264 } |
275 | 265 |
276 /** | 266 /** |
277 * Sets the text for the font scale text view. | 267 * Sets the text for the font scale text view. |
278 */ | 268 */ |
279 private void setFontScaleTextView(float newValue) { | 269 private void setFontScaleTextView(float newValue) { |
280 mFontScaleTextView.setText(mPercentageFormatter.format(newValue)); | 270 mFontScaleTextView.setText(mPercentageFormatter.format(newValue)); |
281 } | 271 } |
282 } | 272 } |
OLD | NEW |