Bug 1233250: Pre - allow specifying snackbar's backgroundColor via Snackbars.jsm r=sebastian
MozReview-Commit-ID: FQqUKE6sLHS
--- 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,38 @@ 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());