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..b67af520b2ab4ef6417d69d72962bf2746c4627d 100644 |
--- a/chrome/android/java/src/org/chromium/chrome/browser/ItemChooserDialog.java |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ItemChooserDialog.java |
@@ -21,9 +21,11 @@ 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; |
+import android.widget.RelativeLayout; |
import android.widget.TextView; |
import org.chromium.base.ApiCompatibilityUtils; |
@@ -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 mNonSelectedIcon; |
+ // Icon when the row is selected. |
+ private int mSelectedIcon; |
+ private String mIconDescription; |
+ |
+ public ItemChooserRowIcon(int icon, int selectedIcon, String iconDescription) { |
+ mNonSelectedIcon = 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 mNonSelectedIcon == icon.mNonSelectedIcon && mSelectedIcon == icon.mSelectedIcon |
+ && mIconDescription.equals(icon.mIconDescription); |
+ } |
+ |
+ @Override |
+ public String toString() { |
+ return mNonSelectedIcon + ":" + mSelectedIcon + ":" + mIconDescription; |
+ } |
+ |
+ @Override |
+ public int hashCode() { |
+ return mNonSelectedIcon + 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; |
- 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,24 @@ 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 (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(); |
} |
} |
@@ -138,6 +193,9 @@ public class ItemChooserDialog { |
// The color of the non-highlighted text. |
private final int mDefaultTextColor; |
+ // The color of disabled text. |
+ private final int mDisabledTextColor; |
+ |
// The zero-based index of the item currently selected in the dialog, |
// or -1 (INVALID_POSITION) if nothing is selected. |
private int mSelectedItem = ListView.INVALID_POSITION; |
@@ -160,6 +218,8 @@ public class ItemChooserDialog { |
R.color.light_active_color); |
mDefaultTextColor = ApiCompatibilityUtils.getColor(getContext().getResources(), |
R.color.default_text_color); |
+ mDisabledTextColor = ApiCompatibilityUtils.getColor( |
+ getContext().getResources(), R.color.primary_text_disabled_material_light); |
} |
@Override |
@@ -188,6 +248,10 @@ public class ItemChooserDialog { |
oldItem.mDescription = item.mDescription; |
addToDescriptionsMap(oldItem.mDescription); |
} |
+ if (item.mIcon != null |
+ && (oldItem.mIcon == null || !oldItem.mIcon.equals(item.mIcon))) { |
+ oldItem.mIcon = item.mIcon; |
+ } |
notifyDataSetChanged(); |
return; |
} |
@@ -240,6 +304,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. |
@@ -272,29 +346,35 @@ public class ItemChooserDialog { |
@Override |
public View getView(int position, View convertView, ViewGroup parent) { |
- TextView view; |
- if (convertView instanceof TextView) { |
- view = (TextView) convertView; |
+ RelativeLayout view; |
+ if (convertView instanceof RelativeLayout) { |
+ view = (RelativeLayout) convertView; |
} else { |
- view = (TextView) mInflater.inflate( |
+ view = (RelativeLayout) mInflater.inflate( |
R.layout.item_chooser_dialog_row, parent, false); |
} |
- // Set highlighting for currently selected item. |
- if (position == mSelectedItem) { |
+ boolean selected = position == mSelectedItem; |
+ TextView description = (TextView) view.findViewById(R.id.description); |
+ description.setText(getDisplayText(position)); |
+ |
+ if (selected) { |
view.setBackgroundColor(mBackgroundHighlightColor); |
- view.setTextColor(Color.WHITE); |
+ description.setTextColor(Color.WHITE); |
} else { |
view.setBackground(null); |
- if (!isEnabled(position)) { |
- view.setTextColor(ApiCompatibilityUtils.getColor(getContext().getResources(), |
- R.color.primary_text_disabled_material_light)); |
- } else { |
- view.setTextColor(mDefaultTextColor); |
- } |
+ description.setTextColor( |
+ isEnabled(position) ? mDefaultTextColor : mDisabledTextColor); |
} |
- view.setText(getDisplayText(position)); |
+ LinearLayout imageContainer = (LinearLayout) view.findViewById(R.id.imageContainer); |
+ imageContainer.setVisibility(hasIcon(position) ? View.VISIBLE : View.GONE); |
+ if (hasIcon(position)) { |
+ ImageView icon = (ImageView) view.findViewById(R.id.icon); |
+ ItemChooserRowIcon rowIcon = getIcon(position); |
+ icon.setContentDescription(rowIcon.mIconDescription); |
+ icon.setImageResource(selected ? rowIcon.mSelectedIcon : rowIcon.mNonSelectedIcon); |
+ } |
return view; |
} |