Animates a fake TaskView in Home gesture tutorial.

The "TaskView" morphs as you drag up from the
nav bar, and transforms into a "home icon" or
final "TaskView" when you release your finger.

If you cancel the gesture (e.g. drag back down),
the view simply fades out and provides a hint
to swipe straight up.

Demo: https://drive.google.com/open?id=1NXlO1W6IhLoX0k2K6b-QZ5IXp0o7OYqO

Bug: 148542211
Change-Id: I3d5e9a45fcadc6a498941ea17813b9487720a504
This commit is contained in:
Andy Wickham
2020-05-21 18:04:18 -07:00
parent 625bd51fe3
commit f8a8117676
12 changed files with 674 additions and 344 deletions
@@ -15,19 +15,134 @@
*/
package com.android.quickstep.interaction;
import static com.android.launcher3.anim.Interpolators.ACCEL;
import static com.android.launcher3.util.DefaultDisplay.getSingleFrameMs;
import static com.android.quickstep.BaseSwipeUpHandlerV2.MAX_SWIPE_DURATION;
import static com.android.quickstep.interaction.TutorialController.TutorialType.HOME_NAVIGATION_COMPLETE;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Insets;
import android.graphics.Outline;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Build;
import android.view.SurfaceControl;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.view.WindowInsets.Type;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.android.launcher3.DeviceProfile;
import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.anim.AnimationSuccessListener;
import com.android.launcher3.anim.AnimatorPlaybackController;
import com.android.launcher3.anim.PendingAnimation;
import com.android.quickstep.AnimatedFloat;
import com.android.quickstep.GestureState;
import com.android.quickstep.OverviewComponentObserver;
import com.android.quickstep.RecentsAnimationDeviceState;
import com.android.quickstep.SwipeUpAnimationLogic;
import com.android.quickstep.SwipeUpAnimationLogic.RunningWindowAnim;
import com.android.quickstep.interaction.EdgeBackGestureHandler.BackGestureResult;
import com.android.quickstep.interaction.NavBarGestureHandler.NavBarGestureResult;
import com.android.quickstep.util.RectFSpringAnim;
import com.android.quickstep.util.TransformParams;
import com.android.systemui.shared.system.QuickStepContract;
import com.android.systemui.shared.system.SyncRtSurfaceTransactionApplierCompat.SurfaceParams;
/** A {@link TutorialController} for the Home tutorial. */
@TargetApi(Build.VERSION_CODES.R)
final class HomeGestureTutorialController extends TutorialController {
private float mFakeTaskViewRadius;
private Rect mFakeTaskViewRect = new Rect();
private final ViewSwipeUpAnimation mViewSwipeUpAnimation;
private RunningWindowAnim mRunningWindowAnim;
HomeGestureTutorialController(HomeGestureTutorialFragment fragment, TutorialType tutorialType) {
super(fragment, tutorialType);
RecentsAnimationDeviceState deviceState = new RecentsAnimationDeviceState(mContext);
OverviewComponentObserver observer = new OverviewComponentObserver(mContext, deviceState);
mViewSwipeUpAnimation = new ViewSwipeUpAnimation(mContext, deviceState,
new GestureState(observer, -1));
observer.onDestroy();
DeviceProfile dp = InvariantDeviceProfile.INSTANCE.get(mContext)
.getDeviceProfile(mContext)
.copy(mContext);
Insets insets = mContext.getSystemService(WindowManager.class)
.getCurrentWindowMetrics()
.getWindowInsets()
.getInsets(Type.systemBars());
dp.updateInsets(new Rect(insets.left, insets.top, insets.right, insets.bottom));
mViewSwipeUpAnimation.initDp(dp);
mFakeTaskViewRadius = QuickStepContract.getWindowCornerRadius(mContext.getResources());
mFakeTaskView.setClipToOutline(true);
mFakeTaskView.setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setRoundRect(mFakeTaskViewRect, mFakeTaskViewRadius);
}
});
}
private void cancelRunningAnimation() {
if (mRunningWindowAnim != null) {
mRunningWindowAnim.cancel();
}
mRunningWindowAnim = null;
}
/** Fades the task view, optionally after animating to a fake Overview. */
private void fadeOutFakeTaskView(boolean toOverviewFirst, @Nullable Runnable onEndRunnable) {
cancelRunningAnimation();
PendingAnimation anim = new PendingAnimation(300);
AnimatorListenerAdapter resetTaskView = new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation, boolean isReverse) {
mFakeTaskView.setVisibility(View.INVISIBLE);
mFakeTaskView.setAlpha(1);
mRunningWindowAnim = null;
}
};
if (toOverviewFirst) {
anim.setFloat(mViewSwipeUpAnimation.getCurrentShift(), AnimatedFloat.VALUE, 1, ACCEL);
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation, boolean isReverse) {
PendingAnimation fadeAnim = new PendingAnimation(300);
fadeAnim.setViewAlpha(mFakeTaskView, 0, ACCEL);
fadeAnim.addListener(resetTaskView);
AnimatorSet animset = fadeAnim.buildAnim();
animset.setStartDelay(100);
animset.start();
mRunningWindowAnim = RunningWindowAnim.wrap(animset);
}
});
} else {
anim.setViewAlpha(mFakeTaskView, 0, ACCEL);
anim.addListener(resetTaskView);
}
if (onEndRunnable != null) {
anim.addListener(AnimationSuccessListener.forRunnable(onEndRunnable));
}
AnimatorSet animset = anim.buildAnim();
animset.start();
mRunningWindowAnim = RunningWindowAnim.wrap(animset);
}
@Override
@@ -85,22 +200,35 @@ final class HomeGestureTutorialController extends TutorialController {
}
@Override
public void onNavBarGestureAttempted(NavBarGestureResult result) {
public void onNavBarGestureAttempted(NavBarGestureResult result, PointF finalVelocity) {
switch (mTutorialType) {
case HOME_NAVIGATION:
switch (result) {
case HOME_GESTURE_COMPLETED:
case HOME_GESTURE_COMPLETED: {
hideFeedback();
cancelRunningAnimation();
hideHandCoachingAnimation();
mTutorialFragment.changeController(HOME_NAVIGATION_COMPLETE);
RectFSpringAnim rectAnim =
mViewSwipeUpAnimation.handleSwipeUpToHome(finalVelocity);
// After home animation finishes, fade out and then move to the next screen.
rectAnim.addAnimatorListener(AnimationSuccessListener.forRunnable(
() -> fadeOutFakeTaskView(false,
() -> mTutorialFragment.changeController(
HOME_NAVIGATION_COMPLETE))));
mRunningWindowAnim = RunningWindowAnim.wrap(rectAnim);
break;
}
case HOME_NOT_STARTED_TOO_FAR_FROM_EDGE:
case OVERVIEW_NOT_STARTED_TOO_FAR_FROM_EDGE:
showFeedback(R.string.home_gesture_feedback_swipe_too_far_from_edge);
break;
case OVERVIEW_GESTURE_COMPLETED:
showFeedback(R.string.home_gesture_feedback_overview_detected);
fadeOutFakeTaskView(true, () ->
showFeedback(R.string.home_gesture_feedback_overview_detected));
break;
case HOME_OR_OVERVIEW_NOT_STARTED_WRONG_SWIPE_DIRECTION:
case HOME_OR_OVERVIEW_CANCELLED:
fadeOutFakeTaskView(false, null);
showFeedback(R.string.home_gesture_feedback_wrong_swipe_direction);
break;
}
@@ -112,4 +240,94 @@ final class HomeGestureTutorialController extends TutorialController {
break;
}
}
@Override
public void setNavBarGestureProgress(@Nullable Float displacement) {
if (displacement == null || mTutorialType == HOME_NAVIGATION_COMPLETE) {
mFakeTaskView.setVisibility(View.INVISIBLE);
} else {
mFakeTaskView.setVisibility(View.VISIBLE);
if (mRunningWindowAnim == null) {
mViewSwipeUpAnimation.updateDisplacement(displacement);
}
}
}
private class ViewSwipeUpAnimation extends SwipeUpAnimationLogic {
ViewSwipeUpAnimation(Context context, RecentsAnimationDeviceState deviceState,
GestureState gestureState) {
super(context, deviceState, gestureState, new FakeTransformParams());
}
void initDp(DeviceProfile dp) {
initTransitionEndpoints(dp);
mTaskViewSimulator.setPreviewBounds(
new Rect(0, 0, dp.widthPx, dp.heightPx), dp.getInsets());
}
@Override
public void updateFinalShift() {
float progress = mCurrentShift.value / mDragLengthFactor;
mWindowTransitionController.setPlayFraction(progress);
mTaskViewSimulator.apply(mTransformParams);
}
AnimatedFloat getCurrentShift() {
return mCurrentShift;
}
RectFSpringAnim handleSwipeUpToHome(PointF velocity) {
PointF velocityPxPerMs = new PointF(velocity.x, velocity.y);
float currentShift = mCurrentShift.value;
final float startShift = Utilities.boundToRange(currentShift - velocityPxPerMs.y
* getSingleFrameMs(mContext) / mTransitionDragLength, 0, mDragLengthFactor);
float distanceToTravel = (1 - currentShift) * mTransitionDragLength;
// we want the page's snap velocity to approximately match the velocity at
// which the user flings, so we scale the duration by a value near to the
// derivative of the scroll interpolator at zero, ie. 2.
long baseDuration = Math.round(Math.abs(distanceToTravel / velocityPxPerMs.y));
long duration = Math.min(MAX_SWIPE_DURATION, 2 * baseDuration);
HomeAnimationFactory homeAnimFactory = new HomeAnimationFactory(null) {
@Override
public AnimatorPlaybackController createActivityAnimationToHome() {
return AnimatorPlaybackController.wrap(new AnimatorSet(), duration);
}
@NonNull
@Override
public RectF getWindowTargetRect() {
int fakeHomeIconSizePx = mDp.allAppsIconSizePx;
int fakeHomeIconLeft = (mDp.widthPx - fakeHomeIconSizePx) / 2;
int fakeHomeIconTop = mDp.heightPx - (mDp.allAppsCellHeightPx * 3);
return new RectF(fakeHomeIconLeft, fakeHomeIconTop,
fakeHomeIconLeft + fakeHomeIconSizePx,
fakeHomeIconTop + fakeHomeIconSizePx);
}
};
RectFSpringAnim windowAnim = createWindowAnimationToHome(startShift, homeAnimFactory);
windowAnim.start(mContext, velocityPxPerMs);
return windowAnim;
}
}
private class FakeTransformParams extends TransformParams {
@Override
public SurfaceParams[] createSurfaceParams(BuilderProxy proxy) {
SurfaceParams.Builder builder = new SurfaceParams.Builder((SurfaceControl) null);
proxy.onBuildTargetParams(builder, null, this);
return new SurfaceParams[] {builder.build()};
}
@Override
public void applySurfaceParams(SurfaceParams[] params) {
SurfaceParams p = params[0];
mFakeTaskView.setAnimationMatrix(p.matrix);
mFakeTaskViewRect.set(p.windowCrop);
mFakeTaskViewRadius = p.cornerRadius;
mFakeTaskView.invalidateOutline();
}
}
}