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

Commit 4f8680bb authored by Eugene Susla's avatar Eugene Susla
Browse files

MagnificationGestureHandler refactoring and unit test

This is aimed at making MagnificationGestureHandler easier to understand
and reason about

Test: provided unit test + manual magnification test
Change-Id: I958ef0bdd2e6f857a2fab24962b1a06480685732
parent 44bc284d
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -78,4 +78,12 @@ public class ExceptionUtils {
        propagateIfInstanceOf(t, RuntimeException.class);
        throw new RuntimeException(t);
    }

    /**
     * Gets the root {@link Throwable#getCause() cause} of {@code t}
     */
    public static @NonNull Throwable getRootCause(@NonNull Throwable t) {
        while (t.getCause() != null) t = t.getCause();
        return t;
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -417,7 +417,8 @@ class AccessibilityInputFilter extends InputFilter implements EventStreamTransfo
            final boolean triggerable = (mEnabledFeatures
                    & FLAG_FEATURE_TRIGGERED_SCREEN_MAGNIFIER) != 0;
            mMagnificationGestureHandler = new MagnificationGestureHandler(
                    mContext, mAms, detectControlGestures, triggerable);
                    mContext, mAms.getMagnificationController(),
                    detectControlGestures, triggerable);
            addFirstEventHandler(mMagnificationGestureHandler);
        }

+7 −13
Original line number Diff line number Diff line
@@ -12,32 +12,27 @@ final class GestureUtils {
        /* cannot be instantiated */
    }

    public static boolean isTap(MotionEvent down, MotionEvent up, int tapTimeSlop,
            int tapDistanceSlop, int actionIndex) {
        return eventsWithinTimeAndDistanceSlop(down, up, tapTimeSlop, tapDistanceSlop, actionIndex);
    }

    public static boolean isMultiTap(MotionEvent firstUp, MotionEvent secondUp,
            int multiTapTimeSlop, int multiTapDistanceSlop, int actionIndex) {
            int multiTapTimeSlop, int multiTapDistanceSlop) {
        if (firstUp == null || secondUp == null) return false;
        return eventsWithinTimeAndDistanceSlop(firstUp, secondUp, multiTapTimeSlop,
                multiTapDistanceSlop, actionIndex);
                multiTapDistanceSlop);
    }

    private static boolean eventsWithinTimeAndDistanceSlop(MotionEvent first, MotionEvent second,
            int timeout, int distance, int actionIndex) {
            int timeout, int distance) {
        if (isTimedOut(first, second, timeout)) {
            return false;
        }
        final double deltaMove = computeDistance(first, second, actionIndex);
        final double deltaMove = distance(first, second);
        if (deltaMove >= distance) {
            return false;
        }
        return true;
    }

    public static double computeDistance(MotionEvent first, MotionEvent second, int pointerIndex) {
         return MathUtils.dist(first.getX(pointerIndex), first.getY(pointerIndex),
                 second.getX(pointerIndex), second.getY(pointerIndex));
    public static double distance(MotionEvent first, MotionEvent second) {
        return MathUtils.dist(first.getX(), first.getY(), second.getX(), second.getY());
    }

    public static boolean isTimedOut(MotionEvent firstUp, MotionEvent secondUp, int timeout) {
@@ -54,7 +49,6 @@ final class GestureUtils {
    /**
     * Determines whether a two pointer gesture is a dragging one.
     *
     * @param event The event with the pointer data.
     * @return True if the gesture is a dragging one.
     */
    public static boolean isDraggingGesture(float firstPtrDownX, float firstPtrDownY,
+29 −11
Original line number Diff line number Diff line
@@ -16,11 +16,6 @@

package com.android.server.accessibility;

import com.android.internal.R;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.os.SomeArgs;
import com.android.server.LocalServices;

import android.animation.ValueAnimator;
import android.annotation.NonNull;
import android.content.BroadcastReceiver;
@@ -42,6 +37,12 @@ import android.view.View;
import android.view.WindowManagerInternal;
import android.view.animation.DecelerateInterpolator;

import com.android.internal.R;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.os.SomeArgs;
import com.android.server.LocalServices;

import java.util.Locale;

/**
@@ -138,7 +139,7 @@ class MagnificationController implements Handler.Callback {
    private final WindowManagerInternal mWindowManager;

    // Flag indicating that we are registered with window manager.
    private boolean mRegistered;
    @VisibleForTesting boolean mRegistered;

    private boolean mUnregisterPending;

@@ -148,9 +149,14 @@ class MagnificationController implements Handler.Callback {
        mHandler = new Handler(context.getMainLooper(), this);
    }

    public MagnificationController(Context context, AccessibilityManagerService ams, Object lock,
            Handler handler, WindowManagerInternal windowManagerInternal,
            ValueAnimator valueAnimator, SettingsBridge settingsBridge) {
    public MagnificationController(
            Context context,
            AccessibilityManagerService ams,
            Object lock,
            Handler handler,
            WindowManagerInternal windowManagerInternal,
            ValueAnimator valueAnimator,
            SettingsBridge settingsBridge) {
        mHandler = handler;
        mWindowManager = windowManagerInternal;
        mMainThreadId = context.getMainLooper().getThread().getId();
@@ -672,8 +678,7 @@ class MagnificationController implements Handler.Callback {
     * Resets magnification if magnification and auto-update are both enabled.
     *
     * @param animate whether the animate the transition
     * @return {@code true} if magnification was reset to the disabled state,
     *         {@code false} if magnification is still active
     * @return whether was {@link #isMagnifying magnifying}
     */
    boolean resetIfNeeded(boolean animate) {
        synchronized (mLock) {
@@ -790,6 +795,19 @@ class MagnificationController implements Handler.Callback {
        return true;
    }

    @Override
    public String toString() {
        return "MagnificationController{" +
                "mCurrentMagnificationSpec=" + mCurrentMagnificationSpec +
                ", mMagnificationRegion=" + mMagnificationRegion +
                ", mMagnificationBounds=" + mMagnificationBounds +
                ", mUserId=" + mUserId +
                ", mIdOfLastServiceToMagnify=" + mIdOfLastServiceToMagnify +
                ", mRegistered=" + mRegistered +
                ", mUnregisterPending=" + mUnregisterPending +
                '}';
    }

    /**
     * Class responsible for animating spec on the main thread and sending spec
     * updates to the window manager.
+475 −351

File changed.

Preview size limit exceeded, changes collapsed.

Loading