From cad87be1811b19ebc6ed13c350343797dd9eccc4 Mon Sep 17 00:00:00 2001 From: Wesley Johnston Date: Thu, 6 Feb 2014 16:26:00 -0800 Subject: [PATCH] Bug 959742 - Auto close lists if they don't have any buttons on them. r=bnicholson --- .../android/base/prompts/IconGridInput.java | 1 + mobile/android/base/prompts/Prompt.java | 57 +++++++++++++------ mobile/android/base/prompts/PromptInput.java | 15 +++++ mobile/android/base/prompts/TabInput.java | 1 + 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/mobile/android/base/prompts/IconGridInput.java b/mobile/android/base/prompts/IconGridInput.java index d5189c3ed1d..2701d6fa16c 100644 --- a/mobile/android/base/prompts/IconGridInput.java +++ b/mobile/android/base/prompts/IconGridInput.java @@ -92,6 +92,7 @@ public class IconGridInput extends PromptInput implements OnItemClickListener { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { mSelected = position; + notifyListeners(Integer.toString(position)); } @Override diff --git a/mobile/android/base/prompts/Prompt.java b/mobile/android/base/prompts/Prompt.java index 9d7471a156b..accbd50abd8 100644 --- a/mobile/android/base/prompts/Prompt.java +++ b/mobile/android/base/prompts/Prompt.java @@ -38,7 +38,8 @@ import android.widget.TextView; import java.util.ArrayList; -public class Prompt implements OnClickListener, OnCancelListener, OnItemClickListener { +public class Prompt implements OnClickListener, OnCancelListener, OnItemClickListener, + PromptInput.OnChangeListener { private static final String LOGTAG = "GeckoPromptService"; private String[] mButtons; @@ -203,23 +204,7 @@ public class Prompt implements OnClickListener, OnCancelListener, OnItemClickLis @Override public void onClick(DialogInterface dialog, int which) { ThreadUtils.assertOnUiThread(); - JSONObject ret = new JSONObject(); - try { - addButtonResult(ret, which); - addInputValues(ret); - - if (mAdapter != null) { - addListResult(ret, which); - } - } catch(Exception ex) { - Log.i(LOGTAG, "Error building return: " + ex); - } - - if (dialog != null) { - dialog.dismiss(); - } - - finishDialog(ret); + closeIfNoButtons(which); } /* Adds a set of list items to the prompt. This can be used for either context menu type dialogs, checked lists, @@ -360,6 +345,19 @@ public class Prompt implements OnClickListener, OnCancelListener, OnItemClickLis public void onItemClick(AdapterView parent, View view, int position, long id) { ThreadUtils.assertOnUiThread(); mAdapter.toggleSelected(position); + + // If there are no buttons on this dialog, then we take selecting an item as a sign to close + // the dialog. Note that means it will be hard to select multiple things in this list, but + // given there is no way to confirm+close the dialog, it seems reasonable. + closeIfNoButtons(position); + } + + private boolean closeIfNoButtons(int selected) { + if (mButtons == null || mButtons.length == 0) { + closeDialog(selected); + return true; + } + return false; } /* @DialogInterface.OnCancelListener @@ -387,6 +385,20 @@ public class Prompt implements OnClickListener, OnCancelListener, OnItemClickLis finishDialog(ret); } + /* Called any time we're closing the dialog to cleanup and notify listeners that the dialog + * is closing. + */ + public void closeDialog(int which) { + JSONObject ret = new JSONObject(); + mDialog.dismiss(); + + addButtonResult(ret, which); + addListResult(ret, which); + addInputValues(ret); + + finishDialog(ret); + } + /* Called any time we're closing the dialog to cleanup and notify listeners that the dialog * is closing. */ @@ -421,6 +433,7 @@ public class Prompt implements OnClickListener, OnCancelListener, OnItemClickLis for (int i = 0; i < mInputs.length; i++) { try { mInputs[i] = PromptInput.getInput(inputs.getJSONObject(i)); + mInputs[i].setListener(this); } catch(Exception ex) { } } @@ -437,6 +450,14 @@ public class Prompt implements OnClickListener, OnCancelListener, OnItemClickLis show(title, text, menuitems, choiceMode); } + // Called when the prompt inputs on the dialog change + public void onChange(PromptInput input) { + // If there are no buttons on this dialog, assuming that "changing" an input + // means something was selected and we can close. This provides a way to tap + // on a list item and close the dialog automatically. + closeIfNoButtons(-1); + } + private static JSONArray getSafeArray(JSONObject json, String key) { try { return json.getJSONArray(key); diff --git a/mobile/android/base/prompts/PromptInput.java b/mobile/android/base/prompts/PromptInput.java index fa2ad035f3d..d61553c66e6 100644 --- a/mobile/android/base/prompts/PromptInput.java +++ b/mobile/android/base/prompts/PromptInput.java @@ -38,9 +38,18 @@ public class PromptInput { protected final String mType; protected final String mId; protected final String mValue; + protected OnChangeListener mListener; protected View mView; public static final String LOGTAG = "GeckoPromptInput"; + public interface OnChangeListener { + public void onChange(PromptInput input); + } + + public void setListener(OnChangeListener listener) { + mListener = listener; + } + public static class EditInput extends PromptInput { protected final String mHint; protected final boolean mAutofocus; @@ -376,4 +385,10 @@ public class PromptInput { public boolean canApplyInputStyle() { return true; } + + protected void notifyListeners(String val) { + if (mListener != null) { + mListener.onChange(this); + } + } } diff --git a/mobile/android/base/prompts/TabInput.java b/mobile/android/base/prompts/TabInput.java index d64101d0026..41f0637eaac 100644 --- a/mobile/android/base/prompts/TabInput.java +++ b/mobile/android/base/prompts/TabInput.java @@ -104,6 +104,7 @@ public class TabInput extends PromptInput implements AdapterView.OnItemClickList public void onItemClick(AdapterView parent, View view, int position, long id) { ThreadUtils.assertOnUiThread(); mPosition = position; + notifyListeners(Integer.toString(position)); } }