OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.infobar; | 5 package org.chromium.chrome.browser.infobar; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.res.Resources; | 8 import android.content.res.Resources; |
9 import android.graphics.Paint; | 9 import android.graphics.Paint; |
10 import android.support.annotation.Nullable; | 10 import android.support.annotation.Nullable; |
11 import android.support.v7.widget.SwitchCompat; | 11 import android.support.v7.widget.SwitchCompat; |
12 import android.text.method.LinkMovementMethod; | 12 import android.text.method.LinkMovementMethod; |
13 import android.util.AttributeSet; | |
14 import android.view.LayoutInflater; | 13 import android.view.LayoutInflater; |
15 import android.view.View; | 14 import android.view.View; |
16 import android.view.ViewGroup; | 15 import android.view.ViewGroup; |
17 import android.widget.ArrayAdapter; | 16 import android.widget.ArrayAdapter; |
18 import android.widget.ImageView; | 17 import android.widget.ImageView; |
19 import android.widget.LinearLayout; | 18 import android.widget.LinearLayout; |
| 19 import android.widget.RadioButton; |
| 20 import android.widget.RadioGroup; |
20 import android.widget.RatingBar; | 21 import android.widget.RatingBar; |
21 import android.widget.Spinner; | 22 import android.widget.Spinner; |
22 import android.widget.TextView; | 23 import android.widget.TextView; |
23 | 24 |
24 import org.chromium.base.ApiCompatibilityUtils; | 25 import org.chromium.base.ApiCompatibilityUtils; |
25 import org.chromium.base.VisibleForTesting; | 26 import org.chromium.base.VisibleForTesting; |
26 import org.chromium.chrome.R; | 27 import org.chromium.chrome.R; |
27 import org.chromium.chrome.browser.widget.DualControlLayout; | 28 import org.chromium.chrome.browser.widget.DualControlLayout; |
28 import org.chromium.chrome.browser.widget.RadioButtonLayout; | |
29 | 29 |
30 import java.util.List; | 30 import java.util.List; |
31 | 31 |
32 /** | 32 /** |
33 * Lays out a group of controls (e.g. switches, spinners, or additional text) fo
r InfoBars that need | 33 * Lays out a group of controls (e.g. switches, spinners, or additional text) fo
r InfoBars that need |
34 * more than the normal pair of buttons. | 34 * more than the normal pair of buttons. |
35 * | 35 * |
36 * This class works with the {@link InfoBarLayout} to define a standard set of c
ontrols with | 36 * This class works with the {@link InfoBarLayout} to define a standard set of c
ontrols with |
37 * standardized spacings and text styling that gets laid out in grid form: https
://crbug.com/543205 | 37 * standardized spacings and text styling that gets laid out in grid form: https
://crbug.com/543205 |
38 * | 38 * |
39 * Manually specified margins on the children managed by this layout are EXPLICI
TLY ignored to | 39 * Manually specified margins on the children managed by this layout are EXPLICI
TLY ignored to |
40 * enforce a uniform margin between controls across all InfoBar types. Do NOT c
ircumvent this | 40 * enforce a uniform margin between controls across all InfoBar types. Do NOT c
ircumvent this |
41 * restriction with creative layout definitions. If the layout algorithm doesn'
t work for your new | 41 * restriction with creative layout definitions. If the layout algorithm doesn'
t work for your new |
42 * InfoBar, convince Chrome for Android's UX team to amend the master spec and t
hen change the | 42 * InfoBar, convince Chrome for Android's UX team to amend the master spec and t
hen change the |
43 * layout algorithm to match. | 43 * layout algorithm to match. |
44 * | 44 * |
45 * TODO(dfalcantara): The line spacing multiplier is applied to all lines in JB
& KK, even if the | 45 * TODO(dfalcantara): The line spacing multiplier is applied to all lines in JB
& KK, even if the |
46 * TextView has only one line. This throws off vertical alig
nment. Find a | 46 * TextView has only one line. This throws off vertical alig
nment. Find a |
47 * solution that hopefully doesn't involve subclassing the Te
xtView. | 47 * solution that hopefully doesn't involve subclassing the Te
xtView. |
| 48 * |
| 49 * TODO(dfalcantara): Move this to a more general location. |
48 */ | 50 */ |
49 public final class InfoBarControlLayout extends ViewGroup { | 51 public final class InfoBarControlLayout extends ViewGroup { |
| 52 public static final int INVALID_INDEX = -1; |
| 53 |
50 /** | 54 /** |
51 * ArrayAdapter that automatically determines what size make its Views to ac
commodate all of | 55 * ArrayAdapter that automatically determines what size make its Views to ac
commodate all of |
52 * its potential values. | 56 * its potential values. |
53 * @param <T> Type of object that the ArrayAdapter stores. | 57 * @param <T> Type of object that the ArrayAdapter stores. |
54 */ | 58 */ |
55 public static final class InfoBarArrayAdapter<T> extends ArrayAdapter<T> { | 59 public static final class InfoBarArrayAdapter<T> extends ArrayAdapter<T> { |
56 private final String mLabel; | 60 private final String mLabel; |
57 private int mMinWidthRequiredForValues; | 61 private int mMinWidthRequiredForValues; |
58 | 62 |
59 public InfoBarArrayAdapter(Context context, String label) { | 63 public InfoBarArrayAdapter(Context context, String label) { |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 } | 156 } |
153 } | 157 } |
154 | 158 |
155 private final int mMarginBetweenRows; | 159 private final int mMarginBetweenRows; |
156 private final int mMarginBetweenColumns; | 160 private final int mMarginBetweenColumns; |
157 | 161 |
158 /** | 162 /** |
159 * Do not call this method directly; use {@link InfoBarLayout#addControlLayo
ut()}. | 163 * Do not call this method directly; use {@link InfoBarLayout#addControlLayo
ut()}. |
160 */ | 164 */ |
161 public InfoBarControlLayout(Context context) { | 165 public InfoBarControlLayout(Context context) { |
162 this(context, null); | 166 super(context); |
163 } | |
164 | |
165 public InfoBarControlLayout(Context context, AttributeSet attrs) { | |
166 super(context, attrs); | |
167 | 167 |
168 Resources resources = context.getResources(); | 168 Resources resources = context.getResources(); |
169 mMarginBetweenRows = | 169 mMarginBetweenRows = |
170 resources.getDimensionPixelSize(R.dimen.infobar_control_margin_b
etween_rows); | 170 resources.getDimensionPixelSize(R.dimen.infobar_control_margin_b
etween_rows); |
171 mMarginBetweenColumns = | 171 mMarginBetweenColumns = |
172 resources.getDimensionPixelSize(R.dimen.infobar_control_margin_b
etween_columns); | 172 resources.getDimensionPixelSize(R.dimen.infobar_control_margin_b
etween_columns); |
173 } | 173 } |
174 | 174 |
175 @Override | 175 @Override |
176 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | 176 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 (SwitchCompat) switchLayout.findViewById(R.id.control_toggle_swi
tch); | 354 (SwitchCompat) switchLayout.findViewById(R.id.control_toggle_swi
tch); |
355 switchView.setId(toggleId); | 355 switchView.setId(toggleId); |
356 switchView.setChecked(isChecked); | 356 switchView.setChecked(isChecked); |
357 | 357 |
358 return switchLayout; | 358 return switchLayout; |
359 } | 359 } |
360 | 360 |
361 /** | 361 /** |
362 * Creates a set of standard radio buttons and adds it to the layout. | 362 * Creates a set of standard radio buttons and adds it to the layout. |
363 * | 363 * |
| 364 * ------------------------------------------------- |
| 365 * | O | MESSAGE #1 | |
| 366 * | O | MESSAGE #N | |
| 367 * ------------------------------------------------- |
| 368 * |
364 * @param messages Messages to display for the options. | 369 * @param messages Messages to display for the options. |
365 * @param tags Optional list of tags to attach to the buttons. | 370 * @param tags Optional list of tags to attach to the buttons. |
| 371 * @param selectedIndex Which index to mark as being selected. |
366 */ | 372 */ |
367 public RadioButtonLayout addRadioButtons(List<CharSequence> messages, @Nulla
ble List<?> tags) { | 373 public RadioGroup addRadioButtons( |
| 374 List<CharSequence> messages, @Nullable List<?> tags, int selectedInd
ex) { |
| 375 if (tags != null) assert tags.size() == messages.size(); |
| 376 |
368 ControlLayoutParams params = new ControlLayoutParams(); | 377 ControlLayoutParams params = new ControlLayoutParams(); |
369 params.mMustBeFullWidth = true; | 378 params.mMustBeFullWidth = true; |
370 | 379 |
371 RadioButtonLayout radioLayout = new RadioButtonLayout(getContext()); | 380 RadioGroup radioLayout = new RadioGroup(getContext()); |
372 radioLayout.addOptions(messages, tags); | 381 addView(radioLayout, params); |
373 | 382 |
374 addView(radioLayout, params); | 383 for (int i = 0; i < messages.size(); i++) { |
| 384 RadioButton button = |
| 385 (RadioButton) LayoutInflater.from(getContext()) |
| 386 .inflate(R.layout.infobar_control_radio, radioLayout
, false); |
| 387 button.setText(messages.get(i)); |
| 388 if (tags != null) button.setTag(tags.get(i)); |
| 389 button.setChecked(i == selectedIndex); |
| 390 radioLayout.addView(button); |
| 391 |
| 392 // Add margins between each of the radio buttons. |
| 393 if (i < messages.size() - 1) { |
| 394 ((MarginLayoutParams) button.getLayoutParams()).bottomMargin = m
MarginBetweenRows; |
| 395 } |
| 396 } |
| 397 |
375 return radioLayout; | 398 return radioLayout; |
376 } | 399 } |
377 | 400 |
378 /** | 401 /** |
379 * Creates a standard spinner and adds it to the layout. | 402 * Creates a standard spinner and adds it to the layout. |
380 */ | 403 */ |
381 public <T> Spinner addSpinner(int spinnerId, ArrayAdapter<T> arrayAdapter) { | 404 public <T> Spinner addSpinner(int spinnerId, ArrayAdapter<T> arrayAdapter) { |
382 Spinner spinner = (Spinner) LayoutInflater.from(getContext()).inflate( | 405 Spinner spinner = (Spinner) LayoutInflater.from(getContext()).inflate( |
383 R.layout.infobar_control_spinner, this, false); | 406 R.layout.infobar_control_spinner, this, false); |
384 spinner.setAdapter(arrayAdapter); | 407 spinner.setAdapter(arrayAdapter); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 | 462 |
440 /** | 463 /** |
441 * @return The {@link ControlLayoutParams} for the given child. | 464 * @return The {@link ControlLayoutParams} for the given child. |
442 */ | 465 */ |
443 @VisibleForTesting | 466 @VisibleForTesting |
444 static ControlLayoutParams getControlLayoutParams(View child) { | 467 static ControlLayoutParams getControlLayoutParams(View child) { |
445 return (ControlLayoutParams) child.getLayoutParams(); | 468 return (ControlLayoutParams) child.getLayoutParams(); |
446 } | 469 } |
447 | 470 |
448 } | 471 } |
OLD | NEW |