diff --git a/mobile/android/base/DoorHangerPopup.java b/mobile/android/base/DoorHangerPopup.java index 54a0c928b07..7b30a2d9a36 100644 --- a/mobile/android/base/DoorHangerPopup.java +++ b/mobile/android/base/DoorHangerPopup.java @@ -16,13 +16,13 @@ import org.mozilla.gecko.prompts.PromptInput; import org.mozilla.gecko.util.GeckoEventListener; import org.mozilla.gecko.util.ThreadUtils; import org.mozilla.gecko.widget.AnchoredPopup; -import org.mozilla.gecko.widget.DefaultDoorHanger; import org.mozilla.gecko.widget.DoorHanger; import android.content.Context; import android.util.Log; import android.view.View; import android.widget.CheckBox; +import org.mozilla.gecko.widget.DoorhangerConfig; public class DoorHangerPopup extends AnchoredPopup implements GeckoEventListener, @@ -77,16 +77,12 @@ public class DoorHangerPopup extends AnchoredPopup public void handleMessage(String event, JSONObject geckoObject) { try { if (event.equals("Doorhanger:Add")) { - final int tabId = geckoObject.getInt("tabID"); - final String value = geckoObject.getString("value"); - final String message = geckoObject.getString("message"); - final JSONArray buttons = geckoObject.getJSONArray("buttons"); - final JSONObject options = geckoObject.getJSONObject("options"); + final DoorhangerConfig config = makeConfigFromJSON(geckoObject); ThreadUtils.postToUiThread(new Runnable() { @Override public void run() { - addDoorHanger(tabId, value, message, buttons, options); + addDoorHanger(config); } }); } 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. @Override 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. */ - void addDoorHanger(final int tabId, final String value, final String message, - final JSONArray buttons, final JSONObject options) { + void addDoorHanger(DoorhangerConfig config) { + final int tabId = config.getTabId(); // Don't add a doorhanger for a tab that doesn't exist if (Tabs.getInstance().getTab(tabId) == null) { return; } // Replace the doorhanger if it already exists - DoorHanger oldDoorHanger = getDoorHanger(tabId, value); + DoorHanger oldDoorHanger = getDoorHanger(tabId, config.getId()); if (oldDoorHanger != null) { removeDoorHanger(oldDoorHanger); } @@ -167,10 +179,9 @@ public class DoorHangerPopup extends AnchoredPopup init(); } - final DoorHanger newDoorHanger = new DefaultDoorHanger(mContext, tabId, value); - newDoorHanger.setMessage(message); - newDoorHanger.setOptions(options); + final DoorHanger newDoorHanger = DoorHanger.Get(mContext, config); + final JSONArray buttons = config.getButtons(); for (int i = 0; i < buttons.length(); i++) { try { JSONObject buttonObject = buttons.getJSONObject(i); diff --git a/mobile/android/base/toolbar/SiteIdentityPopup.java b/mobile/android/base/toolbar/SiteIdentityPopup.java index 3b60a8c3f9e..b43dff94e4c 100644 --- a/mobile/android/base/toolbar/SiteIdentityPopup.java +++ b/mobile/android/base/toolbar/SiteIdentityPopup.java @@ -15,7 +15,6 @@ import org.mozilla.gecko.SiteIdentity.TrackingMode; import org.mozilla.gecko.Tab; import org.mozilla.gecko.Tabs; import org.mozilla.gecko.widget.AnchoredPopup; -import org.mozilla.gecko.widget.DefaultDoorHanger; import org.mozilla.gecko.widget.DoorHanger; import org.mozilla.gecko.widget.DoorHanger.OnButtonClickListener; import org.json.JSONException; @@ -28,6 +27,7 @@ import android.view.LayoutInflater; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; +import org.mozilla.gecko.widget.DoorhangerConfig; /** * 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) { // Remove any existing mixed content notification. removeMixedContentNotification(); - mMixedContentNotification = new DefaultDoorHanger(mContext, DoorHanger.Type.SITE); + final DoorhangerConfig config = new DoorhangerConfig(); int icon; - String message; if (blocked) { icon = R.drawable.shield_enabled_doorhanger; - message = mContext.getString(R.string.blocked_mixed_content_message_top) + "\n\n" + - mContext.getString(R.string.blocked_mixed_content_message_bottom); + config.setMessage(mContext.getString(R.string.blocked_mixed_content_message_top) + "\n\n" + + mContext.getString(R.string.blocked_mixed_content_message_bottom)); } else { 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.setMessage(message); - mMixedContentNotification.addLink(mContext.getString(R.string.learn_more), MIXED_CONTENT_SUPPORT_URL, "\n\n"); addNotificationButtons(mMixedContentNotification, blocked); @@ -174,23 +174,25 @@ public class SiteIdentityPopup extends AnchoredPopup { private void addTrackingContentNotification(boolean blocked) { // Remove any existing tracking content notification. removeTrackingContentNotification(); - mTrackingContentNotification = new DefaultDoorHanger(mContext, DoorHanger.Type.SITE); + + final DoorhangerConfig config = new DoorhangerConfig(); int icon; - String message; if (blocked) { icon = R.drawable.shield_enabled_doorhanger; - message = mContext.getString(R.string.blocked_tracking_content_message_top) + "\n\n" + - mContext.getString(R.string.blocked_tracking_content_message_bottom); + config.setMessage(mContext.getString(R.string.blocked_tracking_content_message_top) + "\n\n" + + mContext.getString(R.string.blocked_tracking_content_message_bottom)); } else { icon = R.drawable.shield_disabled_doorhanger; - message = mContext.getString(R.string.loaded_tracking_content_message_top) + "\n\n" + - mContext.getString(R.string.loaded_tracking_content_message_bottom); + config.setMessage(mContext.getString(R.string.loaded_tracking_content_message_top) + "\n\n" + + 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.setMessage(message); - mTrackingContentNotification.addLink(mContext.getString(R.string.learn_more), TRACKING_CONTENT_SUPPORT_URL, "\n\n"); addNotificationButtons(mTrackingContentNotification, blocked); @@ -206,6 +208,7 @@ public class SiteIdentityPopup extends AnchoredPopup { } private void addNotificationButtons(DoorHanger dh, boolean blocked) { + // TODO: Add support for buttons in DoorHangerConfig. if (blocked) { dh.addButton(mContext.getString(R.string.disable_protection), "disable", mButtonClickListener); dh.addButton(mContext.getString(R.string.keep_blocking), "keepBlocking", mButtonClickListener); diff --git a/mobile/android/base/widget/DefaultDoorHanger.java b/mobile/android/base/widget/DefaultDoorHanger.java index ee8d462d47f..94627ea48d4 100644 --- a/mobile/android/base/widget/DefaultDoorHanger.java +++ b/mobile/android/base/widget/DefaultDoorHanger.java @@ -31,30 +31,38 @@ public class DefaultDoorHanger extends DoorHanger { private List mInputs; private CheckBox mCheckBox; - public DefaultDoorHanger(Context context) { - - this(context, 0, null); + public DefaultDoorHanger(Context context, DoorhangerConfig config) { + this(context, config, Type.DEFAULT); } - public DefaultDoorHanger(Context context, Type type) { - this(context, 0, null, 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); + public DefaultDoorHanger(Context context, DoorhangerConfig config, Type type) { + super(context, config, type); mResources = getResources(); if (sSpinnerTextColor == -1) { 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 public List getInputs() { diff --git a/mobile/android/base/widget/DoorHanger.java b/mobile/android/base/widget/DoorHanger.java index 6c94fb5c466..0f36263538c 100644 --- a/mobile/android/base/widget/DoorHanger.java +++ b/mobile/android/base/widget/DoorHanger.java @@ -3,7 +3,7 @@ * 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/. */ - package org.mozilla.gecko.widget; +package org.mozilla.gecko.widget; import android.content.Context; import android.text.Html; @@ -28,7 +28,21 @@ import java.util.List; 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 interface OnButtonClickListener { public void onButtonClick(DoorHanger dh, String tag); } @@ -54,28 +68,19 @@ public abstract class DoorHanger extends LinearLayout { private final ImageView mIcon; private final TextView mMessage; + protected Context mContext; + protected int mDividerColor; protected boolean mPersistWhileVisible; protected int mPersistenceCount; protected long mTimeout; - public DoorHanger(Context context) { - 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) { + protected DoorHanger(Context context, DoorhangerConfig config, Type type) { super(context); - - mTabId = tabId; - mIdentifier = id; + mContext = context; + mTabId = config.getTabId(); + mIdentifier = config.getId(); int resource; switch (type) { @@ -97,15 +102,12 @@ public abstract class DoorHanger extends LinearLayout { mButtonsContainer = (LinearLayout) findViewById(R.id.doorhanger_buttons); mDividerColor = getResources().getColor(R.color.divider_light); - - if (options != null) { - setOptions(options); - } - 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"); if (persistence > 0) { mPersistenceCount = persistence; @@ -140,13 +142,12 @@ public abstract class DoorHanger extends LinearLayout { mIcon.setVisibility(View.VISIBLE); } - public void setMessage(String message) { + protected void setMessage(String message) { Spanned markupMessage = Html.fromHtml(message); 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(); SpannableString titleWithLink = new SpannableString(title + delimiter + label); URLSpan linkSpan = new URLSpan(url) {