2018-01-22 17:32:30 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2018 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.launcher3;
|
|
|
|
|
|
2018-04-12 11:23:33 -07:00
|
|
|
import static com.android.launcher3.Utilities.postAsyncCallback;
|
2019-07-23 11:26:38 -07:00
|
|
|
import static com.android.launcher3.util.DefaultDisplay.getSingleFrameMs;
|
2019-10-16 06:03:42 +00:00
|
|
|
import static com.android.systemui.shared.recents.utilities.Utilities.postAtFrontOfQueueAsynchronously;
|
2018-04-12 11:23:33 -07:00
|
|
|
|
2018-03-16 14:53:05 -07:00
|
|
|
import android.animation.Animator;
|
|
|
|
|
import android.animation.AnimatorListenerAdapter;
|
2018-01-22 17:32:30 -08:00
|
|
|
import android.animation.AnimatorSet;
|
2018-03-16 14:53:05 -07:00
|
|
|
import android.annotation.TargetApi;
|
2019-07-23 11:26:38 -07:00
|
|
|
import android.content.Context;
|
2018-03-16 14:53:05 -07:00
|
|
|
import android.os.Build;
|
|
|
|
|
import android.os.Handler;
|
2018-01-22 17:32:30 -08:00
|
|
|
|
2018-08-14 15:21:45 -07:00
|
|
|
import androidx.annotation.BinderThread;
|
|
|
|
|
import androidx.annotation.UiThread;
|
|
|
|
|
|
2019-09-09 16:46:06 -07:00
|
|
|
import com.android.systemui.shared.system.RemoteAnimationRunnerCompat;
|
|
|
|
|
import com.android.systemui.shared.system.RemoteAnimationTargetCompat;
|
|
|
|
|
|
2018-03-16 14:53:05 -07:00
|
|
|
@TargetApi(Build.VERSION_CODES.P)
|
2020-03-02 18:05:58 -08:00
|
|
|
public abstract class LauncherAnimationRunner implements RemoteAnimationRunnerCompat,
|
|
|
|
|
WrappedAnimationRunnerImpl {
|
2018-03-16 14:53:05 -07:00
|
|
|
|
|
|
|
|
private final Handler mHandler;
|
2018-04-24 15:50:16 +02:00
|
|
|
private final boolean mStartAtFrontOfQueue;
|
2018-04-12 11:23:33 -07:00
|
|
|
private AnimationResult mAnimationResult;
|
2018-01-22 17:32:30 -08:00
|
|
|
|
2018-04-24 15:50:16 +02:00
|
|
|
/**
|
|
|
|
|
* @param startAtFrontOfQueue If true, the animation start will be posted at the front of the
|
|
|
|
|
* queue to minimize latency.
|
|
|
|
|
*/
|
|
|
|
|
public LauncherAnimationRunner(Handler handler, boolean startAtFrontOfQueue) {
|
2018-03-16 14:53:05 -07:00
|
|
|
mHandler = handler;
|
2018-04-24 15:50:16 +02:00
|
|
|
mStartAtFrontOfQueue = startAtFrontOfQueue;
|
2018-01-22 17:32:30 -08:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 18:05:58 -08:00
|
|
|
public Handler getHandler() {
|
|
|
|
|
return mHandler;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 16:46:06 -07:00
|
|
|
// Called only in R+ platform
|
2018-03-16 14:53:05 -07:00
|
|
|
@BinderThread
|
2019-09-09 16:46:06 -07:00
|
|
|
public void onAnimationStart(RemoteAnimationTargetCompat[] appTargets,
|
|
|
|
|
RemoteAnimationTargetCompat[] wallpaperTargets, Runnable runnable) {
|
2018-04-24 15:50:16 +02:00
|
|
|
Runnable r = () -> {
|
2018-04-12 11:23:33 -07:00
|
|
|
finishExistingAnimation();
|
2020-01-28 13:29:39 -08:00
|
|
|
mAnimationResult = new AnimationResult(() -> {
|
|
|
|
|
runnable.run();
|
|
|
|
|
mAnimationResult = null;
|
|
|
|
|
});
|
2019-10-16 06:03:42 +00:00
|
|
|
onCreateAnimation(appTargets, wallpaperTargets, mAnimationResult);
|
2018-04-24 15:50:16 +02:00
|
|
|
};
|
|
|
|
|
if (mStartAtFrontOfQueue) {
|
|
|
|
|
postAtFrontOfQueueAsynchronously(mHandler, r);
|
|
|
|
|
} else {
|
|
|
|
|
postAsyncCallback(mHandler, r);
|
|
|
|
|
}
|
2018-03-16 14:53:05 -07:00
|
|
|
}
|
|
|
|
|
|
2019-09-09 16:46:06 -07:00
|
|
|
// Called only in Q platform
|
|
|
|
|
@BinderThread
|
|
|
|
|
@Deprecated
|
|
|
|
|
public void onAnimationStart(RemoteAnimationTargetCompat[] appTargets, Runnable runnable) {
|
|
|
|
|
onAnimationStart(appTargets, new RemoteAnimationTargetCompat[0], runnable);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-12 11:23:33 -07:00
|
|
|
/**
|
|
|
|
|
* Called on the UI thread when the animation targets are received. The implementation must
|
2019-07-23 11:26:38 -07:00
|
|
|
* call {@link AnimationResult#setAnimation} with the target animation to be run.
|
2018-04-12 11:23:33 -07:00
|
|
|
*/
|
2018-03-16 14:53:05 -07:00
|
|
|
@UiThread
|
2018-04-12 11:23:33 -07:00
|
|
|
public abstract void onCreateAnimation(
|
2019-10-16 06:03:42 +00:00
|
|
|
RemoteAnimationTargetCompat[] appTargets,
|
|
|
|
|
RemoteAnimationTargetCompat[] wallpaperTargets, AnimationResult result);
|
2018-03-16 14:53:05 -07:00
|
|
|
|
|
|
|
|
@UiThread
|
2018-04-12 11:23:33 -07:00
|
|
|
private void finishExistingAnimation() {
|
|
|
|
|
if (mAnimationResult != null) {
|
|
|
|
|
mAnimationResult.finish();
|
|
|
|
|
mAnimationResult = null;
|
2018-03-16 14:53:05 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Called by the system
|
|
|
|
|
*/
|
|
|
|
|
@BinderThread
|
2018-01-22 17:32:30 -08:00
|
|
|
@Override
|
|
|
|
|
public void onAnimationCancelled() {
|
2018-04-12 11:23:33 -07:00
|
|
|
postAsyncCallback(mHandler, this::finishExistingAnimation);
|
2018-01-22 17:32:30 -08:00
|
|
|
}
|
2018-03-16 14:53:05 -07:00
|
|
|
|
2018-04-12 11:23:33 -07:00
|
|
|
public static final class AnimationResult {
|
|
|
|
|
|
|
|
|
|
private final Runnable mFinishRunnable;
|
|
|
|
|
|
|
|
|
|
private AnimatorSet mAnimator;
|
|
|
|
|
private boolean mFinished = false;
|
|
|
|
|
private boolean mInitialized = false;
|
|
|
|
|
|
|
|
|
|
private AnimationResult(Runnable finishRunnable) {
|
|
|
|
|
mFinishRunnable = finishRunnable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@UiThread
|
|
|
|
|
private void finish() {
|
|
|
|
|
if (!mFinished) {
|
|
|
|
|
mFinishRunnable.run();
|
|
|
|
|
mFinished = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@UiThread
|
2019-07-23 11:26:38 -07:00
|
|
|
public void setAnimation(AnimatorSet animation, Context context) {
|
2018-04-12 11:23:33 -07:00
|
|
|
if (mInitialized) {
|
|
|
|
|
throw new IllegalStateException("Animation already initialized");
|
|
|
|
|
}
|
|
|
|
|
mInitialized = true;
|
|
|
|
|
mAnimator = animation;
|
|
|
|
|
if (mAnimator == null) {
|
|
|
|
|
finish();
|
|
|
|
|
} else if (mFinished) {
|
|
|
|
|
// Animation callback was already finished, skip the animation.
|
2018-05-14 11:23:51 -07:00
|
|
|
mAnimator.start();
|
2018-04-12 11:23:33 -07:00
|
|
|
mAnimator.end();
|
|
|
|
|
} else {
|
|
|
|
|
// Start the animation
|
|
|
|
|
mAnimator.addListener(new AnimatorListenerAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationEnd(Animator animation) {
|
|
|
|
|
finish();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
mAnimator.start();
|
|
|
|
|
|
|
|
|
|
// Because t=0 has the app icon in its original spot, we can skip the
|
|
|
|
|
// first frame and have the same movement one frame earlier.
|
2019-07-23 11:26:38 -07:00
|
|
|
mAnimator.setCurrentPlayTime(getSingleFrameMs(context));
|
2018-04-12 11:23:33 -07:00
|
|
|
}
|
2018-03-16 14:53:05 -07:00
|
|
|
}
|
|
|
|
|
}
|
2019-10-16 06:03:42 +00:00
|
|
|
}
|