2011-11-18 22:28:17 +04:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-11-18 22:28:17 +04:00
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
2014-07-30 19:19:35 +04:00
|
|
|
import java.util.HashSet;
|
2012-08-03 04:13:40 +04:00
|
|
|
|
2015-06-18 20:53:09 +03:00
|
|
|
import android.widget.PopupWindow;
|
2013-06-22 07:36:35 +04:00
|
|
|
import org.json.JSONException;
|
2012-07-28 04:53:54 +04:00
|
|
|
import org.json.JSONObject;
|
2015-05-22 02:27:38 +03:00
|
|
|
import org.json.JSONArray;
|
2014-07-30 19:19:35 +04:00
|
|
|
import org.mozilla.gecko.AppConstants.Versions;
|
|
|
|
import org.mozilla.gecko.util.GeckoEventListener;
|
|
|
|
import org.mozilla.gecko.util.ThreadUtils;
|
2015-03-05 05:09:25 +03:00
|
|
|
import org.mozilla.gecko.widget.AnchoredPopup;
|
2014-07-30 19:19:35 +04:00
|
|
|
import org.mozilla.gecko.widget.DoorHanger;
|
2011-11-18 22:28:17 +04:00
|
|
|
|
2014-06-20 01:30:16 +04:00
|
|
|
import android.content.Context;
|
2011-11-18 22:28:17 +04:00
|
|
|
import android.util.Log;
|
2012-06-12 02:18:40 +04:00
|
|
|
import android.view.View;
|
2015-03-24 06:34:58 +03:00
|
|
|
import org.mozilla.gecko.widget.DoorhangerConfig;
|
2011-11-18 22:28:17 +04:00
|
|
|
|
2015-03-05 05:09:25 +03:00
|
|
|
public class DoorHangerPopup extends AnchoredPopup
|
2013-06-22 07:36:35 +04:00
|
|
|
implements GeckoEventListener,
|
|
|
|
Tabs.OnTabsChangedListener,
|
2015-06-18 20:53:09 +03:00
|
|
|
PopupWindow.OnDismissListener,
|
2013-06-22 07:36:35 +04:00
|
|
|
DoorHanger.OnButtonClickListener {
|
2011-11-18 22:28:17 +04:00
|
|
|
private static final String LOGTAG = "GeckoDoorHangerPopup";
|
|
|
|
|
2012-08-05 03:34:44 +04:00
|
|
|
// Stores a set of all active DoorHanger notifications. A DoorHanger is
|
|
|
|
// uniquely identified by its tabId and value.
|
2014-10-11 03:17:01 +04:00
|
|
|
private final HashSet<DoorHanger> mDoorHangers;
|
2012-08-05 03:34:44 +04:00
|
|
|
|
2013-09-14 03:07:17 +04:00
|
|
|
// Whether or not the doorhanger popup is disabled.
|
|
|
|
private boolean mDisabled;
|
|
|
|
|
2014-06-20 01:30:16 +04:00
|
|
|
public DoorHangerPopup(Context context) {
|
|
|
|
super(context);
|
2012-07-10 02:05:53 +04:00
|
|
|
|
2012-08-05 03:34:44 +04:00
|
|
|
mDoorHangers = new HashSet<DoorHanger>();
|
2012-08-05 03:34:42 +04:00
|
|
|
|
2014-05-02 22:34:40 +04:00
|
|
|
EventDispatcher.getInstance().registerGeckoThreadListener(this,
|
|
|
|
"Doorhanger:Add",
|
|
|
|
"Doorhanger:Remove");
|
2012-08-05 03:34:47 +04:00
|
|
|
Tabs.registerOnTabsChangedListener(this);
|
2015-06-18 20:53:09 +03:00
|
|
|
|
|
|
|
setOnDismissListener(this);
|
2012-08-05 03:34:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void destroy() {
|
2014-05-02 22:34:40 +04:00
|
|
|
EventDispatcher.getInstance().unregisterGeckoThreadListener(this,
|
|
|
|
"Doorhanger:Add",
|
|
|
|
"Doorhanger:Remove");
|
2012-08-05 03:34:47 +04:00
|
|
|
Tabs.unregisterOnTabsChangedListener(this);
|
2012-08-05 03:34:42 +04:00
|
|
|
}
|
|
|
|
|
2013-09-14 03:07:17 +04:00
|
|
|
/**
|
|
|
|
* Temporarily disables the doorhanger popup. If the popup is disabled,
|
|
|
|
* it will not be shown to the user, but it will continue to process
|
|
|
|
* calls to add/remove doorhanger notifications.
|
|
|
|
*/
|
|
|
|
void disable() {
|
|
|
|
mDisabled = true;
|
|
|
|
updatePopup();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Re-enables the doorhanger popup.
|
|
|
|
*/
|
|
|
|
void enable() {
|
|
|
|
mDisabled = false;
|
|
|
|
updatePopup();
|
|
|
|
}
|
|
|
|
|
2013-02-27 09:48:00 +04:00
|
|
|
@Override
|
2012-08-05 03:34:42 +04:00
|
|
|
public void handleMessage(String event, JSONObject geckoObject) {
|
|
|
|
try {
|
|
|
|
if (event.equals("Doorhanger:Add")) {
|
2015-03-24 06:34:58 +03:00
|
|
|
final DoorhangerConfig config = makeConfigFromJSON(geckoObject);
|
2012-08-05 03:34:42 +04:00
|
|
|
|
2014-06-20 01:30:16 +04:00
|
|
|
ThreadUtils.postToUiThread(new Runnable() {
|
2013-02-27 09:48:00 +04:00
|
|
|
@Override
|
2012-10-30 21:31:29 +04:00
|
|
|
public void run() {
|
2015-03-24 06:34:58 +03:00
|
|
|
addDoorHanger(config);
|
2012-10-30 21:31:29 +04:00
|
|
|
}
|
|
|
|
});
|
2012-08-05 03:34:42 +04:00
|
|
|
} else if (event.equals("Doorhanger:Remove")) {
|
2012-10-30 21:31:29 +04:00
|
|
|
final int tabId = geckoObject.getInt("tabID");
|
|
|
|
final String value = geckoObject.getString("value");
|
2012-08-05 03:34:42 +04:00
|
|
|
|
2014-06-20 01:30:16 +04:00
|
|
|
ThreadUtils.postToUiThread(new Runnable() {
|
2013-02-27 09:48:00 +04:00
|
|
|
@Override
|
2012-08-05 03:34:42 +04:00
|
|
|
public void run() {
|
2012-10-30 21:31:29 +04:00
|
|
|
DoorHanger doorHanger = getDoorHanger(tabId, value);
|
|
|
|
if (doorHanger == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
removeDoorHanger(doorHanger);
|
2012-08-05 03:34:42 +04:00
|
|
|
updatePopup();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e);
|
|
|
|
}
|
|
|
|
}
|
2011-11-18 22:28:17 +04:00
|
|
|
|
2015-03-24 06:34:58 +03:00
|
|
|
private DoorhangerConfig makeConfigFromJSON(JSONObject json) throws JSONException {
|
|
|
|
final int tabId = json.getInt("tabID");
|
|
|
|
final String id = json.getString("value");
|
2015-03-31 05:26:14 +03:00
|
|
|
|
|
|
|
final String typeString = json.optString("category");
|
2015-07-10 04:46:37 +03:00
|
|
|
DoorHanger.Type doorhangerType = DoorHanger.Type.DEFAULT;
|
|
|
|
if (DoorHanger.Type.LOGIN.toString().equals(typeString)) {
|
|
|
|
doorhangerType = DoorHanger.Type.LOGIN;
|
|
|
|
} else if (DoorHanger.Type.GEOLOCATION.toString().equals(typeString)) {
|
|
|
|
doorhangerType = DoorHanger.Type.GEOLOCATION;
|
|
|
|
}
|
2015-03-31 05:26:14 +03:00
|
|
|
|
|
|
|
final DoorhangerConfig config = new DoorhangerConfig(tabId, id, doorhangerType, this);
|
2015-03-24 06:34:58 +03:00
|
|
|
|
|
|
|
config.setMessage(json.getString("message"));
|
|
|
|
config.setOptions(json.getJSONObject("options"));
|
|
|
|
|
2015-05-22 02:27:38 +03:00
|
|
|
final JSONArray buttonArray = json.getJSONArray("buttons");
|
|
|
|
int numButtons = buttonArray.length();
|
|
|
|
if (numButtons > 2) {
|
|
|
|
Log.e(LOGTAG, "Doorhanger can have a maximum of two buttons!");
|
|
|
|
numButtons = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < numButtons; i++) {
|
|
|
|
final JSONObject buttonJSON = buttonArray.getJSONObject(i);
|
|
|
|
final boolean isPositive = buttonJSON.optBoolean("positive", false);
|
|
|
|
config.setButton(buttonJSON.getString("label"), buttonJSON.getInt("callback"), isPositive);
|
|
|
|
}
|
|
|
|
|
2015-03-24 06:34:58 +03:00
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2012-10-30 21:31:29 +04:00
|
|
|
// This callback is automatically executed on the UI thread.
|
2013-02-27 09:48:00 +04:00
|
|
|
@Override
|
2012-10-30 21:31:29 +04:00
|
|
|
public void onTabChanged(final Tab tab, final Tabs.TabEvents msg, final Object data) {
|
2012-08-05 03:34:47 +04:00
|
|
|
switch(msg) {
|
|
|
|
case CLOSED:
|
2012-08-30 03:42:15 +04:00
|
|
|
// Remove any doorhangers for a tab when it's closed (make
|
|
|
|
// a temporary set to avoid a ConcurrentModificationException)
|
2015-06-18 20:53:09 +03:00
|
|
|
removeTabDoorHangers(tab.getId(), true);
|
2012-08-05 03:34:47 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCATION_CHANGE:
|
|
|
|
// Only remove doorhangers if the popup is hidden or if we're navigating to a new URL
|
|
|
|
if (!isShowing() || !data.equals(tab.getURL()))
|
2015-06-18 20:53:09 +03:00
|
|
|
removeTabDoorHangers(tab.getId(), false);
|
2012-08-05 03:34:47 +04:00
|
|
|
|
|
|
|
// Update the popup if the location change was on the current tab
|
|
|
|
if (Tabs.getInstance().isSelectedTab(tab))
|
|
|
|
updatePopup();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SELECTED:
|
|
|
|
// Always update the popup when a new tab is selected. This will cover cases
|
|
|
|
// where a different tab was closed, since we always need to select a new tab.
|
|
|
|
updatePopup();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-30 21:31:29 +04:00
|
|
|
/**
|
|
|
|
* Adds a doorhanger.
|
|
|
|
*
|
|
|
|
* This method must be called on the UI thread.
|
|
|
|
*/
|
2015-03-24 06:34:58 +03:00
|
|
|
void addDoorHanger(DoorhangerConfig config) {
|
|
|
|
final int tabId = config.getTabId();
|
2012-08-05 03:34:44 +04:00
|
|
|
// Don't add a doorhanger for a tab that doesn't exist
|
2013-06-22 07:36:35 +04:00
|
|
|
if (Tabs.getInstance().getTab(tabId) == null) {
|
2012-08-05 03:34:44 +04:00
|
|
|
return;
|
2013-06-22 07:36:35 +04:00
|
|
|
}
|
2012-02-13 22:29:29 +04:00
|
|
|
|
2011-11-18 22:28:17 +04:00
|
|
|
// Replace the doorhanger if it already exists
|
2015-03-24 06:34:58 +03:00
|
|
|
DoorHanger oldDoorHanger = getDoorHanger(tabId, config.getId());
|
2013-06-22 07:36:35 +04:00
|
|
|
if (oldDoorHanger != null) {
|
2012-08-05 03:34:44 +04:00
|
|
|
removeDoorHanger(oldDoorHanger);
|
2013-06-22 07:36:35 +04:00
|
|
|
}
|
2012-08-05 03:34:44 +04:00
|
|
|
|
2013-06-22 07:36:35 +04:00
|
|
|
if (!mInflated) {
|
2012-10-30 21:31:29 +04:00
|
|
|
init();
|
2013-06-22 07:36:35 +04:00
|
|
|
}
|
2012-08-05 03:34:44 +04:00
|
|
|
|
2015-03-24 06:34:58 +03:00
|
|
|
final DoorHanger newDoorHanger = DoorHanger.Get(mContext, config);
|
2013-06-22 07:36:35 +04:00
|
|
|
|
|
|
|
mDoorHangers.add(newDoorHanger);
|
2012-10-30 21:31:29 +04:00
|
|
|
mContent.addView(newDoorHanger);
|
2012-08-05 03:34:44 +04:00
|
|
|
|
2014-10-12 03:28:29 +04:00
|
|
|
// Only update the popup if we're adding a notification to the selected tab
|
2012-10-30 21:31:29 +04:00
|
|
|
if (tabId == Tabs.getInstance().getSelectedTab().getId())
|
|
|
|
updatePopup();
|
2012-08-05 03:34:44 +04:00
|
|
|
}
|
|
|
|
|
2013-06-22 07:36:35 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* DoorHanger.OnButtonClickListener implementation
|
|
|
|
*/
|
|
|
|
@Override
|
2015-03-31 05:26:14 +03:00
|
|
|
public void onButtonClick(JSONObject response, DoorHanger doorhanger) {
|
2013-06-22 07:36:35 +04:00
|
|
|
GeckoEvent e = GeckoEvent.createBroadcastEvent("Doorhanger:Reply", response.toString());
|
|
|
|
GeckoAppShell.sendEventToGecko(e);
|
2015-03-31 05:26:14 +03:00
|
|
|
removeDoorHanger(doorhanger);
|
2013-06-22 07:36:35 +04:00
|
|
|
updatePopup();
|
|
|
|
}
|
|
|
|
|
2012-10-30 21:31:29 +04:00
|
|
|
/**
|
|
|
|
* Gets a doorhanger.
|
|
|
|
*
|
|
|
|
* This method must be called on the UI thread.
|
|
|
|
*/
|
2012-08-05 03:34:44 +04:00
|
|
|
DoorHanger getDoorHanger(int tabId, String value) {
|
|
|
|
for (DoorHanger dh : mDoorHangers) {
|
2015-03-19 01:51:10 +03:00
|
|
|
if (dh.getTabId() == tabId && dh.getIdentifier().equals(value))
|
2012-08-05 03:34:44 +04:00
|
|
|
return dh;
|
2011-11-18 22:28:17 +04:00
|
|
|
}
|
2012-08-05 03:34:44 +04:00
|
|
|
|
|
|
|
// If there's no doorhanger for the given tabId and value, return null
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-10-30 21:31:29 +04:00
|
|
|
/**
|
|
|
|
* Removes a doorhanger.
|
|
|
|
*
|
|
|
|
* This method must be called on the UI thread.
|
|
|
|
*/
|
2012-08-05 03:34:44 +04:00
|
|
|
void removeDoorHanger(final DoorHanger doorHanger) {
|
|
|
|
mDoorHangers.remove(doorHanger);
|
2012-10-30 21:31:29 +04:00
|
|
|
mContent.removeView(doorHanger);
|
2012-08-05 03:34:44 +04:00
|
|
|
}
|
2011-11-18 22:28:17 +04:00
|
|
|
|
2012-10-30 21:31:29 +04:00
|
|
|
/**
|
|
|
|
* Removes doorhangers for a given tab.
|
2015-06-18 20:53:09 +03:00
|
|
|
* @param tabId identifier of the tab to remove doorhangers from
|
|
|
|
* @param forceRemove boolean for force-removing tabs. If true, all doorhangers associated
|
|
|
|
* with the tab specified are removed; if false, only remove the doorhangers
|
|
|
|
* that are not persistent, as specified by the doorhanger options.
|
2012-10-30 21:31:29 +04:00
|
|
|
*
|
|
|
|
* This method must be called on the UI thread.
|
|
|
|
*/
|
2015-06-18 20:53:09 +03:00
|
|
|
void removeTabDoorHangers(int tabId, boolean forceRemove) {
|
2012-08-05 03:34:44 +04:00
|
|
|
// Make a temporary set to avoid a ConcurrentModificationException
|
|
|
|
HashSet<DoorHanger> doorHangersToRemove = new HashSet<DoorHanger>();
|
|
|
|
for (DoorHanger dh : mDoorHangers) {
|
|
|
|
// Only remove transient doorhangers for the given tab
|
2015-06-18 20:53:09 +03:00
|
|
|
if (dh.getTabId() == tabId
|
|
|
|
&& (forceRemove || (!forceRemove && dh.shouldRemove(isShowing())))) {
|
|
|
|
doorHangersToRemove.add(dh);
|
|
|
|
}
|
2012-08-05 03:34:44 +04:00
|
|
|
}
|
2011-11-18 22:28:17 +04:00
|
|
|
|
2012-08-05 03:34:44 +04:00
|
|
|
for (DoorHanger dh : doorHangersToRemove) {
|
|
|
|
removeDoorHanger(dh);
|
|
|
|
}
|
2011-11-18 22:28:17 +04:00
|
|
|
}
|
|
|
|
|
2012-10-30 21:31:29 +04:00
|
|
|
/**
|
|
|
|
* Updates the popup state.
|
|
|
|
*
|
|
|
|
* This method must be called on the UI thread.
|
|
|
|
*/
|
2013-11-15 04:06:01 +04:00
|
|
|
void updatePopup() {
|
2012-08-05 03:34:44 +04:00
|
|
|
// Bail if the selected tab is null, if there are no active doorhangers,
|
2013-09-14 03:07:17 +04:00
|
|
|
// if we haven't inflated the layout yet (this can happen if updatePopup()
|
|
|
|
// is called before the runnable from addDoorHanger() runs), or if the
|
|
|
|
// doorhanger popup is temporarily disabled.
|
2011-11-18 22:28:17 +04:00
|
|
|
Tab tab = Tabs.getInstance().getSelectedTab();
|
2013-09-14 03:07:17 +04:00
|
|
|
if (tab == null || mDoorHangers.size() == 0 || !mInflated || mDisabled) {
|
2012-08-05 03:34:42 +04:00
|
|
|
dismiss();
|
2011-11-22 02:24:24 +04:00
|
|
|
return;
|
|
|
|
}
|
2012-08-05 03:34:44 +04:00
|
|
|
|
|
|
|
// Show doorhangers for the selected tab
|
|
|
|
int tabId = tab.getId();
|
|
|
|
boolean shouldShowPopup = false;
|
2015-06-17 00:03:09 +03:00
|
|
|
DoorHanger firstDoorhanger = null;
|
2012-08-05 03:34:44 +04:00
|
|
|
for (DoorHanger dh : mDoorHangers) {
|
|
|
|
if (dh.getTabId() == tabId) {
|
|
|
|
dh.setVisibility(View.VISIBLE);
|
|
|
|
shouldShowPopup = true;
|
2015-06-17 00:03:09 +03:00
|
|
|
if (firstDoorhanger == null) {
|
|
|
|
firstDoorhanger = dh;
|
|
|
|
} else {
|
|
|
|
dh.hideTitle();
|
|
|
|
}
|
2012-08-05 03:34:44 +04:00
|
|
|
} else {
|
|
|
|
dh.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
}
|
2015-06-17 00:03:09 +03:00
|
|
|
|
2012-08-05 03:34:44 +04:00
|
|
|
// Dismiss the popup if there are no doorhangers to show for this tab
|
|
|
|
if (!shouldShowPopup) {
|
2012-08-05 03:34:42 +04:00
|
|
|
dismiss();
|
2011-11-18 22:28:17 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-12 01:07:11 +04:00
|
|
|
showDividers();
|
2012-07-10 02:05:53 +04:00
|
|
|
if (isShowing()) {
|
2013-11-15 04:06:01 +04:00
|
|
|
show();
|
2012-07-10 02:05:53 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-17 00:03:09 +03:00
|
|
|
firstDoorhanger.showTitle(tab.getFavicon(), tab.getBaseDomain());
|
|
|
|
|
2013-06-13 23:42:45 +04:00
|
|
|
// Make the popup focusable for accessibility. This gets done here
|
|
|
|
// so the node can be accessibility focused, but on pre-ICS devices this
|
|
|
|
// causes crashes, so it is done after the popup is shown.
|
2014-07-30 19:19:35 +04:00
|
|
|
if (Versions.feature14Plus) {
|
2013-06-13 23:42:45 +04:00
|
|
|
setFocusable(true);
|
|
|
|
}
|
|
|
|
|
2013-06-20 19:06:15 +04:00
|
|
|
show();
|
2012-08-05 03:34:42 +04:00
|
|
|
|
2014-07-30 19:19:35 +04:00
|
|
|
if (Versions.preICS) {
|
2013-06-13 23:42:45 +04:00
|
|
|
// Make the popup focusable for keyboard accessibility.
|
|
|
|
setFocusable(true);
|
|
|
|
}
|
2011-11-18 22:28:17 +04:00
|
|
|
}
|
|
|
|
|
2013-07-02 16:10:27 +04:00
|
|
|
//Show all inter-DoorHanger dividers (ie. Dividers on all visible DoorHangers except the last one)
|
2013-03-12 01:07:11 +04:00
|
|
|
private void showDividers() {
|
|
|
|
int count = mContent.getChildCount();
|
2013-07-02 16:10:27 +04:00
|
|
|
DoorHanger lastVisibleDoorHanger = null;
|
2013-03-12 01:07:11 +04:00
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
DoorHanger dh = (DoorHanger) mContent.getChildAt(i);
|
|
|
|
dh.showDivider();
|
2013-07-02 16:10:27 +04:00
|
|
|
if (dh.getVisibility() == View.VISIBLE) {
|
|
|
|
lastVisibleDoorHanger = dh;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lastVisibleDoorHanger != null) {
|
|
|
|
lastVisibleDoorHanger.hideDivider();
|
2013-03-12 01:07:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 20:53:09 +03:00
|
|
|
@Override
|
|
|
|
public void onDismiss() {
|
|
|
|
final int tabId = Tabs.getInstance().getSelectedTab().getId();
|
|
|
|
removeTabDoorHangers(tabId, true);
|
|
|
|
}
|
|
|
|
|
2012-09-08 09:31:54 +04:00
|
|
|
@Override
|
|
|
|
public void dismiss() {
|
|
|
|
// If the popup is focusable while it is hidden, we run into crashes
|
|
|
|
// on pre-ICS devices when the popup gets focus before it is shown.
|
|
|
|
setFocusable(false);
|
|
|
|
super.dismiss();
|
|
|
|
}
|
2011-11-18 22:28:17 +04:00
|
|
|
}
|