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;
|
2020-08-11 12:06:49 -07:00
|
|
|
import static com.android.launcher3.util.DisplayController.getSingleFrameMs;
|
2021-03-07 15:09:11 -08:00
|
|
|
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
|
2020-11-11 19:25:09 -08:00
|
|
|
import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
|
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;
|
2021-03-07 15:09:11 -08:00
|
|
|
import androidx.annotation.Nullable;
|
2018-08-14 15:21:45 -07:00
|
|
|
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-10-23 09:26:44 -07:00
|
|
|
public abstract class LauncherAnimationRunner implements RemoteAnimationRunnerCompat {
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-04 14:23:42 +01:00
|
|
|
// Called only in S+ platform
|
2018-03-16 14:53:05 -07:00
|
|
|
@BinderThread
|
2021-02-04 14:23:42 +01:00
|
|
|
public void onAnimationStart(
|
|
|
|
|
int transit,
|
|
|
|
|
RemoteAnimationTargetCompat[] appTargets,
|
|
|
|
|
RemoteAnimationTargetCompat[] wallpaperTargets,
|
|
|
|
|
RemoteAnimationTargetCompat[] nonAppTargets,
|
|
|
|
|
Runnable runnable) {
|
2018-04-24 15:50:16 +02:00
|
|
|
Runnable r = () -> {
|
2018-04-12 11:23:33 -07:00
|
|
|
finishExistingAnimation();
|
2021-03-07 15:09:11 -08:00
|
|
|
mAnimationResult = new AnimationResult(() -> mAnimationResult = null, runnable);
|
2021-02-04 14:23:42 +01:00
|
|
|
onCreateAnimation(transit, appTargets, wallpaperTargets, nonAppTargets,
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2021-02-04 14:23:42 +01:00
|
|
|
// Called only in R platform
|
|
|
|
|
@BinderThread
|
|
|
|
|
public void onAnimationStart(RemoteAnimationTargetCompat[] appTargets,
|
|
|
|
|
RemoteAnimationTargetCompat[] wallpaperTargets, Runnable runnable) {
|
|
|
|
|
onAnimationStart(0 /* transit */, appTargets, wallpaperTargets,
|
|
|
|
|
new RemoteAnimationTargetCompat[0], runnable);
|
|
|
|
|
}
|
|
|
|
|
|
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(
|
2021-02-04 14:23:42 +01:00
|
|
|
int transit,
|
2019-10-16 06:03:42 +00:00
|
|
|
RemoteAnimationTargetCompat[] appTargets,
|
2021-02-04 14:23:42 +01:00
|
|
|
RemoteAnimationTargetCompat[] wallpaperTargets,
|
|
|
|
|
RemoteAnimationTargetCompat[] nonAppTargets,
|
|
|
|
|
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 {
|
|
|
|
|
|
2021-03-07 15:09:11 -08:00
|
|
|
private final Runnable mSyncFinishRunnable;
|
|
|
|
|
private final Runnable mASyncFinishRunnable;
|
2018-04-12 11:23:33 -07:00
|
|
|
|
|
|
|
|
private AnimatorSet mAnimator;
|
2021-03-07 15:09:11 -08:00
|
|
|
private Runnable mOnCompleteCallback;
|
2018-04-12 11:23:33 -07:00
|
|
|
private boolean mFinished = false;
|
|
|
|
|
private boolean mInitialized = false;
|
|
|
|
|
|
2021-03-07 15:09:11 -08:00
|
|
|
private AnimationResult(Runnable syncFinishRunnable, Runnable asyncFinishRunnable) {
|
|
|
|
|
mSyncFinishRunnable = syncFinishRunnable;
|
|
|
|
|
mASyncFinishRunnable = asyncFinishRunnable;
|
2018-04-12 11:23:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@UiThread
|
|
|
|
|
private void finish() {
|
|
|
|
|
if (!mFinished) {
|
2021-03-07 15:09:11 -08:00
|
|
|
mSyncFinishRunnable.run();
|
|
|
|
|
UI_HELPER_EXECUTOR.execute(() -> {
|
|
|
|
|
mASyncFinishRunnable.run();
|
|
|
|
|
if (mOnCompleteCallback != null) {
|
|
|
|
|
MAIN_EXECUTOR.execute(mOnCompleteCallback);
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-04-12 11:23:33 -07:00
|
|
|
mFinished = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@UiThread
|
2019-07-23 11:26:38 -07:00
|
|
|
public void setAnimation(AnimatorSet animation, Context context) {
|
2021-06-11 16:30:47 -07:00
|
|
|
setAnimation(animation, context, null, true);
|
2021-03-07 15:09:11 -08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the animation to play for this app launch
|
2021-06-11 16:30:47 -07:00
|
|
|
* @param skipFirstFrame Iff true, we skip the first frame of the animation.
|
|
|
|
|
* We set to false when skipping first frame causes jank.
|
2021-03-07 15:09:11 -08:00
|
|
|
*/
|
|
|
|
|
@UiThread
|
|
|
|
|
public void setAnimation(AnimatorSet animation, Context context,
|
2021-06-11 16:30:47 -07:00
|
|
|
@Nullable Runnable onCompleteCallback, boolean skipFirstFrame) {
|
2018-04-12 11:23:33 -07:00
|
|
|
if (mInitialized) {
|
|
|
|
|
throw new IllegalStateException("Animation already initialized");
|
|
|
|
|
}
|
|
|
|
|
mInitialized = true;
|
|
|
|
|
mAnimator = animation;
|
2021-03-07 15:09:11 -08:00
|
|
|
mOnCompleteCallback = onCompleteCallback;
|
2018-04-12 11:23:33 -07:00
|
|
|
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();
|
2021-03-07 15:09:11 -08:00
|
|
|
if (mOnCompleteCallback != null) {
|
|
|
|
|
mOnCompleteCallback.run();
|
|
|
|
|
}
|
2018-04-12 11:23:33 -07:00
|
|
|
} else {
|
|
|
|
|
// Start the animation
|
|
|
|
|
mAnimator.addListener(new AnimatorListenerAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationEnd(Animator animation) {
|
|
|
|
|
finish();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
mAnimator.start();
|
|
|
|
|
|
2021-06-11 16:30:47 -07:00
|
|
|
if (skipFirstFrame) {
|
|
|
|
|
// 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.
|
|
|
|
|
mAnimator.setCurrentPlayTime(
|
|
|
|
|
Math.min(getSingleFrameMs(context), mAnimator.getTotalDuration()));
|
|
|
|
|
}
|
2018-04-12 11:23:33 -07:00
|
|
|
}
|
2018-03-16 14:53:05 -07:00
|
|
|
}
|
|
|
|
|
}
|
2019-10-16 06:03:42 +00:00
|
|
|
}
|