Chromium Code Reviews| 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 15 matching lines...) Expand all Loading... | |
| 26 import android.widget.ProgressBar; | 26 import android.widget.ProgressBar; |
| 27 import android.widget.TextView; | 27 import android.widget.TextView; |
| 28 | 28 |
| 29 import org.chromium.base.ApiCompatibilityUtils; | 29 import org.chromium.base.ApiCompatibilityUtils; |
| 30 import org.chromium.base.VisibleForTesting; | 30 import org.chromium.base.VisibleForTesting; |
| 31 import org.chromium.chrome.R; | 31 import org.chromium.chrome.R; |
| 32 import org.chromium.chrome.browser.util.MathUtils; | 32 import org.chromium.chrome.browser.util.MathUtils; |
| 33 import org.chromium.ui.base.DeviceFormFactor; | 33 import org.chromium.ui.base.DeviceFormFactor; |
| 34 import org.chromium.ui.widget.TextViewWithClickableSpans; | 34 import org.chromium.ui.widget.TextViewWithClickableSpans; |
| 35 | 35 |
| 36 import java.util.HashMap; | |
| 36 import java.util.HashSet; | 37 import java.util.HashSet; |
| 37 import java.util.Set; | 38 import java.util.Set; |
| 38 | 39 |
| 39 /** | 40 /** |
| 40 * A general-purpose dialog for presenting a list of things to pick from. | 41 * A general-purpose dialog for presenting a list of things to pick from. |
| 41 */ | 42 */ |
| 42 public class ItemChooserDialog { | 43 public class ItemChooserDialog { |
| 43 /** | 44 /** |
| 44 * An interface to implement to get a callback when something has been | 45 * An interface to implement to get a callback when something has been |
| 45 * selected. | 46 * selected. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 // The color of the non-highlighted text. | 130 // The color of the non-highlighted text. |
| 130 private final int mDefaultTextColor; | 131 private final int mDefaultTextColor; |
| 131 | 132 |
| 132 // The zero-based index of the item currently selected in the dialog, | 133 // The zero-based index of the item currently selected in the dialog, |
| 133 // or -1 (INVALID_POSITION) if nothing is selected. | 134 // or -1 (INVALID_POSITION) if nothing is selected. |
| 134 private int mSelectedItem = ListView.INVALID_POSITION; | 135 private int mSelectedItem = ListView.INVALID_POSITION; |
| 135 | 136 |
| 136 // A set of keys that are marked as disabled in the dialog. | 137 // A set of keys that are marked as disabled in the dialog. |
| 137 private Set<String> mDisabledEntries = new HashSet<String>(); | 138 private Set<String> mDisabledEntries = new HashSet<String>(); |
| 138 | 139 |
| 140 // Device names are counted in a map. | |
| 141 private HashMap<String, Integer> mDeviceNameMap = new HashMap<String, In teger>(); | |
|
Yaron
2016/06/29 21:18:19
doesn't seem like "deviceName" should be part of "
juncai
2016/06/29 22:48:38
Done.
| |
| 142 | |
| 139 public ItemAdapter(Context context, int resource) { | 143 public ItemAdapter(Context context, int resource) { |
| 140 super(context, resource); | 144 super(context, resource); |
| 141 | 145 |
| 142 mInflater = LayoutInflater.from(context); | 146 mInflater = LayoutInflater.from(context); |
| 143 | 147 |
| 144 mBackgroundHighlightColor = ApiCompatibilityUtils.getColor(getContex t().getResources(), | 148 mBackgroundHighlightColor = ApiCompatibilityUtils.getColor(getContex t().getResources(), |
| 145 R.color.light_active_color); | 149 R.color.light_active_color); |
| 146 mDefaultTextColor = ApiCompatibilityUtils.getColor(getContext().getR esources(), | 150 mDefaultTextColor = ApiCompatibilityUtils.getColor(getContext().getR esources(), |
| 147 R.color.default_text_color); | 151 R.color.default_text_color); |
| 148 } | 152 } |
| 149 | 153 |
| 150 @Override | 154 @Override |
| 155 public void add(ItemChooserRow item) { | |
| 156 String description = item.mDescription; | |
| 157 Integer counter = mDeviceNameMap.get(description); | |
|
Yaron
2016/06/29 21:18:19
Nit: I think it's better to code against primitive
juncai
2016/06/29 22:48:38
Done.
| |
| 158 mDeviceNameMap.put(description, counter == null ? 1 : counter + 1); | |
| 159 super.add(item); | |
| 160 } | |
| 161 | |
| 162 @Override | |
| 163 public void remove(ItemChooserRow item) { | |
| 164 String description = item.mDescription; | |
| 165 Integer counter = mDeviceNameMap.get(description); | |
| 166 if (counter != null) { | |
| 167 if (counter.intValue() == 1) { | |
| 168 mDeviceNameMap.remove(description); | |
| 169 } else { | |
| 170 mDeviceNameMap.put(description, counter - 1); | |
| 171 } | |
| 172 } | |
| 173 super.remove(item); | |
| 174 } | |
| 175 | |
| 176 @Override | |
| 151 public void clear() { | 177 public void clear() { |
| 152 mSelectedItem = ListView.INVALID_POSITION; | 178 mSelectedItem = ListView.INVALID_POSITION; |
| 153 mConfirmButton.setEnabled(false); | 179 mConfirmButton.setEnabled(false); |
| 154 super.clear(); | 180 super.clear(); |
| 155 } | 181 } |
| 156 | 182 |
| 157 /** | 183 /** |
| 158 * Returns the key of the currently selected item or blank if nothing is | 184 * Returns the key of the currently selected item or blank if nothing is |
| 159 * selected. | 185 * selected. |
| 160 */ | 186 */ |
| 161 public String getSelectedItemKey() { | 187 public String getSelectedItemKey() { |
| 162 ItemChooserRow row = getItem(mSelectedItem); | 188 ItemChooserRow row = getItem(mSelectedItem); |
| 163 if (row == null) return ""; | 189 if (row == null) return ""; |
| 164 return row.mKey; | 190 return row.mKey; |
| 165 } | 191 } |
| 166 | 192 |
| 167 /** | 193 /** |
| 194 * Returns the description of the |position|th item. For items with | |
| 195 * the same description, their unique keys are appended to distinguish | |
| 196 * them. | |
| 197 */ | |
| 198 public String getItemDescription(int position) { | |
| 199 ItemChooserRow item = getItem(position); | |
| 200 String description = item.mDescription; | |
| 201 Integer counter = mDeviceNameMap.get(description); | |
| 202 return counter.intValue() == 1 | |
| 203 ? description | |
| 204 : mActivity.getString( | |
| 205 R.string.item_chooser_item_name_with_id, descripti on, item.mKey); | |
| 206 } | |
| 207 | |
| 208 /** | |
| 168 * Sets whether the itam is enabled. Disabled items are grayed out. | 209 * Sets whether the itam is enabled. Disabled items are grayed out. |
| 169 * @param id The id of the item to affect. | 210 * @param id The id of the item to affect. |
| 170 * @param enabled Whether the item should be enabled or not. | 211 * @param enabled Whether the item should be enabled or not. |
| 171 */ | 212 */ |
| 172 public void setEnabled(String id, boolean enabled) { | 213 public void setEnabled(String id, boolean enabled) { |
| 173 if (enabled) { | 214 if (enabled) { |
| 174 mDisabledEntries.remove(id); | 215 mDisabledEntries.remove(id); |
| 175 } else { | 216 } else { |
| 176 mDisabledEntries.add(id); | 217 mDisabledEntries.add(id); |
| 177 } | 218 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 211 } else { | 252 } else { |
| 212 view.setBackground(null); | 253 view.setBackground(null); |
| 213 if (!isEnabled(position)) { | 254 if (!isEnabled(position)) { |
| 214 view.setTextColor(ApiCompatibilityUtils.getColor(getContext( ).getResources(), | 255 view.setTextColor(ApiCompatibilityUtils.getColor(getContext( ).getResources(), |
| 215 R.color.primary_text_disabled_material_light)); | 256 R.color.primary_text_disabled_material_light)); |
| 216 } else { | 257 } else { |
| 217 view.setTextColor(mDefaultTextColor); | 258 view.setTextColor(mDefaultTextColor); |
| 218 } | 259 } |
| 219 } | 260 } |
| 220 | 261 |
| 221 ItemChooserRow item = getItem(position); | 262 view.setText(getItemDescription(position)); |
| 222 view.setText(item.mDescription); | |
| 223 return view; | 263 return view; |
| 224 } | 264 } |
| 225 | 265 |
| 226 @Override | 266 @Override |
| 227 public void onItemClick(AdapterView<?> adapter, View view, int position, long id) { | 267 public void onItemClick(AdapterView<?> adapter, View view, int position, long id) { |
| 228 mSelectedItem = position; | 268 mSelectedItem = position; |
| 229 mConfirmButton.setEnabled(true); | 269 mConfirmButton.setEnabled(true); |
| 230 mItemAdapter.notifyDataSetChanged(); | 270 mItemAdapter.notifyDataSetChanged(); |
| 231 } | 271 } |
| 232 } | 272 } |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 446 return mDialog; | 486 return mDialog; |
| 447 } | 487 } |
| 448 | 488 |
| 449 /** | 489 /** |
| 450 * Returns the ItemAdapter associated with this class. For use with tests on ly. | 490 * Returns the ItemAdapter associated with this class. For use with tests on ly. |
| 451 */ | 491 */ |
| 452 @VisibleForTesting | 492 @VisibleForTesting |
| 453 public ItemAdapter getItemAdapterForTesting() { | 493 public ItemAdapter getItemAdapterForTesting() { |
| 454 return mItemAdapter; | 494 return mItemAdapter; |
| 455 } | 495 } |
| 496 | |
| 497 /** | |
| 498 * Returns the description of |position|th item. For use with tests only. | |
| 499 */ | |
| 500 @VisibleForTesting | |
| 501 public String getItemDescriptionForTesting(int position) { | |
|
Yaron
2016/06/29 21:18:19
Seems unnecessary. You can just use |getItemAdapte
juncai
2016/06/29 22:48:38
Done.
| |
| 502 return mItemAdapter.getItemDescription(position); | |
| 503 } | |
| 456 } | 504 } |
| OLD | NEW |