Bug 1416319 - 5. Update DynamicToolbarAnimator usage in other classes; r?rbarker draft
authorJim Chen <nchen@mozilla.com>
Wed, 22 Nov 2017 14:12:22 -0500
changeset 702128 af5a55d7398c896d74e0f361cbcfac43e095799d
parent 702127 85d42387419012bbe66a9e5d70b890333e8f0923
child 702129 81e834ef25038fd735281def9d7780c517595cea
push id90386
push userbmo:nchen@mozilla.com
push dateWed, 22 Nov 2017 19:12:37 +0000
reviewersrbarker
bugs1416319
milestone59.0a1
Bug 1416319 - 5. Update DynamicToolbarAnimator usage in other classes; r?rbarker Use DynamicToolbarAnimator through the session in places where we need to interact with the toolbar or get the current toolbar height. MozReview-Commit-ID: PdGX4FEHp5
mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
mobile/android/base/java/org/mozilla/gecko/DynamicToolbar.java
mobile/android/base/java/org/mozilla/gecko/FormAssistPopup.java
mobile/android/base/java/org/mozilla/gecko/GeckoAccessibility.java
mobile/android/base/java/org/mozilla/gecko/text/FloatingToolbarTextSelection.java
mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoInputConnection.java
mobile/android/geckoview/src/main/java/org/mozilla/gecko/gfx/LayerView.java
mobile/android/geckoview/src/main/java/org/mozilla/gecko/gfx/NativePanZoomController.java
mobile/android/geckoview/src/main/java/org/mozilla/gecko/gfx/OverscrollEdgeEffect.java
--- a/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
@@ -1733,17 +1733,17 @@ public class BrowserApp extends GeckoApp
         if (mBrowserChrome != null) {
             height = mBrowserChrome.getHeight();
         }
 
         mHomeScreenContainer.setPadding(0, height, 0, 0);
 
         if (mLayerView != null && height != mToolbarHeight) {
             mToolbarHeight = height;
-            mLayerView.setMaxToolbarHeight(height);
+            mLayerView.getDynamicToolbarAnimator().setMaxToolbarHeight(height);
             mDynamicToolbar.setVisible(true, VisibilityTransition.IMMEDIATE);
         }
     }
 
     @Override
     void toggleChrome(final boolean aShow) {
         if (mDynamicToolbar != null) {
             mDynamicToolbar.setVisible(aShow, VisibilityTransition.IMMEDIATE);
--- a/mobile/android/base/java/org/mozilla/gecko/DynamicToolbar.java
+++ b/mobile/android/base/java/org/mozilla/gecko/DynamicToolbar.java
@@ -1,13 +1,12 @@
 package org.mozilla.gecko;
 
 import org.mozilla.gecko.PrefsHelper.PrefHandlerBase;
 import org.mozilla.gecko.gfx.DynamicToolbarAnimator.PinReason;
-import org.mozilla.gecko.gfx.LayerView;
 import org.mozilla.gecko.util.ThreadUtils;
 
 import android.os.Build;
 import android.os.Bundle;
 import android.util.Log;
 
 public class DynamicToolbar {
     private static final String LOGTAG = "DynamicToolbar";
@@ -20,17 +19,17 @@ public class DynamicToolbar {
     // the pref from Gecko telling us to turn it on.
     private volatile boolean prefEnabled;
     private boolean accessibilityEnabled;
     // On some device we have to force-disable the dynamic toolbar because of
     // bugs in the Android code. See bug 1231554.
     private final boolean forceDisabled;
 
     private final PrefsHelper.PrefHandler prefObserver;
-    private LayerView layerView;
+    private GeckoView layerView;
     private OnEnabledChangedListener enabledChangedListener;
     private boolean temporarilyVisible;
 
     public enum VisibilityTransition {
         IMMEDIATE,
         ANIMATE
     }
 
@@ -72,17 +71,17 @@ public class DynamicToolbar {
         }
         return false;
     }
 
     public void destroy() {
         PrefsHelper.removeObserver(prefObserver);
     }
 
-    public void setLayerView(LayerView layerView) {
+    public void setLayerView(GeckoView layerView) {
         ThreadUtils.assertOnUiThread();
 
         this.layerView = layerView;
     }
 
     public void setEnabledChangedListener(OnEnabledChangedListener listener) {
         ThreadUtils.assertOnUiThread();
 
--- a/mobile/android/base/java/org/mozilla/gecko/FormAssistPopup.java
+++ b/mobile/android/base/java/org/mozilla/gecko/FormAssistPopup.java
@@ -249,17 +249,17 @@ public class FormAssistPopup extends Rel
         mW = rect.getDouble("w");
         mH = rect.getDouble("h");
         mPopupType = (isAutoComplete ?
                       PopupType.AUTOCOMPLETE : PopupType.VALIDATIONMESSAGE);
         return true;
     }
 
     private void positionAndShowPopup() {
-        positionAndShowPopup(mGeckoView.getViewportMetrics());
+        positionAndShowPopup(mGeckoView.getSession().getViewportMetrics());
     }
 
     private void positionAndShowPopup(ImmutableViewportMetrics aMetrics) {
         ThreadUtils.assertOnUiThread();
 
         // Don't show the form assist popup when using fullscreen VKB
         InputMethodManager imm = (InputMethodManager)
                 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
@@ -283,17 +283,17 @@ public class FormAssistPopup extends Rel
         }
 
         float zoom = aMetrics.zoomFactor;
 
         // These values correspond to the input box for which we want to
         // display the FormAssistPopup.
         int left = (int) (mX * zoom - aMetrics.viewportRectLeft);
         int top = (int) (mY * zoom - aMetrics.viewportRectTop + mGeckoView.getTop() +
-                         mGeckoView.getCurrentToolbarHeight());
+                         mGeckoView.getDynamicToolbarAnimator().getCurrentToolbarHeight());
         int width = (int) (mW * zoom);
         int height = (int) (mH * zoom);
 
         int popupWidth = LayoutParams.MATCH_PARENT;
         int popupLeft = left < 0 ? 0 : left;
 
         FloatSize viewport = aMetrics.getSize();
 
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoAccessibility.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoAccessibility.java
@@ -171,17 +171,17 @@ public class GeckoAccessibility {
 
             final GeckoBundle bounds = message.getBundle("bounds");
             if (bounds != null) {
                 Rect relativeBounds = new Rect(bounds.getInt("left"), bounds.getInt("top"),
                                                bounds.getInt("right"), bounds.getInt("bottom"));
                 sVirtualCursorNode.setBoundsInParent(relativeBounds);
                 int[] locationOnScreen = new int[2];
                 view.getLocationOnScreen(locationOnScreen);
-                locationOnScreen[1] += view.getCurrentToolbarHeight();
+                locationOnScreen[1] += view.getDynamicToolbarAnimator().getCurrentToolbarHeight();
                 Rect screenBounds = new Rect(relativeBounds);
                 screenBounds.offset(locationOnScreen[0], locationOnScreen[1]);
                 sVirtualCursorNode.setBoundsInScreen(screenBounds);
             }
 
             final GeckoBundle braille = message.getBundle("brailleOutput");
             if (braille != null) {
                 sendBrailleText(view, braille.getString("text", ""),
--- a/mobile/android/base/java/org/mozilla/gecko/text/FloatingToolbarTextSelection.java
+++ b/mobile/android/base/java/org/mozilla/gecko/text/FloatingToolbarTextSelection.java
@@ -168,18 +168,18 @@ public class FloatingToolbarTextSelectio
     }
 
     private void updateRect(final GeckoBundle message) {
         final double x = message.getDouble("x");
         final double y = (int) message.getDouble("y");
         final double width = (int) message.getDouble("width");
         final double height = (int) message.getDouble("height");
 
-        final float toolbarOffset = geckoView.getCurrentToolbarHeight();
-        final float zoomFactor = geckoView.getZoomFactor();
+        final float toolbarOffset = geckoView.getDynamicToolbarAnimator().getCurrentToolbarHeight();
+        final float zoomFactor = geckoView.getSession().getViewportMetrics().zoomFactor;
         geckoView.getLocationInWindow(locationInWindow);
 
         contentRect = new Rect(
                 (int) (x * zoomFactor + locationInWindow[0]),
                 (int) (y * zoomFactor + locationInWindow[1] + toolbarOffset),
                 (int) ((x + width) * zoomFactor + locationInWindow[0]),
                 (int) ((y + height) * zoomFactor + locationInWindow[1] +
                        (height > 0 ? handlesOffset : 0)));
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoInputConnection.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoInputConnection.java
@@ -230,17 +230,17 @@ class GeckoInputConnection
             public void run() {
                 if (v.hasFocus() && !imm.isActive(v)) {
                     // Marshmallow workaround: The view has focus but it is not the active
                     // view for the input method. (Bug 1211848)
                     v.clearFocus();
                     v.requestFocus();
                 }
                 final GeckoView view = getView();
-                if (view != null) {
+                if (view != null && view.getSession() != null) {
                     if (showToolbar) {
                         view.getDynamicToolbarAnimator().showToolbar(/*immediately*/ true);
                     }
                     view.getEventDispatcher().dispatch("GeckoView:ZoomToInput", null);
                 }
                 mSoftInputReentrancyGuard = true;
                 imm.showSoftInput(v, 0);
                 mSoftInputReentrancyGuard = false;
@@ -359,22 +359,22 @@ class GeckoInputConnection
 
         if (mCursorAnchorInfoBuilder == null) {
             mCursorAnchorInfoBuilder = new CursorAnchorInfo.Builder();
         }
         mCursorAnchorInfoBuilder.reset();
 
         // Calculate Gecko logical coords to screen coords
         final GeckoView view = getView();
-        if (view == null) {
+        if (view == null || view.getSession() == null) {
             return;
         }
 
         // First aRects element is the widget bounds in device units.
-        final float zoom = view.getZoomFactor();
+        final float zoom = view.getSession().getViewportMetrics().zoomFactor;
         final Matrix matrix = new Matrix();
         matrix.postScale(zoom, zoom);
         matrix.postTranslate(aRects[0].left, aRects[0].top);
         mCursorAnchorInfoBuilder.setMatrix(matrix);
 
         final Editable content = getEditable();
         if (content == null) {
             return;
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/gfx/LayerView.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/gfx/LayerView.java
@@ -150,16 +150,17 @@ public class LayerView extends FrameLayo
         }
     }
 
     /* package */ void onCompositorReady() {
         mCompositor.setDefaultClearColor(mDefaultClearColor);
         mCompositor.enableLayerUpdateNotifications(!mDrawListeners.isEmpty());
     }
 
+    /* protected */ LayerSession mSession;
     private LayerSession.Compositor mCompositor;
 
     public LayerView(Context context, AttributeSet attrs) {
         super(context, attrs);
 
         mFullScreenState = FullScreenState.NONE;
 
         mOverscroll = new OverscrollEdgeEffect(this);
@@ -208,17 +209,17 @@ public class LayerView extends FrameLayo
     }
 
     @Override
     public void dispatchDraw(final Canvas canvas) {
         super.dispatchDraw(canvas);
 
         // We must have a layer client to get valid viewport metrics
         if (mOverscroll != null) {
-            mOverscroll.draw(canvas, getViewportMetrics());
+            mOverscroll.draw(canvas, mSession.getViewportMetrics());
         }
     }
 
     @Override
     public boolean onTouchEvent(MotionEvent event) {
         if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
             requestFocus();
         }
@@ -316,16 +317,17 @@ public class LayerView extends FrameLayo
     /* package */ void recvScreenPixels(int width, int height, int[] pixels) {
         if (mGetPixelsResult != null) {
             mGetPixelsResult.onPixelsResult(width, height, IntBuffer.wrap(pixels));
             mGetPixelsResult = null;
         }
     }
 
     protected void attachCompositor(final LayerSession session) {
+        mSession = session;
         mCompositor = session.mCompositor;
         mCompositor.layerView = this;
 
         mToolbarAnimator.notifyCompositorCreated(mCompositor);
 
         final NativePanZoomController npzc = (NativePanZoomController) mPanZoomController;
 
         if (GeckoThread.isStateAtLeast(GeckoThread.State.PROFILE_READY)) {
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/gfx/NativePanZoomController.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/gfx/NativePanZoomController.java
@@ -107,17 +107,19 @@ class NativePanZoomController extends JN
         if (count <= 0) {
             return false;
         }
 
         final MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords();
         event.getPointerCoords(0, coords);
         final float x = coords.x;
         // Scroll events are not adjusted by the AndroidDyanmicToolbarAnimator so adjust the offset here.
-        final float y = coords.y - mView.getCurrentToolbarHeight();
+        final int toolbarHeight = (mView.mSession != null) ?
+            mView.mSession.getDynamicToolbarAnimator().getCurrentToolbarHeight() : 0;
+        final float y = coords.y - toolbarHeight;
 
         final float hScroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL) *
                               mPointerScrollFactor;
         final float vScroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL) *
                               mPointerScrollFactor;
 
         return handleScrollEvent(event.getEventTime(), event.getMetaState(), x, y, hScroll, vScroll);
     }
@@ -133,17 +135,19 @@ class NativePanZoomController extends JN
             return false;
         }
 
         final MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords();
         event.getPointerCoords(0, coords);
         final float x = coords.x;
         // Mouse events are not adjusted by the AndroidDyanmicToolbarAnimator so adjust the offset
         // here.
-        final float y = coords.y - mView.getCurrentToolbarHeight();
+        final int toolbarHeight = (mView.mSession != null) ?
+            mView.mSession.getDynamicToolbarAnimator().getCurrentToolbarHeight() : 0;
+        final float y = coords.y - toolbarHeight;
 
         return handleMouseEvent(event.getActionMasked(), event.getEventTime(), event.getMetaState(), x, y, event.getButtonState());
     }
 
 
     NativePanZoomController(View view) {
         MAX_SCROLL = 0.075f * view.getContext().getResources().getDisplayMetrics().densityDpi;
 
@@ -255,18 +259,27 @@ class NativePanZoomController extends JN
         }
     }
 
     /**
      * Active SelectionCaretDrag requires DynamicToolbarAnimator to be pinned
      * to avoid unwanted scroll interactions.
      */
     @WrapForJNI(calledFrom = "gecko")
-    private void onSelectionDragState(boolean state) {
-        mView.getDynamicToolbarAnimator().setPinned(state, PinReason.CARET_DRAG);
+    private void onSelectionDragState(final boolean state) {
+        final LayerView view = mView;
+        ThreadUtils.postToUiThread(new Runnable() {
+            @Override
+            public void run() {
+                if (view.mSession != null) {
+                    view.mSession.getDynamicToolbarAnimator()
+                                 .setPinned(state, PinReason.CARET_DRAG);
+                }
+            }
+        });
     }
 
     private static class PointerInfo {
         // We reserve one pointer ID for the mouse, so that tests don't have
         // to worry about tracking pointer IDs if they just want to test mouse
         // event synthesization. If somebody tries to use this ID for a
         // synthesized touch event we'll throw an exception.
         public static final int RESERVED_MOUSE_POINTER_ID = 100000;
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/gfx/OverscrollEdgeEffect.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/gfx/OverscrollEdgeEffect.java
@@ -114,39 +114,41 @@ public class OverscrollEdgeEffect implem
 
         final EdgeEffect edge = getEdgeForAxisAndSide(axis, (int)distance);
         edge.onPull(distance / (axis == Axis.X ? mView.getWidth() : mView.getHeight()));
         invalidate();
     }
 
     @Override
     public void draw(final Canvas canvas, final ImmutableViewportMetrics metrics) {
-        if (metrics == null) {
+        if (metrics == null || mView.mSession == null) {
             return;
         }
 
-        PointF visibleEnd = mView.getDynamicToolbarAnimator().getVisibleEndOfLayerView();
-        float toolbarEnd = (float)mView.getDynamicToolbarAnimator().getCurrentToolbarHeight();
+        final float width = mView.getWidth();
+        final float height = mView.getHeight();
+        final float toolbarEnd = mView.mSession.getDynamicToolbarAnimator()
+                                               .getCurrentToolbarHeight();
 
         // If we're pulling an edge, or fading it out, draw!
         boolean invalidate = false;
         if (!mEdges[TOP].isFinished()) {
             invalidate |= draw(mEdges[TOP], canvas, 0, toolbarEnd, 0);
         }
 
         if (!mEdges[BOTTOM].isFinished()) {
-            invalidate |= draw(mEdges[BOTTOM], canvas, visibleEnd.x, visibleEnd.y, 180);
+            invalidate |= draw(mEdges[BOTTOM], canvas, width, height, 180);
         }
 
         if (!mEdges[LEFT].isFinished()) {
-            invalidate |= draw(mEdges[LEFT], canvas, 0, visibleEnd.y, 270);
+            invalidate |= draw(mEdges[LEFT], canvas, 0, height, 270);
         }
 
         if (!mEdges[RIGHT].isFinished()) {
-            invalidate |= draw(mEdges[RIGHT], canvas, visibleEnd.x, 0, 90);
+            invalidate |= draw(mEdges[RIGHT], canvas, width, 0, 90);
         }
 
         // If the edge effect is animating off screen, invalidate.
         if (invalidate) {
             invalidate();
         }
     }