Bug 1019735 - Hide the button toast if the user touches outside of it. r=lucasr
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Thu, 12 Jun 2014 09:31:47 -0700
changeset 188829 eeec5cc72be7c0bd29b4360294dfa5c9832ae97f
parent 188828 3cc0b4b7911d583ab5ae66272ad5aac8905cff12
child 188830 30460db75b0974d8467838abe349c0f39c9f2eaf
push id7299
push usermleibovic@mozilla.com
push dateMon, 16 Jun 2014 16:38:02 +0000
treeherderfx-team@eeec5cc72be7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerslucasr
bugs1019735
milestone33.0a1
Bug 1019735 - Hide the button toast if the user touches outside of it. r=lucasr * * * Bug 1019735 - (Part 2) Don't do unnecssary work on every touch
mobile/android/base/BrowserApp.java
mobile/android/base/widget/ButtonToast.java
--- a/mobile/android/base/BrowserApp.java
+++ b/mobile/android/base/BrowserApp.java
@@ -482,17 +482,17 @@ abstract public class BrowserApp extends
         mBrowserToolbar.setProgressBar(mProgressView);
         if (Intent.ACTION_VIEW.equals(intent.getAction())) {
             // Show the target URL immediately in the toolbar.
             mBrowserToolbar.setTitle(intent.getDataString());
 
             Telemetry.sendUIEvent(TelemetryContract.Event.LOAD_URL, TelemetryContract.Method.INTENT);
         }
 
-        ((GeckoApp.MainLayout) mMainLayout).setTouchEventInterceptor(new HideTabsTouchListener());
+        ((GeckoApp.MainLayout) mMainLayout).setTouchEventInterceptor(new HideOnTouchListener());
         ((GeckoApp.MainLayout) mMainLayout).setMotionEventInterceptor(new MotionEventInterceptor() {
             @Override
             public boolean onInterceptMotionEvent(View view, MotionEvent event) {
                 // If we get a gamepad panning MotionEvent while the focus is not on the layerview,
                 // put the focus on the layerview and carry on
                 if (mLayerView != null && !mLayerView.hasFocus() && GamepadUtils.isPanningControl(event)) {
                     if (mHomePager == null) {
                         return false;
@@ -1980,21 +1980,30 @@ abstract public class BrowserApp extends
 
         mBrowserSearchContainer.setVisibility(View.INVISIBLE);
 
         getSupportFragmentManager().beginTransaction()
                 .remove(mBrowserSearch).commitAllowingStateLoss();
         mBrowserSearch.setUserVisibleHint(false);
     }
 
-    private class HideTabsTouchListener implements TouchEventInterceptor {
+    /**
+     * Hides certain UI elements (e.g. button toast, tabs tray) when the
+     * user touches the main layout.
+     */
+    private class HideOnTouchListener implements TouchEventInterceptor {
         private boolean mIsHidingTabs = false;
 
         @Override
         public boolean onInterceptTouchEvent(View view, MotionEvent event) {
+            // Only try to hide the button toast if it's already inflated.
+            if (mToast != null) {
+                mToast.hide(false, ButtonToast.ReasonHidden.TOUCH_OUTSIDE);
+            }
+
             // We need to account for scroll state for the touched view otherwise
             // tapping on an "empty" part of the view will still be considered a
             // valid touch event.
             if (view.getScrollX() != 0 || view.getScrollY() != 0) {
                 Rect rect = new Rect();
                 view.getHitRect(rect);
                 rect.offset(-view.getScrollX(), -view.getScrollY());
 
--- a/mobile/android/base/widget/ButtonToast.java
+++ b/mobile/android/base/widget/ButtonToast.java
@@ -37,16 +37,17 @@ public class ButtonToast {
     private final View mView;
     private final TextView mMessageView;
     private final Button mButton;
     private final Handler mHideHandler = new Handler();
     private Toast mCurrentToast;
 
     public enum ReasonHidden {
         CLICKED,
+        TOUCH_OUTSIDE,
         TIMEOUT,
         REPLACED,
         STARTUP
     }
 
     // State objects
     private static class Toast {
         public final CharSequence buttonMessage;
@@ -124,16 +125,21 @@ public class ButtonToast {
         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();
     }
 
     public void hide(boolean immediate, ReasonHidden reason) {
+        // There's nothing to do if the view is already hidden.
+        if (mView.getVisibility() == View.GONE) {
+            return;
+        }
+
         if (mCurrentToast != null && mCurrentToast.listener != null) {
             mCurrentToast.listener.onToastHidden(reason);
         }
         mCurrentToast = null;
         mButton.setEnabled(false);
         mHideHandler.removeCallbacks(mHideRunnable);
         int duration = immediate ? 0 : mView.getResources().getInteger(android.R.integer.config_longAnimTime);