Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(357)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/ItemChooserDialog.java

Issue 2704263004: bluetooth: Add connected icon to Bluetooth Chooser on Android (Closed)
Patch Set: Address tedchoc's comments Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 a0f3ebdae3286ce1920f9d4a36aaacd22d30e230..96a9e8cff6cdcd17770810545cd668a6cdef6e59 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,11 +23,13 @@ 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.TextView;
+import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.VisibleForTesting;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.util.MathUtils;
@@ -64,10 +68,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, @Nullable Drawable icon,
+ @Nullable String iconDescription) {
mKey = key;
mDescription = description;
+ mIcon = icon;
+ mIconDescription = iconDescription;
}
/**
@@ -75,10 +84,14 @@ 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 Drawable icon,
+ @Nullable String iconDescription) {
if (!TextUtils.equals(mKey, key)) return false;
if (!TextUtils.equals(mDescription, description)) return false;
+ if (!ApiCompatibilityUtils.objectEquals(mIcon, icon)) return false;
+ if (!TextUtils.equals(mIconDescription, iconDescription)) return false;
return true;
}
}
@@ -123,8 +136,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 +169,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<>();
+ // True when there is at least one row with an icon.
+ private boolean mHasIcon;
+
public ItemAdapter(Context context, int resource) {
super(context, resource);
@@ -174,10 +192,20 @@ public class ItemChooserDialog {
return isEmpty;
}
- public void addOrUpdate(String key, String description) {
+ /**
+ * Adds an item to the list to show in the dialog if the item
+ * was not in the chooser. Otherwise updates the items description, icon
+ * and icon description.
+ * @param key Unique identifier for that item.
+ * @param description Text in the row.
+ * @param icon Drawable to show next to the item.
+ * @param iconDescription Description of the icon.
+ */
+ public void addOrUpdate(String key, String description, @Nullable Drawable icon,
+ @Nullable String iconDescription) {
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;
}
@@ -188,12 +216,17 @@ public class ItemChooserDialog {
addToDescriptionsMap(oldItem.mDescription);
}
+ if (!ApiCompatibilityUtils.objectEquals(icon, oldItem.mIcon)) {
+ 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);
@@ -307,10 +340,37 @@ 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 (!mHasIcon) {
+ row.mImageView.setVisibility(View.GONE);
+ } else {
+ ItemChooserRow item = getItem(position);
+ if (item.mIcon != null) {
+ row.mImageView.setContentDescription(item.mIconDescription);
+ row.mImageView.setImageDrawable(item.mIcon);
+ row.mImageView.setVisibility(View.VISIBLE);
+ } else {
+ row.mImageView.setVisibility(View.INVISIBLE);
+ row.mImageView.setImageDrawable(null);
+ row.mImageView.setContentDescription(null);
+ }
+ row.mImageView.setSelected(position == mSelectedItem);
+ }
return convertView;
}
@Override
+ public void notifyDataSetChanged() {
+ mHasIcon = false;
+ for (ItemChooserRow row : mKeyToItemMap.values()) {
+ if (row.mIcon != null) mHasIcon = true;
+ }
+ super.notifyDataSetChanged();
+ }
+
+ @Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
mSelectedItem = position;
mConfirmButton.setEnabled(true);
@@ -504,8 +564,24 @@ public class ItemChooserDialog {
* @param description Text in the row.
*/
public void addOrUpdateItem(String key, String description) {
+ addOrUpdateItem(key, description, null /* icon */, null /* iconDescription */);
+ }
+
+ /**
+ * 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.
+ * Note that as long as at least one item has an icon all rows will be inset
+ * with the icon dimensions.
+ *
+ * @param key Unique identifier for that item.
+ * @param description Text in the row.
+ * @param icon Drawable to show left of the description.
+ * @param iconDescription Description of the icon.
+ */
+ public void addOrUpdateItem(String key, String description, @Nullable Drawable icon,
+ @Nullable String iconDescription) {
mProgressBar.setVisibility(View.GONE);
- mItemAdapter.addOrUpdate(key, description);
+ mItemAdapter.addOrUpdate(key, description, icon, iconDescription);
setState(State.PROGRESS_UPDATE_AVAILABLE);
}

Powered by Google App Engine
This is Rietveld 408576698