Commit 6a7671ea authored by juncai's avatar juncai Committed by Commit bot

Distinguish devices with same name in chooser on Android

For devices with different names, the chooser just shows their names;
for devices with the same name, the chooser shows their name with
its unique device identifier.

So the chooser will look like:
device_different_name
device_same_name (device_1)
device_same_name (device_2)
device_same_name (device_3)

BUG=544121

Review-Url: https://codereview.chromium.org/2098983002
Cr-Commit-Position: refs/heads/master@{#403295}
parent d0cf15e9
...@@ -33,7 +33,9 @@ import org.chromium.chrome.browser.util.MathUtils; ...@@ -33,7 +33,9 @@ import org.chromium.chrome.browser.util.MathUtils;
import org.chromium.ui.base.DeviceFormFactor; import org.chromium.ui.base.DeviceFormFactor;
import org.chromium.ui.widget.TextViewWithClickableSpans; import org.chromium.ui.widget.TextViewWithClickableSpans;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;
/** /**
...@@ -119,7 +121,7 @@ public class ItemChooserDialog { ...@@ -119,7 +121,7 @@ public class ItemChooserDialog {
/** /**
* An adapter for keeping track of which items to show in the dialog. * An adapter for keeping track of which items to show in the dialog.
*/ */
private class ItemAdapter extends ArrayAdapter<ItemChooserRow> public class ItemAdapter extends ArrayAdapter<ItemChooserRow>
implements AdapterView.OnItemClickListener { implements AdapterView.OnItemClickListener {
private final LayoutInflater mInflater; private final LayoutInflater mInflater;
...@@ -136,6 +138,9 @@ public class ItemChooserDialog { ...@@ -136,6 +138,9 @@ public class ItemChooserDialog {
// A set of keys that are marked as disabled in the dialog. // A set of keys that are marked as disabled in the dialog.
private Set<String> mDisabledEntries = new HashSet<String>(); private Set<String> mDisabledEntries = new HashSet<String>();
// Item descriptions are counted in a map.
private Map<String, Integer> mItemDescriptionMap = new HashMap<>();
public ItemAdapter(Context context, int resource) { public ItemAdapter(Context context, int resource) {
super(context, resource); super(context, resource);
...@@ -147,6 +152,29 @@ public class ItemChooserDialog { ...@@ -147,6 +152,29 @@ public class ItemChooserDialog {
R.color.default_text_color); R.color.default_text_color);
} }
@Override
public void add(ItemChooserRow item) {
String description = item.mDescription;
int count = mItemDescriptionMap.containsKey(description)
? mItemDescriptionMap.get(description) : 0;
mItemDescriptionMap.put(description, count + 1);
super.add(item);
}
@Override
public void remove(ItemChooserRow item) {
String description = item.mDescription;
if (mItemDescriptionMap.containsKey(description)) {
int count = mItemDescriptionMap.get(description);
if (count == 1) {
mItemDescriptionMap.remove(description);
} else {
mItemDescriptionMap.put(description, count - 1);
}
}
super.remove(item);
}
@Override @Override
public void clear() { public void clear() {
mSelectedItem = ListView.INVALID_POSITION; mSelectedItem = ListView.INVALID_POSITION;
...@@ -164,6 +192,20 @@ public class ItemChooserDialog { ...@@ -164,6 +192,20 @@ public class ItemChooserDialog {
return row.mKey; return row.mKey;
} }
/**
* 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.
* @param position The index of the item.
*/
public String getDisplayText(int position) {
ItemChooserRow item = getItem(position);
String description = item.mDescription;
int counter = mItemDescriptionMap.get(description);
return counter == 1 ? description
: mActivity.getString(R.string.item_chooser_item_name_with_id, description,
item.mKey);
}
/** /**
* Sets whether the itam is enabled. Disabled items are grayed out. * Sets whether the itam is enabled. Disabled items are grayed out.
* @param id The id of the item to affect. * @param id The id of the item to affect.
...@@ -218,8 +260,7 @@ public class ItemChooserDialog { ...@@ -218,8 +260,7 @@ public class ItemChooserDialog {
} }
} }
ItemChooserRow item = getItem(position); view.setText(getDisplayText(position));
view.setText(item.mDescription);
return view; return view;
} }
......
...@@ -2694,6 +2694,11 @@ You can control the Physical Web in Chrome Settings. ...@@ -2694,6 +2694,11 @@ You can control the Physical Web in Chrome Settings.
<message name="IDS_TAB_SWITCHER_CALLOUT_BODY" desc="Indicates that clicking the tab switcher button gives you quick access to your tabs."> <message name="IDS_TAB_SWITCHER_CALLOUT_BODY" desc="Indicates that clicking the tab switcher button gives you quick access to your tabs.">
Tap this button for quick access to your tabs. Tap this button for quick access to your tabs.
</message> </message>
<!-- Item Chooser UI strings -->
<message name="IDS_ITEM_CHOOSER_ITEM_NAME_WITH_ID" desc="To distinguish items with the same name, the item chooser shows the item name with id.">
<ph name="ITEM_NAME">%1$s<ex>item_name</ex></ph> (<ph name="ITEM_ID">%2$s<ex>item id</ex></ph>)
</message>
</messages> </messages>
</release> </release>
</grit> </grit>
...@@ -261,6 +261,64 @@ public class ItemChooserDialogTest extends ChromeActivityTestCaseBase<ChromeActi ...@@ -261,6 +261,64 @@ public class ItemChooserDialogTest extends ChromeActivityTestCaseBase<ChromeActi
mChooserDialog.dismiss(); mChooserDialog.dismiss();
} }
@SmallTest
public void testAddItemWithSameNameToListAndRemoveItemFromList() throws InterruptedException {
Dialog dialog = mChooserDialog.getDialogForTesting();
assertTrue(dialog.isShowing());
ItemChooserDialog.ItemAdapter itemAdapter = mChooserDialog.getItemAdapterForTesting();
// Add item 1.
ItemChooserDialog.ItemChooserRow item1 =
new ItemChooserDialog.ItemChooserRow("device_id_1", "same_device_name");
mChooserDialog.addItemToList(item1);
assertEquals(1, itemAdapter.getCount());
assertEquals(itemAdapter.getItem(0), item1);
// Add item 2.
ItemChooserDialog.ItemChooserRow item2 =
new ItemChooserDialog.ItemChooserRow("device_id_2", "different_device_name");
mChooserDialog.addItemToList(item2);
assertEquals(2, itemAdapter.getCount());
assertEquals(itemAdapter.getItem(0), item1);
assertEquals(itemAdapter.getItem(1), item2);
// Add item 3.
ItemChooserDialog.ItemChooserRow item3 =
new ItemChooserDialog.ItemChooserRow("device_id_3", "same_device_name");
mChooserDialog.addItemToList(item3);
assertEquals(3, itemAdapter.getCount());
assertEquals(itemAdapter.getItem(0), item1);
assertEquals(itemAdapter.getItem(1), item2);
assertEquals(itemAdapter.getItem(2), item3);
// Since two items have the same name, their display text should have their unique
// keys appended.
assertEquals("same_device_name (device_id_1)", itemAdapter.getDisplayText(0));
assertEquals("different_device_name", itemAdapter.getDisplayText(1));
assertEquals("same_device_name (device_id_3)", itemAdapter.getDisplayText(2));
// Remove item 2.
mChooserDialog.removeItemFromList(item2);
assertEquals(2, itemAdapter.getCount());
// Make sure the remaining items are item 1 and item 3.
assertEquals(itemAdapter.getItem(0), item1);
assertEquals(itemAdapter.getItem(1), item3);
assertEquals("same_device_name (device_id_1)", itemAdapter.getDisplayText(0));
assertEquals("same_device_name (device_id_3)", itemAdapter.getDisplayText(1));
// Remove item 1.
mChooserDialog.removeItemFromList(item1);
assertEquals(1, itemAdapter.getCount());
// Make sure the remaining item is item 3.
assertEquals(itemAdapter.getItem(0), item3);
// After removing item 1, item 3 is the only remaining item, so its display text
// also changed to its original description.
assertEquals("same_device_name", itemAdapter.getDisplayText(0));
mChooserDialog.dismiss();
}
@SmallTest @SmallTest
public void testListHeight() throws InterruptedException { public void testListHeight() throws InterruptedException {
// 500 * .3 is 150, which is 48 * 3.125. 48 * 3.5 is 168. // 500 * .3 is 150, which is 48 * 3.125. 48 * 3.5 is 168.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment