OSDN Git Service

Revert "Revert "FloatingToolbar: Fix orientation change handling.""
authorAbodunrinwa Toki <toki@google.com>
Mon, 1 Feb 2016 13:54:10 +0000 (13:54 +0000)
committerAbodunrinwa Toki <toki@google.com>
Mon, 1 Feb 2016 15:28:42 +0000 (15:28 +0000)
This reverts commit 6608a80fb317ca1bd4d9644bf6ac3a4cb108fd4c.
...and fix the syntax errors.

Change-Id: If2ca31d426b2e3eeac8444f7cb24ab21f169d30d

core/java/com/android/internal/widget/FloatingToolbar.java

index 480d806..02efcc6 100644 (file)
@@ -21,9 +21,7 @@ import android.animation.AnimatorListenerAdapter;
 import android.animation.AnimatorSet;
 import android.animation.ObjectAnimator;
 import android.animation.ValueAnimator;
-import android.content.ComponentCallbacks;
 import android.content.Context;
-import android.content.res.Configuration;
 import android.content.res.TypedArray;
 import android.graphics.Color;
 import android.graphics.Point;
@@ -42,6 +40,7 @@ import android.view.MenuItem;
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.View.MeasureSpec;
+import android.view.View.OnLayoutChangeListener;
 import android.view.ViewGroup;
 import android.view.ViewTreeObserver;
 import android.view.Window;
@@ -90,6 +89,7 @@ public final class FloatingToolbar {
             };
 
     private final Context mContext;
+    private final Window mWindow;
     private final FloatingToolbarPopup mPopup;
 
     private final Rect mContentRect = new Rect();
@@ -102,26 +102,31 @@ public final class FloatingToolbar {
     private int mSuggestedWidth;
     private boolean mWidthChanged = true;
 
-    private final ComponentCallbacks mOrientationChangeHandler = new ComponentCallbacks() {
+    private final OnLayoutChangeListener mOrientationChangeHandler = new OnLayoutChangeListener() {
+
+        private final Rect mNewRect = new Rect();
+        private final Rect mOldRect = new Rect();
+
         @Override
-        public void onConfigurationChanged(Configuration newConfig) {
-            if (mPopup.isShowing() && mPopup.viewPortHasChanged()) {
+        public void onLayoutChange(
+                View view,
+                int newLeft, int newRight, int newTop, int newBottom,
+                int oldLeft, int oldRight, int oldTop, int oldBottom) {
+            mNewRect.set(newLeft, newRight, newTop, newBottom);
+            mOldRect.set(oldLeft, oldRight, oldTop, oldBottom);
+            if (mPopup.isShowing() && !mNewRect.equals(mOldRect)) {
                 mWidthChanged = true;
                 updateLayout();
             }
         }
-
-        @Override
-        public void onLowMemory() {}
     };
 
     /**
      * Initializes a floating toolbar.
      */
     public FloatingToolbar(Context context, Window window) {
-        Preconditions.checkNotNull(context);
-        Preconditions.checkNotNull(window);
-        mContext = applyDefaultTheme(context);
+        mContext = applyDefaultTheme(Preconditions.checkNotNull(context));
+        mWindow = Preconditions.checkNotNull(window);
         mPopup = new FloatingToolbarPopup(mContext, window.getDecorView());
     }
 
@@ -179,21 +184,8 @@ public final class FloatingToolbar {
      * Shows this floating toolbar.
      */
     public FloatingToolbar show() {
-        mContext.unregisterComponentCallbacks(mOrientationChangeHandler);
-        mContext.registerComponentCallbacks(mOrientationChangeHandler);
-        List<MenuItem> menuItems = getVisibleAndEnabledMenuItems(mMenu);
-        if (!isCurrentlyShowing(menuItems) || mWidthChanged) {
-            mPopup.dismiss();
-            mPopup.layoutMenuItems(menuItems, mMenuItemClickListener, mSuggestedWidth);
-            mShowingMenuItems = getShowingMenuItemsReferences(menuItems);
-        }
-        if (!mPopup.isShowing()) {
-            mPopup.show(mContentRect);
-        } else if (!mPreviousContentRect.equals(mContentRect)) {
-            mPopup.updateCoordinates(mContentRect);
-        }
-        mWidthChanged = false;
-        mPreviousContentRect.set(mContentRect);
+        registerOrientationHandler();
+        doShow();
         return this;
     }
 
@@ -203,8 +195,7 @@ public final class FloatingToolbar {
      */
     public FloatingToolbar updateLayout() {
         if (mPopup.isShowing()) {
-            // show() performs all the logic we need here.
-            show();
+            doShow();
         }
         return this;
     }
@@ -213,7 +204,7 @@ public final class FloatingToolbar {
      * Dismisses this floating toolbar.
      */
     public void dismiss() {
-        mContext.unregisterComponentCallbacks(mOrientationChangeHandler);
+        unregisterOrientationHandler();
         mPopup.dismiss();
     }
 
@@ -239,6 +230,22 @@ public final class FloatingToolbar {
         return mPopup.isHidden();
     }
 
+    private void doShow() {
+        List<MenuItem> menuItems = getVisibleAndEnabledMenuItems(mMenu);
+        if (!isCurrentlyShowing(menuItems) || mWidthChanged) {
+            mPopup.dismiss();
+            mPopup.layoutMenuItems(menuItems, mMenuItemClickListener, mSuggestedWidth);
+            mShowingMenuItems = getShowingMenuItemsReferences(menuItems);
+        }
+        if (!mPopup.isShowing()) {
+            mPopup.show(mContentRect);
+        } else if (!mPreviousContentRect.equals(mContentRect)) {
+            mPopup.updateCoordinates(mContentRect);
+        }
+        mWidthChanged = false;
+        mPreviousContentRect.set(mContentRect);
+    }
+
     /**
      * Returns true if this floating toolbar is currently showing the specified menu items.
      */
@@ -278,6 +285,15 @@ public final class FloatingToolbar {
         return references;
     }
 
+    private void registerOrientationHandler() {
+        unregisterOrientationHandler();
+        mWindow.getDecorView().addOnLayoutChangeListener(mOrientationChangeHandler);
+    }
+
+    private void unregisterOrientationHandler() {
+        mWindow.getDecorView().removeOnLayoutChangeListener(mOrientationChangeHandler);
+    }
+
 
     /**
      * A popup window used by the floating toolbar.
@@ -1012,11 +1028,6 @@ public final class FloatingToolbar {
             mParent.getWindowVisibleDisplayFrame(mViewPortOnScreen);
         }
 
-        private boolean viewPortHasChanged() {
-            mParent.getWindowVisibleDisplayFrame(mTmpRect);
-            return !mTmpRect.equals(mViewPortOnScreen);
-        }
-
         private int getAdjustedToolbarWidth(int suggestedWidth) {
             int width = suggestedWidth;
             refreshViewPort();
@@ -1663,4 +1674,4 @@ public final class FloatingToolbar {
         a.recycle();
         return new ContextThemeWrapper(originalContext, themeId);
     }
-}
\ No newline at end of file
+}