Bug 1224530 - Replace button toasts created with getButtonToast().show() with snackbars. r?mcomella draft
authorSebastian Kaspari <s.kaspari@gmail.com>
Fri, 13 Nov 2015 15:09:57 +0100
changeset 308717 066ea49889a89021872b6ed2845ff8234b90466e
parent 308716 68df6df19968bf7015ace65985e63a577a7d4725
child 308776 280e87ec4883075ac80c23c4f95632a0c0280ec0
push id7514
push users.kaspari@gmail.com
push dateFri, 13 Nov 2015 14:12:41 +0000
reviewersmcomella
bugs1224530
milestone45.0a1
Bug 1224530 - Replace button toasts created with getButtonToast().show() with snackbars. r?mcomella
mobile/android/base/BrowserApp.java
mobile/android/base/GeckoApp.java
--- a/mobile/android/base/BrowserApp.java
+++ b/mobile/android/base/BrowserApp.java
@@ -1,17 +1,16 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * 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;
 
 import org.mozilla.gecko.annotation.RobocopTarget;
-import org.mozilla.gecko.AdjustConstants;
 import org.mozilla.gecko.AppConstants.Versions;
 import org.mozilla.gecko.DynamicToolbar.PinReason;
 import org.mozilla.gecko.DynamicToolbar.VisibilityTransition;
 import org.mozilla.gecko.GeckoProfileDirectories.NoMozillaDirectoryException;
 import org.mozilla.gecko.Tabs.TabEvents;
 import org.mozilla.gecko.animation.PropertyAnimator;
 import org.mozilla.gecko.animation.TransitionsTracker;
 import org.mozilla.gecko.animation.ViewHelper;
@@ -107,16 +106,17 @@ import android.nfc.NdefMessage;
 import android.nfc.NdefRecord;
 import android.nfc.NfcAdapter;
 import android.nfc.NfcEvent;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.StrictMode;
 import android.support.annotation.NonNull;
 import android.support.annotation.WorkerThread;
+import android.support.design.widget.Snackbar;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentManager;
 import android.support.v4.view.MenuItemCompat;
 import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.util.Base64;
 import android.util.Base64OutputStream;
 import android.util.Log;
@@ -325,17 +325,17 @@ public class BrowserApp extends GeckoApp
                 if (Tabs.getInstance().isSelectedTab(tab)) {
                     invalidateOptionsMenu();
                 }
                 break;
             case PAGE_SHOW:
                 tab.loadFavicon();
                 break;
             case BOOKMARK_ADDED:
-                showBookmarkAddedToast();
+                showBookmarkAddedSnackbar();
                 break;
             case BOOKMARK_REMOVED:
                 showBookmarkRemovedToast();
                 break;
             case READING_LIST_ADDED:
                 onAddedToReadingList(tab.getURL());
                 break;
             case READING_LIST_REMOVED:
@@ -391,67 +391,58 @@ public class BrowserApp extends GeckoApp
         // must be called after we restore the editing state in the edit text View.
         if (editingState.isBrowserSearchShown()) {
             showBrowserSearch();
         } else {
             hideBrowserSearch();
         }
     }
 
-    private void showBookmarkAddedToast() {
+    private void showBookmarkAddedSnackbar() {
         // This flow is from the option menu which has check to see if a bookmark was already added.
         // So, it is safe here to show the toast that bookmark_added without any checks.
-        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() {
-                        Telemetry.sendUIEvent(TelemetryContract.Event.SHOW,
-                            TelemetryContract.Method.TOAST, "bookmark_options");
-                        showBookmarkDialog();
-                    }
-
-                    @Override
-                    public void onToastHidden(ButtonToast.ReasonHidden reason) { }
-                });
+
+        final SnackbarCallback callback = new SnackbarCallback() {
+            @Override
+            public void onClick(View v) {
+                Telemetry.sendUIEvent(TelemetryContract.Event.SHOW, TelemetryContract.Method.TOAST, "bookmark_options");
+                showBookmarkDialog();
+            }
+        };
+
+        showSnackbar(getResources().getString(R.string.bookmark_added),
+                     Snackbar.LENGTH_LONG,
+                     getResources().getString(R.string.bookmark_options),
+                     callback);
     }
 
     private void showBookmarkRemovedToast() {
         Toast.makeText(this, R.string.bookmark_removed, Toast.LENGTH_SHORT).show();
     }
 
