mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-25 13:51:41 +00:00
Bug 1270543 - Part 1: Enable double tapping to select an action in the prompt dialog. r=liuche
On newer Android versions, double tapping an entry in the app chooser is equivalent to tapping it once and selecting "Just once". To enable this functionality for our own app chooser when downloading a file, this patch enables passing a button index to the prompt dialog which will be chosen by default if a double tap has been detected. To do this, we track the input value we receive in onChange. If we receive the same value twice in a row and a button has been configured, we close the dialogue and pass the result with that button back to the caller. MozReview-Commit-ID: EVs2x3OtHmB --HG-- extra : transplant_source : %85Td%83%0D%DD%D0%1D%F48a%5D%A0%CF%B4%A5%CE%5E%22%7E extra : histedit_source : 190cf52e481b637172ea3b49ccec606f31dc86cf
This commit is contained in:
parent
4da5c1c52b
commit
222df1171c
@ -41,6 +41,7 @@ public class Prompt implements OnClickListener, OnCancelListener, OnItemClickLis
|
||||
private String[] mButtons;
|
||||
private PromptInput[] mInputs;
|
||||
private AlertDialog mDialog;
|
||||
private int mDoubleTapButtonType;
|
||||
|
||||
private final LayoutInflater mInflater;
|
||||
private final Context mContext;
|
||||
@ -52,6 +53,7 @@ public class Prompt implements OnClickListener, OnCancelListener, OnItemClickLis
|
||||
private static int mInputPaddingSize;
|
||||
|
||||
private int mTabId = Tabs.INVALID_TAB_ID;
|
||||
private Object mPreviousInputValue = null;
|
||||
|
||||
public Prompt(Context context, PromptCallback callback) {
|
||||
this(context);
|
||||
@ -83,6 +85,9 @@ public class Prompt implements OnClickListener, OnCancelListener, OnItemClickLis
|
||||
mGuid = message.optString("guid");
|
||||
|
||||
mButtons = getStringArray(message, "buttons");
|
||||
final int buttonCount = mButtons == null ? 0 : mButtons.length;
|
||||
mDoubleTapButtonType = convertIndexToButtonType(message.optInt("doubleTapButton", -1), buttonCount);
|
||||
mPreviousInputValue = null;
|
||||
|
||||
JSONArray inputs = getSafeArray(message, "inputs");
|
||||
mInputs = new PromptInput[inputs.length()];
|
||||
@ -110,6 +115,25 @@ public class Prompt implements OnClickListener, OnCancelListener, OnItemClickLis
|
||||
show(title, text, menuitems, choiceMode);
|
||||
}
|
||||
|
||||
private int convertIndexToButtonType(final int buttonIndex, final int buttonCount) {
|
||||
if (buttonIndex < 0 || buttonIndex >= buttonCount) {
|
||||
// All valid DialogInterface button values are < 0,
|
||||
// so we return 0 as an invalid value.
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (buttonIndex) {
|
||||
case 0:
|
||||
return DialogInterface.BUTTON_POSITIVE;
|
||||
case 1:
|
||||
return DialogInterface.BUTTON_NEUTRAL;
|
||||
case 2:
|
||||
return DialogInterface.BUTTON_NEGATIVE;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void show(String title, String text, PromptListItem[] listItems, int choiceMode) {
|
||||
ThreadUtils.assertOnUiThread();
|
||||
|
||||
@ -507,7 +531,21 @@ public class Prompt implements OnClickListener, OnCancelListener, OnItemClickLis
|
||||
// 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);
|
||||
if (!closeIfNoButtons(-1)) {
|
||||
// Alternatively, if a default button has been specified for double tapping,
|
||||
// we want to close the dialog if the same input value has been transmitted
|
||||
// twice in a row.
|
||||
closeIfDoubleTapEnabled(input.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
private boolean closeIfDoubleTapEnabled(Object inputValue) {
|
||||
if (mDoubleTapButtonType != 0 && inputValue == mPreviousInputValue) {
|
||||
closeDialog(mDoubleTapButtonType);
|
||||
return true;
|
||||
}
|
||||
mPreviousInputValue = inputValue;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static JSONArray getSafeArray(JSONObject json, String key) {
|
||||
|
@ -43,6 +43,9 @@ function Prompt(aOptions) {
|
||||
if ("buttons" in aOptions && aOptions.buttons != null)
|
||||
this.msg.buttons = aOptions.buttons;
|
||||
|
||||
if ("doubleTapButton" in aOptions && aOptions.doubleTapButton != null)
|
||||
this.msg.doubleTapButton = aOptions.doubleTapButton;
|
||||
|
||||
if ("hint" in aOptions && aOptions.hint != null)
|
||||
this.msg.hint = aOptions.hint;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user