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

Commit 8f795844 authored by Schneider Victor-tulias's avatar Schneider Victor-tulias Committed by Android (Google) Code Review
Browse files

Merge "Refactor the BorderAnimator for simplicity" into main

parents 9cf11627 7335bcf7
Loading
Loading
Loading
Loading
+11 −8
Original line number Diff line number Diff line
@@ -40,6 +40,8 @@ import com.android.systemui.shared.recents.model.ThumbnailData;

import java.util.function.Consumer;

import kotlin.Unit;

/**
 * A view that displays a recent task during a keyboard quick switch.
 */
@@ -96,17 +98,18 @@ public class KeyboardQuickSwitchTaskView extends ConstraintLayout {
        Resources resources = mContext.getResources();

        Preconditions.assertNotNull(mContent);
        mBorderAnimator = new BorderAnimator(
        mBorderAnimator = BorderAnimator.createScalingBorderAnimator(
                /* borderRadiusPx= */ resources.getDimensionPixelSize(
                        R.dimen.keyboard_quick_switch_task_view_radius),
                /* borderColor= */ mBorderColor,
                /* borderAnimationParams= */ new BorderAnimator.ScalingParams(
                /* borderWidthPx= */ resources.getDimensionPixelSize(
                                R.dimen.keyboard_quick_switch_border_width),
                        /* boundsBuilder= */ bounds -> bounds.set(
                                0, 0, getWidth(), getHeight()),
                /* boundsBuilder= */ bounds -> {
                    bounds.set(0, 0, getWidth(), getHeight());
                    return Unit.INSTANCE;
                },
                /* targetView= */ this,
                        /* contentView= */ mContent));
                /* contentView= */ mContent,
                /* borderColor= */ mBorderColor);
    }

    @Nullable
+0 −352
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.quickstep.util;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.ColorInt;
import android.annotation.Nullable;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
import android.view.animation.Interpolator;

import androidx.annotation.NonNull;
import androidx.annotation.Px;

import com.android.app.animation.Interpolators;
import com.android.launcher3.anim.AnimatedFloat;
import com.android.launcher3.anim.AnimatorListeners;

/**
 * Utility class for drawing a rounded-rect border around a view.
 * <p>
 * To use this class:
 * 1. Create an instance in the target view. NOTE: The border will animate outwards from the
 *      provided border bounds. See {@link SimpleParams} and {@link ScalingParams} to determine
 *      which would be best for your target view.
 * 2. Override the target view's {@link android.view.View#draw(Canvas)} method and call
 *      {@link BorderAnimator#drawBorder(Canvas)} after {@code super.draw(canvas)}.
 * 3. Call {@link BorderAnimator#buildAnimator(boolean)} and start the animation or call
 *      {@link BorderAnimator#setBorderVisible(boolean)} where appropriate.
 */
public final class BorderAnimator {

    public static final int DEFAULT_BORDER_COLOR = Color.WHITE;

    private static final long DEFAULT_APPEARANCE_ANIMATION_DURATION_MS = 300;
    private static final long DEFAULT_DISAPPEARANCE_ANIMATION_DURATION_MS = 133;
    private static final Interpolator DEFAULT_INTERPOLATOR = Interpolators.EMPHASIZED_DECELERATE;

    @NonNull private final AnimatedFloat mBorderAnimationProgress = new AnimatedFloat(
            this::updateOutline);
    @Px private final int mBorderRadiusPx;
    @NonNull private final BorderAnimationParams mBorderAnimationParams;
    private final long mAppearanceDurationMs;
    private final long mDisappearanceDurationMs;
    @NonNull private final Interpolator mInterpolator;
    @NonNull private final Paint mBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    @Nullable private Animator mRunningBorderAnimation;

