Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bffa851f authored by Derek Sollenberger's avatar Derek Sollenberger
Browse files

Finish refactoring zoom logic from WebView to ZoomManager.

This is the final CL in a series of CL's that moved the
location of the zoom logic from WebView to ZoomManager.

Change-Id: Ie1e80b9b3108bd4cb0b0ee2822dc984da91c2d86
http://b/2671604
parent 15c5ddb7
Loading
Loading
Loading
Loading
+25 −31
Original line number Diff line number Diff line
@@ -1296,29 +1296,23 @@ public class WebView extends AbsoluteLayout
        // now update the bundle
        b.putInt("scrollX", mScrollX);
        b.putInt("scrollY", mScrollY);
        b.putFloat("scale", mZoomManager.mActualScale);
        b.putFloat("textwrapScale", mZoomManager.mTextWrapScale);
        b.putBoolean("overview", mZoomManager.mInZoomOverview);
        mZoomManager.saveZoomState(b);
        return true;
    }

    private void restoreHistoryPictureFields(Picture p, Bundle b) {
        int sx = b.getInt("scrollX", 0);
        int sy = b.getInt("scrollY", 0);
        float scale = b.getFloat("scale", 1.0f);

        mDrawHistory = true;
        mHistoryPicture = p;
        mScrollX = sx;
        mScrollY = sy;
        mZoomManager.restoreZoomState(b);
        final float scale = mZoomManager.getScale();
        mHistoryWidth = Math.round(p.getWidth() * scale);
        mHistoryHeight = Math.round(p.getHeight() * scale);
        // as getWidth() / getHeight() of the view are not available yet, set up
        // mActualScale, so that when onSizeChanged() is called, the rest will
        // be set correctly
        mZoomManager.mActualScale = scale;
        mZoomManager.mInvActualScale = 1 / scale;
        mZoomManager.mTextWrapScale = b.getFloat("textwrapScale", scale);
        mZoomManager.mInZoomOverview = b.getBoolean("overview");

        invalidate();
    }

