Bug 1233250: Pre - allow specifying snackbar's backgroundColor via Snackbars.jsm r=sebastian draft
authorGrigory Kruglov <gkruglov@mozilla.com>
Fri, 06 May 2016 11:35:39 -0700
changeset 364489 3a47ea585a13e2bd891d43d964eb0a3603fba91e
parent 364136 bbda46c289f1c84303db1f8efc89094b4b78ccff
child 364490 e6f7212780e86c82c85f8cdfe7dfa46edefa957d
push id17469
push usergkruglov@mozilla.com
push dateFri, 06 May 2016 18:37:51 +0000
reviewerssebastian
bugs1233250
milestone49.0a1
Bug 1233250: Pre - allow specifying snackbar's backgroundColor via Snackbars.jsm r=sebastian MozReview-Commit-ID: FQqUKE6sLHS
mobile/android/base/java/org/mozilla/gecko/SnackbarHelper.java
mobile/android/modules/Snackbars.jsm
--- a/mobile/android/base/java/org/mozilla/gecko/SnackbarHelper.java
+++ b/mobile/android/base/java/org/mozilla/gecko/SnackbarHelper.java
@@ -4,37 +4,40 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko;
 
 import org.mozilla.gecko.util.EventCallback;
 import org.mozilla.gecko.util.NativeJSObject;
 
 import android.app.Activity;
+import android.graphics.Color;
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.InsetDrawable;
 import android.support.design.widget.Snackbar;
 import android.support.v4.content.ContextCompat;
 import android.text.TextUtils;
+import android.util.Log;
 import android.util.TypedValue;
 import android.view.View;
 import android.widget.TextView;
 
 import java.lang.ref.WeakReference;
 
 /**
  * Helper class for creating and dismissing snackbars. Use this class to guarantee a consistent style and behavior
  * across the app.
  */
 public class SnackbarHelper {
     /**
      * Combined interface for handling all callbacks from a snackbar because anonymous classes can only extend one
      * interface or class.
      */
     public static abstract class SnackbarCallback extends Snackbar.Callback implements View.OnClickListener {}
+    public static final String LOGTAG = "GeckoSnackbarHelper";
 
     /**
      * SnackbarCallback implementation for delegating snackbar events to an EventCallback.
      */
     private static class SnackbarEventCallback extends SnackbarCallback {
         private EventCallback callback;
 
         public SnackbarEventCallback(EventCallback callback) {
@@ -78,23 +81,35 @@ public class SnackbarHelper {
 
     /**
      * Build and show a snackbar from a Gecko Snackbar:Show event.
      */
     public static void showSnackbar(Activity activity, final NativeJSObject object, final EventCallback callback) {
         final String message = object.getString("message");
         final int duration = object.getInt("duration");
 
+        Integer backgroundColor = null;
+
+        if (object.has("backgroundColor")) {
+            final String providedColor = object.getString("backgroundColor");
+            try {
+                backgroundColor = Color.parseColor(providedColor);
+            } catch (IllegalArgumentException e) {
+                Log.w(LOGTAG, "Failed to parse color string: " + providedColor);
+            }
+        }
+
         NativeJSObject action = object.optObject("action", null);
 
-        showSnackbarWithAction(activity,
+        showSnackbarWithActionAndColors(activity,
                 message,
                 duration,
                 action != null ? action.optString("label", null) : null,
-                new SnackbarHelper.SnackbarEventCallback(callback));
+                new SnackbarHelper.SnackbarEventCallback(callback),
+                null, backgroundColor, null);
     }
 
     /**
      * Show a snackbar to display a message and an action.
      *
      * @param activity Activity to show the snackbar in.
      * @param message The text to show. Can be formatted text.
      * @param duration How long to display the message.
--- a/mobile/android/modules/Snackbars.jsm
+++ b/mobile/android/modules/Snackbars.jsm
@@ -29,16 +29,20 @@ var Snackbars = {
     }
 
     let msg = {
       type: 'Snackbar:Show',
       message: aMessage,
       duration: aDuration,
     };
 
+    if (aOptions && aOptions.backgroundColor) {
+      msg.backgroundColor = aOptions.backgroundColor;
+    }
+
     if (aOptions && aOptions.action) {
       msg.action = {};
 
       if (aOptions.action.label) {
         msg.action.label = aOptions.action.label;
       }
 
       Messaging.sendRequestForResult(msg).then(result => aOptions.action.callback());