    public BorderAnimator(
            @Px int borderRadiusPx,
            @ColorInt int borderColor,
            @NonNull BorderAnimationParams borderAnimationParams) {
        this(borderRadiusPx,
                borderColor,
                borderAnimationParams,
                DEFAULT_APPEARANCE_ANIMATION_DURATION_MS,
                DEFAULT_DISAPPEARANCE_ANIMATION_DURATION_MS,
                DEFAULT_INTERPOLATOR);
    }

    /**
     * @param borderRadiusPx the radius of the border's corners, in pixels
     * @param borderColor the border's color
     * @param borderAnimationParams params for handling different target view layout situation.
     * @param appearanceDurationMs appearance animation duration, in milliseconds
     * @param disappearanceDurationMs disappearance animation duration, in milliseconds
     * @param interpolator animation interpolator
     */
    public BorderAnimator(
            @Px int borderRadiusPx,
            @ColorInt int borderColor,
            @NonNull BorderAnimationParams borderAnimationParams,
            long appearanceDurationMs,
            long disappearanceDurationMs,
            @NonNull Interpolator interpolator) {
        mBorderRadiusPx = borderRadiusPx;
        mBorderAnimationParams = borderAnimationParams;
        mAppearanceDurationMs = appearanceDurationMs;
        mDisappearanceDurationMs = disappearanceDurationMs;
        mInterpolator = interpolator;

        mBorderPaint.setColor(borderColor);
        mBorderPaint.setStyle(Paint.Style.STROKE);
        mBorderPaint.setAlpha(0);
    }

    private void updateOutline() {
        float interpolatedProgress = mInterpolator.getInterpolation(
                mBorderAnimationProgress.value);

        mBorderAnimationParams.setProgress(interpolatedProgress);
        mBorderPaint.setAlpha(Math.round(255 * interpolatedProgress));
        mBorderPaint.setStrokeWidth(mBorderAnimationParams.getBorderWidth());
        mBorderAnimationParams.mTargetView.invalidate();
    }

    /**
     * Draws the border on the given canvas.
     * <p>
     * Call this method in the target view's {@link android.view.View#draw(Canvas)} method after
     * calling super.
     */
    public void drawBorder(Canvas canvas) {
        float alignmentAdjustment = mBorderAnimationParams.getAlignmentAdjustment();
        canvas.drawRoundRect(
                /* left= */ mBorderAnimationParams.mBorderBounds.left + alignmentAdjustment,
                /* top= */ mBorderAnimationParams.mBorderBounds.top + alignmentAdjustment,
                /* right= */ mBorderAnimationParams.mBorderBounds.right - alignmentAdjustment,
                /* bottom= */ mBorderAnimationParams.mBorderBounds.bottom - alignmentAdjustment,
                /* rx= */ mBorderRadiusPx + mBorderAnimationParams.getRadiusAdjustment(),
                /* ry= */ mBorderRadiusPx + mBorderAnimationParams.getRadiusAdjustment(),
                /* paint= */ mBorderPaint);
    }

