Follow up Androidx dynamic animation api change

Refactor codes using DynamicAnimation Animation handler api codes
for following up Androidx codes changing.

Copied from ag/17010932

Bug: 167382784
Bug: 221303983
Test: build
Change-Id: Iaa0e787675f53ac16cf61f2d2b6adae02ab55623
This commit is contained in:
Sally
2022-04-28 20:08:42 +00:00
committed by Sally Yuen
parent f60dccf9ec
commit dc5b93b371
2 changed files with 22 additions and 27 deletions

View File

@@ -19,12 +19,13 @@ package com.android.wm.shell.animation
import android.util.ArrayMap import android.util.ArrayMap
import android.util.Log import android.util.Log
import android.view.View import android.view.View
import androidx.dynamicanimation.animation.AnimationHandler
import androidx.dynamicanimation.animation.DynamicAnimation import androidx.dynamicanimation.animation.DynamicAnimation
import androidx.dynamicanimation.animation.FlingAnimation import androidx.dynamicanimation.animation.FlingAnimation
import androidx.dynamicanimation.animation.FloatPropertyCompat import androidx.dynamicanimation.animation.FloatPropertyCompat
import androidx.dynamicanimation.animation.FrameCallbackScheduler
import androidx.dynamicanimation.animation.SpringAnimation import androidx.dynamicanimation.animation.SpringAnimation
import androidx.dynamicanimation.animation.SpringForce import androidx.dynamicanimation.animation.SpringForce
import com.android.wm.shell.animation.PhysicsAnimator.Companion.getInstance import com.android.wm.shell.animation.PhysicsAnimator.Companion.getInstance
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import java.util.WeakHashMap import java.util.WeakHashMap
@@ -124,10 +125,10 @@ class PhysicsAnimator<T> private constructor (target: T) {
private var defaultFling: FlingConfig = globalDefaultFling private var defaultFling: FlingConfig = globalDefaultFling
/** /**
* AnimationHandler to use if it need custom AnimationHandler, if this is null, it will use * FrameCallbackScheduler to use if it need custom FrameCallbackScheduler, if this is null,
* the default AnimationHandler in the DynamicAnimation. * it will use the default FrameCallbackScheduler in the DynamicAnimation.
*/ */
private var customAnimationHandler: AnimationHandler? = null private var customScheduler: FrameCallbackScheduler? = null
/** /**
* Internal listeners that respond to DynamicAnimations updating and ending, and dispatch to * Internal listeners that respond to DynamicAnimations updating and ending, and dispatch to
@@ -454,11 +455,11 @@ class PhysicsAnimator<T> private constructor (target: T) {
} }
/** /**
* Set the custom AnimationHandler for all aniatmion in this animator. Set this with null for * Set the custom FrameCallbackScheduler for all aniatmion in this animator. Set this with null for
* restoring to default AnimationHandler. * restoring to default FrameCallbackScheduler.
*/ */
fun setCustomAnimationHandler(handler: AnimationHandler) { fun setCustomScheduler(scheduler: FrameCallbackScheduler) {
this.customAnimationHandler = handler this.customScheduler = scheduler
} }
/** Starts the animations! */ /** Starts the animations! */
@@ -510,10 +511,9 @@ class PhysicsAnimator<T> private constructor (target: T) {
// springs) on this property before flinging. // springs) on this property before flinging.
cancel(animatedProperty) cancel(animatedProperty)
// Apply the custom animation handler if it not null // Apply the custom animation scheduler if it not null
val flingAnim = getFlingAnimation(animatedProperty, target) val flingAnim = getFlingAnimation(animatedProperty, target)
flingAnim.animationHandler = flingAnim.scheduler = customScheduler ?: flingAnim.scheduler
customAnimationHandler ?: flingAnim.animationHandler
// Apply the configuration and start the animation. // Apply the configuration and start the animation.
flingAnim.also { flingConfig.applyToAnimation(it) }.start() flingAnim.also { flingConfig.applyToAnimation(it) }.start()
@@ -529,17 +529,16 @@ class PhysicsAnimator<T> private constructor (target: T) {
// Apply the configuration and start the animation. // Apply the configuration and start the animation.
val springAnim = getSpringAnimation(animatedProperty, target) val springAnim = getSpringAnimation(animatedProperty, target)
// If customAnimationHander is exist and has not been set to the animation, // If customScheduler is exist and has not been set to the animation,
// it should set here. // it should set here.
if (customAnimationHandler != null && if (customScheduler != null &&
springAnim.animationHandler != customAnimationHandler) { springAnim.scheduler != customScheduler) {
// Cancel the animation before set animation handler // Cancel the animation before set animation handler
if (springAnim.isRunning) { if (springAnim.isRunning) {
cancel(animatedProperty) cancel(animatedProperty)
} }
// Apply the custom animation handler if it not null // Apply the custom scheduler handler if it not null
springAnim.animationHandler = springAnim.scheduler = customScheduler ?: springAnim.scheduler
customAnimationHandler ?: springAnim.animationHandler
} }
// Apply the configuration and start the animation. // Apply the configuration and start the animation.
@@ -597,10 +596,9 @@ class PhysicsAnimator<T> private constructor (target: T) {
} }
} }
// Apply the custom animation handler if it not null // Apply the custom animation scheduler if it not null
val springAnim = getSpringAnimation(animatedProperty, target) val springAnim = getSpringAnimation(animatedProperty, target)
springAnim.animationHandler = springAnim.scheduler = customScheduler ?: springAnim.scheduler
customAnimationHandler ?: springAnim.animationHandler
// Apply the configuration and start the spring animation. // Apply the configuration and start the spring animation.
springAnim.also { springConfig.applyToAnimation(it) }.start() springAnim.also { springConfig.applyToAnimation(it) }.start()

