Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/ItemChooserDialog.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ItemChooserDialog.java b/chrome/android/java/src/org/chromium/chrome/browser/ItemChooserDialog.java |
| index d93e107d842d9a3df9b1288538dbc1e9eab095a6..92251b912b21471eb39dd8e1c005a077d334b918 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/ItemChooserDialog.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/ItemChooserDialog.java |
| @@ -21,6 +21,7 @@ import android.view.Window; |
| import android.widget.AdapterView; |
| import android.widget.ArrayAdapter; |
| import android.widget.Button; |
| +import android.widget.ImageView; |
| import android.widget.LinearLayout; |
| import android.widget.ListView; |
| import android.widget.ProgressBar; |
| @@ -28,6 +29,7 @@ import android.widget.TextView; |
| import org.chromium.base.ApiCompatibilityUtils; |
| import org.chromium.base.VisibleForTesting; |
| +import org.chromium.base.annotations.SuppressFBWarnings; |
| import org.chromium.chrome.R; |
| import org.chromium.chrome.browser.util.MathUtils; |
| import org.chromium.ui.base.DeviceFormFactor; |
| @@ -60,15 +62,56 @@ public class ItemChooserDialog { |
| } |
| /** |
| + * A class representing an icon to be shown in a chooser row. |
| + */ |
| + public static class ItemChooserRowIcon { |
| + private int mIcon; |
| + // Icon when the row is selected. |
| + private int mSelectedIcon; |
| + private String mIconDescription; |
| + |
| + public ItemChooserRowIcon(int icon, int selectedIcon, String iconDescription) { |
| + mIcon = icon; |
| + mSelectedIcon = selectedIcon; |
| + mIconDescription = iconDescription; |
| + } |
| + |
| + @Override |
| + public boolean equals(Object obj) { |
| + if (!(obj instanceof ItemChooserRowIcon)) return false; |
| + if (this == obj) return true; |
| + ItemChooserRowIcon icon = (ItemChooserRowIcon) obj; |
| + return mIcon == icon.mIcon && mSelectedIcon == icon.mSelectedIcon |
| + && mIconDescription.equals(icon.mIconDescription); |
| + } |
| + |
| + @Override |
| + public String toString() { |
| + return mIcon + ":" + mSelectedIcon + ":" + mIconDescription; |
| + } |
| + |
| + @Override |
| + public int hashCode() { |
| + return mIcon + mSelectedIcon + mIconDescription.hashCode(); |
| + } |
| + } |
| + |
| + /** |
| * A class representing one data row in the picker. |
| */ |
| public static class ItemChooserRow { |
| private final String mKey; |
| private String mDescription; |
| + private ItemChooserRowIcon mIcon; |
|
juncai
2016/08/26 19:37:48
The class ItemChooserRowIcon has a member variable
ortuno
2016/09/12 05:11:28
Done.
|
| - public ItemChooserRow(String key, String description) { |
| + public ItemChooserRow(String key, String description, ItemChooserRowIcon icon) { |
| mKey = key; |
| mDescription = description; |
| + mIcon = icon; |
| + } |
| + |
| + public ItemChooserRow(String key, String description) { |
| + this(key, description, null); |
| } |
| @Override |
| @@ -76,12 +119,25 @@ public class ItemChooserDialog { |
| if (!(obj instanceof ItemChooserRow)) return false; |
| if (this == obj) return true; |
| ItemChooserRow item = (ItemChooserRow) obj; |
| - return mKey.equals(item.mKey) && mDescription.equals(item.mDescription); |
| + |
| + // If only one is null i.e. XOR. |
| + if (mIcon == null ^ item.mIcon == null) return false; |
| + |
| + if (mIcon != null && item.mIcon != null && !mIcon.equals(item.mIcon)) return false; |
| + |
| + if (mKey.equals(item.mKey) && mDescription.equals(item.mDescription)) return true; |
| + |
| + return false; |
| } |
| @Override |
| public int hashCode() { |
| - return mKey.hashCode() + mDescription.hashCode(); |
| + return mKey.hashCode() + mDescription.hashCode() + mIcon.hashCode(); |
| + } |
| + |
| + @Override |
| + public String toString() { |
| + return mKey + ":" + mDescription + ":" + mIcon.toString(); |
| } |
| } |
| @@ -188,6 +244,9 @@ public class ItemChooserDialog { |
| oldItem.mDescription = item.mDescription; |
| addToDescriptionsMap(oldItem.mDescription); |
| } |
| + if (oldItem.mIcon == null || !oldItem.mIcon.equals(item.mIcon)) { |
| + oldItem.mIcon = item.mIcon; |
| + } |
| notifyDataSetChanged(); |
| return; |
| } |
| @@ -240,6 +299,16 @@ public class ItemChooserDialog { |
| item.mKey); |
| } |
| + public ItemChooserRowIcon getIcon(int position) { |
| + ItemChooserRow item = getItem(position); |
| + return item.mIcon; |
| + } |
| + |
| + public boolean hasIcon(int position) { |
| + ItemChooserRow item = getItem(position); |
| + return item.mIcon != null; |
| + } |
| + |
| /** |
| * Sets whether the itam is enabled. Disabled items are grayed out. |
| * @param id The id of the item to affect. |
| @@ -270,8 +339,48 @@ public class ItemChooserDialog { |
| return position; |
| } |
| - @Override |
| - public View getView(int position, View convertView, ViewGroup parent) { |
| + // view = (LinearLayout) mInflater.inflate(...); causes a DLS_DEAD_LOCAL_STORE error. |
| + @SuppressFBWarnings("DLS_DEAD_LOCAL_STORE") |
| + private View getViewWithIcon(int position, View convertView, ViewGroup parent) { |
| + LinearLayout view; |
| + if (convertView instanceof LinearLayout) { |
| + view = (LinearLayout) convertView; |
| + } else { |
| + view = (LinearLayout) mInflater.inflate( |
| + R.layout.item_chooser_dialog_row_with_icon, parent, false); |
| + } |
| + |
| + TextView description = (TextView) view.findViewById(R.id.description); |
| + LinearLayout imageContainer = (LinearLayout) view.findViewById(R.id.imageContainer); |
| + ImageView icon = (ImageView) view.findViewById(R.id.icon); |
| + |
| + ItemChooserRowIcon rowIcon = getIcon(position); |
| + |
| + // Set highlighting for currently selected item. |
| + if (position == mSelectedItem) { |
| + view.setBackgroundColor(mBackgroundHighlightColor); |
| + description.setTextColor(Color.WHITE); |
| + icon.setImageResource(rowIcon.mSelectedIcon); |
| + icon.setContentDescription(rowIcon.mIconDescription); |
| + } else { |
| + view.setBackground(null); |
| + icon.setImageResource(rowIcon.mIcon); |
| + icon.setContentDescription(rowIcon.mIconDescription); |
| + if (!isEnabled(position)) { |
| + description.setTextColor( |
| + ApiCompatibilityUtils.getColor(getContext().getResources(), |
| + R.color.primary_text_disabled_material_light)); |
| + } else { |
| + description.setTextColor(mDefaultTextColor); |
| + } |
| + } |
| + |
| + description.setText(getDisplayText(position)); |
| + |
| + return view; |
| + } |
| + |
| + private View getTextView(int position, View convertView, ViewGroup parent) { |
| TextView view; |
| if (convertView instanceof TextView) { |
| view = (TextView) convertView; |
| @@ -299,6 +408,14 @@ public class ItemChooserDialog { |
| } |
| @Override |
| + public View getView(int position, View convertView, ViewGroup parent) { |
| + if (hasIcon(position)) { |
| + return getViewWithIcon(position, convertView, parent); |
| + } |
| + return getTextView(position, convertView, parent); |
| + } |
| + |
| + @Override |
| public void onItemClick(AdapterView<?> adapter, View view, int position, long id) { |
| mSelectedItem = position; |
| mConfirmButton.setEnabled(true); |