Bug 1088220 - Switch to using DoorhangerConfig. r=margaret

This commit is contained in:
Chenxia Liu 2015-03-23 20:34:58 -07:00
parent 4de91f73a1
commit 45d4382eea
4 changed files with 91 additions and 68 deletions

View File

@ -16,13 +16,13 @@ import org.mozilla.gecko.prompts.PromptInput;
import org.mozilla.gecko.util.GeckoEventListener; import org.mozilla.gecko.util.GeckoEventListener;
import org.mozilla.gecko.util.ThreadUtils; import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.gecko.widget.AnchoredPopup; import org.mozilla.gecko.widget.AnchoredPopup;
import org.mozilla.gecko.widget.DefaultDoorHanger;
import org.mozilla.gecko.widget.DoorHanger; import org.mozilla.gecko.widget.DoorHanger;
import android.content.Context; import android.content.Context;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.CheckBox; import android.widget.CheckBox;
import org.mozilla.gecko.widget.DoorhangerConfig;
public class DoorHangerPopup extends AnchoredPopup public class DoorHangerPopup extends AnchoredPopup
implements GeckoEventListener, implements GeckoEventListener,
@ -77,16 +77,12 @@ public class DoorHangerPopup extends AnchoredPopup
public void handleMessage(String event, JSONObject geckoObject) { public void handleMessage(String event, JSONObject geckoObject) {
try { try {
if (event.equals("Doorhanger:Add")) { if (event.equals("Doorhanger:Add")) {
final int tabId = geckoObject.getInt("tabID"); final DoorhangerConfig config = makeConfigFromJSON(geckoObject);
final String value = geckoObject.getString("value");
final String message = geckoObject.getString("message");
final JSONArray buttons = geckoObject.getJSONArray("buttons");
final JSONObject options = geckoObject.getJSONObject("options");
ThreadUtils.postToUiThread(new Runnable() { ThreadUtils.postToUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
addDoorHanger(tabId, value, message, buttons, options); addDoorHanger(config);
} }
}); });
} else if (event.equals("Doorhanger:Remove")) { } else if (event.equals("Doorhanger:Remove")) {
@ -110,6 +106,22 @@ public class DoorHangerPopup extends AnchoredPopup
} }
} }
private DoorhangerConfig makeConfigFromJSON(JSONObject json) throws JSONException {
final int tabId = json.getInt("tabID");
final String id = json.getString("value");
final DoorhangerConfig config = new DoorhangerConfig(tabId, id);
config.setMessage(json.getString("message"));
config.setButtons(json.getJSONArray("buttons"));
config.setOptions(json.getJSONObject("options"));
final String typeString = json.optString("category");
if (DoorHanger.Type.PASSWORD.toString().equals(typeString)) {
config.setType(DoorHanger.Type.PASSWORD);
}
return config;
}
// This callback is automatically executed on the UI thread. // This callback is automatically executed on the UI thread.
@Override @Override
public void onTabChanged(final Tab tab, final Tabs.TabEvents msg, final Object data) { public void onTabChanged(final Tab tab, final Tabs.TabEvents msg, final Object data) {
@ -150,15 +162,15 @@ public class DoorHangerPopup extends AnchoredPopup
* *
* This method must be called on the UI thread. * This method must be called on the UI thread.
*/ */
void addDoorHanger(final int tabId, final String value, final String message, void addDoorHanger(DoorhangerConfig config) {
final JSONArray buttons, final JSONObject options) { final int tabId = config.getTabId();
// Don't add a doorhanger for a tab that doesn't exist // Don't add a doorhanger for a tab that doesn't exist
if (Tabs.getInstance().getTab(tabId) == null) { if (Tabs.getInstance().getTab(tabId) == null) {
return; return;
} }
// Replace the doorhanger if it already exists // Replace the doorhanger if it already exists
DoorHanger oldDoorHanger = getDoorHanger(tabId, value); DoorHanger oldDoorHanger = getDoorHanger(tabId, config.getId());
if (oldDoorHanger != null) { if (oldDoorHanger != null) {
removeDoorHanger(oldDoorHanger); removeDoorHanger(oldDoorHanger);
} }
@ -167,10 +179,9 @@ public class DoorHangerPopup extends AnchoredPopup
init(); init();
} }
final DoorHanger newDoorHanger = new DefaultDoorHanger(mContext, tabId, value); final DoorHanger newDoorHanger = DoorHanger.Get(mContext, config);
newDoorHanger.setMessage(message);
newDoorHanger.setOptions(options);
final JSONArray buttons = config.getButtons();
for (int i = 0; i < buttons.length(); i++) { for (int i = 0; i < buttons.length(); i++) {
try { try {
JSONObject buttonObject = buttons.getJSONObject(i); JSONObject buttonObject = buttons.getJSONObject(i);

View File

@ -15,7 +15,6 @@ import org.mozilla.gecko.SiteIdentity.TrackingMode;
import org.mozilla.gecko.Tab; import org.mozilla.gecko.Tab;
import org.mozilla.gecko.Tabs; import org.mozilla.gecko.Tabs;
import org.mozilla.gecko.widget.AnchoredPopup; import org.mozilla.gecko.widget.AnchoredPopup;
import org.mozilla.gecko.widget.DefaultDoorHanger;
import org.mozilla.gecko.widget.DoorHanger; import org.mozilla.gecko.widget.DoorHanger;
import org.mozilla.gecko.widget.DoorHanger.OnButtonClickListener; import org.mozilla.gecko.widget.DoorHanger.OnButtonClickListener;
import org.json.JSONException; import org.json.JSONException;
@ -28,6 +27,7 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import org.mozilla.gecko.widget.DoorhangerConfig;
/** /**
* SiteIdentityPopup is a singleton class that displays site identity data in * SiteIdentityPopup is a singleton class that displays site identity data in
@ -141,22 +141,22 @@ public class SiteIdentityPopup extends AnchoredPopup {
private void addMixedContentNotification(boolean blocked) { private void addMixedContentNotification(boolean blocked) {
// Remove any existing mixed content notification. // Remove any existing mixed content notification.
removeMixedContentNotification(); removeMixedContentNotification();
mMixedContentNotification = new DefaultDoorHanger(mContext, DoorHanger.Type.SITE);
final DoorhangerConfig config = new DoorhangerConfig();
int icon; int icon;
String message;
if (blocked) { if (blocked) {
icon = R.drawable.shield_enabled_doorhanger; icon = R.drawable.shield_enabled_doorhanger;
message = mContext.getString(R.string.blocked_mixed_content_message_top) + "\n\n" + config.setMessage(mContext.getString(R.string.blocked_mixed_content_message_top) + "\n\n" +
mContext.getString(R.string.blocked_mixed_content_message_bottom); mContext.getString(R.string.blocked_mixed_content_message_bottom));
} else { } else {
icon = R.drawable.shield_disabled_doorhanger; icon = R.drawable.shield_disabled_doorhanger;
message = mContext.getString(R.string.loaded_mixed_content_message); config.setMessage(mContext.getString(R.string.loaded_mixed_content_message));
} }
config.setLink(mContext.getString(R.string.learn_more), MIXED_CONTENT_SUPPORT_URL, "\n\n");
config.setType(DoorHanger.Type.SITE);
mMixedContentNotification = DoorHanger.Get(mContext, config);
mMixedContentNotification.setIcon(icon); mMixedContentNotification.setIcon(icon);
mMixedContentNotification.setMessage(message);
mMixedContentNotification.addLink(mContext.getString(R.string.learn_more), MIXED_CONTENT_SUPPORT_URL, "\n\n");
addNotificationButtons(mMixedContentNotification, blocked); addNotificationButtons(mMixedContentNotification, blocked);
@ -174,23 +174,25 @@ public class SiteIdentityPopup extends AnchoredPopup {
private void addTrackingContentNotification(boolean blocked) { private void addTrackingContentNotification(boolean blocked) {
// Remove any existing tracking content notification. // Remove any existing tracking content notification.
removeTrackingContentNotification(); removeTrackingContentNotification();
mTrackingContentNotification = new DefaultDoorHanger(mContext, DoorHanger.Type.SITE);
final DoorhangerConfig config = new DoorhangerConfig();
int icon; int icon;
String message;
if (blocked) { if (blocked) {
icon = R.drawable.shield_enabled_doorhanger; icon = R.drawable.shield_enabled_doorhanger;
message = mContext.getString(R.string.blocked_tracking_content_message_top) + "\n\n" + config.setMessage(mContext.getString(R.string.blocked_tracking_content_message_top) + "\n\n" +
mContext.getString(R.string.blocked_tracking_content_message_bottom); mContext.getString(R.string.blocked_tracking_content_message_bottom));
} else { } else {
icon = R.drawable.shield_disabled_doorhanger; icon = R.drawable.shield_disabled_doorhanger;
message = mContext.getString(R.string.loaded_tracking_content_message_top) + "\n\n" + config.setMessage(mContext.getString(R.string.loaded_tracking_content_message_top) + "\n\n" +
mContext.getString(R.string.loaded_tracking_content_message_bottom); mContext.getString(R.string.loaded_tracking_content_message_bottom));
} }
config.setLink(mContext.getString(R.string.learn_more), TRACKING_CONTENT_SUPPORT_URL, "\n\n");
config.setType(DoorHanger.Type.SITE);
mTrackingContentNotification = DoorHanger.Get(mContext, config);
mTrackingContentNotification.setIcon(icon); mTrackingContentNotification.setIcon(icon);
mTrackingContentNotification.setMessage(message);
mTrackingContentNotification.addLink(mContext.getString(R.string.learn_more), TRACKING_CONTENT_SUPPORT_URL, "\n\n");
addNotificationButtons(mTrackingContentNotification, blocked); addNotificationButtons(mTrackingContentNotification, blocked);
@ -206,6 +208,7 @@ public class SiteIdentityPopup extends AnchoredPopup {
} }
private void addNotificationButtons(DoorHanger dh, boolean blocked) { private void addNotificationButtons(DoorHanger dh, boolean blocked) {
// TODO: Add support for buttons in DoorHangerConfig.
if (blocked) { if (blocked) {
dh.addButton(mContext.getString(R.string.disable_protection), "disable", mButtonClickListener); dh.addButton(mContext.getString(R.string.disable_protection), "disable", mButtonClickListener);
dh.addButton(mContext.getString(R.string.keep_blocking), "keepBlocking", mButtonClickListener); dh.addButton(mContext.getString(R.string.keep_blocking), "keepBlocking", mButtonClickListener);

View File

@ -31,30 +31,38 @@ public class DefaultDoorHanger extends DoorHanger {
private List<PromptInput> mInputs; private List<PromptInput> mInputs;
private CheckBox mCheckBox; private CheckBox mCheckBox;
public DefaultDoorHanger(Context context) { public DefaultDoorHanger(Context context, DoorhangerConfig config) {
this(context, config, Type.DEFAULT);
this(context, 0, null);
} }
public DefaultDoorHanger(Context context, Type type) { public DefaultDoorHanger(Context context, DoorhangerConfig config, Type type) {
this(context, 0, null, type); super(context, config, type);
}
public DefaultDoorHanger(Context context, int tabId, String id) {
this(context, tabId, id, Type.DEFAULT);
}
public DefaultDoorHanger(Context context, int tabId, String id, Type type) {
super(context, tabId, id, type);
mResources = getResources(); mResources = getResources();
if (sSpinnerTextColor == -1) { if (sSpinnerTextColor == -1) {
sSpinnerTextColor = getResources().getColor(R.color.text_color_primary_disable_only); sSpinnerTextColor = getResources().getColor(R.color.text_color_primary_disable_only);
} }
loadConfig(config);
} }
@Override
protected void loadConfig(DoorhangerConfig config) {
final String message = config.getMessage();
if (message != null) {
setMessage(message);
}
final JSONObject options = config.getOptions();
if (options != null) {
setOptions(options);
}
final DoorhangerConfig.Link link = config.getLink();
if (link != null) {
addLink(link.label, link.url, link.delimiter);
}
}
@Override @Override
public List<PromptInput> getInputs() { public List<PromptInput> getInputs() {

View File

@ -3,7 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.widget; package org.mozilla.gecko.widget;
import android.content.Context; import android.content.Context;
import android.text.Html; import android.text.Html;
@ -28,7 +28,21 @@ import java.util.List;
public abstract class DoorHanger extends LinearLayout { public abstract class DoorHanger extends LinearLayout {
public static DoorHanger Get(Context context, DoorhangerConfig config) {
final Type type = config.getType();
if (type != null) {
switch (type) {
case PASSWORD:
case SITE:
return new DefaultDoorHanger(context, config, type);
}
}
return new DefaultDoorHanger(context, config);
}
public static enum Type { DEFAULT, PASSWORD, SITE } public static enum Type { DEFAULT, PASSWORD, SITE }
public interface OnButtonClickListener { public interface OnButtonClickListener {
public void onButtonClick(DoorHanger dh, String tag); public void onButtonClick(DoorHanger dh, String tag);
} }
@ -54,28 +68,19 @@ public abstract class DoorHanger extends LinearLayout {
private final ImageView mIcon; private final ImageView mIcon;
private final TextView mMessage; private final TextView mMessage;
protected Context mContext;
protected int mDividerColor; protected int mDividerColor;
protected boolean mPersistWhileVisible; protected boolean mPersistWhileVisible;
protected int mPersistenceCount; protected int mPersistenceCount;
protected long mTimeout; protected long mTimeout;
public DoorHanger(Context context) { protected DoorHanger(Context context, DoorhangerConfig config, Type type) {
this(context, 0, null);
}
public DoorHanger(Context context, int tabId, String id) {
this(context, tabId, id, Type.DEFAULT);
}
public DoorHanger(Context context, int tabId, String id, Type type) {
this(context, tabId, id, type, null);
}
public DoorHanger(Context context, int tabId, String id, Type type, JSONObject options) {
super(context); super(context);
mContext = context;
mTabId = tabId; mTabId = config.getTabId();
mIdentifier = id; mIdentifier = config.getId();
int resource; int resource;
switch (type) { switch (type) {
@ -97,15 +102,12 @@ public abstract class DoorHanger extends LinearLayout {
mButtonsContainer = (LinearLayout) findViewById(R.id.doorhanger_buttons); mButtonsContainer = (LinearLayout) findViewById(R.id.doorhanger_buttons);
mDividerColor = getResources().getColor(R.color.divider_light); mDividerColor = getResources().getColor(R.color.divider_light);
if (options != null) {
setOptions(options);
}
setOrientation(VERTICAL); setOrientation(VERTICAL);
} }
public void setOptions(final JSONObject options) { abstract protected void loadConfig(DoorhangerConfig config);
protected void setOptions(final JSONObject options) {
final int persistence = options.optInt("persistence"); final int persistence = options.optInt("persistence");
if (persistence > 0) { if (persistence > 0) {
mPersistenceCount = persistence; mPersistenceCount = persistence;
@ -140,13 +142,12 @@ public abstract class DoorHanger extends LinearLayout {
mIcon.setVisibility(View.VISIBLE); mIcon.setVisibility(View.VISIBLE);
} }
public void setMessage(String message) { protected void setMessage(String message) {
Spanned markupMessage = Html.fromHtml(message); Spanned markupMessage = Html.fromHtml(message);
mMessage.setText(markupMessage); mMessage.setText(markupMessage);
mMessage.setMovementMethod(LinkMovementMethod.getInstance());
} }
public void addLink(String label, String url, String delimiter) { protected void addLink(String label, String url, String delimiter) {
String title = mMessage.getText().toString(); String title = mMessage.getText().toString();
SpannableString titleWithLink = new SpannableString(title + delimiter + label); SpannableString titleWithLink = new SpannableString(title + delimiter + label);
URLSpan linkSpan = new URLSpan(url) { URLSpan linkSpan = new URLSpan(url) {