-    private void showSwitchToReadingListToast(String message) {
-        getButtonToast().show(false,
-                message,
-                ButtonToast.LENGTH_SHORT,
-                getResources().getString(R.string.switch_button_message),
-                R.drawable.switch_button_icon,
-                new ButtonToast.ToastListener() {
-                    @Override
-                    public void onButtonClicked() {
-                        Telemetry.sendUIEvent(TelemetryContract.Event.SHOW, TelemetryContract.Method.TOAST, "reading_list");
-
-                        final String aboutPageUrl = AboutPages.getURLForBuiltinPanelType(PanelType.READING_LIST);
-                        Tabs.getInstance().loadUrlInTab(aboutPageUrl);
-                    }
-
-                    @Override
-                    public void onToastHidden(ButtonToast.ReasonHidden reason) { }
-                });
+    private void showSwitchToReadingListSnackbar(String message) {
+        final SnackbarCallback callback = new SnackbarCallback() {
+            @Override
+            public void onClick(View v) {
+                Telemetry.sendUIEvent(TelemetryContract.Event.SHOW, TelemetryContract.Method.TOAST, "reading_list");
+
+                final String aboutPageUrl = AboutPages.getURLForBuiltinPanelType(PanelType.READING_LIST);
+                Tabs.getInstance().loadUrlInTab(aboutPageUrl);
+            }
+        };
+
+        showSnackbar(message, Snackbar.LENGTH_LONG, getResources().getString(R.string.switch_button_message), callback);
     }
 
     public void onAddedToReadingList(String url) {
-        showSwitchToReadingListToast(getResources().getString(R.string.reading_list_added));
+        showSwitchToReadingListSnackbar(getResources().getString(R.string.reading_list_added));
     }
 
     public void onAlreadyInReadingList(String url) {
-        showSwitchToReadingListToast(getResources().getString(R.string.reading_list_duplicate));
+        showSwitchToReadingListSnackbar(getResources().getString(R.string.reading_list_duplicate));
     }
 
     public void onRemovedFromReadingList(String url) {
         Toast.makeText(this, R.string.reading_list_removed, Toast.LENGTH_SHORT).show();
     }
 
     @Override
     public boolean onKey(View v, int keyCode, KeyEvent event) {
@@ -3657,38 +3648,31 @@ public class BrowserApp extends GeckoApp
         final Tab newTab = Tabs.getInstance().loadUrl(url, loadFlags);
 
         // We switch to the desired tab by unique ID, which closes any window
         // for a race between opening the tab and closing it, and switching to
         // it. We could also switch to the Tab explicitly, but we don't want to
         // hold a reference to the Tab itself in the anonymous listener class.
         final int newTabId = newTab.getId();
 
-        final ToastListener listener = new ButtonToast.ToastListener() {
+        final SnackbarCallback callback = new SnackbarCallback() {
             @Override
-            public void onButtonClicked() {
+            public void onClick(View v) {
                 Telemetry.sendUIEvent(TelemetryContract.Event.SHOW, TelemetryContract.Method.TOAST, "switchtab");
 
                 maybeSwitchToTab(newTabId);
             }
-
-            @Override
-            public void onToastHidden(ButtonToast.ReasonHidden reason) { }
         };
 
         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);
-        getButtonToast().show(false,
-                              message,
-                              ButtonToast.LENGTH_SHORT,
-                              buttonMessage,
-                              R.drawable.switch_button_icon,
-                              listener);
+
+        showSnackbar(message, Snackbar.LENGTH_LONG, buttonMessage, callback);
     }
 
     // BrowserSearch.OnSearchListener
     @Override
     public void onSearch(SearchEngine engine, String text) {
         // Don't store searches that happen in private tabs. This assumes the user can only
         // perform a search inside the currently selected tab, which is true for searches
         // that come from SearchEngineRow.
--- a/mobile/android/base/GeckoApp.java
+++ b/mobile/android/base/GeckoApp.java
@@ -652,20 +652,22 @@ public abstract class GeckoApp
             Telemetry.sendUIEvent(TelemetryContract.Event.SHARE, TelemetryContract.Method.LIST, "text");
 
         } else if ("Snackbar:Show".equals(event)) {
             final String msg = message.getString("message");
             final int duration = message.getInt("duration");
 
             NativeJSObject action = message.optObject("action", null);
 
+            final SnackbarEventCallback snackbarCallback = new SnackbarEventCallback(callback);
+
             showSnackbar(msg,
                     duration,
                     action != null ? action.optString("label", null) : null,
-                    callback);
+                    snackbarCallback);
         } 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) {
@@ -854,31 +856,31 @@ public abstract class GeckoApp
         }
 
         ViewStub toastStub = (ViewStub) findViewById(R.id.toast_stub);
         mToast = new ButtonToast(toastStub.inflate());
 
         return mToast;
     }
 
-    void showSnackbar(final String message, final int duration, final String action, final EventCallback callback) {
+    void showSnackbar(final String message, final int duration, final String action, final SnackbarCallback callback) {
         final Snackbar snackbar = Snackbar.make(mRootLayout, message, duration);
 
-        if (!TextUtils.isEmpty(action)) {
-            final SnackbarEventCallback snackbarCallback = new SnackbarEventCallback(callback);
-
-            snackbar.setAction(action, snackbarCallback);
+        if (callback != null && !TextUtils.isEmpty(action)) {
+            snackbar.setAction(action, callback);
             snackbar.setActionTextColor(ContextCompat.getColor(this, R.color.fennec_ui_orange));
-            snackbar.setCallback(snackbarCallback);
+            snackbar.setCallback(callback);
         }
 
         snackbar.show();
     }
 
-    private static class SnackbarEventCallback extends Snackbar.Callback implements View.OnClickListener {
+    public static abstract class SnackbarCallback extends Snackbar.Callback implements View.OnClickListener {};
+
+    private static class SnackbarEventCallback extends SnackbarCallback {
         private EventCallback callback;
 
         public SnackbarEventCallback(EventCallback callback) {
             this.callback = callback;
         }
 
         @Override
         public synchronized void onClick(View view) {