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 50de53333e99111ccdac0bd2f397b8138da8ea76..b91457a1af355481e8daf7800b025b87c17ba3c1 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.method.LinkMovementMethod; |
| import android.view.Gravity; |
| @@ -21,11 +22,14 @@ 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.Log; |
| import org.chromium.base.ApiCompatibilityUtils; |
| import org.chromium.base.VisibleForTesting; |
| import org.chromium.chrome.R; |
| @@ -65,10 +69,23 @@ 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; |
| + } |
| + |
| + public ItemChooserRow(String key, String description) { |
| + this(key, description, null, null); |
| + } |
| + |
| + public Drawable getIcon() { |
| + return mIcon; |
| } |
| @Override |
| @@ -76,12 +93,32 @@ 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.getConstantState().equals(item.mIcon.getConstantState())) |
| + return false; |
| + |
| + if (mIconDescription == null ^ item.mIconDescription == null) return false; |
| + |
| + if (mIconDescription != null && mIconDescription.equals(item.mIconDescription)) { |
| + 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 +175,12 @@ public class ItemChooserDialog { |
| // The color of the non-highlighted text. |
| private final int mDefaultTextColor; |
| + // The color of disabled text. |
| + private final int mDisabledTextColor; |
| + |
| + // Indicates whether rows will contain an icon. |
| + private boolean mUsingIcon; |
| + |
| // 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; |
| @@ -151,15 +194,19 @@ 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<>(); |
| - public ItemAdapter(Context context, int resource) { |
| + public ItemAdapter(Context context, int resource, boolean usingIcon) { |
| super(context, resource); |
| mInflater = LayoutInflater.from(context); |
| + mUsingIcon = usingIcon; |
| + |
| mBackgroundHighlightColor = ApiCompatibilityUtils.getColor(getContext().getResources(), |
| 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 |
| @@ -176,18 +223,27 @@ public class ItemChooserDialog { |
| return isEmpty; |
| } |
| - public void addOrUpdate(ItemChooserRow item) { |
| + public void addOrUpdate(ItemChooserRow item, boolean didUpdateOldDrawable) { |
|
Ian Wen
2016/10/14 01:39:51
I see many equal checks here and above. Which lead
|
| ItemChooserRow oldItem = mKeyToItemMap.get(item.mKey); |
| + |
| if (oldItem != null) { |
| - if (oldItem.equals(item)) { |
| + if (oldItem.equals(item) && !didUpdateOldDrawable) { |
| // No need to update anything. |
| return; |
| } |
| + |
| if (!oldItem.mDescription.equals(item.mDescription)) { |
| removeFromDescriptionsMap(oldItem.mDescription); |
| oldItem.mDescription = item.mDescription; |
| addToDescriptionsMap(oldItem.mDescription); |
| } |
| + |
| + if (item.mIcon != null && (oldItem.mIcon == null |
| + || !item.mIcon.getConstantState().equals( |
| + oldItem.mIcon.getConstantState()))) { |
| + oldItem.mIcon = item.mIcon; |
| + } |
| + |
| notifyDataSetChanged(); |
| return; |
| } |
| @@ -198,6 +254,10 @@ public class ItemChooserDialog { |
| add(item); |
| } |
| + public ItemChooserRow getItemChooserRow(String key) { |
| + return mKeyToItemMap.get(key); |
| + } |
| + |
| @Override |
| public void remove(ItemChooserRow item) { |
| ItemChooserRow oldItem = mKeyToItemMap.remove(item.mKey); |
| @@ -292,29 +352,46 @@ 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) { |
|
Ian Wen
2016/10/14 01:39:51
Can the view be of a different type? If not I woul
|
| + view = (RelativeLayout) convertView; |
| } else { |
| - view = (TextView) mInflater.inflate( |
| + view = (RelativeLayout) mInflater.inflate( |
| R.layout.item_chooser_dialog_row, parent, false); |
| + View icon = view.findViewById(R.id.icon); |
| + if (mUsingIcon) { |
|
Ian Wen
2016/10/14 01:39:51
Instead of using this boolean, shall we grab the c
|
| + icon.setVisibility(View.VISIBLE); |
| + } else { |
| + icon.setVisibility(View.GONE); |
| + } |
| } |
| - // 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) { |
|
Ian Wen
2016/10/14 01:39:51
The color manipulation could be simplified by colo
|
| 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); |
| + } |
| + |
| + if (mUsingIcon) { |
| + ImageView icon = (ImageView) view.findViewById(R.id.icon); |
| + Drawable d = getItem(position).mIcon; |
| + icon.setVisibility(d == null ? View.INVISIBLE : View.VISIBLE); |
| + if (d != null) { |
| + icon.setImageLevel(d.getLevel()); |
| } |
| + icon.setImageDrawable(d); |
| + icon.setSelected(selected); |
| + icon.invalidateDrawable(d); |
| + icon.setContentDescription(getItem(position).mIconDescription); |
| } |
| - view.setText(getDisplayText(position)); |
| return view; |
| } |
| @@ -383,8 +460,8 @@ public class ItemChooserDialog { |
| * @param callback The callback used to communicate back what was selected. |
| * @param labels The labels to show in the dialog. |
| */ |
| - public ItemChooserDialog( |
| - Activity activity, ItemSelectedCallback callback, ItemChooserLabels labels) { |
| + public ItemChooserDialog(Activity activity, ItemSelectedCallback callback, |
| + ItemChooserLabels labels, boolean usingIcon) { |
| mActivity = activity; |
| mItemSelectedCallback = callback; |
| mLabels = labels; |
| @@ -418,7 +495,7 @@ public class ItemChooserDialog { |
| } |
| }); |
| - mItemAdapter = new ItemAdapter(mActivity, R.layout.item_chooser_dialog_row); |
| + mItemAdapter = new ItemAdapter(mActivity, R.layout.item_chooser_dialog_row, usingIcon); |
| mItemAdapter.setNotifyOnChange(true); |
| mListView.setAdapter(mItemAdapter); |
| mListView.setEmptyView(mEmptyMessage); |
| @@ -492,11 +569,19 @@ public class ItemChooserDialog { |
| * @param item The item to be added to the end of the chooser or updated. |
| */ |
| public void addOrUpdateItem(ItemChooserRow item) { |
| + addOrUpdateItem(item, false /* didUpdateOldDrawable */); |
|
Ian Wen
2016/10/14 01:39:51
I would remove this comment.
|
| + } |
| + |
| + public void addOrUpdateItem(ItemChooserRow item, boolean didUpdateOldDrawable) { |
| mProgressBar.setVisibility(View.GONE); |
| - mItemAdapter.addOrUpdate(item); |
| + mItemAdapter.addOrUpdate(item, didUpdateOldDrawable); |
| setState(State.PROGRESS_UPDATE_AVAILABLE); |
| } |
| + public ItemChooserRow getItemChooserRow(String key) { |
| + return mItemAdapter.getItemChooserRow(key); |
| + } |
| + |
| /** |
| * Remove an item that is shown in the dialog. |
| * |