Bug 732169 - Associate prompts with tabs. r=lucasr

This commit is contained in:
Wes Johnston 2014-08-14 23:17:04 -07:00
parent 0395008ae9
commit 072c4af638
3 changed files with 58 additions and 7 deletions

View File

@ -63,6 +63,8 @@ public class Tabs implements GeckoEventListener {
private static final long PERSIST_TABS_AFTER_MILLISECONDS = 1000 * 5;
public static final int INVALID_TAB_ID = -1;
private static AtomicInteger sTabId = new AtomicInteger(0);
private volatile boolean mInitialTabsAdded;

View File

@ -12,6 +12,8 @@ import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.GeckoEvent;
import org.mozilla.gecko.R;
import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.gecko.Tab;
import org.mozilla.gecko.Tabs;
import android.app.AlertDialog;
import android.content.Context;
@ -39,7 +41,7 @@ import android.widget.TextView;
import java.util.ArrayList;
public class Prompt implements OnClickListener, OnCancelListener, OnItemClickListener,
PromptInput.OnChangeListener {
PromptInput.OnChangeListener, Tabs.OnTabsChangedListener {
private static final String LOGTAG = "GeckoPromptService";
private String[] mButtons;
@ -55,6 +57,8 @@ public class Prompt implements OnClickListener, OnCancelListener, OnItemClickLis
private static boolean mInitialized;
private static int mInputPaddingSize;
private int mTabId = Tabs.INVALID_TAB_ID;
public Prompt(Context context, PromptCallback callback) {
this(context);
mCallback = callback;
@ -105,6 +109,10 @@ public class Prompt implements OnClickListener, OnCancelListener, OnItemClickLis
choiceMode = ListView.CHOICE_MODE_MULTIPLE;
}
if (message.has("tabId")) {
mTabId = message.optInt("tabId", Tabs.INVALID_TAB_ID);
}
show(title, text, menuitems, choiceMode);
}
@ -118,8 +126,39 @@ public class Prompt implements OnClickListener, OnCancelListener, OnItemClickLis
return;
}
if (mTabId != Tabs.INVALID_TAB_ID) {
Tabs.registerOnTabsChangedListener(this);
final Tab tab = Tabs.getInstance().getTab(mTabId);
if (Tabs.getInstance().getSelectedTab() == tab) {
mDialog.show();
}
} else {
mDialog.show();
}
}
@Override
public void onTabChanged(final Tab tab, final Tabs.TabEvents msg, final Object data) {
if (tab != Tabs.getInstance().getTab(mTabId)) {
return;
}
switch(msg) {
case SELECTED:
Log.i(LOGTAG, "Selected");
mDialog.show();
break;
case UNSELECTED:
Log.i(LOGTAG, "Unselected");
mDialog.hide();
break;
case LOCATION_CHANGE:
Log.i(LOGTAG, "Location change");
mDialog.cancel();
break;
}
}
private void create(String title, String text, PromptListItem[] listItems, int choiceMode)
throws IllegalStateException {
@ -140,8 +179,7 @@ public class Prompt implements OnClickListener, OnCancelListener, OnItemClickLis
if (listItems != null && listItems.length > 0) {
addListItems(builder, listItems, choiceMode);
} else if (!addInputs(builder)) {
// If we failed to add any requested input elements, don't show the dialog
return;
throw new IllegalStateException("Could not add inputs to dialog");
}
int length = mButtons == null ? 0 : mButtons.length;
@ -452,6 +490,10 @@ public class Prompt implements OnClickListener, OnCancelListener, OnItemClickLis
aReturn.put("guid", mGuid);
} catch(JSONException ex) { }
if (mTabId != Tabs.INVALID_TAB_ID) {
Tabs.unregisterOnTabsChangedListener(this);
}
// poke the Gecko thread in case it's waiting for new events
GeckoAppShell.sendEventToGecko(GeckoEvent.createNoOpEvent());

View File

@ -18,8 +18,17 @@ function log(msg) {
function Prompt(aOptions) {
this.window = "window" in aOptions ? aOptions.window : null;
this.msg = { async: true };
if (this.window) {
let window = Services.wm.getMostRecentWindow("navigator:browser");
var tab = window.BrowserApp.getTabForWindow(this.window);
if (tab) {
this.msg.tabId = tab.id;
}
}
if (aOptions.priority === 1)
this.msg.type = "Prompt:ShowTop"
else
@ -36,8 +45,6 @@ function Prompt(aOptions) {
if ("hint" in aOptions && aOptions.hint != null)
this.msg.hint = aOptions.hint;
let idService = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);
}
Prompt.prototype = {