Bug 1018417 - Add duration parameter to ButtonToast. r=margaret
authorAmogh Pradeep <amoghbl1@gmail.com>
Thu, 31 Jul 2014 09:36:03 -0700
changeset 197156 246fab00de4a6fc682d94750e240c4bcfa1a6eb3
parent 197155 ac4d0d124429eaf5af915ea806a7bfa14c326507
child 197157 b55408fd3fa5a1ce19f52d03096f57bc84108eda
push id27232
push userryanvm@gmail.com
push dateThu, 31 Jul 2014 20:03:46 +0000
treeherdermozilla-central@3cde40ba857c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret
bugs1018417
milestone34.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 1018417 - Add duration parameter to ButtonToast. r=margaret
mobile/android/base/BrowserApp.java
mobile/android/base/GeckoApp.java
mobile/android/base/home/HomeFragment.java
mobile/android/base/widget/ButtonToast.java
--- a/mobile/android/base/BrowserApp.java
+++ b/mobile/android/base/BrowserApp.java
@@ -2540,16 +2540,17 @@ public class BrowserApp extends GeckoApp
                     tab.removeBookmark();
                     Toast.makeText(this, R.string.bookmark_removed, Toast.LENGTH_SHORT).show();
                     item.setIcon(R.drawable.ic_menu_bookmark_add);
                 } else {
                     Telemetry.sendUIEvent(TelemetryContract.Event.SAVE, TelemetryContract.Method.MENU, "bookmark");
                     tab.addBookmark();
                     getButtonToast().show(false,
                         getResources().getString(R.string.bookmark_added),
+                        ButtonToast.LENGTH_SHORT,
                         getResources().getString(R.string.bookmark_options),
                         null,
                         new ButtonToast.ToastListener() {
                             @Override
                             public void onButtonClicked() {
                                 showBookmarkDialog();
                             }
 
--- a/mobile/android/base/GeckoApp.java
+++ b/mobile/android/base/GeckoApp.java
@@ -600,24 +600,24 @@ public abstract class GeckoApp
             // Context: Sharing via chrome list (no explicit session is active)
             Telemetry.sendUIEvent(TelemetryContract.Event.SHARE, TelemetryContract.Method.LIST);
 
         } else if ("SystemUI:Visibility".equals(event)) {
             setSystemUiVisible(message.getBoolean("visible"));
 
         } else if ("Toast:Show".equals(event)) {
             final String msg = message.getString("message");
+            final String duration = message.getString("duration");
             final NativeJSObject button = message.optObject("button", null);
             if (button != null) {
                 final String label = button.optString("label", "");
                 final String icon = button.optString("icon", "");
                 final String id = button.optString("id", "");
-                showButtonToast(msg, label, icon, id);
+                showButtonToast(msg, duration, label, icon, id);
             } else {
-                final String duration = message.getString("duration");
                 showNormalToast(msg, duration);
             }
 
         } else if ("ToggleChrome:Focus".equals(event)) {
             focusChrome();
 
         } else if ("ToggleChrome:Hide".equals(event)) {
             toggleChrome(false);
@@ -783,22 +783,24 @@ public abstract class GeckoApp
         }
 
         ViewStub toastStub = (ViewStub) findViewById(R.id.toast_stub);
         mToast = new ButtonToast(toastStub.inflate());
 
         return mToast;
     }
 
-    void showButtonToast(final String message, final String buttonText,
-                         final String buttonIcon, final String buttonId) {
+    void showButtonToast(final String message, final String duration,
+                         final String buttonText, final String buttonIcon,
+                         final String buttonId) {
         BitmapUtils.getDrawable(GeckoApp.this, buttonIcon, new BitmapUtils.BitmapLoader() {
             @Override
             public void onBitmapFound(final Drawable d) {
-                getButtonToast().show(false, message, buttonText, d, new ButtonToast.ToastListener() {
+                final int toastDuration = duration.equals("long") ? ButtonToast.LENGTH_LONG : ButtonToast.LENGTH_SHORT;
+                getButtonToast().show(false, message, toastDuration ,buttonText, d, new ButtonToast.ToastListener() {
                     @Override
                     public void onButtonClicked() {
                         GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Toast:Click", buttonId));
                     }
 
                     @Override
                     public void onToastHidden(ButtonToast.ReasonHidden reason) {
                         if (reason == ButtonToast.ReasonHidden.TIMEOUT) {
--- a/mobile/android/base/home/HomeFragment.java
+++ b/mobile/android/base/home/HomeFragment.java
@@ -198,16 +198,17 @@ abstract class HomeFragment extends Frag
 
             final String message = isPrivate ?
                     getResources().getString(R.string.new_private_tab_opened) :
                     getResources().getString(R.string.new_tab_opened);
             final String buttonMessage = getResources().getString(R.string.switch_button_message);
             final GeckoApp geckoApp = (GeckoApp) context;
             geckoApp.getButtonToast().show(false,
                     message,
+                    ButtonToast.LENGTH_SHORT,
                     buttonMessage,
                     R.drawable.switch_button_icon,
                     new ButtonToast.ToastListener() {
                         @Override
                         public void onButtonClicked() {
                             Tabs.getInstance().selectTab(newTabId);
                         }
 
--- a/mobile/android/base/widget/ButtonToast.java
+++ b/mobile/android/base/widget/ButtonToast.java
@@ -27,17 +27,18 @@ import android.os.Handler;
 import android.view.View;
 import android.widget.Button;
 import android.widget.TextView;
 
 public class ButtonToast {
     @SuppressWarnings("unused")
     private final static String LOGTAG = "GeckoButtonToast";
 
-    private final static int TOAST_DURATION = 5000;
+    public static int LENGTH_SHORT = 3000;
+    public static int LENGTH_LONG = 5000;
 
     private final View mView;
     private final TextView mMessageView;
     private final Button mButton;
     private final Handler mHideHandler = new Handler();
     private Toast mCurrentToast;
 
     public enum ReasonHidden {
@@ -48,21 +49,24 @@ public class ButtonToast {
         STARTUP
     }
 
     // State objects
     private static class Toast {
         public final CharSequence buttonMessage;
         public Drawable buttonDrawable;
         public final CharSequence message;
+        public final int duration;
         public ToastListener listener;
 
-        public Toast(CharSequence aMessage, CharSequence aButtonMessage,
-                     Drawable aDrawable, ToastListener aListener) {
+        public Toast(CharSequence aMessage, int aDuration,
+                CharSequence aButtonMessage, Drawable aDrawable,
+                ToastListener aListener) {
             message = aMessage;
+            duration = aDuration;
             buttonMessage = aButtonMessage;
             buttonDrawable = aDrawable;
             listener = aListener;
         }
     }
 
     public interface ToastListener {
         void onButtonClicked();
@@ -86,26 +90,26 @@ public class ButtonToast {
                         }
                     }
                 });
 
         hide(true, ReasonHidden.STARTUP);
     }
 
     public void show(boolean immediate, CharSequence message,
-                     CharSequence buttonMessage, int buttonDrawableId,
-                     ToastListener listener) {
+                     int duration, CharSequence buttonMessage,
+                     int buttonDrawableId, ToastListener listener) {
         final Drawable d = mView.getContext().getResources().getDrawable(buttonDrawableId);
-        show(false, message, buttonMessage, d, listener);
+        show(false, message, duration, buttonMessage, d, listener);
     }
 
     public void show(boolean immediate, CharSequence message,
-                     CharSequence buttonMessage, Drawable buttonDrawable,
-                     ToastListener listener) {
-        show(new Toast(message, buttonMessage, buttonDrawable, listener), immediate);
+                     int duration, CharSequence buttonMessage,
+                     Drawable buttonDrawable, ToastListener listener) {
+        show(new Toast(message, duration, buttonMessage, buttonDrawable, listener), immediate);
     }
 
     private void show(Toast t, boolean immediate) {
         // If we're already showing a toast, replace it with the new one by hiding the old one and quickly showing the new one.
         if (mCurrentToast != null) {
             hide(true, ReasonHidden.REPLACED);
             immediate = true;
         }
@@ -114,17 +118,17 @@ public class ButtonToast {
         mButton.setEnabled(true);
 
         // Our toast is re-used, so we update all fields to clear any old values.
         mMessageView.setText(null != t.message ? t.message : "");
         mButton.setText(null != t.buttonMessage ? t.buttonMessage : "");
         mButton.setCompoundDrawablesWithIntrinsicBounds(t.buttonDrawable, null, null, null);
 
         mHideHandler.removeCallbacks(mHideRunnable);
-        mHideHandler.postDelayed(mHideRunnable, TOAST_DURATION);
+        mHideHandler.postDelayed(mHideRunnable, t.duration);
 
         mView.setVisibility(View.VISIBLE);
         int duration = immediate ? 0 : mView.getResources().getInteger(android.R.integer.config_longAnimTime);
 
         PropertyAnimator animator = new PropertyAnimator(duration);
         animator.attach(mView, PropertyAnimator.Property.ALPHA, 1.0f);
         animator.start();
     }