    /**
     * Builds the border appearance/disappearance animation.
     */
    @NonNull
    public Animator buildAnimator(boolean isAppearing) {
        mRunningBorderAnimation = mBorderAnimationProgress.animateToValue(isAppearing ? 1f : 0f);
        mRunningBorderAnimation.setDuration(
                isAppearing ? mAppearanceDurationMs : mDisappearanceDurationMs);

        mRunningBorderAnimation.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                mBorderAnimationParams.onShowBorder();
            }
        });
        mRunningBorderAnimation.addListener(
                AnimatorListeners.forEndCallback(() -> {
                    mRunningBorderAnimation = null;
                    if (isAppearing) {
                        return;
                    }
                    mBorderAnimationParams.onHideBorder();
                }));

        return mRunningBorderAnimation;
    }

    /**
     * Immediately shows/hides the border without an animation.
     * <p>
     * To animate the appearance/disappearance, see {@link BorderAnimator#buildAnimator(boolean)}
     */
    public void setBorderVisible(boolean visible) {
        if (mRunningBorderAnimation != null) {
            mRunningBorderAnimation.end();
        }
        if (visible) {
            mBorderAnimationParams.onShowBorder();
        }
        mBorderAnimationProgress.updateValue(visible ? 1f : 0f);
        if (!visible) {
            mBorderAnimationParams.onHideBorder();
        }
    }

    /**
     * Callback to update the border bounds when building this animation.
     */
    public interface BorderBoundsBuilder {

        /**
         * Sets the given rect to the most up-to-date bounds.
         */
        void updateBorderBounds(Rect rect);
    }

    /**
     * Params for handling different target view layout situation.
     */
    private abstract static class BorderAnimationParams {

        @NonNull private final Rect mBorderBounds = new Rect();
        @NonNull private final BorderBoundsBuilder mBoundsBuilder;

        @NonNull final View mTargetView;
        @Px final int mBorderWidthPx;

        private float mAnimationProgress = 0f;
        @Nullable private View.OnLayoutChangeListener mLayoutChangeListener;

        /**
         * @param borderWidthPx the width of the border, in pixels
         * @param boundsBuilder callback to update the border bounds
         * @param targetView the view that will be drawing the border
         */
        private BorderAnimationParams(
                @Px int borderWidthPx,
                @NonNull BorderBoundsBuilder boundsBuilder,
                @NonNull View targetView) {
            mBorderWidthPx = borderWidthPx;
            mBoundsBuilder = boundsBuilder;
            mTargetView = targetView;
        }

        private void setProgress(float progress) {
            mAnimationProgress = progress;
        }

        private float getBorderWidth() {
            return mBorderWidthPx * mAnimationProgress;
        }

        float getAlignmentAdjustment() {
            // Outset the border by half the width to create an outwards-growth animation
            return (-getBorderWidth() / 2f) + getAlignmentAdjustmentInset();
        }


        void onShowBorder() {
            if (mLayoutChangeListener == null) {
                mLayoutChangeListener =
                        (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
                            onShowBorder();
                            mTargetView.invalidate();
                        };
                mTargetView.addOnLayoutChangeListener(mLayoutChangeListener);
            }
            mBoundsBuilder.updateBorderBounds(mBorderBounds);
        }

        void onHideBorder() {
            if (mLayoutChangeListener != null) {
                mTargetView.removeOnLayoutChangeListener(mLayoutChangeListener);
                mLayoutChangeListener = null;
            }
        }

        abstract int getAlignmentAdjustmentInset();

        abstract float getRadiusAdjustment();
    }

    /**
     * Use an instance of this {@link BorderAnimationParams} if the border can be drawn outside the
     * target view's bounds without any additional logic.
     */
    public static final class SimpleParams extends BorderAnimationParams {

        public SimpleParams(
                @Px int borderWidthPx,
                @NonNull BorderBoundsBuilder boundsBuilder,
                @NonNull View targetView) {
            super(borderWidthPx, boundsBuilder, targetView);
        }

        @Override
        int getAlignmentAdjustmentInset() {
            return 0;
        }

        @Override
        float getRadiusAdjustment() {
            return -getAlignmentAdjustment();
        }
    }

    /**
     * Use an instance of this {@link BorderAnimationParams} if the border would other be clipped by
     * the target view's bound.
     * <p>
     * Note: using these params will set the scales and pivots of the
     * container and content views, however will only reset the scales back to 1.
     */
    public static final class ScalingParams extends BorderAnimationParams {

        @NonNull private final View mContentView;

        /**
         * @param targetView the view that will be drawing the border. this view will be scaled up
         *                   to make room for the border
         * @param contentView the view around which the border will be drawn. this view will be
         *                    scaled down reciprocally to keep its original size and location.
         */
        public ScalingParams(
                @Px int borderWidthPx,
                @NonNull BorderBoundsBuilder boundsBuilder,
                @NonNull View targetView,
                @NonNull View contentView) {
            super(borderWidthPx, boundsBuilder, targetView);
            mContentView = contentView;
        }

        @Override
        void onShowBorder() {
            super.onShowBorder();
            float width = mTargetView.getWidth();
            float height = mTargetView.getHeight();
            // Scale up just enough to make room for the border. Fail fast and fix the scaling
            // onLayout.
            float scaleX = width == 0 ? 1f : 1f + ((2 * mBorderWidthPx) / width);
            float scaleY = height == 0 ? 1f : 1f + ((2 * mBorderWidthPx) / height);

            mTargetView.setPivotX(width / 2);
            mTargetView.setPivotY(height / 2);
            mTargetView.setScaleX(scaleX);
            mTargetView.setScaleY(scaleY);

            mContentView.setPivotX(mContentView.getWidth() / 2f);
            mContentView.setPivotY(mContentView.getHeight() / 2f);
            mContentView.setScaleX(1f / scaleX);
            mContentView.setScaleY(1f / scaleY);
        }

        @Override
        void onHideBorder() {
            super.onHideBorder();
            mTargetView.setPivotX(mTargetView.getWidth());
            mTargetView.setPivotY(mTargetView.getHeight());
            mTargetView.setScaleX(1f);
            mTargetView.setScaleY(1f);

            mContentView.setPivotX(mContentView.getWidth() / 2f);
            mContentView.setPivotY(mContentView.getHeight() / 2f);
            mContentView.setScaleX(1f);
            mContentView.setScaleY(1f);
        }

        @Override
        int getAlignmentAdjustmentInset() {
            // Inset the border since we are scaling the container up
            return mBorderWidthPx;
        }

        @Override
        float getRadiusAdjustment() {
            // Increase the radius since we are scaling the container up
            return getAlignmentAdjustment();
        }
    }
}
+315 −0

