Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/ItemChooserDialog.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ItemChooserDialog.java b/chrome/android/java/src/org/chromium/chrome/browser/ItemChooserDialog.java |
| index 6c92d6240828507a80d4aea4012f7601389e50fa..b64f4c74bb963133e68b3362e543aa9888b046c3 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/ItemChooserDialog.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/ItemChooserDialog.java |
| @@ -30,7 +30,9 @@ import org.chromium.chrome.R; |
| import org.chromium.ui.base.DeviceFormFactor; |
| import org.chromium.ui.widget.TextViewWithClickableSpans; |
| +import java.util.HashSet; |
| import java.util.List; |
| +import java.util.Set; |
| /** |
| * A general-purpose dialog for presenting a list of things to pick from. |
| @@ -75,20 +77,36 @@ public class ItemChooserDialog { |
| public final SpannableString mNoneFound; |
| // A status message to show above the button row. |
| public final SpannableString mStatus; |
| + // An error state. |
| + public final SpannableString mErrorMessage; |
| + // A status message to go with the error state. |
| + public final SpannableString mErrorStatus; |
| // The label for the positive button (e.g. Select/Pair). |
| public final String mPositiveButton; |
| public ItemChooserLabels(SpannableString title, String searching, SpannableString noneFound, |
| - SpannableString status, String positiveButton) { |
| + SpannableString status, SpannableString errorMessage, SpannableString errorStatus, |
| + String positiveButton) { |
| mTitle = title; |
| mSearching = searching; |
| mNoneFound = noneFound; |
| mStatus = status; |
| + mErrorMessage = errorMessage; |
| + mErrorStatus = errorStatus; |
| mPositiveButton = positiveButton; |
| } |
| } |
| /** |
| + * The various states the dialog can represent. |
| + */ |
| + private enum State { |
| + STARTING, |
| + PROGRESS_UPDATE_AVAILABLE, |
| + SHOWING_ERROR, |
| + } |
| + |
| + /** |
| * An adapter for keeping track of which items to show in the dialog. |
| */ |
| private class ItemAdapter extends ArrayAdapter<ItemChooserRow> |
| @@ -105,6 +123,9 @@ public class ItemChooserDialog { |
| // or -1 (INVALID_POSITION) if nothing is selected. |
| private int mSelectedItem = ListView.INVALID_POSITION; |
| + // A set of keys that are marked as unavailable for selection in the dialog. |
| + private Set<String> mUnavailableEntries = new HashSet<String>(); |
|
newt (away)
2015/09/01 21:51:23
For consistent terminology with Android, let's use
Finnur
2015/09/02 16:01:43
Done.
|
| + |
| public ItemAdapter(Context context, int resource) { |
| super(context, resource); |
| @@ -133,6 +154,24 @@ public class ItemChooserDialog { |
| return row.mKey; |
| } |
| + /** |
| + * Sets the availability of an item. Unavailable items are grayed out. |
| + * @param id The id of the item to affect. |
| + * @param available Whether the item should be available or not. |
| + */ |
| + public void setAvailability(String id, boolean available) { |
| + if (available) { |
| + mUnavailableEntries.remove(id); |
| + } else { |
| + mUnavailableEntries.add(id); |
| + } |
| + notifyDataSetChanged(); |
| + } |
| + |
| + private boolean isAvailable(String id) { |
|
newt (away)
2015/09/01 21:51:23
ArrayAdapter has a method isEnabled(int position),
Finnur
2015/09/02 16:01:43
Ooh, I like that idea. Unfortunately, it makes it
newt (away)
2015/09/02 17:48:42
The view gets marked as "disabled". If the text co
|
| + return !mUnavailableEntries.contains(id); |
| + } |
| + |
| @Override |
| public int getViewTypeCount() { |
| return 1; |
| @@ -163,12 +202,19 @@ public class ItemChooserDialog { |
| } |
| ItemChooserRow item = getItem(position); |
| + if (!isAvailable(item.mKey)) { |
| + view.setTextColor(Color.GRAY); |
| + } |
| view.setText(item.mDescription); |
| return view; |
| } |
| @Override |
| public void onItemClick(AdapterView<?> adapter, View view, int position, long id) { |
| + ItemChooserRow item = getItem(position); |
| + if (!isAvailable(item.mKey)) { |
| + return; |
| + } |
| mSelectedItem = position; |
| mConfirmButton.setEnabled(true); |
| mItemAdapter.notifyDataSetChanged(); |
| @@ -185,7 +231,7 @@ public class ItemChooserDialog { |
| // Individual UI elements. |
| private TextViewWithClickableSpans mTitle; |
| - private TextViewWithClickableSpans mNotFoundMessage; |
| + private TextViewWithClickableSpans mEmptyMessage; |
| private ProgressBar mProgressBar; |
| private ListView mListView; |
| private TextView mStatus; |
| @@ -225,17 +271,14 @@ public class ItemChooserDialog { |
| mStatus = (TextView) dialogContainer.findViewById(R.id.status); |
| mTitle = (TextViewWithClickableSpans) dialogContainer.findViewById( |
| R.id.dialog_title); |
| - mNotFoundMessage = |
| + mEmptyMessage = |
|
newt (away)
2015/09/01 21:51:23
remove wrapping (if possible)
Finnur
2015/09/02 16:01:43
Done.
|
| (TextViewWithClickableSpans) dialogContainer.findViewById( |
| R.id.not_found_message); |
| mTitle.setText(labels.mTitle); |
| mTitle.setMovementMethod(LinkMovementMethod.getInstance()); |
| - mNotFoundMessage.setText(labels.mNoneFound); |
| - mNotFoundMessage.setMovementMethod(LinkMovementMethod.getInstance()); |
| - |
| - mStatus.setText(labels.mSearching); |
| + mEmptyMessage.setMovementMethod(LinkMovementMethod.getInstance()); |
| mStatus.setMovementMethod(LinkMovementMethod.getInstance()); |
| mConfirmButton = (Button) dialogContainer.findViewById(R.id.positive); |
| @@ -252,10 +295,10 @@ public class ItemChooserDialog { |
| mItemAdapter = new ItemAdapter(mContext, R.layout.item_chooser_dialog_row); |
| mListView.setAdapter(mItemAdapter); |
| - mListView.setEmptyView(mNotFoundMessage); |
| - mNotFoundMessage.setVisibility(View.GONE); |
| + mListView.setEmptyView(mEmptyMessage); |
| mListView.setOnItemClickListener(mItemAdapter); |
| mListView.setDivider(null); |
| + setState(State.STARTING); |
| // The list is the main element in the dialog and it should grow and |
| // shrink according to the size of the screen available (clamped to a |
| @@ -296,6 +339,10 @@ public class ItemChooserDialog { |
| mDialog.show(); |
| } |
| + public void dismiss() { |
| + mDialog.dismiss(); |
| + } |
| + |
| /** |
| * Add items to show in the dialog. |
| * |
| @@ -306,17 +353,20 @@ public class ItemChooserDialog { |
| */ |
| public void showList(List<ItemChooserRow> list) { |
| mProgressBar.setVisibility(View.GONE); |
| - mStatus.setText(mLabels.mStatus); |
| - |
| - if (list.isEmpty()) { |
| - boolean showEmptyMessage = mItemAdapter.isEmpty(); |
| - mNotFoundMessage.setVisibility( |
| - showEmptyMessage ? View.VISIBLE : View.GONE); |
| - mListView.setVisibility(showEmptyMessage ? View.GONE : View.VISIBLE); |
| - } else { |
| - mListView.setVisibility(View.VISIBLE); |
| + |
| + if (!list.isEmpty()) { |
| mItemAdapter.addAll(list); |
| } |
| + setState(State.PROGRESS_UPDATE_AVAILABLE); |
| + } |
| + |
| + /** |
| + * Sets the availability of an item. |
| + * @param id The id of the item to affect. |
| + * @param available Whether the item should be available or not. |
| + */ |
| + public void setAvailability(String id, boolean available) { |
| + mItemAdapter.setAvailability(id, available); |
| } |
| /** |
| @@ -324,5 +374,38 @@ public class ItemChooserDialog { |
| */ |
| public void clear() { |
| mItemAdapter.clear(); |
| + setState(State.STARTING); |
| + } |
| + |
| + /** |
| + * Set the error state for the dialog. |
| + */ |
| + public void setErrorState() { |
| + setState(State.SHOWING_ERROR); |
| + } |
| + |
| + private void setState(State state) { |
| + switch (state) { |
| + case STARTING: |
| + mStatus.setText(mLabels.mSearching); |
| + mListView.setVisibility(View.GONE); |
| + mProgressBar.setVisibility(View.VISIBLE); |
| + break; |
| + case SHOWING_ERROR: |
| + mProgressBar.setVisibility(View.GONE); |
| + mEmptyMessage.setText(mLabels.mErrorMessage); |
| + mStatus.setText(mLabels.mErrorStatus); |
| + break; |
| + case PROGRESS_UPDATE_AVAILABLE: |
| + mStatus.setText(mLabels.mStatus); |
| + mProgressBar.setVisibility(View.GONE); |
| + mListView.setVisibility(View.VISIBLE); |
| + |
| + boolean showEmptyMessage = mItemAdapter.isEmpty(); |
| + mEmptyMessage.setText(mLabels.mNoneFound); |
| + mEmptyMessage.setVisibility( |
| + showEmptyMessage ? View.VISIBLE : View.GONE); |
| + break; |
| + } |
| } |
| } |