OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser; | 5 package org.chromium.chrome.browser; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
8 import android.app.Dialog; | 8 import android.app.Dialog; |
9 import android.content.Context; | 9 import android.content.Context; |
10 import android.content.DialogInterface; | 10 import android.content.DialogInterface; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 * without selecting anything. | 57 * without selecting anything. |
58 */ | 58 */ |
59 void onItemSelected(String id); | 59 void onItemSelected(String id); |
60 } | 60 } |
61 | 61 |
62 /** | 62 /** |
63 * A class representing one data row in the picker. | 63 * A class representing one data row in the picker. |
64 */ | 64 */ |
65 public static class ItemChooserRow { | 65 public static class ItemChooserRow { |
66 private final String mKey; | 66 private final String mKey; |
67 private final String mDescription; | 67 private String mDescription; |
68 | 68 |
69 public ItemChooserRow(String key, String description) { | 69 public ItemChooserRow(String key, String description) { |
70 mKey = key; | 70 mKey = key; |
71 mDescription = description; | 71 mDescription = description; |
72 } | 72 } |
73 | 73 |
74 @Override | 74 @Override |
75 public boolean equals(Object obj) { | 75 public boolean equals(Object obj) { |
76 if (!(obj instanceof ItemChooserRow)) return false; | 76 if (!(obj instanceof ItemChooserRow)) return false; |
77 if (this == obj) return true; | 77 if (this == obj) return true; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 // The zero-based index of the item currently selected in the dialog, | 141 // The zero-based index of the item currently selected in the dialog, |
142 // or -1 (INVALID_POSITION) if nothing is selected. | 142 // or -1 (INVALID_POSITION) if nothing is selected. |
143 private int mSelectedItem = ListView.INVALID_POSITION; | 143 private int mSelectedItem = ListView.INVALID_POSITION; |
144 | 144 |
145 // A set of keys that are marked as disabled in the dialog. | 145 // A set of keys that are marked as disabled in the dialog. |
146 private Set<String> mDisabledEntries = new HashSet<String>(); | 146 private Set<String> mDisabledEntries = new HashSet<String>(); |
147 | 147 |
148 // Item descriptions are counted in a map. | 148 // Item descriptions are counted in a map. |
149 private Map<String, Integer> mItemDescriptionMap = new HashMap<>(); | 149 private Map<String, Integer> mItemDescriptionMap = new HashMap<>(); |
150 | 150 |
| 151 // Map of keys to items so that we can access the items in O(1). |
| 152 private Map<String, ItemChooserRow> mKeyToItemMap = new HashMap<>(); |
| 153 |
151 public ItemAdapter(Context context, int resource) { | 154 public ItemAdapter(Context context, int resource) { |
152 super(context, resource); | 155 super(context, resource); |
153 | 156 |
154 mInflater = LayoutInflater.from(context); | 157 mInflater = LayoutInflater.from(context); |
155 | 158 |
156 mBackgroundHighlightColor = ApiCompatibilityUtils.getColor(getContex
t().getResources(), | 159 mBackgroundHighlightColor = ApiCompatibilityUtils.getColor(getContex
t().getResources(), |
157 R.color.light_active_color); | 160 R.color.light_active_color); |
158 mDefaultTextColor = ApiCompatibilityUtils.getColor(getContext().getR
esources(), | 161 mDefaultTextColor = ApiCompatibilityUtils.getColor(getContext().getR
esources(), |
159 R.color.default_text_color); | 162 R.color.default_text_color); |
160 } | 163 } |
161 | 164 |
162 @Override | 165 public void addOrUpdate(ItemChooserRow item) { |
163 public void add(ItemChooserRow item) { | 166 if (mKeyToItemMap.containsKey(item.mKey)) { |
| 167 // TODO(ortuno): Update description. |
| 168 // https://crbug.com/634366 |
| 169 return; |
| 170 } |
| 171 ItemChooserRow result = mKeyToItemMap.put(item.mKey, item); |
| 172 assert result == null; |
| 173 |
164 String description = item.mDescription; | 174 String description = item.mDescription; |
165 int count = mItemDescriptionMap.containsKey(description) | 175 int count = mItemDescriptionMap.containsKey(description) |
166 ? mItemDescriptionMap.get(description) : 0; | 176 ? mItemDescriptionMap.get(description) : 0; |
167 mItemDescriptionMap.put(description, count + 1); | 177 mItemDescriptionMap.put(description, count + 1); |
168 super.add(item); | 178 add(item); |
169 } | 179 } |
170 | 180 |
171 @Override | 181 @Override |
172 public void remove(ItemChooserRow item) { | 182 public void remove(ItemChooserRow item) { |
| 183 mKeyToItemMap.remove(item.mKey); |
173 String description = item.mDescription; | 184 String description = item.mDescription; |
174 if (mItemDescriptionMap.containsKey(description)) { | 185 if (mItemDescriptionMap.containsKey(description)) { |
175 int count = mItemDescriptionMap.get(description); | 186 int count = mItemDescriptionMap.get(description); |
176 if (count == 1) { | 187 if (count == 1) { |
177 mItemDescriptionMap.remove(description); | 188 mItemDescriptionMap.remove(description); |
178 } else { | 189 } else { |
179 mItemDescriptionMap.put(description, count - 1); | 190 mItemDescriptionMap.put(description, count - 1); |
180 } | 191 } |
181 } | 192 } |
182 super.remove(item); | 193 super.remove(item); |
183 } | 194 } |
184 | 195 |
185 @Override | 196 @Override |
186 public void clear() { | 197 public void clear() { |
187 mSelectedItem = ListView.INVALID_POSITION; | 198 mSelectedItem = ListView.INVALID_POSITION; |
| 199 mKeyToItemMap.clear(); |
188 mDisabledEntries.clear(); | 200 mDisabledEntries.clear(); |
189 mItemDescriptionMap.clear(); | 201 mItemDescriptionMap.clear(); |
190 mConfirmButton.setEnabled(false); | 202 mConfirmButton.setEnabled(false); |
191 super.clear(); | 203 super.clear(); |
192 } | 204 } |
193 | 205 |
194 /** | 206 /** |
195 * Returns the key of the currently selected item or blank if nothing is | 207 * Returns the key of the currently selected item or blank if nothing is |
196 * selected. | 208 * selected. |
197 */ | 209 */ |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 } | 419 } |
408 | 420 |
409 mDialog.show(); | 421 mDialog.show(); |
410 } | 422 } |
411 | 423 |
412 public void dismiss() { | 424 public void dismiss() { |
413 mDialog.dismiss(); | 425 mDialog.dismiss(); |
414 } | 426 } |
415 | 427 |
416 /** | 428 /** |
417 * Add an item to the end of the list to show in the dialog. | 429 * Add an item to the end of the list to show in the dialog if the item |
| 430 * was not in the chooser. Otherwise update the items description. |
418 * | 431 * |
419 * @param item The item to be added to the end of the chooser. | 432 * @param item The item to be added to the end of the chooser or updated. |
420 */ | 433 */ |
421 public void addItemToList(ItemChooserRow item) { | 434 public void addOrUpdateItem(ItemChooserRow item) { |
422 mProgressBar.setVisibility(View.GONE); | 435 mProgressBar.setVisibility(View.GONE); |
423 mItemAdapter.add(item); | 436 mItemAdapter.addOrUpdate(item); |
424 setState(State.PROGRESS_UPDATE_AVAILABLE); | 437 setState(State.PROGRESS_UPDATE_AVAILABLE); |
425 } | 438 } |
426 | 439 |
427 /** | 440 /** |
428 * Remove an item that is shown in the dialog. | 441 * Remove an item that is shown in the dialog. |
429 * | 442 * |
430 * @param item The item to be removed in the chooser. | 443 * @param item The item to be removed in the chooser. |
431 */ | 444 */ |
432 public void removeItemFromList(ItemChooserRow item) { | 445 public void removeItemFromList(ItemChooserRow item) { |
433 mItemAdapter.remove(item); | 446 mItemAdapter.remove(item); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
502 } | 515 } |
503 | 516 |
504 /** | 517 /** |
505 * Returns the ItemAdapter associated with this class. For use with tests on
ly. | 518 * Returns the ItemAdapter associated with this class. For use with tests on
ly. |
506 */ | 519 */ |
507 @VisibleForTesting | 520 @VisibleForTesting |
508 public ItemAdapter getItemAdapterForTesting() { | 521 public ItemAdapter getItemAdapterForTesting() { |
509 return mItemAdapter; | 522 return mItemAdapter; |
510 } | 523 } |
511 } | 524 } |
OLD | NEW |