File added.

Preview size limit exceeded, changes collapsed.

+4 −1
Original line number Diff line number Diff line
@@ -62,6 +62,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.function.Consumer;

import kotlin.Unit;

/**
 * TaskView that contains all tasks that are part of the desktop.
 */
@@ -142,9 +144,10 @@ public class DesktopTaskView extends TaskView {
    }

    @Override
    protected void updateBorderBounds(Rect bounds) {
    protected Unit updateBorderBounds(@NonNull Rect bounds) {
        bounds.set(mBackgroundView.getLeft(), mBackgroundView.getTop(), mBackgroundView.getRight(),
                mBackgroundView.getBottom());
        return Unit.INSTANCE;
    }

    @Override
+5 −2
Original line number Diff line number Diff line
@@ -37,6 +37,8 @@ import com.android.wm.shell.common.split.SplitScreenConstants.SnapPosition;
import java.util.HashMap;
import java.util.function.Consumer;

import kotlin.Unit;

/**
 * TaskView that contains and shows thumbnails for not one, BUT TWO(!!) tasks
 *
@@ -76,10 +78,10 @@ public class GroupedTaskView extends TaskView {
    }

    @Override
    protected void updateBorderBounds(Rect bounds) {
    protected Unit updateBorderBounds(@NonNull Rect bounds) {
        if (mSplitBoundsConfig == null) {
            super.updateBorderBounds(bounds);
            return;
            return Unit.INSTANCE;
        }
        bounds.set(
                Math.min(mSnapshotView.getLeft() + Math.round(mSnapshotView.getTranslationX()),
@@ -90,6 +92,7 @@ public class GroupedTaskView extends TaskView {
                        mSnapshotView2.getRight() + Math.round(mSnapshotView2.getTranslationX())),
                Math.max(mSnapshotView.getBottom() + Math.round(mSnapshotView.getTranslationY()),
                        mSnapshotView2.getBottom() + Math.round(mSnapshotView2.getTranslationY())));
        return Unit.INSTANCE;
    }

    @Override
Loading