blob: eb2670426c465d86f38c998a5eeca2c6103679e4 [file] [log] [blame]
Hyunyoung Song645764e2016-06-06 14:19:02 -07001package com.android.launcher3.allapps;
2
Sunny Goyal03102202017-10-27 11:05:26 -07003import static com.android.launcher3.anim.Interpolators.FAST_OUT_SLOW_IN;
4import static com.android.launcher3.anim.Interpolators.LINEAR;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07005
Hyunyoung Song645764e2016-06-06 14:19:02 -07006import android.animation.Animator;
Hyunyoung Songc001cf52016-07-21 17:32:43 -07007import android.animation.AnimatorInflater;
Hyunyoung Song645764e2016-06-06 14:19:02 -07008import android.animation.AnimatorListenerAdapter;
9import android.animation.AnimatorSet;
10import android.animation.ObjectAnimator;
Sunny Goyalbe93f262017-10-20 17:05:27 -070011import android.util.Property;
Hyunyoung Song645764e2016-06-06 14:19:02 -070012import android.view.View;
Hyunyoung Song645764e2016-06-06 14:19:02 -070013import android.view.animation.Interpolator;
14
15import com.android.launcher3.Hotseat;
16import com.android.launcher3.Launcher;
Sunny Goyalf9403d92017-10-18 10:55:56 -070017import com.android.launcher3.LauncherState;
Sunny Goyalc4fa8c32017-11-07 12:23:58 -080018import com.android.launcher3.LauncherStateManager;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070019import com.android.launcher3.LauncherStateManager.AnimationConfig;
Hyunyoung Songeac1dac2016-06-21 16:37:13 -070020import com.android.launcher3.R;
Hyunyoung Songc001cf52016-07-21 17:32:43 -070021import com.android.launcher3.Utilities;
Sunny Goyala92e0df2016-06-09 12:08:22 -070022import com.android.launcher3.Workspace;
Sunny Goyalc4fa8c32017-11-07 12:23:58 -080023import com.android.launcher3.anim.AnimationLayerSet;
Sunny Goyalbe93f262017-10-20 17:05:27 -070024import com.android.launcher3.anim.AnimationSuccessListener;
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070025import com.android.launcher3.anim.Interpolators;
Mario Bertschler8acf8b52017-05-11 10:45:20 -070026import com.android.launcher3.graphics.GradientView;
Sunny Goyal8392c822017-06-20 10:03:56 -070027import com.android.launcher3.util.SystemUiController;
Sunny Goyal1f3f07d2017-02-10 16:52:16 -080028import com.android.launcher3.util.Themes;
Hyunyoung Song645764e2016-06-06 14:19:02 -070029
30/**
31 * Handles AllApps view transition.
32 * 1) Slides all apps view using direct manipulation
33 * 2) When finger is released, animate to either top or bottom accordingly.
Hyunyoung Songe8a2b8e2016-07-14 15:09:11 -070034 * <p/>
Hyunyoung Song645764e2016-06-06 14:19:02 -070035 * Algorithm:
36 * If release velocity > THRES1, snap according to the direction of movement.
37 * If release velocity < THRES1, snap according to either top or bottom depending on whether it's
Hyunyoung Songe8a2b8e2016-07-14 15:09:11 -070038 * closer to top or closer to the page indicator.
Hyunyoung Song645764e2016-06-06 14:19:02 -070039 */
Sunny Goyalc4fa8c32017-11-07 12:23:58 -080040public class AllAppsTransitionController
41 implements SearchUiManager.OnScrollRangeChangeListener, LauncherStateManager.StateHandler {
Hyunyoung Song645764e2016-06-06 14:19:02 -070042
Sunny Goyalbe93f262017-10-20 17:05:27 -070043 private static final Property<AllAppsTransitionController, Float> PROGRESS =
44 new Property<AllAppsTransitionController, Float>(Float.class, "progress") {
45
46 @Override
47 public Float get(AllAppsTransitionController controller) {
48 return controller.mProgress;
49 }
50
51 @Override
52 public void set(AllAppsTransitionController controller, Float progress) {
53 controller.setProgress(progress);
54 }
55 };
56
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070057 private final Interpolator mWorkspaceAccelnterpolator = Interpolators.ACCEL_2;
58 private final Interpolator mHotseatAccelInterpolator = Interpolators.ACCEL_1_5;
Hyunyoung Song74b5af32016-06-08 16:29:32 -070059
Hyunyoung Songa9a8a422016-06-15 16:45:48 -070060 private static final float PARALLAX_COEFFICIENT = .125f;
Hyunyoung Song645764e2016-06-06 14:19:02 -070061
62 private AllAppsContainerView mAppsView;
Sunny Goyala92e0df2016-06-09 12:08:22 -070063 private Workspace mWorkspace;
Hyunyoung Song645764e2016-06-06 14:19:02 -070064 private Hotseat mHotseat;
Hyunyoung Songa0c56472016-06-20 13:54:42 -070065
Hyunyoung Song645764e2016-06-06 14:19:02 -070066 private final Launcher mLauncher;
Sunny Goyal368ae772017-05-24 13:19:15 -070067 private final boolean mIsDarkTheme;
Hyunyoung Song645764e2016-06-06 14:19:02 -070068
Hyunyoung Songc001cf52016-07-21 17:32:43 -070069 // Animation in this class is controlled by a single variable {@link mProgress}.
70 // Visually, it represents top y coordinate of the all apps container if multiplied with
71 // {@link mShiftRange}.
72
73 // When {@link mProgress} is 0, all apps container is pulled up.
74 // When {@link mProgress} is 1, all apps container is pulled down.
Hyunyoung Songeac1dac2016-06-21 16:37:13 -070075 private float mShiftRange; // changes depending on the orientation
Hyunyoung Songc001cf52016-07-21 17:32:43 -070076 private float mProgress; // [0, 1], mShiftRange * mProgress = shiftCurrent
Hyunyoung Songeac1dac2016-06-21 16:37:13 -070077
Hyunyoung Song2359a682016-07-07 15:57:42 -070078 private static final float DEFAULT_SHIFT_RANGE = 10;
79
Hyunyoung Songc001cf52016-07-21 17:32:43 -070080 private boolean mIsTranslateWithoutWorkspace = false;
Sunny Goyale18d3f52017-08-24 13:33:47 -070081 private Animator mDiscoBounceAnimation;
Mario Bertschler8acf8b52017-05-11 10:45:20 -070082 private GradientView mGradientView;
Hyunyoung Songc001cf52016-07-21 17:32:43 -070083
84 public AllAppsTransitionController(Launcher l) {
85 mLauncher = l;
Hyunyoung Songc001cf52016-07-21 17:32:43 -070086 mShiftRange = DEFAULT_SHIFT_RANGE;
87 mProgress = 1f;
Peter Schillera30b51c2016-07-13 14:04:13 -070088
Mario Bertschlera6936942017-05-31 14:48:19 -070089 mIsDarkTheme = Themes.getAttrBoolean(mLauncher, R.attr.isMainColorDark);
Hyunyoung Song645764e2016-06-06 14:19:02 -070090 }
91
Sunny Goyal03102202017-10-27 11:05:26 -070092 public float getShiftRange() {
93 return mShiftRange;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -070094 }
Hyunyoung Songe3876e82016-07-27 11:54:02 -070095
Sunny Goyalbe93f262017-10-20 17:05:27 -070096 private void onProgressAnimationStart() {
97 // Initialize values that should not change until #onDragEnd
98 mHotseat.setVisibility(View.VISIBLE);
99 mAppsView.setVisibility(View.VISIBLE);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700100 }
101
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700102 private void updateLightStatusBar(float shift) {
Sunny Goyal8392c822017-06-20 10:03:56 -0700103 // Use a light system UI (dark icons) if all apps is behind at least half of the status bar.
Sunny Goyalf2dd4212017-09-28 11:31:39 -0700104 boolean forceChange = shift <= mShiftRange / 4;
Sunny Goyal8392c822017-06-20 10:03:56 -0700105 if (forceChange) {
106 mLauncher.getSystemUiController().updateUiState(
107 SystemUiController.UI_STATE_ALL_APPS, !mIsDarkTheme);
108 } else {
109 mLauncher.getSystemUiController().updateUiState(
110 SystemUiController.UI_STATE_ALL_APPS, 0);
111 }
Hyunyoung Song645764e2016-06-06 14:19:02 -0700112 }
113
Mario Bertschler48198d02017-01-30 17:05:24 -0800114 private void updateAllAppsBg(float progress) {
115 // gradient
116 if (mGradientView == null) {
Sunny Goyald66e3b62017-10-02 15:26:42 -0700117 mGradientView = mLauncher.findViewById(R.id.gradient_bg);
Mario Bertschler48198d02017-01-30 17:05:24 -0800118 }
119 mGradientView.setProgress(progress);
Mario Bertschler48198d02017-01-30 17:05:24 -0800120 }
121
Hyunyoung Song645764e2016-06-06 14:19:02 -0700122 /**
Sunny Goyalbe93f262017-10-20 17:05:27 -0700123 * Note this method should not be called outside this class. This is public because it is used
124 * in xml-based animations which also handle updating the appropriate UI.
125 *
126 * @param progress value between 0 and 1, 0 shows all apps and 1 shows workspace
127 *
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800128 * @see #setState(LauncherState)
129 * @see #setStateWithAnimation(LauncherState, AnimationLayerSet, AnimatorSet, AnimationConfig)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700130 */
131 public void setProgress(float progress) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700132 mProgress = progress;
133 float shiftCurrent = progress * mShiftRange;
134
135 float workspaceHotseatAlpha = Utilities.boundToRange(progress, 0f, 1f);
136 float alpha = 1 - workspaceHotseatAlpha;
Mario Bertschler8acf8b52017-05-11 10:45:20 -0700137 float workspaceAlpha = mWorkspaceAccelnterpolator.getInterpolation(workspaceHotseatAlpha);
138 float hotseatAlpha = mHotseatAccelInterpolator.getInterpolation(workspaceHotseatAlpha);
Hyunyoung Song74b5af32016-06-08 16:29:32 -0700139
Sunny Goyalf2dd4212017-09-28 11:31:39 -0700140 updateAllAppsBg(alpha);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700141 mAppsView.setAlpha(alpha);
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700142 mAppsView.setTranslationY(shiftCurrent);
143
144 if (!mLauncher.getDeviceProfile().isVerticalBarLayout()) {
145 mWorkspace.setHotseatTranslationAndAlpha(Workspace.Direction.Y, -mShiftRange + shiftCurrent,
Mario Bertschler8acf8b52017-05-11 10:45:20 -0700146 hotseatAlpha);
Hyunyoung Songeac1dac2016-06-21 16:37:13 -0700147 } else {
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700148 mWorkspace.setHotseatTranslationAndAlpha(Workspace.Direction.Y,
149 PARALLAX_COEFFICIENT * (-mShiftRange + shiftCurrent),
Mario Bertschler8acf8b52017-05-11 10:45:20 -0700150 hotseatAlpha);
Hyunyoung Songeac1dac2016-06-21 16:37:13 -0700151 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700152
153 if (mIsTranslateWithoutWorkspace) {
154 return;
155 }
156 mWorkspace.setWorkspaceYTranslationAndAlpha(
Mario Bertschler8acf8b52017-05-11 10:45:20 -0700157 PARALLAX_COEFFICIENT * (-mShiftRange + shiftCurrent), workspaceAlpha);
Hyunyoung Song5b647062016-08-02 13:31:22 -0700158
Peter Schillere6fe1b52016-07-29 17:05:30 -0700159 updateLightStatusBar(shiftCurrent);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700160 }
161
162 public float getProgress() {
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700163 return mProgress;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700164 }
165
Sunny Goyalbe93f262017-10-20 17:05:27 -0700166 /**
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800167 * Sets the vertical transition progress to {@param state} and updates all the dependent UI
Sunny Goyalbe93f262017-10-20 17:05:27 -0700168 * accordingly.
169 */
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800170 @Override
171 public void setState(LauncherState state) {
172 setProgress(state.verticalProgress);
Sunny Goyalbe93f262017-10-20 17:05:27 -0700173 onProgressAnimationEnd();
174 }
175
176 /**
177 * Creates an animation which updates the vertical transition progress and updates all the
178 * dependent UI using various animation events
Sunny Goyalbe93f262017-10-20 17:05:27 -0700179 */
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800180 @Override
181 public void setStateWithAnimation(LauncherState toState, AnimationLayerSet layerViews,
182 AnimatorSet animationOut, AnimationConfig config) {
183 if (Float.compare(mProgress, toState.verticalProgress) == 0) {
Sunny Goyalbe93f262017-10-20 17:05:27 -0700184 // Fail fast
185 onProgressAnimationEnd();
Sunny Goyalaeb16432017-10-16 11:46:41 -0700186 return;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700187 }
Sunny Goyalbe93f262017-10-20 17:05:27 -0700188
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800189 Interpolator interpolator = config.userControlled ? LINEAR : FAST_OUT_SLOW_IN;
190 ObjectAnimator anim = ObjectAnimator.ofFloat(
191 this, PROGRESS, mProgress, toState.verticalProgress);
192 anim.setDuration(config.duration);
Sunny Goyalbe93f262017-10-20 17:05:27 -0700193 anim.setInterpolator(interpolator);
194 anim.addListener(new AnimationSuccessListener() {
Hyunyoung Song645764e2016-06-06 14:19:02 -0700195 @Override
Sunny Goyalbe93f262017-10-20 17:05:27 -0700196 public void onAnimationSuccess(Animator animator) {
197 onProgressAnimationEnd();
Hyunyoung Song645764e2016-06-06 14:19:02 -0700198 }
Hyunyoung Songe8a2b8e2016-07-14 15:09:11 -0700199
Hyunyoung Song645764e2016-06-06 14:19:02 -0700200 @Override
Jon Miranda610af372017-09-05 11:32:13 -0700201 public void onAnimationStart(Animator animation) {
Sunny Goyalbe93f262017-10-20 17:05:27 -0700202 onProgressAnimationStart();
Hyunyoung Songe8a2b8e2016-07-14 15:09:11 -0700203 }
204 });
Sunny Goyalbe93f262017-10-20 17:05:27 -0700205
206 animationOut.play(anim);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700207 }
208
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700209 public void showDiscoveryBounce() {
210 // cancel existing animation in case user locked and unlocked at a super human speed.
211 cancelDiscoveryAnimation();
212
213 // assumption is that this variable is always null
Sunny Goyale18d3f52017-08-24 13:33:47 -0700214 mDiscoBounceAnimation = AnimatorInflater.loadAnimator(mLauncher,
215 R.animator.discovery_bounce);
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700216 mDiscoBounceAnimation.addListener(new AnimatorListenerAdapter() {
217 @Override
218 public void onAnimationStart(Animator animator) {
219 mIsTranslateWithoutWorkspace = true;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700220 onProgressAnimationStart();
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700221 }
222
223 @Override
224 public void onAnimationEnd(Animator animator) {
Sunny Goyalbe93f262017-10-20 17:05:27 -0700225 onProgressAnimationEnd();
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700226 mDiscoBounceAnimation = null;
227 mIsTranslateWithoutWorkspace = false;
228 }
229 });
230 mDiscoBounceAnimation.setTarget(this);
231 mAppsView.post(new Runnable() {
232 @Override
233 public void run() {
Hyunyoung Songe688e1e2016-08-03 10:40:18 -0700234 if (mDiscoBounceAnimation == null) {
235 return;
236 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700237 mDiscoBounceAnimation.start();
238 }
239 });
240 }
241
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700242 public void cancelDiscoveryAnimation() {
243 if (mDiscoBounceAnimation == null) {
244 return;
245 }
246 mDiscoBounceAnimation.cancel();
247 mDiscoBounceAnimation = null;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700248 }
249
Hyunyoung Songa0c56472016-06-20 13:54:42 -0700250 public void setupViews(AllAppsContainerView appsView, Hotseat hotseat, Workspace workspace) {
251 mAppsView = appsView;
252 mHotseat = hotseat;
253 mWorkspace = workspace;
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700254 mHotseat.bringToFront();
Sunny Goyaldc19a072017-05-12 08:17:35 -0700255 mAppsView.getSearchUiManager().addOnScrollRangeChangeListener(this);
Hyunyoung Song5215b542016-06-29 21:25:33 -0700256 }
257
258 @Override
Sunny Goyaldc19a072017-05-12 08:17:35 -0700259 public void onScrollRangeChanged(int scrollRange) {
260 mShiftRange = scrollRange;
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700261 setProgress(mProgress);
Hyunyoung Songa0c56472016-06-20 13:54:42 -0700262 }
Sunny Goyalbe93f262017-10-20 17:05:27 -0700263
264 /**
265 * Set the final view states based on the progress.
266 * TODO: This logic should go in {@link LauncherState}
267 */
268 private void onProgressAnimationEnd() {
269 if (Float.compare(mProgress, 1f) == 0) {
270 mAppsView.setVisibility(View.INVISIBLE);
271 mHotseat.setVisibility(View.VISIBLE);
272 mAppsView.reset();
273 } else if (Float.compare(mProgress, 0f) == 0) {
274 mHotseat.setVisibility(View.INVISIBLE);
275 mAppsView.setVisibility(View.VISIBLE);
276 }
Sunny Goyalbe93f262017-10-20 17:05:27 -0700277 }
Hyunyoung Song645764e2016-06-06 14:19:02 -0700278}