OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.payments.ui; | 5 package org.chromium.chrome.browser.payments.ui; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.support.v7.content.res.AppCompatResources; | 8 import android.support.v7.content.res.AppCompatResources; |
9 import android.view.LayoutInflater; | 9 import android.view.LayoutInflater; |
10 import android.view.View; | 10 import android.view.View; |
11 import android.view.ViewGroup; | 11 import android.view.ViewGroup; |
12 import android.view.ViewGroup.LayoutParams; | 12 import android.widget.BaseAdapter; |
13 import android.widget.ImageView; | 13 import android.widget.ImageView; |
14 import android.widget.LinearLayout; | |
15 import android.widget.TextView; | 14 import android.widget.TextView; |
16 | 15 |
17 import org.chromium.base.ApiCompatibilityUtils; | |
18 import org.chromium.chrome.R; | 16 import org.chromium.chrome.R; |
19 | 17 |
| 18 import java.util.List; |
| 19 |
20 /** | 20 /** |
21 * Helper class for creating a horizontal list of icons with a title. | 21 * Helper class for creating a horizontal list of icons with a title. |
22 */ | 22 */ |
23 class EditorIconsField { | 23 class EditorIconsField { |
24 private final View mLayout; | 24 private final View mLayout; |
25 | 25 |
26 /** | 26 /** |
27 * Builds a horizontal list of icons. | 27 * Builds a horizontal list of icons. |
28 * | 28 * |
29 * @param context The application context to use when creating widgets. | 29 * @param context The application context to use when creating widgets. |
30 * @param root The object that provides a set of LayoutParams values f
or the view. | 30 * @param root The object that provides a set of LayoutParams values f
or the view. |
31 * @param fieldModel The data model of the icon list. | 31 * @param fieldModel The data model of the icon list. |
32 */ | 32 */ |
33 public EditorIconsField(Context context, ViewGroup root, EditorFieldModel fi
eldModel) { | 33 public EditorIconsField(Context context, ViewGroup root, EditorFieldModel fi
eldModel) { |
34 assert fieldModel.getInputTypeHint() == EditorFieldModel.INPUT_TYPE_HINT
_ICONS; | 34 assert fieldModel.getInputTypeHint() == EditorFieldModel.INPUT_TYPE_HINT
_ICONS; |
35 | 35 |
36 mLayout = LayoutInflater.from(context).inflate( | 36 mLayout = LayoutInflater.from(context).inflate( |
37 R.layout.payment_request_editor_icons, root, false); | 37 R.layout.payment_request_editor_icons, root, false); |
38 | 38 |
39 ((TextView) mLayout.findViewById(R.id.label)).setText(fieldModel.getLabe
l()); | 39 ((TextView) mLayout.findViewById(R.id.label)).setText(fieldModel.getLabe
l()); |
40 | 40 |
41 LinearLayout container = (LinearLayout) mLayout.findViewById(R.id.icons_
container); | 41 ExpandableGridView iconsContainer = |
42 int size = | 42 (ExpandableGridView) mLayout.findViewById(R.id.icons_container); |
43 context.getResources().getDimensionPixelSize(R.dimen.payments_se
ction_logo_width); | 43 iconsContainer.setAdapter(new IconListAdapter(context, fieldModel.getIco
nResourceIds(), |
44 int margin = context.getResources().getDimensionPixelSize( | 44 fieldModel.getIconDescriptionsForAccessibility())); |
45 R.dimen.payments_section_small_spacing); | |
46 LinearLayout.LayoutParams layoutParams = | |
47 new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutP
arams.WRAP_CONTENT); | |
48 ApiCompatibilityUtils.setMarginEnd(layoutParams, margin); | |
49 for (int i = 0; i < fieldModel.getIconResourceIds().size(); i++) { | |
50 ImageView icon = new ImageView(context); | |
51 icon.setImageDrawable(AppCompatResources.getDrawable( | |
52 context, fieldModel.getIconResourceIds().get(i))); | |
53 icon.setContentDescription(context.getString( | |
54 fieldModel.getIconDescriptionsForAccessibility().get(i))); | |
55 icon.setAdjustViewBounds(true); | |
56 icon.setMaxWidth(size); | |
57 icon.setMaxHeight(size); | |
58 container.addView(icon, layoutParams); | |
59 } | |
60 } | 45 } |
61 | 46 |
62 /** @return The View containing everything. */ | 47 /** @return The View containing everything. */ |
63 public View getLayout() { | 48 public View getLayout() { |
64 return mLayout; | 49 return mLayout; |
65 } | 50 } |
| 51 |
| 52 /** |
| 53 * An instance of a {@link BaseAdapter} that provides a list of card icon vi
ews. |
| 54 */ |
| 55 private static class IconListAdapter extends BaseAdapter { |
| 56 private Context mContext; |
| 57 private List<Integer> mIconResourceIds; |
| 58 private List<Integer> mIconDescriptionIds; |
| 59 private int mIconSize; |
| 60 |
| 61 public IconListAdapter( |
| 62 Context context, List<Integer> iconResourceIds, List<Integer> ic
onDescriptionIds) { |
| 63 mContext = context; |
| 64 mIconResourceIds = iconResourceIds; |
| 65 mIconDescriptionIds = iconDescriptionIds; |
| 66 mIconSize = mContext.getResources().getDimensionPixelSize( |
| 67 R.dimen.payments_section_logo_width); |
| 68 assert mIconResourceIds.size() == mIconDescriptionIds.size(); |
| 69 } |
| 70 |
| 71 @Override |
| 72 public int getCount() { |
| 73 return mIconResourceIds.size(); |
| 74 } |
| 75 |
| 76 @Override |
| 77 public Object getItem(int position) { |
| 78 return mIconResourceIds.get(position); |
| 79 } |
| 80 |
| 81 @Override |
| 82 public long getItemId(int position) { |
| 83 return position; |
| 84 } |
| 85 |
| 86 @Override |
| 87 public View getView(int position, View convertView, ViewGroup parent) { |
| 88 ImageView iconView = (ImageView) convertView; |
| 89 if (iconView == null) iconView = new ImageView(mContext); |
| 90 iconView.setImageDrawable( |
| 91 AppCompatResources.getDrawable(mContext, mIconResourceIds.ge
t(position))); |
| 92 iconView.setContentDescription(mContext.getString(mIconDescriptionId
s.get(position))); |
| 93 iconView.setAdjustViewBounds(true); |
| 94 iconView.setMaxWidth(mIconSize); |
| 95 iconView.setMaxHeight(mIconSize); |
| 96 return iconView; |
| 97 } |
| 98 } |
66 } | 99 } |
OLD | NEW |