| 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.ui; | 5 package org.chromium.ui; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.graphics.Color; | 8 import android.graphics.Color; |
| 9 import android.graphics.Typeface; | 9 import android.graphics.Typeface; |
| 10 import android.support.annotation.Nullable; |
| 10 import android.support.v7.content.res.AppCompatResources; | 11 import android.support.v7.content.res.AppCompatResources; |
| 11 import android.text.TextUtils; | 12 import android.text.TextUtils; |
| 12 import android.util.TypedValue; | 13 import android.util.TypedValue; |
| 13 import android.view.LayoutInflater; | 14 import android.view.LayoutInflater; |
| 14 import android.view.View; | 15 import android.view.View; |
| 15 import android.view.ViewGroup; | 16 import android.view.ViewGroup; |
| 16 import android.widget.AbsListView.LayoutParams; | 17 import android.widget.AbsListView.LayoutParams; |
| 17 import android.widget.ArrayAdapter; | 18 import android.widget.ArrayAdapter; |
| 18 import android.widget.ImageView; | 19 import android.widget.ImageView; |
| 19 import android.widget.LinearLayout; | 20 import android.widget.LinearLayout; |
| 20 import android.widget.TextView; | 21 import android.widget.TextView; |
| 21 | 22 |
| 22 import org.chromium.base.ApiCompatibilityUtils; | 23 import org.chromium.base.ApiCompatibilityUtils; |
| 23 | 24 |
| 24 import java.util.List; | 25 import java.util.List; |
| 25 import java.util.Set; | 26 import java.util.Set; |
| 26 | 27 |
| 27 /** | 28 /** |
| 28 * Dropdown item adapter for DropdownPopupWindow. | 29 * Dropdown item adapter for DropdownPopupWindow. |
| 29 */ | 30 */ |
| 30 public class DropdownAdapter extends ArrayAdapter<DropdownItem> { | 31 public class DropdownAdapter extends ArrayAdapter<DropdownItem> { |
| 31 private final Context mContext; | 32 private final Context mContext; |
| 32 private final Set<Integer> mSeparators; | 33 private final Set<Integer> mSeparators; |
| 33 private final boolean mAreAllItemsEnabled; | 34 private final boolean mAreAllItemsEnabled; |
| 34 private final Integer mBackgroundColor; | 35 private final Integer mBackgroundColor; |
| 35 private final Integer mDividerColor; | 36 private final Integer mDividerColor; |
| 36 private final Integer mDropdownItemHeight; | 37 private final Integer mDropdownItemHeight; |
| 38 private final int mLabelVerticalMargin; |
| 39 private final int mLabelHorizontalMargin; |
| 40 private final boolean mHasUniformHorizontalMargin; |
| 37 | 41 |
| 38 /** | 42 /** |
| 39 * Creates an {@code ArrayAdapter} with specified parameters. | 43 * Creates an {@code ArrayAdapter} with specified parameters. |
| 40 * @param context Application context. | 44 * @param context Application context. |
| 41 * @param items List of labels and icons to display. | 45 * @param items List of labels and icons to display. |
| 42 * @param separators Set of positions that separate {@code items}. | 46 * @param separators Set of positions that separate {@code items}. |
| 43 * @param backgroundColor Popup background color, or {@code null} to use def
ault background | 47 * @param backgroundColor Popup background color, or {@code null} to use def
ault background |
| 44 * color. The default color is {@code Color.TRANSPARENT}. | 48 * color. The default color is {@code Color.TRANSPARENT}. |
| 45 * @param dividerColor If {@code null}, use the values in colors.xml for the
divider | 49 * @param dividerColor If {@code null}, use the values in colors.xml for the
divider |
| 46 * between items. Otherwise, uses {@param dividerColor} for the divider betw
een items. Always | 50 * between items. Otherwise, uses {@param dividerColor} for the divider betw
een items. Always |
| 47 * uses the values in colors.xml for the dark divider for the separators. | 51 * uses the values in colors.xml for the dark divider for the separators. |
| 48 * @param dropdownItemHeight If {@code null}, uses the {@code dropdown_item_
height} in | 52 * @param dropdownItemHeight If {@code null}, uses the {@code dropdown_item_
height} in |
| 49 * dimens.xml. Otherwise, uses {@param dropdownItemHeight}. | 53 * dimens.xml. Otherwise, uses {@param dropdownItemHeight}. |
| 54 * @param margin If {@code null}, uses the {@code dropdown_icon_margin} and |
| 55 * {@code dropdown_item_label_margin} in dropdown_item.xml. Otherwise, uses
{@param margin} |
| 56 * for uniform margin for icon, label and between icon and label. |
| 50 */ | 57 */ |
| 51 public DropdownAdapter(Context context, List<? extends DropdownItem> items, | 58 public DropdownAdapter(Context context, List<? extends DropdownItem> items, |
| 52 Set<Integer> separators, Integer backgroundColor, Integer dividerCol
or, | 59 Set<Integer> separators, @Nullable Integer backgroundColor, |
| 53 Integer dropdownItemHeight) { | 60 @Nullable Integer dividerColor, @Nullable Integer dropdownItemHeight
, |
| 61 @Nullable Integer margin) { |
| 54 super(context, R.layout.dropdown_item); | 62 super(context, R.layout.dropdown_item); |
| 55 mContext = context; | 63 mContext = context; |
| 56 addAll(items); | 64 addAll(items); |
| 57 mSeparators = separators; | 65 mSeparators = separators; |
| 58 mAreAllItemsEnabled = checkAreAllItemsEnabled(); | 66 mAreAllItemsEnabled = checkAreAllItemsEnabled(); |
| 59 mBackgroundColor = backgroundColor; | 67 mBackgroundColor = backgroundColor; |
| 60 mDividerColor = dividerColor; | 68 mDividerColor = dividerColor; |
| 61 mDropdownItemHeight = dropdownItemHeight; | 69 mDropdownItemHeight = dropdownItemHeight; |
| 70 mLabelVerticalMargin = context.getResources().getDimensionPixelSize( |
| 71 R.dimen.dropdown_item_label_margin); |
| 72 if (margin == null) { |
| 73 mLabelHorizontalMargin = mLabelVerticalMargin; |
| 74 mHasUniformHorizontalMargin = false; |
| 75 } else { |
| 76 // We use a uniform margin between start and the icon, between icon
and the label and |
| 77 // between the label and the end. |
| 78 // <-- margin -->|icon|<-- margin -->|label|<-- margin --> |
| 79 // <-- margin -->|label|<-- margin --> |
| 80 mLabelHorizontalMargin = (int) TypedValue.applyDimension(TypedValue.
COMPLEX_UNIT_DIP, |
| 81 margin, context.getResources().getDisplayMetrics()); |
| 82 mHasUniformHorizontalMargin = true; |
| 83 } |
| 62 } | 84 } |
| 63 | 85 |
| 64 private boolean checkAreAllItemsEnabled() { | 86 private boolean checkAreAllItemsEnabled() { |
| 65 for (int i = 0; i < getCount(); i++) { | 87 for (int i = 0; i < getCount(); i++) { |
| 66 DropdownItem item = getItem(i); | 88 DropdownItem item = getItem(i); |
| 67 if (item.isEnabled() && !item.isGroupHeader()) { | 89 if (item.isEnabled() && !item.isGroupHeader()) { |
| 68 return false; | 90 return false; |
| 69 } | 91 } |
| 70 } | 92 } |
| 71 return true; | 93 return true; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 } else { | 145 } else { |
| 124 wrapper.setOrientation(LinearLayout.VERTICAL); | 146 wrapper.setOrientation(LinearLayout.VERTICAL); |
| 125 } | 147 } |
| 126 | 148 |
| 127 wrapper.setLayoutParams(new LinearLayout.LayoutParams(0, height, 1)); | 149 wrapper.setLayoutParams(new LinearLayout.LayoutParams(0, height, 1)); |
| 128 | 150 |
| 129 TextView labelView = (TextView) layout.findViewById(R.id.dropdown_label)
; | 151 TextView labelView = (TextView) layout.findViewById(R.id.dropdown_label)
; |
| 130 labelView.setText(item.getLabel()); | 152 labelView.setText(item.getLabel()); |
| 131 labelView.setSingleLine(!item.isMultilineLabel()); | 153 labelView.setSingleLine(!item.isMultilineLabel()); |
| 132 | 154 |
| 155 LinearLayout.LayoutParams layoutParams; |
| 133 if (item.isLabelAndSublabelOnSameLine()) { | 156 if (item.isLabelAndSublabelOnSameLine()) { |
| 134 labelView.setLayoutParams( | 157 layoutParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CO
NTENT, 1); |
| 135 new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT,
1)); | |
| 136 } else { | 158 } else { |
| 137 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutPara
ms( | 159 layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTE
NT, |
| 138 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); | 160 LayoutParams.WRAP_CONTENT); |
| 139 int labelMargin = mContext.getResources().getDimensionPixelSize( | 161 if (item.getIconId() == DropdownItem.NO_ICON || !mHasUniformHorizont
alMargin) { |
| 140 R.dimen.dropdown_item_label_margin); | 162 ApiCompatibilityUtils.setMarginStart(layoutParams, mLabelHorizon
talMargin); |
| 141 ApiCompatibilityUtils.setMarginStart(layoutParams, labelMargin); | 163 } |
| 142 ApiCompatibilityUtils.setMarginEnd(layoutParams, labelMargin); | 164 ApiCompatibilityUtils.setMarginEnd(layoutParams, mLabelHorizontalMar
gin); |
| 143 labelView.setLayoutParams(layoutParams); | |
| 144 if (item.isMultilineLabel()) { | 165 if (item.isMultilineLabel()) { |
| 145 // If there is a multiline label, we add extra padding top and b
ottom because | 166 // If there is a multiline label, we add extra padding at the to
p and bottom because |
| 146 // WRAP_CONTENT, defined above for multiline labels, leaves none
. | 167 // WRAP_CONTENT, defined above for multiline labels, leaves none
. |
| 147 int existingStart = ApiCompatibilityUtils.getPaddingStart(labelV
iew); | 168 int existingStart = ApiCompatibilityUtils.getPaddingStart(labelV
iew); |
| 148 int existingEnd = ApiCompatibilityUtils.getPaddingEnd(labelView)
; | 169 int existingEnd = ApiCompatibilityUtils.getPaddingEnd(labelView)
; |
| 149 ApiCompatibilityUtils.setPaddingRelative( | 170 ApiCompatibilityUtils.setPaddingRelative(labelView, |
| 150 labelView, existingStart, labelMargin, existingEnd, labe
lMargin); | 171 existingStart, mLabelVerticalMargin, existingEnd, mLabel
VerticalMargin); |
| 151 } | 172 } |
| 152 } | 173 } |
| 153 | 174 |
| 175 labelView.setLayoutParams(layoutParams); |
| 154 labelView.setEnabled(item.isEnabled()); | 176 labelView.setEnabled(item.isEnabled()); |
| 155 if (item.isGroupHeader() || item.isBoldLabel()) { | 177 if (item.isGroupHeader() || item.isBoldLabel()) { |
| 156 labelView.setTypeface(null, Typeface.BOLD); | 178 labelView.setTypeface(null, Typeface.BOLD); |
| 157 } else { | 179 } else { |
| 158 labelView.setTypeface(null, Typeface.NORMAL); | 180 labelView.setTypeface(null, Typeface.NORMAL); |
| 159 } | 181 } |
| 160 | 182 |
| 161 labelView.setTextColor(ApiCompatibilityUtils.getColor( | 183 labelView.setTextColor(ApiCompatibilityUtils.getColor( |
| 162 mContext.getResources(), item.getLabelFontColorResId())); | 184 mContext.getResources(), item.getLabelFontColorResId())); |
| 163 labelView.setTextSize(TypedValue.COMPLEX_UNIT_PX, | 185 labelView.setTextSize(TypedValue.COMPLEX_UNIT_PX, |
| 164 mContext.getResources().getDimension(item.getLabelFontSizeResId(
))); | 186 mContext.getResources().getDimension(item.getLabelFontSizeResId(
))); |
| 165 | 187 |
| 166 TextView sublabelView = (TextView) layout.findViewById(R.id.dropdown_sub
label); | 188 TextView sublabelView = (TextView) layout.findViewById(R.id.dropdown_sub
label); |
| 167 CharSequence sublabel = item.getSublabel(); | 189 CharSequence sublabel = item.getSublabel(); |
| 168 if (TextUtils.isEmpty(sublabel)) { | 190 if (TextUtils.isEmpty(sublabel)) { |
| 169 sublabelView.setVisibility(View.GONE); | 191 sublabelView.setVisibility(View.GONE); |
| 170 } else { | 192 } else { |
| 193 sublabelView.setLayoutParams(layoutParams); |
| 171 sublabelView.setText(sublabel); | 194 sublabelView.setText(sublabel); |
| 172 sublabelView.setTextSize(TypedValue.COMPLEX_UNIT_PX, | 195 sublabelView.setTextSize(TypedValue.COMPLEX_UNIT_PX, |
| 173 mContext.getResources().getDimension(item.getSublabelFontSiz
eResId())); | 196 mContext.getResources().getDimension(item.getSublabelFontSiz
eResId())); |
| 174 sublabelView.setVisibility(View.VISIBLE); | 197 sublabelView.setVisibility(View.VISIBLE); |
| 175 } | 198 } |
| 176 | 199 |
| 177 ImageView iconViewStart = (ImageView) layout.findViewById(R.id.start_dro
pdown_icon); | 200 ImageView iconViewStart = (ImageView) layout.findViewById(R.id.start_dro
pdown_icon); |
| 178 ImageView iconViewEnd = (ImageView) layout.findViewById(R.id.end_dropdow
n_icon); | 201 ImageView iconViewEnd = (ImageView) layout.findViewById(R.id.end_dropdow
n_icon); |
| 179 if (item.isIconAtStart()) { | 202 if (item.isIconAtStart()) { |
| 180 iconViewEnd.setVisibility(View.GONE); | 203 iconViewEnd.setVisibility(View.GONE); |
| 181 } else { | 204 } else { |
| 182 iconViewStart.setVisibility(View.GONE); | 205 iconViewStart.setVisibility(View.GONE); |
| 183 } | 206 } |
| 184 | 207 |
| 185 ImageView iconView = item.isIconAtStart() ? iconViewStart : iconViewEnd; | 208 ImageView iconView = item.isIconAtStart() ? iconViewStart : iconViewEnd; |
| 186 if (item.getIconId() == DropdownItem.NO_ICON) { | 209 if (item.getIconId() == DropdownItem.NO_ICON) { |
| 187 iconView.setVisibility(View.GONE); | 210 iconView.setVisibility(View.GONE); |
| 188 } else { | 211 } else { |
| 189 int iconSizeResId = item.getIconSizeResId(); | 212 int iconSizeResId = item.getIconSizeResId(); |
| 190 int iconSize = iconSizeResId == 0 | 213 int iconSize = iconSizeResId == 0 |
| 191 ? LayoutParams.WRAP_CONTENT | 214 ? LayoutParams.WRAP_CONTENT |
| 192 : mContext.getResources().getDimensionPixelSize(iconSizeResI
d); | 215 : mContext.getResources().getDimensionPixelSize(iconSizeResI
d); |
| 193 ViewGroup.MarginLayoutParams layoutParams = | 216 ViewGroup.MarginLayoutParams iconLayoutParams = |
| 194 (ViewGroup.MarginLayoutParams) iconView.getLayoutParams(); | 217 (ViewGroup.MarginLayoutParams) iconView.getLayoutParams(); |
| 195 layoutParams.width = iconSize; | 218 iconLayoutParams.width = iconSize; |
| 196 layoutParams.height = iconSize; | 219 iconLayoutParams.height = iconSize; |
| 197 int iconMargin = | 220 int iconMargin = mHasUniformHorizontalMargin ? mLabelHorizontalMargi
n |
| 198 mContext.getResources().getDimensionPixelSize(item.getIconMa
rginResId()); | 221 : mContext.getResources().getDimensionPixelSize(item.getIcon
MarginResId()); |
| 199 ApiCompatibilityUtils.setMarginStart(layoutParams, iconMargin); | 222 ApiCompatibilityUtils.setMarginStart(iconLayoutParams, iconMargin); |
| 200 ApiCompatibilityUtils.setMarginEnd(layoutParams, iconMargin); | 223 ApiCompatibilityUtils.setMarginEnd(iconLayoutParams, iconMargin); |
| 201 iconView.setLayoutParams(layoutParams); | 224 iconView.setLayoutParams(iconLayoutParams); |
| 202 iconView.setImageDrawable(AppCompatResources.getDrawable(mContext, i
tem.getIconId())); | 225 iconView.setImageDrawable(AppCompatResources.getDrawable(mContext, i
tem.getIconId())); |
| 203 iconView.setVisibility(View.VISIBLE); | 226 iconView.setVisibility(View.VISIBLE); |
| 204 } | 227 } |
| 205 | 228 |
| 206 return layout; | 229 return layout; |
| 207 } | 230 } |
| 208 | 231 |
| 209 @Override | 232 @Override |
| 210 public boolean areAllItemsEnabled() { | 233 public boolean areAllItemsEnabled() { |
| 211 return mAreAllItemsEnabled; | 234 return mAreAllItemsEnabled; |
| 212 } | 235 } |
| 213 | 236 |
| 214 @Override | 237 @Override |
| 215 public boolean isEnabled(int position) { | 238 public boolean isEnabled(int position) { |
| 216 if (position < 0 || position >= getCount()) return false; | 239 if (position < 0 || position >= getCount()) return false; |
| 217 DropdownItem item = getItem(position); | 240 DropdownItem item = getItem(position); |
| 218 return item.isEnabled() && !item.isGroupHeader(); | 241 return item.isEnabled() && !item.isGroupHeader(); |
| 219 } | 242 } |
| 220 } | 243 } |
| OLD | NEW |