View File

@@ -36,8 +36,7 @@ import android.os.Debug;
import android.os.Looper; import android.os.Looper;
import android.view.Choreographer; import android.view.Choreographer;
import androidx.dynamicanimation.animation.AnimationHandler; import androidx.dynamicanimation.animation.FrameCallbackScheduler;
import androidx.dynamicanimation.animation.AnimationHandler.FrameCallbackScheduler;
import com.android.internal.protolog.common.ProtoLog; import com.android.internal.protolog.common.ProtoLog;
import com.android.wm.shell.R; import com.android.wm.shell.R;
@@ -89,7 +88,7 @@ public class PipMotionHelper implements PipAppOpsListener.Callback,
/** Coordinator instance for resolving conflicts with other floating content. */ /** Coordinator instance for resolving conflicts with other floating content. */
private FloatingContentCoordinator mFloatingContentCoordinator; private FloatingContentCoordinator mFloatingContentCoordinator;
private ThreadLocal<AnimationHandler> mSfAnimationHandlerThreadLocal = private ThreadLocal<FrameCallbackScheduler> mSfSchedulerThreadLocal =
ThreadLocal.withInitial(() -> { ThreadLocal.withInitial(() -> {
final Looper initialLooper = Looper.myLooper(); final Looper initialLooper = Looper.myLooper();
final FrameCallbackScheduler scheduler = new FrameCallbackScheduler() { final FrameCallbackScheduler scheduler = new FrameCallbackScheduler() {
@@ -105,8 +104,7 @@ public class PipMotionHelper implements PipAppOpsListener.Callback,
return Looper.myLooper() == initialLooper; return Looper.myLooper() == initialLooper;
} }
}; };
AnimationHandler handler = new AnimationHandler(scheduler); return scheduler;
return handler;
}); });
/** /**
@@ -214,8 +212,7 @@ public class PipMotionHelper implements PipAppOpsListener.Callback,
// Note: Needs to get the shell main thread sf vsync animation handler // Note: Needs to get the shell main thread sf vsync animation handler
mTemporaryBoundsPhysicsAnimator = PhysicsAnimator.getInstance( mTemporaryBoundsPhysicsAnimator = PhysicsAnimator.getInstance(
mPipBoundsState.getMotionBoundsState().getBoundsInMotion()); mPipBoundsState.getMotionBoundsState().getBoundsInMotion());
mTemporaryBoundsPhysicsAnimator.setCustomAnimationHandler( mTemporaryBoundsPhysicsAnimator.setCustomScheduler(mSfSchedulerThreadLocal.get());
mSfAnimationHandlerThreadLocal.get());
} }
@NonNull @NonNull