Bug 959742 - Close dialogs when inputs change if there are no buttons on the dialog. r=bnicholson

This commit is contained in:
Wesley Johnston 2014-02-06 16:26:00 -08:00
Родитель 95bdc0d0fb
Коммит a816aa2261
5 изменённых файлов: 69 добавлений и 20 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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;
@ -148,6 +149,10 @@ public class Prompt implements OnClickListener, OnCancelListener, OnItemClickLis
* selected attribute to an array of booleans.
*/
private void addListResult(final JSONObject result, int which) {
if (mAdapter == null) {
return;
}
try {
JSONArray selected = new JSONArray();
@ -203,23 +208,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);
closeDialog(which);
}
/* Adds a set of list items to the prompt. This can be used for either context menu type dialogs, checked lists,
@ -275,7 +264,12 @@ public class Prompt implements OnClickListener, OnCancelListener, OnItemClickLis
*/
private void addSingleSelectList(AlertDialog.Builder builder, PromptListItem[] listItems) {
mAdapter = new PromptListAdapter(mContext, R.layout.select_dialog_singlechoice, listItems);
builder.setSingleChoiceItems(mAdapter, mAdapter.getSelectedIndex(), this);
builder.setSingleChoiceItems(mAdapter, mAdapter.getSelectedIndex(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
closeIfNoButtons(which);
}
});
}
/* Shows a single-select list.
@ -360,6 +354,20 @@ 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) {
ThreadUtils.assertOnUiThread();
if (mButtons == null || mButtons.length == 0) {
closeDialog(selected);
return true;
}
return false;
}
/* @DialogInterface.OnCancelListener
@ -387,6 +395,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.
*/
private 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 +443,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 +460,15 @@ public class Prompt implements OnClickListener, OnCancelListener, OnItemClickLis
show(title, text, menuitems, choiceMode);
}
// Called when the prompt inputs on the dialog change
@Override
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);

Просмотреть файл

@ -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);
}
}
}

Просмотреть файл

@ -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));
}
}

Просмотреть файл

@ -12,7 +12,7 @@ Components.utils.import("resource://gre/modules/Messaging.jsm");
this.EXPORTED_SYMBOLS = ["Prompt"];
function log(msg) {
//Services.console.logStringMessage(msg);
Services.console.logStringMessage(msg);
}
function Prompt(aOptions) {