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; |
11 import android.graphics.Color; | 11 import android.graphics.Color; |
12 import android.graphics.drawable.ColorDrawable; | 12 import android.graphics.drawable.ColorDrawable; |
13 import android.text.SpannableString; | 13 import android.text.SpannableString; |
14 import android.text.method.LinkMovementMethod; | 14 import android.text.method.LinkMovementMethod; |
15 import android.view.Gravity; | 15 import android.view.Gravity; |
16 import android.view.LayoutInflater; | 16 import android.view.LayoutInflater; |
17 import android.view.View; | 17 import android.view.View; |
18 import android.view.ViewGroup; | 18 import android.view.ViewGroup; |
19 import android.view.ViewGroup.LayoutParams; | 19 import android.view.ViewGroup.LayoutParams; |
20 import android.view.Window; | 20 import android.view.Window; |
21 import android.widget.AdapterView; | 21 import android.widget.AdapterView; |
22 import android.widget.ArrayAdapter; | 22 import android.widget.ArrayAdapter; |
23 import android.widget.Button; | 23 import android.widget.Button; |
24 import android.widget.ImageView; | |
24 import android.widget.LinearLayout; | 25 import android.widget.LinearLayout; |
25 import android.widget.ListView; | 26 import android.widget.ListView; |
26 import android.widget.ProgressBar; | 27 import android.widget.ProgressBar; |
27 import android.widget.TextView; | 28 import android.widget.TextView; |
28 | 29 |
29 import org.chromium.base.ApiCompatibilityUtils; | 30 import org.chromium.base.ApiCompatibilityUtils; |
30 import org.chromium.base.VisibleForTesting; | 31 import org.chromium.base.VisibleForTesting; |
32 import org.chromium.base.annotations.SuppressFBWarnings; | |
31 import org.chromium.chrome.R; | 33 import org.chromium.chrome.R; |
32 import org.chromium.chrome.browser.util.MathUtils; | 34 import org.chromium.chrome.browser.util.MathUtils; |
33 import org.chromium.ui.base.DeviceFormFactor; | 35 import org.chromium.ui.base.DeviceFormFactor; |
34 import org.chromium.ui.widget.TextViewWithClickableSpans; | 36 import org.chromium.ui.widget.TextViewWithClickableSpans; |
35 | 37 |
36 import java.util.HashMap; | 38 import java.util.HashMap; |
37 import java.util.HashSet; | 39 import java.util.HashSet; |
38 import java.util.Map; | 40 import java.util.Map; |
39 import java.util.Set; | 41 import java.util.Set; |
40 | 42 |
(...skipping 12 matching lines...) Expand all Loading... | |
53 /** | 55 /** |
54 * Returns the user selection. | 56 * Returns the user selection. |
55 * | 57 * |
56 * @param id The id of the item selected. Blank if the dialog was closed | 58 * @param id The id of the item selected. Blank if the dialog was closed |
57 * without selecting anything. | 59 * without selecting anything. |
58 */ | 60 */ |
59 void onItemSelected(String id); | 61 void onItemSelected(String id); |
60 } | 62 } |
61 | 63 |
62 /** | 64 /** |
65 * A class representing an icon to be shown in a chooser row. | |
66 */ | |
67 public static class ItemChooserRowIcon { | |
68 private int mIcon; | |
69 // Icon when the row is selected. | |
70 private int mSelectedIcon; | |
71 private String mIconDescription; | |
72 | |
73 public ItemChooserRowIcon(int icon, int selectedIcon, String iconDescrip tion) { | |
74 mIcon = icon; | |
75 mSelectedIcon = selectedIcon; | |
76 mIconDescription = iconDescription; | |
77 } | |
78 | |
79 @Override | |
80 public boolean equals(Object obj) { | |
81 if (!(obj instanceof ItemChooserRowIcon)) return false; | |
82 if (this == obj) return true; | |
83 ItemChooserRowIcon icon = (ItemChooserRowIcon) obj; | |
84 return mIcon == icon.mIcon && mSelectedIcon == icon.mSelectedIcon | |
85 && mIconDescription.equals(icon.mIconDescription); | |
86 } | |
87 | |
88 @Override | |
89 public String toString() { | |
90 return mIcon + ":" + mSelectedIcon + ":" + mIconDescription; | |
91 } | |
92 | |
93 @Override | |
94 public int hashCode() { | |
95 return mIcon + mSelectedIcon + mIconDescription.hashCode(); | |
96 } | |
97 } | |
98 | |
99 /** | |
63 * A class representing one data row in the picker. | 100 * A class representing one data row in the picker. |
64 */ | 101 */ |
65 public static class ItemChooserRow { | 102 public static class ItemChooserRow { |
66 private final String mKey; | 103 private final String mKey; |
67 private String mDescription; | 104 private String mDescription; |
105 private ItemChooserRowIcon mIcon; | |
juncai
2016/08/26 19:37:48
The class ItemChooserRowIcon has a member variable
ortuno
2016/09/12 05:11:28
Done.
| |
106 | |
107 public ItemChooserRow(String key, String description, ItemChooserRowIcon icon) { | |
108 mKey = key; | |
109 mDescription = description; | |
110 mIcon = icon; | |
111 } | |
68 | 112 |
69 public ItemChooserRow(String key, String description) { | 113 public ItemChooserRow(String key, String description) { |
70 mKey = key; | 114 this(key, description, null); |
71 mDescription = description; | |
72 } | 115 } |
73 | 116 |
74 @Override | 117 @Override |
75 public boolean equals(Object obj) { | 118 public boolean equals(Object obj) { |
76 if (!(obj instanceof ItemChooserRow)) return false; | 119 if (!(obj instanceof ItemChooserRow)) return false; |
77 if (this == obj) return true; | 120 if (this == obj) return true; |
78 ItemChooserRow item = (ItemChooserRow) obj; | 121 ItemChooserRow item = (ItemChooserRow) obj; |
79 return mKey.equals(item.mKey) && mDescription.equals(item.mDescripti on); | 122 |
123 // If only one is null i.e. XOR. | |
124 if (mIcon == null ^ item.mIcon == null) return false; | |
125 | |
126 if (mIcon != null && item.mIcon != null && !mIcon.equals(item.mIcon) ) return false; | |
127 | |
128 if (mKey.equals(item.mKey) && mDescription.equals(item.mDescription) ) return true; | |
129 | |
130 return false; | |
80 } | 131 } |
81 | 132 |
82 @Override | 133 @Override |
83 public int hashCode() { | 134 public int hashCode() { |
84 return mKey.hashCode() + mDescription.hashCode(); | 135 return mKey.hashCode() + mDescription.hashCode() + mIcon.hashCode(); |
136 } | |
137 | |
138 @Override | |
139 public String toString() { | |
140 return mKey + ":" + mDescription + ":" + mIcon.toString(); | |
85 } | 141 } |
86 } | 142 } |
87 | 143 |
88 /** | 144 /** |
89 * The labels to show in the dialog. | 145 * The labels to show in the dialog. |
90 */ | 146 */ |
91 public static class ItemChooserLabels { | 147 public static class ItemChooserLabels { |
92 // The title at the top of the dialog. | 148 // The title at the top of the dialog. |
93 public final CharSequence title; | 149 public final CharSequence title; |
94 // The message to show while there are no results. | 150 // The message to show while there are no results. |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 if (oldItem != null) { | 237 if (oldItem != null) { |
182 if (oldItem.equals(item)) { | 238 if (oldItem.equals(item)) { |
183 // No need to update anything. | 239 // No need to update anything. |
184 return; | 240 return; |
185 } | 241 } |
186 if (!oldItem.mDescription.equals(item.mDescription)) { | 242 if (!oldItem.mDescription.equals(item.mDescription)) { |
187 removeFromDescriptionsMap(oldItem.mDescription); | 243 removeFromDescriptionsMap(oldItem.mDescription); |
188 oldItem.mDescription = item.mDescription; | 244 oldItem.mDescription = item.mDescription; |
189 addToDescriptionsMap(oldItem.mDescription); | 245 addToDescriptionsMap(oldItem.mDescription); |
190 } | 246 } |
247 if (oldItem.mIcon == null || !oldItem.mIcon.equals(item.mIcon)) { | |
248 oldItem.mIcon = item.mIcon; | |
249 } | |
191 notifyDataSetChanged(); | 250 notifyDataSetChanged(); |
192 return; | 251 return; |
193 } | 252 } |
194 ItemChooserRow result = mKeyToItemMap.put(item.mKey, item); | 253 ItemChooserRow result = mKeyToItemMap.put(item.mKey, item); |
195 assert result == null; | 254 assert result == null; |
196 | 255 |
197 addToDescriptionsMap(item.mDescription); | 256 addToDescriptionsMap(item.mDescription); |
198 add(item); | 257 add(item); |
199 } | 258 } |
200 | 259 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 */ | 292 */ |
234 public String getDisplayText(int position) { | 293 public String getDisplayText(int position) { |
235 ItemChooserRow item = getItem(position); | 294 ItemChooserRow item = getItem(position); |
236 String description = item.mDescription; | 295 String description = item.mDescription; |
237 int counter = mItemDescriptionMap.get(description); | 296 int counter = mItemDescriptionMap.get(description); |
238 return counter == 1 ? description | 297 return counter == 1 ? description |
239 : mActivity.getString(R.string.item_chooser_item_name_with_i d, description, | 298 : mActivity.getString(R.string.item_chooser_item_name_with_i d, description, |
240 item.mKey); | 299 item.mKey); |
241 } | 300 } |
242 | 301 |
302 public ItemChooserRowIcon getIcon(int position) { | |
303 ItemChooserRow item = getItem(position); | |
304 return item.mIcon; | |
305 } | |
306 | |
307 public boolean hasIcon(int position) { | |
308 ItemChooserRow item = getItem(position); | |
309 return item.mIcon != null; | |
310 } | |
311 | |
243 /** | 312 /** |
244 * Sets whether the itam is enabled. Disabled items are grayed out. | 313 * Sets whether the itam is enabled. Disabled items are grayed out. |
245 * @param id The id of the item to affect. | 314 * @param id The id of the item to affect. |
246 * @param enabled Whether the item should be enabled or not. | 315 * @param enabled Whether the item should be enabled or not. |
247 */ | 316 */ |
248 public void setEnabled(String id, boolean enabled) { | 317 public void setEnabled(String id, boolean enabled) { |
249 if (enabled) { | 318 if (enabled) { |
250 mDisabledEntries.remove(id); | 319 mDisabledEntries.remove(id); |
251 } else { | 320 } else { |
252 mDisabledEntries.add(id); | 321 mDisabledEntries.add(id); |
(...skipping 10 matching lines...) Expand all Loading... | |
263 @Override | 332 @Override |
264 public int getViewTypeCount() { | 333 public int getViewTypeCount() { |
265 return 1; | 334 return 1; |
266 } | 335 } |
267 | 336 |
268 @Override | 337 @Override |
269 public long getItemId(int position) { | 338 public long getItemId(int position) { |
270 return position; | 339 return position; |
271 } | 340 } |
272 | 341 |
273 @Override | 342 // view = (LinearLayout) mInflater.inflate(...); causes a DLS_DEAD_LOCAL _STORE error. |
274 public View getView(int position, View convertView, ViewGroup parent) { | 343 @SuppressFBWarnings("DLS_DEAD_LOCAL_STORE") |
344 private View getViewWithIcon(int position, View convertView, ViewGroup p arent) { | |
345 LinearLayout view; | |
346 if (convertView instanceof LinearLayout) { | |
347 view = (LinearLayout) convertView; | |
348 } else { | |
349 view = (LinearLayout) mInflater.inflate( | |
350 R.layout.item_chooser_dialog_row_with_icon, parent, fals e); | |
351 } | |
352 | |
353 TextView description = (TextView) view.findViewById(R.id.description ); | |
354 LinearLayout imageContainer = (LinearLayout) view.findViewById(R.id. imageContainer); | |
355 ImageView icon = (ImageView) view.findViewById(R.id.icon); | |
356 | |
357 ItemChooserRowIcon rowIcon = getIcon(position); | |
358 | |
359 // Set highlighting for currently selected item. | |
360 if (position == mSelectedItem) { | |
361 view.setBackgroundColor(mBackgroundHighlightColor); | |
362 description.setTextColor(Color.WHITE); | |
363 icon.setImageResource(rowIcon.mSelectedIcon); | |
364 icon.setContentDescription(rowIcon.mIconDescription); | |
365 } else { | |
366 view.setBackground(null); | |
367 icon.setImageResource(rowIcon.mIcon); | |
368 icon.setContentDescription(rowIcon.mIconDescription); | |
369 if (!isEnabled(position)) { | |
370 description.setTextColor( | |
371 ApiCompatibilityUtils.getColor(getContext().getResou rces(), | |
372 R.color.primary_text_disabled_material_light )); | |
373 } else { | |
374 description.setTextColor(mDefaultTextColor); | |
375 } | |
376 } | |
377 | |
378 description.setText(getDisplayText(position)); | |
379 | |
380 return view; | |
381 } | |
382 | |
383 private View getTextView(int position, View convertView, ViewGroup paren t) { | |
275 TextView view; | 384 TextView view; |
276 if (convertView instanceof TextView) { | 385 if (convertView instanceof TextView) { |
277 view = (TextView) convertView; | 386 view = (TextView) convertView; |
278 } else { | 387 } else { |
279 view = (TextView) mInflater.inflate( | 388 view = (TextView) mInflater.inflate( |
280 R.layout.item_chooser_dialog_row, parent, false); | 389 R.layout.item_chooser_dialog_row, parent, false); |
281 } | 390 } |
282 | 391 |
283 // Set highlighting for currently selected item. | 392 // Set highlighting for currently selected item. |
284 if (position == mSelectedItem) { | 393 if (position == mSelectedItem) { |
285 view.setBackgroundColor(mBackgroundHighlightColor); | 394 view.setBackgroundColor(mBackgroundHighlightColor); |
286 view.setTextColor(Color.WHITE); | 395 view.setTextColor(Color.WHITE); |
287 } else { | 396 } else { |
288 view.setBackground(null); | 397 view.setBackground(null); |
289 if (!isEnabled(position)) { | 398 if (!isEnabled(position)) { |
290 view.setTextColor(ApiCompatibilityUtils.getColor(getContext( ).getResources(), | 399 view.setTextColor(ApiCompatibilityUtils.getColor(getContext( ).getResources(), |
291 R.color.primary_text_disabled_material_light)); | 400 R.color.primary_text_disabled_material_light)); |
292 } else { | 401 } else { |
293 view.setTextColor(mDefaultTextColor); | 402 view.setTextColor(mDefaultTextColor); |
294 } | 403 } |
295 } | 404 } |
296 | 405 |
297 view.setText(getDisplayText(position)); | 406 view.setText(getDisplayText(position)); |
298 return view; | 407 return view; |
299 } | 408 } |
300 | 409 |
301 @Override | 410 @Override |
411 public View getView(int position, View convertView, ViewGroup parent) { | |
412 if (hasIcon(position)) { | |
413 return getViewWithIcon(position, convertView, parent); | |
414 } | |
415 return getTextView(position, convertView, parent); | |
416 } | |
417 | |
418 @Override | |
302 public void onItemClick(AdapterView<?> adapter, View view, int position, long id) { | 419 public void onItemClick(AdapterView<?> adapter, View view, int position, long id) { |
303 mSelectedItem = position; | 420 mSelectedItem = position; |
304 mConfirmButton.setEnabled(true); | 421 mConfirmButton.setEnabled(true); |
305 mItemAdapter.notifyDataSetChanged(); | 422 mItemAdapter.notifyDataSetChanged(); |
306 } | 423 } |
307 | 424 |
308 private void addToDescriptionsMap(String description) { | 425 private void addToDescriptionsMap(String description) { |
309 int count = mItemDescriptionMap.containsKey(description) | 426 int count = mItemDescriptionMap.containsKey(description) |
310 ? mItemDescriptionMap.get(description) | 427 ? mItemDescriptionMap.get(description) |
311 : 0; | 428 : 0; |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
547 } | 664 } |
548 | 665 |
549 /** | 666 /** |
550 * Returns the ItemAdapter associated with this class. For use with tests on ly. | 667 * Returns the ItemAdapter associated with this class. For use with tests on ly. |
551 */ | 668 */ |
552 @VisibleForTesting | 669 @VisibleForTesting |
553 public ItemAdapter getItemAdapterForTesting() { | 670 public ItemAdapter getItemAdapterForTesting() { |
554 return mItemAdapter; | 671 return mItemAdapter; |
555 } | 672 } |
556 } | 673 } |
OLD | NEW |