Bug 736278 - (1/2) Add support for checkbox in doorhanger message. r=mfinkle
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Tue, 27 Mar 2012 14:36:20 -0700
changeset 93939 6d62f902a321d8298f27570732fa66717a27ff17
parent 93938 3e6a7f9fb34e265380c1905b7677684eff876bf6
child 93940 220fcfedb8a21d538b1a09901e02a16f75ce1080
push id886
push userlsblakk@mozilla.com
push dateMon, 04 Jun 2012 19:57:52 +0000
treeherdermozilla-beta@bbd8d5efd6d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs736278
milestone14.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 736278 - (1/2) Add support for checkbox in doorhanger message. r=mfinkle
mobile/android/base/DoorHanger.java
mobile/android/base/resources/layout/doorhanger.xml
mobile/android/chrome/content/browser.js
--- a/mobile/android/base/DoorHanger.java
+++ b/mobile/android/base/DoorHanger.java
@@ -38,34 +38,41 @@
 
 package org.mozilla.gecko;
 
 import android.content.Context;
 import android.text.SpannableString;
 import android.text.method.LinkMovementMethod;
 import android.text.style.ForegroundColorSpan;
 import android.text.style.URLSpan;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.Button;
+import android.widget.CheckBox;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
 import org.json.JSONObject;
 import org.json.JSONException;
 
 public class DoorHanger extends LinearLayout implements Button.OnClickListener {
+    private static final String LOGTAG = "DoorHanger";
+
     private Context mContext;
     private LinearLayout mChoicesLayout;
     private TextView mTextView;
     static private LayoutParams mLayoutParams;
     public Tab mTab;
     // value used to identify the notification
     private String mValue;
 
+    // Optional checkbox added underneath message text
+    private CheckBox mCheckBox;
+
     static private LayoutInflater mInflater;
 
     private int mPersistence = 0;
     private long mTimeout = 0;
 
     public DoorHanger(Context aContext, String aValue) {
         super(aContext);
 
@@ -94,17 +101,28 @@ public class DoorHanger extends LinearLa
         Button mButton = new Button(mContext);
         mButton.setText(aText);
         mButton.setTag(Integer.toString(aCallback));
         mButton.setOnClickListener(this);
         mChoicesLayout.addView(mButton, mLayoutParams);
     }
 
     public void onClick(View v) {
-        GeckoEvent e = GeckoEvent.createBroadcastEvent("Doorhanger:Reply", v.getTag().toString());
+        JSONObject response = new JSONObject();
+        try {
+            response.put("callback", v.getTag().toString());
+
+            // If the checkbox is being used, pass its value
+            if (mCheckBox != null)
+                response.put("checked", mCheckBox.isChecked());
+        } catch (JSONException ex) {
+            Log.e(LOGTAG, "Error creating onClick response: " + ex);
+        }
+
+        GeckoEvent e = GeckoEvent.createBroadcastEvent("Doorhanger:Reply", response.toString());
         GeckoAppShell.sendEventToGecko(e);
         mTab.removeDoorHanger(mValue);
 
         // This will hide the doorhanger (and hide the popup if there are no
         // more doorhangers to show)
         GeckoApp.mDoorHangerPopup.updatePopup();
     }
 
@@ -161,16 +179,23 @@ public class DoorHanger extends LinearLa
             // prevent text outside the link from flashing when clicked
             ForegroundColorSpan colorSpan = new ForegroundColorSpan(mTextView.getCurrentTextColor());
             titleWithLink.setSpan(colorSpan, 0, title.length(), 0);
 
             titleWithLink.setSpan(linkSpan, title.length() + 1, titleWithLink.length(), 0);
             mTextView.setText(titleWithLink);
             mTextView.setMovementMethod(LinkMovementMethod.getInstance());
         } catch (JSONException e) { }
+
+        try {
+            String checkBoxText = options.getString("checkbox");
+            mCheckBox = (CheckBox) findViewById(R.id.doorhanger_checkbox);
+            mCheckBox.setText(checkBoxText);
+            mCheckBox.setVisibility(VISIBLE);
+        } catch (JSONException e) { }
     }
 
     // This method checks with persistence and timeout options to see if
     // it's okay to remove a doorhanger.
     public boolean shouldRemove() {
         // If persistence is set to -1, the doorhanger will never be
         // automatically removed.
         if (mPersistence != 0) {
--- a/mobile/android/base/resources/layout/doorhanger.xml
+++ b/mobile/android/base/resources/layout/doorhanger.xml
@@ -4,15 +4,21 @@
     <TextView android:id="@+id/doorhanger_title"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:textAppearance="?android:attr/textAppearanceMedium"
               android:textColor="?android:attr/textColorPrimary"
               android:textColorLink="@color/doorhanger_link"
               android:padding="10dp"/>
 
+    <CheckBox android:id="@+id/doorhanger_checkbox"
+              android:layout_width="fill_parent"
+              android:layout_height="wrap_content"
+              android:checked="true"
+              android:visibility="gone"/>
+
     <LinearLayout android:id="@+id/doorhanger_choices"
                   style="@android:style/ButtonBar"
                   android:layout_width="fill_parent"
                   android:layout_height="wrap_content"
                   android:orientation="horizontal"/>
 
 </merge>
--- a/mobile/android/chrome/content/browser.js
+++ b/mobile/android/chrome/content/browser.js
@@ -1067,20 +1067,25 @@ var NativeWindow = {
     }
   },
 
   observe: function(aSubject, aTopic, aData) {
     if (aTopic == "Menu:Clicked") {
       if (this.menu._callbacks[aData])
         this.menu._callbacks[aData]();
     } else if (aTopic == "Doorhanger:Reply") {
-      let reply_id = aData;
+      let data = JSON.parse(aData);
+      let reply_id = data["callback"];
+
       if (this.doorhanger._callbacks[reply_id]) {
+        // Pass the value of the optional checkbox to the callback
+        let checked = data["checked"];
+        this.doorhanger._callbacks[reply_id].cb(checked);
+
         let prompt = this.doorhanger._callbacks[reply_id].prompt;
-        this.doorhanger._callbacks[reply_id].cb();
         for (let id in this.doorhanger._callbacks) {
           if (this.doorhanger._callbacks[id].prompt == prompt) {
             delete this.doorhanger._callbacks[id];
           }
         }
       }
     }
   },