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 } |
69 } | 76 } |
70 | 77 |
71 /** | 78 /** |
72 * The labels to show in the dialog. | 79 * The labels to show in the dialog. |
73 */ | 80 */ |
74 public static class ItemChooserLabels { | 81 public static class ItemChooserLabels { |
75 // The title at the top of the dialog. | 82 // The title at the top of the dialog. |
76 public final SpannableString mTitle; | 83 public final CharSequence mTitle; |
77 // The message to show while there are no results. | 84 // The message to show while there are no results. |
78 public final SpannableString mSearching; | 85 public final CharSequence mSearching; |
79 // The message to show when no results were produced. | 86 // The message to show when no results were produced. |
80 public final SpannableString mNoneFound; | 87 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 | 88 // A status message to show above the button row after discovery has |
85 // stopped and no devices have been found. | 89 // stopped and no devices have been found. |
86 public final SpannableString mStatusIdleNoneFound; | 90 public final CharSequence mStatusIdleNoneFound; |
87 // A status message to show above the button row after an item has | 91 // A status message to show above the button row after an item has |
88 // been added and discovery has stopped. | 92 // been added and discovery has stopped. |
89 public final SpannableString mStatusIdleSomeFound; | 93 public final CharSequence mStatusIdleSomeFound; |
90 // The label for the positive button (e.g. Select/Pair). | 94 // The label for the positive button (e.g. Select/Pair). |
91 public final String mPositiveButton; | 95 public final CharSequence mPositiveButton; |
92 | 96 |
93 public ItemChooserLabels(SpannableString title, SpannableString searchin
g, | 97 public ItemChooserLabels(CharSequence title, CharSequence searching, Cha
rSequence noneFound, |
94 SpannableString noneFound, SpannableString statusActive, | 98 CharSequence statusIdleNoneFound, CharSequence statusIdleSomeFou
nd, |
95 SpannableString statusIdleNoneFound, SpannableString statusIdleS
omeFound, | 99 CharSequence positiveButton) { |
96 String positiveButton) { | |
97 mTitle = title; | 100 mTitle = title; |
98 mSearching = searching; | 101 mSearching = searching; |
99 mNoneFound = noneFound; | 102 mNoneFound = noneFound; |
100 mStatusActive = statusActive; | |
101 mStatusIdleNoneFound = statusIdleNoneFound; | 103 mStatusIdleNoneFound = statusIdleNoneFound; |
102 mStatusIdleSomeFound = statusIdleSomeFound; | 104 mStatusIdleSomeFound = statusIdleSomeFound; |
103 mPositiveButton = positiveButton; | 105 mPositiveButton = positiveButton; |
104 } | 106 } |
105 } | 107 } |
106 | 108 |
107 /** | 109 /** |
108 * The various states the dialog can represent. | 110 * The various states the dialog can represent. |
109 */ | 111 */ |
110 private enum State { STARTING, PROGRESS_UPDATE_AVAILABLE, DISCOVERY_IDLE } | 112 private enum State { STARTING, DISCOVERY_IDLE } |
111 | 113 |
112 /** | 114 /** |
113 * An adapter for keeping track of which items to show in the dialog. | 115 * An adapter for keeping track of which items to show in the dialog. |
114 */ | 116 */ |
115 private class ItemAdapter extends ArrayAdapter<ItemChooserRow> | 117 private class ItemAdapter extends ArrayAdapter<ItemChooserRow> |
116 implements AdapterView.OnItemClickListener { | 118 implements AdapterView.OnItemClickListener { |
117 private final LayoutInflater mInflater; | 119 private final LayoutInflater mInflater; |
118 | 120 |
119 // The background color of the highlighted item. | 121 // The background color of the highlighted item. |
120 private final int mBackgroundHighlightColor; | 122 private final int mBackgroundHighlightColor; |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 } | 347 } |
346 | 348 |
347 mDialog.show(); | 349 mDialog.show(); |
348 } | 350 } |
349 | 351 |
350 public void dismiss() { | 352 public void dismiss() { |
351 mDialog.dismiss(); | 353 mDialog.dismiss(); |
352 } | 354 } |
353 | 355 |
354 /** | 356 /** |
355 * Add items to show in the dialog. | 357 * Add an item to the end of the list to show in the dialog. |
356 * | 358 * |
357 * @param list The list of items to add to the chooser. This function can be | 359 * @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 | 360 */ |
359 * the end of the list. | 361 public void addItemToList(ItemChooserRow item) { |
360 */ | |
361 public void addItemsToList(List<ItemChooserRow> list) { | |
362 mProgressBar.setVisibility(View.GONE); | 362 mProgressBar.setVisibility(View.GONE); |
363 | 363 mItemAdapter.add(item); |
364 if (!list.isEmpty()) { | 364 setState(State.DISCOVERY_IDLE); |
365 mItemAdapter.addAll(list); | |
366 } | |
367 setState(State.PROGRESS_UPDATE_AVAILABLE); | |
368 } | 365 } |
369 | 366 |
370 /** | 367 /** |
| 368 * Remove an item that is shown in the dialog. |
| 369 * |
| 370 * @param item The item to be removed in the chooser. |
| 371 */ |
| 372 public void removeItemFromList(ItemChooserRow item) { |
| 373 mItemAdapter.remove(item); |
| 374 setState(State.DISCOVERY_IDLE); |
| 375 } |
| 376 |
| 377 /** |
371 * Indicates the chooser that no more items will be added. | 378 * Indicates the chooser that no more items will be added. |
372 */ | 379 */ |
373 public void setIdleState() { | 380 public void setIdleState() { |
374 mProgressBar.setVisibility(View.GONE); | 381 mProgressBar.setVisibility(View.GONE); |
375 setState(State.DISCOVERY_IDLE); | 382 setState(State.DISCOVERY_IDLE); |
376 } | 383 } |
377 | 384 |
378 /** | 385 /** |
379 * Sets whether the item is enabled. | 386 * Sets whether the item is enabled. |
380 * @param id The id of the item to affect. | 387 * @param id The id of the item to affect. |
(...skipping 23 matching lines...) Expand all Loading... |
404 } | 411 } |
405 | 412 |
406 private void setState(State state) { | 413 private void setState(State state) { |
407 switch (state) { | 414 switch (state) { |
408 case STARTING: | 415 case STARTING: |
409 mStatus.setText(mLabels.mSearching); | 416 mStatus.setText(mLabels.mSearching); |
410 mListView.setVisibility(View.GONE); | 417 mListView.setVisibility(View.GONE); |
411 mProgressBar.setVisibility(View.VISIBLE); | 418 mProgressBar.setVisibility(View.VISIBLE); |
412 mEmptyMessage.setVisibility(View.GONE); | 419 mEmptyMessage.setVisibility(View.GONE); |
413 break; | 420 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: | 421 case DISCOVERY_IDLE: |
420 boolean showEmptyMessage = mItemAdapter.isEmpty(); | 422 boolean showEmptyMessage = mItemAdapter.isEmpty(); |
421 mStatus.setText(showEmptyMessage | 423 mStatus.setText(showEmptyMessage |
422 ? mLabels.mStatusIdleNoneFound : mLabels.mStatusIdleSome
Found); | 424 ? mLabels.mStatusIdleNoneFound : mLabels.mStatusIdleSome
Found); |
423 mEmptyMessage.setText(mLabels.mNoneFound); | 425 mEmptyMessage.setText(mLabels.mNoneFound); |
424 mEmptyMessage.setVisibility(showEmptyMessage ? View.VISIBLE : Vi
ew.GONE); | 426 mEmptyMessage.setVisibility(showEmptyMessage ? View.VISIBLE : Vi
ew.GONE); |
425 break; | 427 break; |
426 } | 428 } |
427 } | 429 } |
428 | 430 |
429 /** | 431 /** |
430 * Returns the dialog associated with this class. For use with tests only. | 432 * Returns the dialog associated with this class. For use with tests only. |
431 */ | 433 */ |
432 @VisibleForTesting | 434 @VisibleForTesting |
433 public Dialog getDialogForTesting() { | 435 public Dialog getDialogForTesting() { |
434 return mDialog; | 436 return mDialog; |
435 } | 437 } |
| 438 |
| 439 /** |
| 440 * Returns the ItemAdapter associated with this class. For use with tests on
ly. |
| 441 */ |
| 442 @VisibleForTesting |
| 443 public ItemAdapter getItemAdapterForTesting() { |
| 444 return mItemAdapter; |
| 445 } |
436 } | 446 } |
OLD | NEW |