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 3ddfb79821af2d508304674fde109c920c626e65..6bfb361ffdb8a5b51dea867f4866e4a317781eb9 100644 |
--- a/chrome/android/java/src/org/chromium/chrome/browser/ItemChooserDialog.java |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ItemChooserDialog.java |
@@ -10,6 +10,8 @@ import android.content.Context; |
import android.content.DialogInterface; |
import android.graphics.Color; |
import android.graphics.drawable.ColorDrawable; |
+import android.graphics.drawable.Drawable; |
+import android.support.annotation.Nullable; |
import android.text.TextUtils; |
import android.text.method.LinkMovementMethod; |
import android.view.Gravity; |
@@ -21,6 +23,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; |
@@ -59,15 +62,49 @@ public class ItemChooserDialog { |
} |
/** |
+ * A class that contains a Drawable icon, a variant of this icon for when the row |
+ * is selected and a description of this icon. |
+ */ |
+ public static class ItemChooserRowIcon { |
+ private final Drawable mIcon; |
+ private final Drawable mSelected; |
+ private final String mDescription; |
+ |
+ public ItemChooserRowIcon(Drawable icon, Drawable selectedIcon, String iconDescription) { |
+ mIcon = icon; |
+ mSelected = selectedIcon; |
+ mDescription = iconDescription; |
+ } |
+ |
+ /** |
+ * Returns true if |icon1| is equal to |icon2| or all their members are equal. |
+ * |
+ * @param icon1 Icon object to compare. |
+ * @param icon2 Icon object to compare. |
+ */ |
+ public static boolean equals( |
+ @Nullable ItemChooserRowIcon icon1, @Nullable ItemChooserRowIcon icon2) { |
+ if (icon1 == icon2) return true; |
+ if (icon1 == null || icon2 == null) return false; |
+ if (icon1.mIcon != icon2.mIcon) return false; |
+ if (icon1.mSelected != icon2.mSelected) return false; |
+ if (!TextUtils.equals(icon1.mDescription, icon2.mDescription)) return false; |
+ return true; |
+ } |
+ } |
+ |
+ /** |
* 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, @Nullable ItemChooserRowIcon icon) { |
mKey = key; |
mDescription = description; |
+ mIcon = icon; |
} |
/** |
@@ -75,12 +112,25 @@ public class ItemChooserDialog { |
* |
* @param key Expected item unique identifier. |
* @param description Expected item description. |
+ * @param icon Expected item icon. |
*/ |
- public boolean hasSameContents(String key, String description) { |
+ public boolean hasSameContents( |
+ String key, String description, @Nullable ItemChooserRowIcon icon) { |
if (!TextUtils.equals(mKey, key)) return false; |
if (!TextUtils.equals(mDescription, description)) return false; |
+ if (!ItemChooserRowIcon.equals(mIcon, icon)) return false; |
return true; |
} |
+ |
+ /** |
+ * Returns true if all parameters match the corresponding member. |
+ * |
+ * @param key Expected item unique identifier. |
+ * @param description Expected item description. |
+ */ |
+ public boolean hasSameContents(String key, String description) { |
+ return hasSameContents(key, description, null /* icon */); |
+ } |
} |
/** |
@@ -123,8 +173,10 @@ public class ItemChooserDialog { |
*/ |
private static class ViewHolder { |
private TextView mTextView; |
+ private ImageView mImageView; |
public ViewHolder(View view) { |
+ mImageView = (ImageView) view.findViewById(R.id.icon); |
mTextView = (TextView) view.findViewById(R.id.description); |
} |
} |
@@ -154,6 +206,9 @@ public class ItemChooserDialog { |
// Map of keys to items so that we can access the items in O(1). |
private Map<String, ItemChooserRow> mKeyToItemMap = new HashMap<>(); |
+ // Set of keys to items that have icons. |
+ private Set<String> mItemsWithIcons = new HashSet<String>(); |
+ |
public ItemAdapter(Context context, int resource) { |
super(context, resource); |
@@ -167,6 +222,7 @@ public class ItemChooserDialog { |
assert mKeyToItemMap.isEmpty(); |
assert mDisabledEntries.isEmpty(); |
assert mItemDescriptionMap.isEmpty(); |
+ assert mItemsWithIcons.isEmpty(); |
} else { |
assert !mKeyToItemMap.isEmpty(); |
assert !mItemDescriptionMap.isEmpty(); |
@@ -174,10 +230,10 @@ public class ItemChooserDialog { |
return isEmpty; |
} |
- public void addOrUpdate(String key, String description) { |
+ public void addOrUpdate(String key, String description, @Nullable ItemChooserRowIcon icon) { |
ItemChooserRow oldItem = mKeyToItemMap.get(key); |
if (oldItem != null) { |
- if (oldItem.hasSameContents(key, description)) { |
+ if (oldItem.hasSameContents(key, description, icon)) { |
// No need to update anything. |
return; |
} |
@@ -188,15 +244,21 @@ public class ItemChooserDialog { |
addToDescriptionsMap(oldItem.mDescription); |
} |
+ if (!ItemChooserRowIcon.equals(icon, oldItem.mIcon)) { |
+ oldItem.mIcon = icon; |
+ updateItemsWithIconsSet(oldItem.mKey, oldItem.mIcon); |
+ } |
+ |
notifyDataSetChanged(); |
return; |
} |
assert !mKeyToItemMap.containsKey(key); |
- ItemChooserRow newItem = new ItemChooserRow(key, description); |
+ ItemChooserRow newItem = new ItemChooserRow(key, description, icon); |
mKeyToItemMap.put(key, newItem); |
addToDescriptionsMap(newItem.mDescription); |
+ updateItemsWithIconsSet(key, newItem.mIcon); |
add(newItem); |
} |
@@ -215,6 +277,7 @@ public class ItemChooserDialog { |
--mSelectedItem; |
} |
removeFromDescriptionsMap(oldItem.mDescription); |
+ updateItemsWithIconsSet(key, null /* newIcon */); |
super.remove(oldItem); |
} |
@@ -239,6 +302,10 @@ public class ItemChooserDialog { |
return row.mKey; |
} |
+ private boolean isSelected(int position) { |
+ return mSelectedItem == position; |
+ } |
+ |
/** |
* Returns the text to be displayed on the chooser for an item. For items with the same |
* description, their unique keys are appended to distinguish them. |
@@ -305,6 +372,24 @@ public class ItemChooserDialog { |
row.mTextView.setEnabled(isEnabled(position)); |
row.mTextView.setText(getDisplayText(position)); |
+ // If there is at least one item with an icon then we set mImageView's |
+ // visibility to INVISIBLE for all items with no icons. We do this |
+ // so that all items' desriptions are aligned. |
+ if (mItemsWithIcons.isEmpty()) { |
+ row.mImageView.setVisibility(View.GONE); |
+ } else { |
+ ItemChooserRowIcon icon = getItem(position).mIcon; |
+ if (icon != null) { |
+ row.mImageView.setContentDescription(icon.mDescription); |
+ row.mImageView.setImageDrawable( |
+ isSelected(position) ? icon.mSelected : icon.mIcon); |
+ row.mImageView.setVisibility(View.VISIBLE); |
+ } else { |
+ row.mImageView.setVisibility(View.INVISIBLE); |
+ row.mImageView.setImageDrawable(null); |
+ row.mImageView.setContentDescription(null); |
+ } |
+ } |
return convertView; |
} |
@@ -315,6 +400,14 @@ public class ItemChooserDialog { |
notifyDataSetChanged(); |
} |
+ private void updateItemsWithIconsSet(String key, @Nullable ItemChooserRowIcon newIcon) { |
+ if (newIcon == null) { |
+ mItemsWithIcons.remove(key); |
+ } else { |
+ mItemsWithIcons.add(key); |
+ } |
+ } |
+ |
private void addToDescriptionsMap(String description) { |
int count = mItemDescriptionMap.containsKey(description) |
? mItemDescriptionMap.get(description) |
@@ -484,8 +577,20 @@ public class ItemChooserDialog { |
* @param description Text in the row. |
*/ |
public void addOrUpdateItem(String key, String description) { |
+ addOrUpdateItem(key, description, null /* icon */); |
+ } |
+ |
+ /** |
+ * Adds an item to the end of the list to show in the dialog if the item |
+ * was not in the chooser. Otherwise updates the items description or icon. |
+ * |
+ * @param key Unique identifier for that item. |
+ * @param description Text in the row. |
+ * @param icon Icon for the row. |
+ */ |
+ public void addOrUpdateItem(String key, String description, @Nullable ItemChooserRowIcon icon) { |
mProgressBar.setVisibility(View.GONE); |
- mItemAdapter.addOrUpdate(key, description); |
+ mItemAdapter.addOrUpdate(key, description, icon); |
setState(State.PROGRESS_UPDATE_AVAILABLE); |
} |