@@ -1741,7 +1735,7 @@ public class WebView extends AbsoluteLayout
     * @return The current scale.
     */
    public float getScale() {
        return mZoomManager.mActualScale;
        return mZoomManager.getScale();
    }

    /**
@@ -1954,7 +1948,7 @@ public class WebView extends AbsoluteLayout
     * height.
     */
    private int viewToContentDimension(int d) {
        return Math.round(d * mZoomManager.mInvActualScale);
        return Math.round(d * mZoomManager.getInvScale());
    }

    /**
@@ -1980,7 +1974,7 @@ public class WebView extends AbsoluteLayout
     * Returns the result as a float.
     */
    private float viewToContentXf(int x) {
        return x * mZoomManager.mInvActualScale;
        return x * mZoomManager.getInvScale();
    }

    /**
@@ -1989,7 +1983,7 @@ public class WebView extends AbsoluteLayout
     * embedded into the WebView. Returns the result as a float.
     */
    private float viewToContentYf(int y) {
        return (y - getTitleHeight()) * mZoomManager.mInvActualScale;
        return (y - getTitleHeight()) * mZoomManager.getInvScale();
    }

    /**
@@ -1999,7 +1993,7 @@ public class WebView extends AbsoluteLayout
     * height.
     */
    /*package*/ int contentToViewDimension(int d) {
        return Math.round(d * mZoomManager.mActualScale);
        return Math.round(d * mZoomManager.getScale());
    }

    /**
@@ -2040,7 +2034,7 @@ public class WebView extends AbsoluteLayout
    // Called by JNI to invalidate the View, given rectangle coordinates in
    // content space
    private void viewInvalidate(int l, int t, int r, int b) {
        final float scale = mZoomManager.mActualScale;
        final float scale = mZoomManager.getScale();
        final int dy = getTitleHeight();
        invalidate((int)Math.floor(l * scale),
                   (int)Math.floor(t * scale) + dy,
@@ -2051,7 +2045,7 @@ public class WebView extends AbsoluteLayout
    // Called by JNI to invalidate the View after a delay, given rectangle
    // coordinates in content space
    private void viewInvalidateDelayed(long delay, int l, int t, int r, int b) {
        final float scale = mZoomManager.mActualScale;
        final float scale = mZoomManager.getScale();
        final int dy = getTitleHeight();
        postInvalidateDelayed(delay,
                              (int)Math.floor(l * scale),
@@ -2196,8 +2190,8 @@ public class WebView extends AbsoluteLayout
        if (mZoomManager.isPreventingWebkitUpdates()) return false;

        int viewWidth = getViewWidth();
        int newWidth = Math.round(viewWidth * mZoomManager.mInvActualScale);
        int newHeight = Math.round(getViewHeight() * mZoomManager.mInvActualScale);
        int newWidth = Math.round(viewWidth * mZoomManager.getInvScale());
        int newHeight = Math.round(getViewHeight() * mZoomManager.getInvScale());
        /*
         * Because the native side may have already done a layout before the
         * View system was able to measure us, we have to send a height of 0 to
@@ -2214,8 +2208,8 @@ public class WebView extends AbsoluteLayout
            ViewSizeData data = new ViewSizeData();
            data.mWidth = newWidth;
            data.mHeight = newHeight;
            data.mTextWrapWidth = Math.round(viewWidth / mZoomManager.mTextWrapScale);
            data.mScale = mZoomManager.mActualScale;
            data.mTextWrapWidth = Math.round(viewWidth / mZoomManager.getTextWrapScale());
            data.mScale = mZoomManager.getScale();
            data.mIgnoreHeight = mZoomManager.isFixedLengthAnimationInProgress()
                    && !mHeightCanMeasure;
            data.mAnchorX = mZoomManager.getDocumentAnchorX();
@@ -2239,7 +2233,7 @@ public class WebView extends AbsoluteLayout
            return computeHorizontalScrollExtent();
        } else {
            // to avoid rounding error caused unnecessary scrollbar, use floor
            return (int) Math.floor(mContentWidth * mZoomManager.mActualScale);
            return (int) Math.floor(mContentWidth * mZoomManager.getScale());
        }
    }

@@ -2253,7 +2247,7 @@ public class WebView extends AbsoluteLayout
            return computeVerticalScrollExtent();
        } else {
            // to avoid rounding error caused unnecessary scrollbar, use floor
            return (int) Math.floor(mContentHeight * mZoomManager.mActualScale);
            return (int) Math.floor(mContentHeight * mZoomManager.getScale());
        }
    }

@@ -3262,7 +3256,7 @@ public class WebView extends AbsoluteLayout
    private void drawCoreAndCursorRing(Canvas canvas, int color,
        boolean drawCursorRing) {
        if (mDrawHistory) {
            canvas.scale(mZoomManager.mActualScale, mZoomManager.mActualScale);
            canvas.scale(mZoomManager.getScale(), mZoomManager.getScale());
            canvas.drawPicture(mHistoryPicture);
            return;
        }
@@ -3288,7 +3282,7 @@ public class WebView extends AbsoluteLayout
        if (animateZoom) {
            mZoomManager.animateZoom(canvas);
        } else {
            canvas.scale(mZoomManager.mActualScale, mZoomManager.mActualScale);
            canvas.scale(mZoomManager.getScale(), mZoomManager.getScale());
        }

        boolean UIAnimationsRunning = false;
@@ -3316,7 +3310,7 @@ public class WebView extends AbsoluteLayout
            if (!mZoomManager.isZoomAnimating()) {
                extras = DRAW_EXTRAS_SELECTION;
                nativeSetSelectionRegion(mTouchSelection || mExtendSelection);
                nativeSetSelectionPointer(!mTouchSelection, mZoomManager.mInvActualScale,
                nativeSetSelectionPointer(!mTouchSelection, mZoomManager.getInvScale(),
                        mSelectX, mSelectY - getTitleHeight(),
                        mExtendSelection);
            }
@@ -3436,7 +3430,7 @@ public class WebView extends AbsoluteLayout
                getContext().getSystemService(Context.INPUT_METHOD_SERVICE);

        // bring it back to the default scale so that user can enter text
        boolean zoom = mZoomManager.mActualScale < mZoomManager.getDefaultScale();
        boolean zoom = mZoomManager.getScale() < mZoomManager.getDefaultScale();
        if (zoom) {
            mZoomManager.setZoomCenter(mLastTouchX, mLastTouchY);
            mZoomManager.setZoomScale(mZoomManager.getDefaultScale(), false);
@@ -4123,7 +4117,7 @@ public class WebView extends AbsoluteLayout
        // adjust the max viewport width depending on the view dimensions. This
        // is to ensure the scaling is not going insane. So do not shrink it if
        // the view size is temporarily smaller, e.g. when soft keyboard is up.
        int newMaxViewportWidth = (int) (Math.max(w, h) / ZoomManager.DEFAULT_MIN_ZOOM_SCALE);
        int newMaxViewportWidth = (int) (Math.max(w, h) / ZoomManager.getDefaultMinZoomScale());
        if (newMaxViewportWidth > sMaxViewportWidth) {
            sMaxViewportWidth = newMaxViewportWidth;
        }
@@ -5616,7 +5610,7 @@ public class WebView extends AbsoluteLayout
                    contentToViewY(docY + docHeight / 2) - viewHeight / 2,
                    true, 0);
        } else {
            float actualScale = mZoomManager.mActualScale;
            float actualScale = mZoomManager.getScale();
            float oldScreenX = docX * actualScale - mScrollX;
            float rectViewX = docX * scale;
            float rectViewWidth = docWidth * scale;
@@ -6762,7 +6756,7 @@ public class WebView extends AbsoluteLayout
        // FIXME the divisor should be retrieved from somewhere
        // the closest thing today is hard-coded into ScrollView.java
        // (from ScrollView.java, line 363)   int maxJump = height/2;
        return Math.round(height * mZoomManager.mInvActualScale);
        return Math.round(height * mZoomManager.getInvScale());
    }

    /**
+76 −35
Original line number Diff line number Diff line
@@ -19,12 +19,17 @@ package android.webkit;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.graphics.Point;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.ScaleGestureDetector;
import android.view.View;

import java.io.File;
import java.io.FileOutputStream;

/**
 * The ZoomManager is responsible for maintaining the WebView's current zoom
 * level state.  It is also responsible for managing the on-screen zoom controls
@@ -50,25 +55,21 @@ class ZoomManager {
    private final WebView mWebView;
    private final CallbackProxy mCallbackProxy;

    // manages the on-screen zoom functions of the WebView
    // Widgets responsible for the on-screen zoom functions of the WebView.
    private ZoomControlEmbedded mEmbeddedZoomControl;

    private ZoomControlExternal mExternalZoomControl;

    /*
     * TODO: clean up the visibility of the class variables when the zoom
     * refactoring is complete
     */
    // The default scale limits, which are dependent on the display density.
    private static float DEFAULT_MAX_ZOOM_SCALE;

    // default scale limits, which are dependent on the display density
    static float DEFAULT_MAX_ZOOM_SCALE;
    static float DEFAULT_MIN_ZOOM_SCALE;
    private static float DEFAULT_MIN_ZOOM_SCALE;

    // actual scale limits, which can be set through a webpage viewport meta tag
    // The actual scale limits, which can be set through a webpage's viewport
    // meta-tag.
    private float mMaxZoomScale;
    private float mMinZoomScale;

    // locks the minimum ZoomScale to the value currently set in mMinZoomScale
    // Locks the minimum ZoomScale to the value currently set in mMinZoomScale.
    private boolean mMinZoomScaleFixed = true;

    /*
@@ -79,15 +80,17 @@ class ZoomManager {
     * mode, but this value should only be modified by changes to the zoom
     * scale.
     */
    boolean mInZoomOverview = false;
    private boolean mInZoomOverview = false;
    private int mZoomOverviewWidth;
    private float mInvZoomOverviewWidth;

    // These keep track of the center point of the zoom and they are used to
    // determine the point around which we should zoom. They are stored in view
    // coordinates.
    float mZoomCenterX;
    float mZoomCenterY;
    /*
     * These variables track the center point of the zoom and they are used to
     * determine the point around which we should zoom. They are stored in view
     * coordinates.
     */
    private float mZoomCenterX;
    private float mZoomCenterY;

    /*
     * These values represent the point around which the screen should be
@@ -100,18 +103,21 @@ class ZoomManager {
    private int mAnchorX;
    private int mAnchorY;

    float mTextWrapScale;
    // The scale factor that is used to determine the column width for text
    private float mTextWrapScale;

    // the default zoom scale. This value will is initially set based on the
    // display density, but can be changed at any time via the WebSettings.
    /*
     * The default zoom scale is the scale factor used when the user triggers a
     * zoom in by double tapping on the WebView. The value is initially set
     * based on the display density, but can be changed at any time via the
     * WebSettings.
     */
    private float mDefaultScale;
    private float mInvDefaultScale;

    private static float MINIMUM_SCALE_INCREMENT = 0.01f;

    // the current computed zoom scale and its inverse.
    float mActualScale;
    float mInvActualScale;
    private float mActualScale;
    private float mInvActualScale;
    
    /*
     * The initial scale for the WebView. 0 means default. If initial scale is
@@ -123,6 +129,8 @@ class ZoomManager {
     */
    private float mInitialScale;

    private static float MINIMUM_SCALE_INCREMENT = 0.01f;

    /*
     * The following member variables are only to be used for animating zoom. If
     * mZoomScale is non-zero then we are in the middle of a zoom animation. The
@@ -135,7 +143,8 @@ class ZoomManager {
    private int mInitialScrollX;
    private int mInitialScrollY;
    private long mZoomStart;
    static final int ZOOM_ANIMATION_LENGTH = 500;

    private static final int ZOOM_ANIMATION_LENGTH = 500;

    // whether support multi-touch
    private boolean mSupportMultiTouch;
@@ -184,32 +193,48 @@ class ZoomManager {
        DEFAULT_MIN_ZOOM_SCALE = 0.25f * defaultScale;
    }

    public float getDefaultScale() {
    public final float getScale() {
        return mActualScale;
    }

    public final float getInvScale() {
        return mInvActualScale;
    }

    public final float getTextWrapScale() {
        return mTextWrapScale;
    }

    public final float getDefaultScale() {
        return mDefaultScale;
    }

    public int getDocumentAnchorX() {
    public final int getDocumentAnchorX() {
        return mAnchorX;
    }

    public int getDocumentAnchorY() {
    public final int getDocumentAnchorY() {
        return mAnchorY;
    }

    public void clearDocumentAnchor() {
    public final void clearDocumentAnchor() {
        mAnchorX = mAnchorY = 0;
    }

    public void setZoomCenter(float x, float y) {
    public final void setZoomCenter(float x, float y) {
        mZoomCenterX = x;
        mZoomCenterY = y;
    }

    public void setInitialScaleInPercent(int scaleInPercent) {
    public final void setInitialScaleInPercent(int scaleInPercent) {
        mInitialScale = scaleInPercent * 0.01f;
    }

    public float computeScaleWithLimits(float scale) {
    public static final float getDefaultMinZoomScale() {
        return DEFAULT_MIN_ZOOM_SCALE;
    }

    public final float computeScaleWithLimits(float scale) {
        if (scale < mMinZoomScale) {
            scale = mMinZoomScale;
        } else if (scale > mMaxZoomScale) {
@@ -218,7 +243,7 @@ class ZoomManager {
        return scale;
    }

    public boolean isZoomScaleFixed() {
    public final boolean isZoomScaleFixed() {
        return mMinZoomScale >= mMaxZoomScale;
    }

@@ -230,11 +255,11 @@ class ZoomManager {
        return exceedsMinScaleIncrement(scale, mActualScale);
    }

    public boolean canZoomIn() {
    public final boolean canZoomIn() {
        return mMaxZoomScale - mActualScale > MINIMUM_SCALE_INCREMENT;
    }

    public boolean canZoomOut() {
    public final boolean canZoomOut() {
        return mActualScale - mMinZoomScale > MINIMUM_SCALE_INCREMENT;
    }

@@ -746,6 +771,22 @@ class ZoomManager {
        }
    }

    public void saveZoomState(Bundle b) {
        b.putFloat("scale", mActualScale);
        b.putFloat("textwrapScale", mTextWrapScale);
        b.putBoolean("overview", mInZoomOverview);
    }

    public void restoreZoomState(Bundle b) {
        // as getWidth() / getHeight() of the view are not available yet, set up
        // mActualScale, so that when onSizeChanged() is called, the rest will
        // be set correctly
        mActualScale = b.getFloat("scale", 1.0f);
        mInvActualScale = 1 / mActualScale;
        mTextWrapScale = b.getFloat("textwrapScale", mActualScale);
        mInZoomOverview = b.getBoolean("overview");
    }

    private ZoomControlBase getCurrentZoomControl() {
        if (mWebView.getSettings() != null && mWebView.getSettings().supportZoom()) {
            if (mWebView.getSettings().getBuiltInZoomControls()) {