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.support.v7.widget.SwitchCompat; | 9 import android.support.v7.widget.SwitchCompat; |
10 import android.text.method.LinkMovementMethod; | 10 import android.text.method.LinkMovementMethod; |
(...skipping 18 matching lines...) Expand all Loading... | |
29 * This class works with the {@link InfoBarLayout} to define a standard set of c ontrols with | 29 * This class works with the {@link InfoBarLayout} to define a standard set of c ontrols with |
30 * standardized spacings and text styling that gets laid out in grid form: https ://crbug.com/543205 | 30 * standardized spacings and text styling that gets laid out in grid form: https ://crbug.com/543205 |
31 * | 31 * |
32 * Manually specified margins on the children managed by this layout are EXPLICI TLY ignored to | 32 * Manually specified margins on the children managed by this layout are EXPLICI TLY ignored to |
33 * enforce a uniform margin between controls across all InfoBar types. Do NOT c ircumvent this | 33 * enforce a uniform margin between controls across all InfoBar types. Do NOT c ircumvent this |
34 * restriction with creative layout definitions. If the layout algorithm doesn' t work for your new | 34 * restriction with creative layout definitions. If the layout algorithm doesn' t work for your new |
35 * InfoBar, convince Chrome for Android's UX team to amend the master spec and t hen change the | 35 * InfoBar, convince Chrome for Android's UX team to amend the master spec and t hen change the |
36 * layout algorithm to match. | 36 * layout algorithm to match. |
37 * | 37 * |
38 * TODO(dfalcantara): Standardize all the possible control types. | 38 * TODO(dfalcantara): Standardize all the possible control types. |
39 * TODO(dfalcantara): The line spacing multiplier is applied to all lines in JB & KK, even if the | |
40 * TextView has only one line. This throws off vertical alig nment. Find a | |
41 * solution that hopefully doesn't involve subclassing the Te xtView. | |
39 */ | 42 */ |
40 public final class InfoBarControlLayout extends ViewGroup { | 43 public final class InfoBarControlLayout extends ViewGroup { |
41 | 44 |
42 /** | 45 /** |
43 * Extends the regular LayoutParams by determining where a control should be located. | 46 * Extends the regular LayoutParams by determining where a control should be located. |
44 */ | 47 */ |
45 @VisibleForTesting | 48 @VisibleForTesting |
46 static final class ControlLayoutParams extends LayoutParams { | 49 static final class ControlLayoutParams extends LayoutParams { |
47 public int start; | 50 public int start; |
48 public int top; | 51 public int top; |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 if (isRtl) childLeft = width - childLeft - child.getMeasuredWidth(); | 181 if (isRtl) childLeft = width - childLeft - child.getMeasuredWidth(); |
179 | 182 |
180 int childTop = getControlLayoutParams(child).top; | 183 int childTop = getControlLayoutParams(child).top; |
181 int childRight = childLeft + child.getMeasuredWidth(); | 184 int childRight = childLeft + child.getMeasuredWidth(); |
182 int childBottom = childTop + child.getMeasuredHeight(); | 185 int childBottom = childTop + child.getMeasuredHeight(); |
183 child.layout(childLeft, childTop, childRight, childBottom); | 186 child.layout(childLeft, childTop, childRight, childBottom); |
184 } | 187 } |
185 } | 188 } |
186 | 189 |
187 /** | 190 /** |
191 * Adds an icon with a descriptive message to the layout. | |
192 * | |
193 * ----------------------------------------------------- | |
194 * | ICON | PRIMARY MESSAGE SECONDARY MESSAGE | | |
195 * ----------------------------------------------------- | |
196 * If an icon is not provided, the ImageView that would normally show it is hidden. | |
197 * | |
198 * @param iconResourceId ID of the drawable to use for the icon, or 0 to h ide the ImageView. | |
newt (away)
2016/01/05 21:20:53
Do we need to allow 0 here? I'd prefer not to, unl
gone
2016/01/05 21:43:54
Nope, carryover from a rebase. Nuked it.
| |
199 * @param primaryMessage Message to display for the toggle. | |
200 * @param secondaryMessage Additional descriptive text for the toggle. May be null. | |
201 */ | |
202 public View addIcon( | |
203 int iconResourceId, CharSequence primaryMessage, CharSequence second aryMessage) { | |
204 LinearLayout layout = (LinearLayout) LayoutInflater.from(getContext()).i nflate( | |
205 R.layout.infobar_control_icon_with_description, this, false); | |
206 addView(layout, new ControlLayoutParams()); | |
207 | |
208 ImageView iconView = (ImageView) layout.findViewById(R.id.control_icon); | |
209 if (iconResourceId == 0) { | |
210 layout.removeView(iconView); | |
211 } else { | |
212 iconView.setImageResource(iconResourceId); | |
213 } | |
214 | |
215 // The primary message text is always displayed. | |
216 TextView primaryView = (TextView) layout.findViewById(R.id.control_messa ge); | |
217 primaryView.setText(primaryMessage); | |
218 | |
219 // The secondary message text is optional. | |
220 TextView secondaryView = | |
221 (TextView) layout.findViewById(R.id.control_secondary_message); | |
222 if (secondaryMessage == null) { | |
223 layout.removeView(secondaryView); | |
224 } else { | |
225 secondaryView.setText(secondaryMessage); | |
226 } | |
227 | |
228 return layout; | |
229 } | |
230 | |
231 /** | |
188 * Creates a standard toggle switch and adds it to the layout. | 232 * Creates a standard toggle switch and adds it to the layout. |
189 * | 233 * |
190 * ------------------------------------------------- | 234 * ------------------------------------------------- |
191 * | ICON | MESSAGE | TOGGLE | | 235 * | ICON | MESSAGE | TOGGLE | |
192 * ------------------------------------------------- | 236 * ------------------------------------------------- |
193 * If an icon is not provided, the ImageView that would normally show it is hidden. | 237 * If an icon is not provided, the ImageView that would normally show it is hidden. |
194 * | 238 * |
195 * @param iconResourceId ID of the drawable to use for the icon, or 0 to hid e the ImageView. | 239 * @param iconResourceId ID of the drawable to use for the icon, or 0 to hid e the ImageView. |
196 * @param toggleMessage Message to display for the toggle. | 240 * @param toggleMessage Message to display for the toggle. |
197 * @param toggleId ID to use for the toggle. | 241 * @param toggleId ID to use for the toggle. |
198 * @param isChecked Whether the toggle should start off checked. | 242 * @param isChecked Whether the toggle should start off checked. |
199 */ | 243 */ |
200 public View addSwitch( | 244 public View addSwitch( |
201 int iconResourceId, CharSequence toggleMessage, int toggleId, boolea n isChecked) { | 245 int iconResourceId, CharSequence toggleMessage, int toggleId, boolea n isChecked) { |
202 LinearLayout switchLayout = (LinearLayout) LayoutInflater.from(getContex t()).inflate( | 246 LinearLayout switchLayout = (LinearLayout) LayoutInflater.from(getContex t()).inflate( |
203 R.layout.infobar_control_toggle, this, false); | 247 R.layout.infobar_control_toggle, this, false); |
204 addView(switchLayout, new ControlLayoutParams()); | 248 addView(switchLayout, new ControlLayoutParams()); |
205 | 249 |
206 ImageView iconView = (ImageView) switchLayout.findViewById(R.id.control_ toggle_icon); | 250 ImageView iconView = (ImageView) switchLayout.findViewById(R.id.control_ icon); |
207 if (iconResourceId == 0) { | 251 if (iconResourceId == 0) { |
208 switchLayout.removeView(iconView); | 252 switchLayout.removeView(iconView); |
209 } else { | 253 } else { |
210 iconView.setImageResource(iconResourceId); | 254 iconView.setImageResource(iconResourceId); |
211 } | 255 } |
212 | 256 |
213 TextView messageView = (TextView) switchLayout.findViewById(R.id.control _toggle_message); | 257 TextView messageView = (TextView) switchLayout.findViewById(R.id.control _message); |
214 messageView.setText(toggleMessage); | 258 messageView.setText(toggleMessage); |
215 | 259 |
216 SwitchCompat switchView = | 260 SwitchCompat switchView = |
217 (SwitchCompat) switchLayout.findViewById(R.id.control_toggle_swi tch); | 261 (SwitchCompat) switchLayout.findViewById(R.id.control_toggle_swi tch); |
218 switchView.setId(toggleId); | 262 switchView.setId(toggleId); |
219 switchView.setChecked(isChecked); | 263 switchView.setChecked(isChecked); |
220 | 264 |
221 return switchLayout; | 265 return switchLayout; |
222 } | 266 } |
223 | 267 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 | 334 |
291 /** | 335 /** |
292 * @return The {@link ControlLayoutParams} for the given child. | 336 * @return The {@link ControlLayoutParams} for the given child. |
293 */ | 337 */ |
294 @VisibleForTesting | 338 @VisibleForTesting |
295 static ControlLayoutParams getControlLayoutParams(View child) { | 339 static ControlLayoutParams getControlLayoutParams(View child) { |
296 return (ControlLayoutParams) child.getLayoutParams(); | 340 return (ControlLayoutParams) child.getLayoutParams(); |
297 } | 341 } |
298 | 342 |
299 } | 343 } |
OLD | NEW |