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 b71813ac54578667f7a1eb69654fe357ffb28b7a..7f301c758231612cdfe88ab4dd1083557515f180 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,7 @@ import android.content.Context; |
| import android.content.DialogInterface; |
| import android.graphics.Color; |
| import android.graphics.drawable.ColorDrawable; |
| +import android.graphics.drawable.Drawable; |
| import android.text.SpannableString; |
| import android.text.TextUtils; |
| import android.text.method.LinkMovementMethod; |
| @@ -22,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; |
| @@ -65,10 +67,15 @@ public class ItemChooserDialog { |
| public static class ItemChooserRow { |
| private final String mKey; |
| private String mDescription; |
| + private Drawable mIcon; |
| + private String mIconDescription; |
| - public ItemChooserRow(String key, String description) { |
| + public ItemChooserRow( |
| + String key, String description, Drawable icon, String iconDescription) { |
| mKey = key; |
| mDescription = description; |
| + mIcon = icon; |
| + mIconDescription = iconDescription; |
| } |
| /** |
| @@ -78,8 +85,29 @@ public class ItemChooserDialog { |
| * @param description Expected item description. |
| */ |
| public boolean hasSameContents(String key, String description) { |
| + return hasSameContents(key, description, null, null); |
| + } |
| + |
| + /** |
| + * Returns true if all parameters match the corresponding member. |
| + * |
| + * @param key Expected item unique identifier. |
| + * @param description Expected item description. |
| + * @param icon Expected icon. |
| + * @param iconDescription Expected icon description. |
| + */ |
| + public boolean hasSameContents( |
| + String key, String description, Drawable icon, String iconDescription) { |
| if (!TextUtils.equals(mKey, key)) return false; |
| if (!TextUtils.equals(mDescription, description)) return false; |
| + |
| + if (mIcon == null ^ icon == null) return false; |
| + if (mIcon != null && mIcon.getConstantState().equals(icon.getConstantState())) { |
| + return false; |
| + } |
| + |
| + if (!TextUtils.equals(mIconDescription, iconDescription)) return false; |
| + |
| return true; |
| } |
| } |
| @@ -124,8 +152,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); |
| } |
| } |
| @@ -149,6 +179,8 @@ public class ItemChooserDialog { |
| // A set of keys that are marked as disabled in the dialog. |
| private Set<String> mDisabledEntries = new HashSet<String>(); |
| + private Set<String> mItemsWithIcons = new HashSet<String>(); |
| + |
| // Item descriptions are counted in a map. |
| private Map<String, Integer> mItemDescriptionMap = new HashMap<>(); |
| @@ -168,6 +200,7 @@ public class ItemChooserDialog { |
| assert mKeyToItemMap.isEmpty(); |
| assert mDisabledEntries.isEmpty(); |
| assert mItemDescriptionMap.isEmpty(); |
| + assert mItemsWithIcons.isEmpty(); |
| } else { |
| assert !mKeyToItemMap.isEmpty(); |
| assert !mItemDescriptionMap.isEmpty(); |
| @@ -175,10 +208,13 @@ public class ItemChooserDialog { |
| return isEmpty; |
| } |
| - public void addOrUpdate(String key, String description) { |
| + public void addOrUpdate( |
| + String key, String description, Drawable icon, String iconDescription) { |
|
Ian Wen
2016/10/26 21:53:32
Nit: add @nullable before icon and iconDescription
|
| + if (icon != null) mItemsWithIcons.add(key); |
| + |
| ItemChooserRow oldItem = mKeyToItemMap.get(key); |
| if (oldItem != null) { |
| - if (oldItem.hasSameContents(key, description)) { |
| + if (oldItem.hasSameContents(key, description, icon, iconDescription)) { |
| // No need to update anything. |
| return; |
| } |
| @@ -189,12 +225,19 @@ public class ItemChooserDialog { |
| addToDescriptionsMap(oldItem.mDescription); |
| } |
| + if (icon != null && (oldItem.mIcon == null |
|
Ian Wen
2016/10/26 21:53:32
I feel like this equality check is not necessary.
|
| + || !oldItem.mIcon.getConstantState().equals( |
| + icon.getConstantState()))) { |
| + oldItem.mIcon = icon; |
| + oldItem.mIconDescription = iconDescription; |
| + } |
| + |
| notifyDataSetChanged(); |
| return; |
| } |
| assert !mKeyToItemMap.containsKey(key); |
| - ItemChooserRow newItem = new ItemChooserRow(key, description); |
| + ItemChooserRow newItem = new ItemChooserRow(key, description, icon, iconDescription); |
| mKeyToItemMap.put(key, newItem); |
| addToDescriptionsMap(newItem.mDescription); |
| @@ -202,6 +245,7 @@ public class ItemChooserDialog { |
| } |
| public void removeItemWithKey(String key) { |
| + mItemsWithIcons.remove(key); |
| ItemChooserRow oldItem = mKeyToItemMap.remove(key); |
| if (oldItem == null) return; |
| int oldItemPosition = getPosition(oldItem); |
| @@ -306,6 +350,15 @@ public class ItemChooserDialog { |
| row.mTextView.setEnabled(isEnabled(position)); |
| row.mTextView.setText(getDisplayText(position)); |
| + if (mItemsWithIcons.isEmpty()) { |
|
Ian Wen
2016/10/26 21:53:32
It seems this HashSet is only used here and it's u
|
| + row.mImageView.setVisibility(View.GONE); |
| + } else { |
| + Drawable d = getItem(position).mIcon; |
| + row.mImageView.setVisibility(d == null ? View.INVISIBLE : View.VISIBLE); |
| + row.mImageView.setImageDrawable(d); |
| + row.mImageView.setContentDescription(getItem(position).mIconDescription); |
| + } |
| + |
| return convertView; |
| } |
| @@ -485,8 +538,13 @@ public class ItemChooserDialog { |
| * @param description Text in the row. |
| */ |
| public void addOrUpdateItem(String key, String description) { |
| + addOrUpdateItem(key, description, null, null); |
| + } |
| + |
| + public void addOrUpdateItem( |
|
Ian Wen
2016/10/26 21:53:32
Javadoc plz. :)
|
| + String key, String description, Drawable icon, String iconDescription) { |
| mProgressBar.setVisibility(View.GONE); |
| - mItemAdapter.addOrUpdate(key, description); |
| + mItemAdapter.addOrUpdate(key, description, icon, iconDescription); |
| setState(State.PROGRESS_UPDATE_AVAILABLE); |
| } |