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

Commit dcf3bf17 authored by Jerry Chang's avatar Jerry Chang
Browse files

Decouple DividerView from SystemUI

Moves FlingAnimationUtils to wm shell library as a common utils for both
wm shell and SystemUI.

There are some basic interpolators needed for wm shell library. Since
they are relative basic and already been put into different packages,
duplicates them directly to wm shell library.

Bug: 161116823
Test: atest SystemUITests
Test: manual check divider animation
Change-Id: Iffabb7eff829cad69ef3d273b803f8fbd3ab6685
parent fa817ade
Loading
Loading
Loading
Loading
+71 −63
Original line number Diff line number Diff line
@@ -11,10 +11,10 @@
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 * limitations under the License.
 */

package com.android.systemui.statusbar;
package com.android.wm.shell.animation;

import android.animation.Animator;
import android.util.DisplayMetrics;
@@ -23,11 +23,6 @@ import android.view.ViewPropertyAnimator;
import android.view.animation.Interpolator;
import android.view.animation.PathInterpolator;

import com.android.systemui.Interpolators;
import com.android.systemui.statusbar.notification.NotificationUtils;

import javax.inject.Inject;

/**
 * Utility class to calculate general fling animation when the finger is released.
 */
@@ -77,8 +72,8 @@ public class FlingAnimationUtils {
     * @param speedUpFactor    a factor from 0 to 1 how much the slow down should be shifted towards
     *                         the end of the animation. 0 means it's at the beginning and no
     *                         acceleration will take place.
     * @param x2 the x value to take for the second point of the bezier spline. If a value below 0
     *           is provided, the value is automatically calculated.
     * @param x2               the x value to take for the second point of the bezier spline. If a
     *                         value below 0 is provided, the value is automatically calculated.
     * @param y2               the y value to take for the second point of the bezier spline
     */
    public FlingAnimationUtils(DisplayMetrics displayMetrics, float maxLengthSeconds,
@@ -86,7 +81,7 @@ public class FlingAnimationUtils {
        mMaxLengthSeconds = maxLengthSeconds;
        mSpeedUpFactor = speedUpFactor;
        if (x2 < 0) {
            mLinearOutSlowInX2 = NotificationUtils.interpolate(LINEAR_OUT_SLOW_IN_X2,
            mLinearOutSlowInX2 = interpolate(LINEAR_OUT_SLOW_IN_X2,
                    LINEAR_OUT_SLOW_IN_X2_MAX,
                    mSpeedUpFactor);
        } else {
@@ -140,8 +135,8 @@ public class FlingAnimationUtils {
            float maxDistance) {
        AnimatorProperties properties = getProperties(currValue, endValue, velocity,
                maxDistance);
        animator.setDuration(properties.duration);
        animator.setInterpolator(properties.interpolator);
        animator.setDuration(properties.mDuration);
        animator.setInterpolator(properties.mInterpolator);
    }

    /**
@@ -159,8 +154,8 @@ public class FlingAnimationUtils {
            float velocity, float maxDistance) {
        AnimatorProperties properties = getProperties(currValue, endValue, velocity,
                maxDistance);
        animator.setDuration(properties.duration);
        animator.setInterpolator(properties.interpolator);
        animator.setDuration(properties.mDuration);
        animator.setInterpolator(properties.mInterpolator);
    }

    private AnimatorProperties getProperties(float currValue,
@@ -171,28 +166,28 @@ public class FlingAnimationUtils {
        float velAbs = Math.abs(velocity);
        float velocityFactor = mSpeedUpFactor == 0.0f
                ? 1.0f : Math.min(velAbs / HIGH_VELOCITY_DP_PER_SECOND, 1.0f);
        float startGradient = NotificationUtils.interpolate(LINEAR_OUT_SLOW_IN_START_GRADIENT,
        float startGradient = interpolate(LINEAR_OUT_SLOW_IN_START_GRADIENT,
                mY2 / mLinearOutSlowInX2, velocityFactor);
        float durationSeconds = startGradient * diff / velAbs;
        Interpolator slowInInterpolator = getInterpolator(startGradient, velocityFactor);
        if (durationSeconds <= maxLengthSeconds) {
            mAnimatorProperties.interpolator = slowInInterpolator;
            mAnimatorProperties.mInterpolator = slowInInterpolator;
        } else if (velAbs >= mMinVelocityPxPerSecond) {

            // Cross fade between fast-out-slow-in and linear interpolator with current velocity.
            durationSeconds = maxLengthSeconds;
            VelocityInterpolator velocityInterpolator
                    = new VelocityInterpolator(durationSeconds, velAbs, diff);
            VelocityInterpolator velocityInterpolator = new VelocityInterpolator(
                    durationSeconds, velAbs, diff);
            InterpolatorInterpolator superInterpolator = new InterpolatorInterpolator(
                    velocityInterpolator, slowInInterpolator, Interpolators.LINEAR_OUT_SLOW_IN);
            mAnimatorProperties.interpolator = superInterpolator;
            mAnimatorProperties.mInterpolator = superInterpolator;
        } else {

            // Just use a normal interpolator which doesn't take the velocity into account.
            durationSeconds = maxLengthSeconds;
            mAnimatorProperties.interpolator = Interpolators.FAST_OUT_SLOW_IN;
            mAnimatorProperties.mInterpolator = Interpolators.FAST_OUT_SLOW_IN;
        }
        mAnimatorProperties.duration = (long) (durationSeconds * 1000);
        mAnimatorProperties.mDuration = (long) (durationSeconds * 1000);
        return mAnimatorProperties;
    }

@@ -236,8 +231,8 @@ public class FlingAnimationUtils {
            float velocity, float maxDistance) {
        AnimatorProperties properties = getDismissingProperties(currValue, endValue, velocity,
                maxDistance);
        animator.setDuration(properties.duration);
        animator.setInterpolator(properties.interpolator);
        animator.setDuration(properties.mDuration);
        animator.setInterpolator(properties.mInterpolator);
    }

    /**
@@ -256,8 +251,8 @@ public class FlingAnimationUtils {
            float velocity, float maxDistance) {
        AnimatorProperties properties = getDismissingProperties(currValue, endValue, velocity,
                maxDistance);
        animator.setDuration(properties.duration);
        animator.setInterpolator(properties.interpolator);
        animator.setDuration(properties.mDuration);
        animator.setInterpolator(properties.mInterpolator);
    }

    private AnimatorProperties getDismissingProperties(float currValue, float endValue,
@@ -272,24 +267,24 @@ public class FlingAnimationUtils {
        Interpolator mLinearOutFasterIn = new PathInterpolator(0, 0, LINEAR_OUT_FASTER_IN_X2, y2);
        float durationSeconds = startGradient * diff / velAbs;
        if (durationSeconds <= maxLengthSeconds) {
            mAnimatorProperties.interpolator = mLinearOutFasterIn;
            mAnimatorProperties.mInterpolator = mLinearOutFasterIn;
        } else if (velAbs >= mMinVelocityPxPerSecond) {

            // Cross fade between linear-out-faster-in and linear interpolator with current
            // velocity.
            durationSeconds = maxLengthSeconds;
            VelocityInterpolator velocityInterpolator
                    = new VelocityInterpolator(durationSeconds, velAbs, diff);
            VelocityInterpolator velocityInterpolator = new VelocityInterpolator(
                    durationSeconds, velAbs, diff);
            InterpolatorInterpolator superInterpolator = new InterpolatorInterpolator(
                    velocityInterpolator, mLinearOutFasterIn, Interpolators.LINEAR_OUT_SLOW_IN);
            mAnimatorProperties.interpolator = superInterpolator;
            mAnimatorProperties.mInterpolator = superInterpolator;
        } else {

            // Just use a normal interpolator which doesn't take the velocity into account.
            durationSeconds = maxLengthSeconds;
            mAnimatorProperties.interpolator = Interpolators.FAST_OUT_LINEAR_IN;
            mAnimatorProperties.mInterpolator = Interpolators.FAST_OUT_LINEAR_IN;
        }
        mAnimatorProperties.duration = (long) (durationSeconds * 1000);
        mAnimatorProperties.mDuration = (long) (durationSeconds * 1000);
        return mAnimatorProperties;
    }

@@ -361,10 +356,11 @@ public class FlingAnimationUtils {
    }

    private static class AnimatorProperties {
        Interpolator interpolator;
        long duration;
        Interpolator mInterpolator;
        long mDuration;
    }

    /** Builder for {@link #FlingAnimationUtils}. */
    public static class Builder {
        private final DisplayMetrics mDisplayMetrics;
        float mMaxLengthSeconds;
@@ -372,32 +368,39 @@ public class FlingAnimationUtils {
        float mX2;
        float mY2;

        @Inject
        public Builder(DisplayMetrics displayMetrics) {
            mDisplayMetrics = displayMetrics;
            reset();
        }

        /** Sets the longest duration an animation can become in seconds. */
        public Builder setMaxLengthSeconds(float maxLengthSeconds) {
            mMaxLengthSeconds = maxLengthSeconds;
            return this;
        }

        /**
         * Sets the factor for how much the slow down should be shifted towards the end of the
         * animation.
         */
        public Builder setSpeedUpFactor(float speedUpFactor) {
            mSpeedUpFactor = speedUpFactor;
            return this;
        }

        /** Sets the x value to take for the second point of the bezier spline. */
        public Builder setX2(float x2) {
            mX2 = x2;
            return this;
        }

        /** Sets the y value to take for the second point of the bezier spline. */
        public Builder setY2(float y2) {
            mY2 = y2;
            return this;
        }

        /** Resets all parameters of the builder. */
        public Builder reset() {
            mMaxLengthSeconds = 0;
            mSpeedUpFactor = 0.0f;
@@ -407,9 +410,14 @@ public class FlingAnimationUtils {
            return this;
        }

        /** Builds {@link #FlingAnimationUtils}. */
        public FlingAnimationUtils build() {
            return new FlingAnimationUtils(mDisplayMetrics, mMaxLengthSeconds, mSpeedUpFactor,
                    mX2, mY2);
        }
    }

    private static float interpolate(float start, float end, float amount) {
        return start * (1.0f - amount) + end * amount;
    }
}
+45 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.wm.shell.animation;

import android.view.animation.Interpolator;
import android.view.animation.PathInterpolator;

/**
 * Common interpolators used in wm shell library.
 */
public class Interpolators {
    /**
     * Interpolator for fast out linear in animation.
     */
    public static final Interpolator FAST_OUT_LINEAR_IN = new PathInterpolator(0.4f, 0f, 1f, 1f);

    /**
     * Interpolator for fast out slow in animation.
     */
    public static final Interpolator FAST_OUT_SLOW_IN = new PathInterpolator(0.4f, 0f, 0.2f, 1f);

    /**
     * Interpolator for linear out slow in animation.
     */
    public static final Interpolator LINEAR_OUT_SLOW_IN = new PathInterpolator(0f, 0f, 0.2f, 1f);

    /**
     * Interpolator to be used when animating a move based on a click. Pair with enough duration.
     */
    public static final Interpolator TOUCH_RESPONSE = new PathInterpolator(0.3f, 0f, 0.1f, 1f);
}
+1 −1
Original line number Diff line number Diff line
@@ -54,8 +54,8 @@ import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.dagger.qualifiers.UiBackground;
import com.android.systemui.plugins.statusbar.StatusBarStateController;
import com.android.systemui.statusbar.CommandQueue;
import com.android.systemui.statusbar.FlingAnimationUtils;
import com.android.systemui.statusbar.StatusBarState;
import com.android.wm.shell.animation.FlingAnimationUtils;

import java.util.concurrent.Executor;

+1 −1
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ import androidx.annotation.CallSuper;
import com.android.systemui.R;
import com.android.systemui.car.CarDeviceProvisionedController;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.statusbar.FlingAnimationUtils;
import com.android.wm.shell.animation.FlingAnimationUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
+1 −1
Original line number Diff line number Diff line
@@ -40,8 +40,8 @@ import androidx.test.filters.SmallTest;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.car.CarDeviceProvisionedController;
import com.android.systemui.car.CarSystemUiTest;
import com.android.systemui.statusbar.FlingAnimationUtils;
import com.android.systemui.tests.R;
import com.android.wm.shell.animation.FlingAnimationUtils;

import org.junit.Before;
import org.junit.Test;
Loading