1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.quickstep.util;
17 
18 import static com.android.launcher3.anim.Interpolators.DEACCEL;
19 import static com.android.launcher3.anim.Interpolators.LINEAR;
20 import static com.android.quickstep.views.RecentsView.RECENTS_SCALE_PROPERTY;
21 import static com.android.quickstep.views.RecentsView.TASK_SECONDARY_TRANSLATION;
22 
23 import android.animation.TimeInterpolator;
24 import android.content.Context;
25 import android.graphics.Matrix;
26 import android.graphics.PointF;
27 import android.graphics.Rect;
28 import android.graphics.RectF;
29 import android.util.FloatProperty;
30 
31 import androidx.annotation.Nullable;
32 
33 import com.android.launcher3.DeviceProfile;
34 import com.android.launcher3.Launcher;
35 import com.android.launcher3.Utilities;
36 import com.android.launcher3.anim.AnimatorPlaybackController;
37 import com.android.launcher3.anim.PendingAnimation;
38 import com.android.launcher3.touch.PagedOrientationHandler;
39 import com.android.quickstep.LauncherActivityInterface;
40 import com.android.quickstep.views.RecentsView;
41 
42 /**
43  * Controls an animation that can go beyond progress = 1, at which point resistance should be
44  * applied. Internally, this is a wrapper around 2 {@link AnimatorPlaybackController}s, one that
45  * runs from progress 0 to 1 like normal, then one that seamlessly continues that animation but
46  * starts applying resistance as well.
47  */
48 public class AnimatorControllerWithResistance {
49 
50     private enum RecentsResistanceParams {
51         FROM_APP(0.75f, 0.5f, 1f, false),
52         FROM_APP_TABLET(1f, 0.7f, 1f, true),
53         FROM_OVERVIEW(1f, 0.75f, 0.5f, false);
54 
RecentsResistanceParams(float scaleStartResist, float scaleMaxResist, float translationFactor, boolean stopScalingAtTop)55         RecentsResistanceParams(float scaleStartResist, float scaleMaxResist,
56                 float translationFactor, boolean stopScalingAtTop) {
57             this.scaleStartResist = scaleStartResist;
58             this.scaleMaxResist = scaleMaxResist;
59             this.translationFactor = translationFactor;
60             this.stopScalingAtTop = stopScalingAtTop;
61         }
62 
63         /**
64          * Start slowing down the rate of scaling down when recents view is smaller than this scale.
65          */
66         public final float scaleStartResist;
67 
68         /**
69          * Recents view will reach this scale at the very end of the drag.
70          */
71         public final float scaleMaxResist;
72 
73         /**
74          * How much translation to apply to RecentsView when the drag reaches the top of the screen,
75          * where 0 will keep it centered and 1 will have it barely touch the top of the screen.
76          */
77         public final float translationFactor;
78 
79         /**
80          * Whether to end scaling effect when the scaled down version of TaskView's top reaches the
81          * non-scaled version of TaskView's top.
82          */
83         public final boolean stopScalingAtTop;
84     }
85 
86     private static final TimeInterpolator RECENTS_SCALE_RESIST_INTERPOLATOR = DEACCEL;
87     private static final TimeInterpolator RECENTS_TRANSLATE_RESIST_INTERPOLATOR = LINEAR;
88 
89     private final AnimatorPlaybackController mNormalController;
90     private final AnimatorPlaybackController mResistanceController;
91 
92     // Initialize to -1 so the first 0 gets applied.
93     private float mLastNormalProgress = -1;
94     private float mLastResistProgress;
95 
AnimatorControllerWithResistance(AnimatorPlaybackController normalController, AnimatorPlaybackController resistanceController)96     public AnimatorControllerWithResistance(AnimatorPlaybackController normalController,
97             AnimatorPlaybackController resistanceController) {
98         mNormalController = normalController;
99         mResistanceController = resistanceController;
100     }
101 
getNormalController()102     public AnimatorPlaybackController getNormalController() {
103         return mNormalController;
104     }
105 
106     /**
107      * Applies the current progress of the animation.
108      * @param progress From 0 to maxProgress, where 1 is the target we are animating towards.
109      * @param maxProgress > 1, this is where the resistance will be applied.
110      */
setProgress(float progress, float maxProgress)111     public void setProgress(float progress, float maxProgress) {
112         float normalProgress = Utilities.boundToRange(progress, 0, 1);
113         if (normalProgress != mLastNormalProgress) {
114             mLastNormalProgress = normalProgress;
115             mNormalController.setPlayFraction(normalProgress);
116         }
117         if (maxProgress <= 1) {
118             return;
119         }
120         float resistProgress = progress <= 1 ? 0 : Utilities.getProgress(progress, 1, maxProgress);
121         if (resistProgress != mLastResistProgress) {
122             mLastResistProgress = resistProgress;
123             mResistanceController.setPlayFraction(resistProgress);
124         }
125     }
126 
127     /**
128      * Applies resistance to recents when swiping up past its target position.
129      * @param normalController The controller to run from 0 to 1 before this resistance applies.
130      * @param context Used to compute start and end values.
131      * @param recentsOrientedState Used to compute start and end values.
132      * @param dp Used to compute start and end values.
133      * @param scaleTarget The target for the scaleProperty.
134      * @param scaleProperty Animate the value to change the scale of the window/recents view.
135      * @param translationTarget The target for the translationProperty.
136      * @param translationProperty Animate the value to change the translation of the recents view.
137      */
createForRecents( AnimatorPlaybackController normalController, Context context, RecentsOrientedState recentsOrientedState, DeviceProfile dp, SCALE scaleTarget, FloatProperty<SCALE> scaleProperty, TRANSLATION translationTarget, FloatProperty<TRANSLATION> translationProperty)138     public static <SCALE, TRANSLATION> AnimatorControllerWithResistance createForRecents(
139             AnimatorPlaybackController normalController, Context context,
140             RecentsOrientedState recentsOrientedState, DeviceProfile dp, SCALE scaleTarget,
141             FloatProperty<SCALE> scaleProperty, TRANSLATION translationTarget,
142             FloatProperty<TRANSLATION> translationProperty) {
143 
144         RecentsParams params = new RecentsParams(context, recentsOrientedState, dp, scaleTarget,
145                 scaleProperty, translationTarget, translationProperty);
146         PendingAnimation resistAnim = createRecentsResistanceAnim(params);
147 
148         AnimatorPlaybackController resistanceController = resistAnim.createPlaybackController();
149         return new AnimatorControllerWithResistance(normalController, resistanceController);
150     }
151 
152     /**
153      * Creates the resistance animation for {@link #createForRecents}, or can be used separately
154      * when starting from recents, i.e. {@link #createRecentsResistanceFromOverviewAnim}.
155      */
createRecentsResistanceAnim( RecentsParams<SCALE, TRANSLATION> params)156     public static <SCALE, TRANSLATION> PendingAnimation createRecentsResistanceAnim(
157             RecentsParams<SCALE, TRANSLATION> params) {
158         Rect startRect = new Rect();
159         PagedOrientationHandler orientationHandler = params.recentsOrientedState
160                 .getOrientationHandler();
161         LauncherActivityInterface.INSTANCE.calculateTaskSize(params.context, params.dp, startRect);
162         long distanceToCover = startRect.bottom;
163         PendingAnimation resistAnim = params.resistAnim != null
164                 ? params.resistAnim
165                 : new PendingAnimation(distanceToCover * 2);
166 
167         PointF pivot = new PointF();
168         float fullscreenScale = params.recentsOrientedState.getFullScreenScaleAndPivot(
169                 startRect, params.dp, pivot);
170 
171         // Compute where the task view would be based on the end scale.
172         RectF endRectF = new RectF(startRect);
173         Matrix temp = new Matrix();
174         temp.setScale(params.resistanceParams.scaleMaxResist,
175                 params.resistanceParams.scaleMaxResist, pivot.x, pivot.y);
176         temp.mapRect(endRectF);
177         // Translate such that the task view touches the top of the screen when drag does.
178         float endTranslation = endRectF.top
179                 * orientationHandler.getSecondaryTranslationDirectionFactor()
180                 * params.resistanceParams.translationFactor;
181         resistAnim.addFloat(params.translationTarget, params.translationProperty,
182                 params.startTranslation, endTranslation, RECENTS_TRANSLATE_RESIST_INTERPOLATOR);
183 
184         float prevScaleRate = (fullscreenScale - params.startScale)
185                 / (params.dp.heightPx - startRect.bottom);
186         // This is what the scale would be at the end of the drag if we didn't apply resistance.
187         float endScale = params.startScale - prevScaleRate * distanceToCover;
188         // Create an interpolator that resists the scale so the scale doesn't get smaller than
189         // RECENTS_SCALE_MAX_RESIST.
190         float startResist = Utilities.getProgress(params.resistanceParams.scaleStartResist,
191                 params.startScale, endScale);
192         float maxResist = Utilities.getProgress(params.resistanceParams.scaleMaxResist,
193                 params.startScale, endScale);
194         float stopResist =
195                 params.resistanceParams.stopScalingAtTop ? 1f - startRect.top / endRectF.top : 1f;
196         final TimeInterpolator scaleInterpolator = t -> {
197             if (t < startResist) {
198                 return t;
199             }
200             if (t > stopResist) {
201                 return maxResist;
202             }
203             float resistProgress = Utilities.getProgress(t, startResist, stopResist);
204             resistProgress = RECENTS_SCALE_RESIST_INTERPOLATOR.getInterpolation(resistProgress);
205             return startResist + resistProgress * (maxResist - startResist);
206         };
207         resistAnim.addFloat(params.scaleTarget, params.scaleProperty, params.startScale, endScale,
208                 scaleInterpolator);
209 
210         return resistAnim;
211     }
212 
213     /**
214      * Helper method to update or create a PendingAnimation suitable for animating
215      * a RecentsView interaction that started from the overview state.
216      */
createRecentsResistanceFromOverviewAnim( Launcher launcher, @Nullable PendingAnimation resistanceAnim)217     public static PendingAnimation createRecentsResistanceFromOverviewAnim(
218             Launcher launcher, @Nullable PendingAnimation resistanceAnim) {
219         RecentsView recentsView = launcher.getOverviewPanel();
220         RecentsParams params = new RecentsParams(launcher, recentsView.getPagedViewOrientedState(),
221                 launcher.getDeviceProfile(), recentsView, RECENTS_SCALE_PROPERTY, recentsView,
222                 TASK_SECONDARY_TRANSLATION)
223                 .setResistAnim(resistanceAnim)
224                 .setResistanceParams(RecentsResistanceParams.FROM_OVERVIEW)
225                 .setStartScale(recentsView.getScaleX());
226         return createRecentsResistanceAnim(params);
227     }
228 
229     /**
230      * Params to compute resistance when scaling/translating recents.
231      */
232     private static class RecentsParams<SCALE, TRANSLATION> {
233         // These are all required and can't have default values, hence are final.
234         public final Context context;
235         public final RecentsOrientedState recentsOrientedState;
236         public final DeviceProfile dp;
237         public final SCALE scaleTarget;
238         public final FloatProperty<SCALE> scaleProperty;
239         public final TRANSLATION translationTarget;
240         public final FloatProperty<TRANSLATION> translationProperty;
241 
242         // These are not required, or can have a default value that is generally correct.
243         @Nullable public PendingAnimation resistAnim = null;
244         public RecentsResistanceParams resistanceParams;
245         public float startScale = 1f;
246         public float startTranslation = 0f;
247 
RecentsParams(Context context, RecentsOrientedState recentsOrientedState, DeviceProfile dp, SCALE scaleTarget, FloatProperty<SCALE> scaleProperty, TRANSLATION translationTarget, FloatProperty<TRANSLATION> translationProperty)248         private RecentsParams(Context context, RecentsOrientedState recentsOrientedState,
249                 DeviceProfile dp, SCALE scaleTarget, FloatProperty<SCALE> scaleProperty,
250                 TRANSLATION translationTarget, FloatProperty<TRANSLATION> translationProperty) {
251             this.context = context;
252             this.recentsOrientedState = recentsOrientedState;
253             this.dp = dp;
254             this.scaleTarget = scaleTarget;
255             this.scaleProperty = scaleProperty;
256             this.translationTarget = translationTarget;
257             this.translationProperty = translationProperty;
258             if (dp.isTablet) {
259                 resistanceParams = RecentsResistanceParams.FROM_APP_TABLET;
260             } else {
261                 resistanceParams = RecentsResistanceParams.FROM_APP;
262             }
263         }
264 
setResistAnim(PendingAnimation resistAnim)265         private RecentsParams setResistAnim(PendingAnimation resistAnim) {
266             this.resistAnim = resistAnim;
267             return this;
268         }
269 
setResistanceParams(RecentsResistanceParams resistanceParams)270         private RecentsParams setResistanceParams(RecentsResistanceParams resistanceParams) {
271             this.resistanceParams = resistanceParams;
272             return this;
273         }
274 
setStartScale(float startScale)275         private RecentsParams setStartScale(float startScale) {
276             this.startScale = startScale;
277             return this;
278         }
279 
setStartTranslation(float startTranslation)280         private RecentsParams setStartTranslation(float startTranslation) {
281             this.startTranslation = startTranslation;
282             return this;
283         }
284     }
285 }
286