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.Dialog; | 7 import android.app.Dialog; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.content.DialogInterface; | 9 import android.content.DialogInterface; |
10 import android.graphics.Color; | 10 import android.graphics.Color; |
(...skipping 16 matching lines...) Expand all Loading... |
27 import android.widget.ProgressBar; | 27 import android.widget.ProgressBar; |
28 import android.widget.TextView; | 28 import android.widget.TextView; |
29 | 29 |
30 import org.chromium.base.ApiCompatibilityUtils; | 30 import org.chromium.base.ApiCompatibilityUtils; |
31 import org.chromium.base.VisibleForTesting; | 31 import org.chromium.base.VisibleForTesting; |
32 import org.chromium.chrome.R; | 32 import org.chromium.chrome.R; |
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.HashSet; | 36 import java.util.HashSet; |
37 import java.util.List; | |
38 import java.util.Set; | 37 import java.util.Set; |
39 | 38 |
40 /** | 39 /** |
41 * A general-purpose dialog for presenting a list of things to pick from. | 40 * A general-purpose dialog for presenting a list of things to pick from. |
42 */ | 41 */ |
43 public class ItemChooserDialog { | 42 public class ItemChooserDialog { |
44 /** | 43 /** |
45 * An interface to implement to get a callback when something has been | 44 * An interface to implement to get a callback when something has been |
46 * selected. | 45 * selected. |
47 */ | 46 */ |
(...skipping 11 matching lines...) Expand all Loading... |
59 * A class representing one data row in the picker. | 58 * A class representing one data row in the picker. |
60 */ | 59 */ |
61 public static class ItemChooserRow { | 60 public static class ItemChooserRow { |
62 private final String mKey; | 61 private final String mKey; |
63 private final String mDescription; | 62 private final String mDescription; |
64 | 63 |
65 public ItemChooserRow(String key, String description) { | 64 public ItemChooserRow(String key, String description) { |
66 mKey = key; | 65 mKey = key; |
67 mDescription = description; | 66 mDescription = description; |
68 } | 67 } |
| 68 |
| 69 @Override |
| 70 public boolean equals(Object obj) { |
| 71 if (!(obj instanceof ItemChooserRow)) return false; |
| 72 if (this == obj) return true; |
| 73 ItemChooserRow item = (ItemChooserRow) obj; |
| 74 return mKey.equals(item.mKey) && mDescription.equals(item.mDescripti
on); |
| 75 } |
| 76 |
| 77 @Override |
| 78 public int hashCode() { |
| 79 return mKey.hashCode() + mDescription.hashCode(); |
| 80 } |
69 } | 81 } |
70 | 82 |
71 /** | 83 /** |
72 * The labels to show in the dialog. | 84 * The labels to show in the dialog. |
73 */ | 85 */ |
74 public static class ItemChooserLabels { | 86 public static class ItemChooserLabels { |
75 // The title at the top of the dialog. | 87 // The title at the top of the dialog. |
76 public final SpannableString mTitle; | 88 public final CharSequence mTitle; |
77 // The message to show while there are no results. | 89 // The message to show while there are no results. |
78 public final SpannableString mSearching; | 90 public final CharSequence mSearching; |
79 // The message to show when no results were produced. | 91 // The message to show when no results were produced. |
80 public final SpannableString mNoneFound; | 92 public final CharSequence mNoneFound; |
81 // A status message to show above the button row after an item has | |
82 // been added and discovery is still ongoing. | |
83 public final SpannableString mStatusActive; | |
84 // A status message to show above the button row after discovery has | 93 // A status message to show above the button row after discovery has |
85 // stopped and no devices have been found. | 94 // stopped and no devices have been found. |
86 public final SpannableString mStatusIdleNoneFound; | 95 public final CharSequence mStatusIdleNoneFound; |
87 // A status message to show above the button row after an item has | 96 // A status message to show above the button row after an item has |
88 // been added and discovery has stopped. | 97 // been added and discovery has stopped. |
89 public final SpannableString mStatusIdleSomeFound; | 98 public final CharSequence mStatusIdleSomeFound; |
90 // The label for the positive button (e.g. Select/Pair). | 99 // The label for the positive button (e.g. Select/Pair). |
91 public final String mPositiveButton; | 100 public final CharSequence mPositiveButton; |
92 | 101 |
93 public ItemChooserLabels(SpannableString title, SpannableString searchin
g, | 102 public ItemChooserLabels(CharSequence title, CharSequence searching, Cha
rSequence noneFound, |
94 SpannableString noneFound, SpannableString statusActive, | 103 CharSequence statusIdleNoneFound, CharSequence statusIdleSomeFou
nd, |
95 SpannableString statusIdleNoneFound, SpannableString statusIdleS
omeFound, | 104 CharSequence positiveButton) { |
96 String positiveButton) { | |
97 mTitle = title; | 105 mTitle = title; |
98 mSearching = searching; | 106 mSearching = searching; |
99 mNoneFound = noneFound; | 107 mNoneFound = noneFound; |
100 mStatusActive = statusActive; | |
101 mStatusIdleNoneFound = statusIdleNoneFound; | 108 mStatusIdleNoneFound = statusIdleNoneFound; |
102 mStatusIdleSomeFound = statusIdleSomeFound; | 109 mStatusIdleSomeFound = statusIdleSomeFound; |
103 mPositiveButton = positiveButton; | 110 mPositiveButton = positiveButton; |
104 } | 111 } |
105 } | 112 } |
106 | 113 |
107 /** | 114 /** |
108 * The various states the dialog can represent. | 115 * The various states the dialog can represent. |
109 */ | 116 */ |
110 private enum State { STARTING, PROGRESS_UPDATE_AVAILABLE, DISCOVERY_IDLE } | 117 private enum State { STARTING, DISCOVERY_IDLE } |
111 | 118 |
112 /** | 119 /** |
113 * An adapter for keeping track of which items to show in the dialog. | 120 * An adapter for keeping track of which items to show in the dialog. |
114 */ | 121 */ |
115 private class ItemAdapter extends ArrayAdapter<ItemChooserRow> | 122 private class ItemAdapter extends ArrayAdapter<ItemChooserRow> |
116 implements AdapterView.OnItemClickListener { | 123 implements AdapterView.OnItemClickListener { |
117 private final LayoutInflater mInflater; | 124 private final LayoutInflater mInflater; |
118 | 125 |
119 // The background color of the highlighted item. | 126 // The background color of the highlighted item. |
120 private final int mBackgroundHighlightColor; | 127 private final int mBackgroundHighlightColor; |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 } | 352 } |
346 | 353 |
347 mDialog.show(); | 354 mDialog.show(); |
348 } | 355 } |
349 | 356 |
350 public void dismiss() { | 357 public void dismiss() { |
351 mDialog.dismiss(); | 358 mDialog.dismiss(); |
352 } | 359 } |
353 | 360 |
354 /** | 361 /** |
355 * Add items to show in the dialog. | 362 * Add an item to the end of the list to show in the dialog. |
356 * | 363 * |
357 * @param list The list of items to add to the chooser. This function can be | 364 * @param item The item to be added to the end of the chooser. |
358 * called multiple times to add more items and new items will be appended to | 365 */ |
359 * the end of the list. | 366 public void addItemToList(ItemChooserRow item) { |
360 */ | |
361 public void addItemsToList(List<ItemChooserRow> list) { | |
362 mProgressBar.setVisibility(View.GONE); | 367 mProgressBar.setVisibility(View.GONE); |
363 | 368 mItemAdapter.add(item); |
364 if (!list.isEmpty()) { | 369 setState(State.DISCOVERY_IDLE); |
365 mItemAdapter.addAll(list); | |
366 } | |
367 setState(State.PROGRESS_UPDATE_AVAILABLE); | |
368 } | 370 } |
369 | 371 |
370 /** | 372 /** |
| 373 * Remove an item that is shown in the dialog. |
| 374 * |
| 375 * @param item The item to be removed in the chooser. |
| 376 */ |
| 377 public void removeItemFromList(ItemChooserRow item) { |
| 378 mItemAdapter.remove(item); |
| 379 setState(State.DISCOVERY_IDLE); |
| 380 } |
| 381 |
| 382 /** |
371 * Indicates the chooser that no more items will be added. | 383 * Indicates the chooser that no more items will be added. |
372 */ | 384 */ |
373 public void setIdleState() { | 385 public void setIdleState() { |
374 mProgressBar.setVisibility(View.GONE); | 386 mProgressBar.setVisibility(View.GONE); |
375 setState(State.DISCOVERY_IDLE); | 387 setState(State.DISCOVERY_IDLE); |
376 } | 388 } |
377 | 389 |
378 /** | 390 /** |
379 * Sets whether the item is enabled. | 391 * Sets whether the item is enabled. |
380 * @param id The id of the item to affect. | 392 * @param id The id of the item to affect. |
(...skipping 23 matching lines...) Expand all Loading... |
404 } | 416 } |
405 | 417 |
406 private void setState(State state) { | 418 private void setState(State state) { |
407 switch (state) { | 419 switch (state) { |
408 case STARTING: | 420 case STARTING: |
409 mStatus.setText(mLabels.mSearching); | 421 mStatus.setText(mLabels.mSearching); |
410 mListView.setVisibility(View.GONE); | 422 mListView.setVisibility(View.GONE); |
411 mProgressBar.setVisibility(View.VISIBLE); | 423 mProgressBar.setVisibility(View.VISIBLE); |
412 mEmptyMessage.setVisibility(View.GONE); | 424 mEmptyMessage.setVisibility(View.GONE); |
413 break; | 425 break; |
414 case PROGRESS_UPDATE_AVAILABLE: | |
415 mStatus.setText(mLabels.mStatusActive); | |
416 mProgressBar.setVisibility(View.GONE); | |
417 mListView.setVisibility(View.VISIBLE); | |
418 break; | |
419 case DISCOVERY_IDLE: | 426 case DISCOVERY_IDLE: |
420 boolean showEmptyMessage = mItemAdapter.isEmpty(); | 427 boolean showEmptyMessage = mItemAdapter.isEmpty(); |
421 mStatus.setText(showEmptyMessage | 428 mStatus.setText(showEmptyMessage |
422 ? mLabels.mStatusIdleNoneFound : mLabels.mStatusIdleSome
Found); | 429 ? mLabels.mStatusIdleNoneFound : mLabels.mStatusIdleSome
Found); |
423 mEmptyMessage.setText(mLabels.mNoneFound); | 430 mEmptyMessage.setText(mLabels.mNoneFound); |
424 mEmptyMessage.setVisibility(showEmptyMessage ? View.VISIBLE : Vi
ew.GONE); | 431 mEmptyMessage.setVisibility(showEmptyMessage ? View.VISIBLE : Vi
ew.GONE); |
425 break; | 432 break; |
426 } | 433 } |
427 } | 434 } |
428 | 435 |
429 /** | 436 /** |
430 * Returns the dialog associated with this class. For use with tests only. | 437 * Returns the dialog associated with this class. For use with tests only. |
431 */ | 438 */ |
432 @VisibleForTesting | 439 @VisibleForTesting |
433 public Dialog getDialogForTesting() { | 440 public Dialog getDialogForTesting() { |
434 return mDialog; | 441 return mDialog; |
435 } | 442 } |
| 443 |
| 444 /** |
| 445 * Returns the ItemAdapter associated with this class. For use with tests on
ly. |
| 446 */ |
| 447 @VisibleForTesting |
| 448 public ItemAdapter getItemAdapterForTesting() { |
| 449 return mItemAdapter; |
| 450 } |
436 } | 451 } |
OLD | NEW |