blob: 73ae51c3ecc85d056183ceb44a1c00ad12ed538a [file] [log] [blame]
Winson Chungb745afb2015-03-02 11:51:23 -08001/*
2 * Copyright (C) 2015 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
17package com.android.launcher3;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
23import android.animation.PropertyValuesHolder;
24import android.animation.TimeInterpolator;
25import android.content.res.Resources;
26import android.util.Log;
27import android.view.View;
28import android.view.ViewAnimationUtils;
29import android.view.animation.AccelerateInterpolator;
30import android.view.animation.DecelerateInterpolator;
31
Adam Cohen091440a2015-03-18 14:16:05 -070032import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -070033import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen091440a2015-03-18 14:16:05 -070034
Winson Chungb745afb2015-03-02 11:51:23 -080035import java.util.HashMap;
36
37/**
38 * TODO: figure out what kind of tests we can write for this
39 *
40 * Things to test when changing the following class.
41 * - Home from workspace
42 * - from center screen
43 * - from other screens
44 * - Home from all apps
45 * - from center screen
46 * - from other screens
47 * - Back from all apps
48 * - from center screen
49 * - from other screens
50 * - Launch app from workspace and quit
51 * - with back
52 * - with home
53 * - Launch app from all apps and quit
54 * - with back
55 * - with home
56 * - Go to a screen that's not the default, then all
57 * apps, and launch and app, and go back
58 * - with back
59 * -with home
60 * - On workspace, long press power and go back
61 * - with back
62 * - with home
63 * - On all apps, long press power and go back
64 * - with back
65 * - with home
66 * - On workspace, power off
67 * - On all apps, power off
68 * - Launch an app and turn off the screen while in that app
69 * - Go back with home key
70 * - Go back with back key TODO: make this not go to workspace
71 * - From all apps
72 * - From workspace
73 * - Enter and exit car mode (becuase it causes an extra configuration changed)
74 * - From all apps
75 * - From the center workspace
76 * - From another workspace
77 */
78public class LauncherStateTransitionAnimation {
79
80 /**
81 * Callbacks made during the state transition
82 */
83 interface Callbacks {
84 public void onStateTransitionHideSearchBar();
85 }
86
87 /**
88 * Private callbacks made during transition setup.
89 */
90 static abstract class PrivateTransitionCallbacks {
91 void onRevealViewVisible(View revealView, View contentView, View allAppsButtonView) {}
92 void onAnimationComplete(View revealView, View contentView, View allAppsButtonView) {}
93 float getMaterialRevealViewFinalAlpha(View revealView) {
94 return 0;
95 }
96 float getMaterialRevealViewFinalXDrift(View revealView) {
97 return 0;
98 }
99 float getMaterialRevealViewFinalYDrift(View revealView) {
100 return 0;
101 }
102 float getMaterialRevealViewStartFinalRadius() {
103 return 0;
104 }
105 AnimatorListenerAdapter getMaterialRevealViewAnimatorListener(View revealView,
106 View allAppsButtonView) {
107 return null;
108 }
109 }
110
111 public static final String TAG = "LauncherStateTransitionAnimation";
112
113 // Flags to determine how to set the layers on views before the transition animation
114 public static final int BUILD_LAYER = 0;
115 public static final int BUILD_AND_SET_LAYER = 1;
116 public static final int SINGLE_FRAME_DELAY = 16;
117
Adam Cohen091440a2015-03-18 14:16:05 -0700118 @Thunk Launcher mLauncher;
119 @Thunk Callbacks mCb;
120 @Thunk AnimatorSet mStateAnimation;
Winson Chungb745afb2015-03-02 11:51:23 -0800121
122 public LauncherStateTransitionAnimation(Launcher l, Callbacks cb) {
123 mLauncher = l;
124 mCb = cb;
125 }
126
127 /**
128 * Starts an animation to the apps view.
129 */
130 public void startAnimationToAllApps(final boolean animated) {
131 final AppsContainerView toView = mLauncher.getAppsView();
132 PrivateTransitionCallbacks cb = new PrivateTransitionCallbacks() {
133 private int[] mAllAppsToPanelDelta;
134
135 @Override
136 public void onRevealViewVisible(View revealView, View contentView,
137 View allAppsButtonView) {
138 toView.setBackground(null);
139 // Get the y delta between the center of the page and the center of the all apps
140 // button
141 mAllAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
142 allAppsButtonView, null);
143 }
144 @Override
145 public float getMaterialRevealViewFinalAlpha(View revealView) {
146 return 1f;
147 }
148 @Override
149 public float getMaterialRevealViewFinalXDrift(View revealView) {
150 return mAllAppsToPanelDelta[0];
151 }
152 @Override
153 public float getMaterialRevealViewFinalYDrift(View revealView) {
154 return mAllAppsToPanelDelta[1];
155 }
156 @Override
157 public float getMaterialRevealViewStartFinalRadius() {
158 int allAppsButtonSize = LauncherAppState.getInstance().
159 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
160 return allAppsButtonSize / 2;
161 }
162 @Override
163 public AnimatorListenerAdapter getMaterialRevealViewAnimatorListener(
164 final View revealView, final View allAppsButtonView) {
165 return new AnimatorListenerAdapter() {
166 public void onAnimationStart(Animator animation) {
167 allAppsButtonView.setVisibility(View.INVISIBLE);
168 }
169 public void onAnimationEnd(Animator animation) {
170 allAppsButtonView.setVisibility(View.VISIBLE);
171 }
172 };
173 }
174 };
175 startAnimationToOverlay(Workspace.State.NORMAL_HIDDEN, toView, toView.getContentView(),
Winson Chungdc61c4d2015-04-20 18:26:57 -0700176 toView.getRevealView(), animated,
177 !mLauncher.isAllAppsSearchOverridden() /* hideSearchBar */, cb);
Winson Chungb745afb2015-03-02 11:51:23 -0800178 }
179
180 /**
181 * Starts an animation to the widgets view.
182 */
183 public void startAnimationToWidgets(final boolean animated) {
Hyunyoung Song3f471442015-04-08 19:01:34 -0700184 final WidgetsContainerView toView = mLauncher.getWidgetsView();
Hyunyoung Song4cea4c82015-04-17 19:02:30 -0700185 final Resources res = mLauncher.getResources();
Winson Chungb745afb2015-03-02 11:51:23 -0800186 PrivateTransitionCallbacks cb = new PrivateTransitionCallbacks() {
187 @Override
Hyunyoung Song4cea4c82015-04-17 19:02:30 -0700188 public void onRevealViewVisible(View revealView, View contentView,
189 View allAppsButtonView) {
190 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
191 }
192 @Override
Winson Chungb745afb2015-03-02 11:51:23 -0800193 public float getMaterialRevealViewFinalAlpha(View revealView) {
194 return 0.3f;
195 }
196 @Override
197 public float getMaterialRevealViewFinalYDrift(View revealView) {
198 return revealView.getMeasuredHeight() / 2;
199 }
200 };
Hyunyoung Song4cea4c82015-04-17 19:02:30 -0700201 startAnimationToOverlay(Workspace.State.OVERVIEW_HIDDEN, toView,
202 toView.getContentView(), toView.getRevealView(), animated, true /* hideSearchBar */,
203 cb);
Winson Chungb745afb2015-03-02 11:51:23 -0800204 }
205
206 /**
207 * Starts and animation to the workspace from the current overlay view.
208 */
209 public void startAnimationToWorkspace(final Launcher.State fromState,
Winson Chungdc61c4d2015-04-20 18:26:57 -0700210 final Workspace.State toWorkspaceState, final int toWorkspacePage,
211 final boolean animated, final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -0800212 if (toWorkspaceState != Workspace.State.NORMAL &&
213 toWorkspaceState != Workspace.State.SPRING_LOADED &&
214 toWorkspaceState != Workspace.State.OVERVIEW) {
215 Log.e(TAG, "Unexpected call to startAnimationToWorkspace");
216 }
217
218 if (fromState == Launcher.State.APPS || fromState == Launcher.State.APPS_SPRING_LOADED) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700219 startAnimationToWorkspaceFromAllApps(fromState, toWorkspaceState, toWorkspacePage,
220 animated, onCompleteRunnable);
Winson Chungb745afb2015-03-02 11:51:23 -0800221 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700222 startAnimationToWorkspaceFromWidgets(fromState, toWorkspaceState, toWorkspacePage,
223 animated, onCompleteRunnable);
Winson Chungb745afb2015-03-02 11:51:23 -0800224 }
225 }
226
227 /**
228 * Creates and starts a new animation to a particular overlay view.
229 */
230 private void startAnimationToOverlay(final Workspace.State toWorkspaceState, final View toView,
Winson Chung0f785722015-04-08 10:27:49 -0700231 final View contentView, final View revealView, final boolean animated,
232 final boolean hideSearchBar, final PrivateTransitionCallbacks pCb) {
Winson Chungb745afb2015-03-02 11:51:23 -0800233 final Resources res = mLauncher.getResources();
234 final boolean material = Utilities.isLmpOrAbove();
235 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
236 final int itemsAlphaStagger =
237 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
238
239 final View allAppsButtonView = mLauncher.getAllAppsButton();
240 final View fromView = mLauncher.getWorkspace();
241
242 final HashMap<View, Integer> layerViews = new HashMap<>();
243
244 // If for some reason our views aren't initialized, don't animate
245 boolean initialized = allAppsButtonView != null;
246
247 // Cancel the current animation
248 cancelAnimation();
249
250 // Create the workspace animation.
251 // NOTE: this call apparently also sets the state for the workspace if !animated
Winson Chungcd99cd32015-04-29 11:03:24 -0700252 Animator workspaceAnim = mLauncher.startWorkspaceStateChangeAnimation(toWorkspaceState, -1,
253 animated, layerViews);
Winson Chungb745afb2015-03-02 11:51:23 -0800254
255 if (animated && initialized) {
256 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
257
258 // Setup the reveal view animation
259 int width = revealView.getMeasuredWidth();
260 int height = revealView.getMeasuredHeight();
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700261 float revealRadius = (float) Math.hypot(width / 2, height / 2);
Winson Chungb745afb2015-03-02 11:51:23 -0800262 revealView.setVisibility(View.VISIBLE);
263 revealView.setAlpha(0f);
264 revealView.setTranslationY(0f);
265 revealView.setTranslationX(0f);
266 pCb.onRevealViewVisible(revealView, contentView, allAppsButtonView);
267
268 // Calculate the final animation values
269 final float revealViewToAlpha;
270 final float revealViewToXDrift;
271 final float revealViewToYDrift;
272 if (material) {
273 revealViewToAlpha = pCb.getMaterialRevealViewFinalAlpha(revealView);
274 revealViewToYDrift = pCb.getMaterialRevealViewFinalYDrift(revealView);
275 revealViewToXDrift = pCb.getMaterialRevealViewFinalXDrift(revealView);
276 } else {
277 revealViewToAlpha = 0f;
278 revealViewToYDrift = 2 * height / 3;
279 revealViewToXDrift = 0;
280 }
281
282 // Create the animators
283 PropertyValuesHolder panelAlpha =
284 PropertyValuesHolder.ofFloat("alpha", revealViewToAlpha, 1f);
285 PropertyValuesHolder panelDriftY =
286 PropertyValuesHolder.ofFloat("translationY", revealViewToYDrift, 0);
287 PropertyValuesHolder panelDriftX =
288 PropertyValuesHolder.ofFloat("translationX", revealViewToXDrift, 0);
289 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
290 panelAlpha, panelDriftY, panelDriftX);
291 panelAlphaAndDrift.setDuration(revealDuration);
292 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
293
294 // Play the animation
295 layerViews.put(revealView, BUILD_AND_SET_LAYER);
296 mStateAnimation.play(panelAlphaAndDrift);
297
Winson Chungb745afb2015-03-02 11:51:23 -0800298 // Setup the animation for the content view
299 contentView.setVisibility(View.VISIBLE);
300 contentView.setAlpha(0f);
301 contentView.setTranslationY(revealViewToYDrift);
302 layerViews.put(contentView, BUILD_AND_SET_LAYER);
303
304 // Create the individual animators
305 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(contentView, "translationY",
306 revealViewToYDrift, 0);
307 pageDrift.setDuration(revealDuration);
308 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
309 pageDrift.setStartDelay(itemsAlphaStagger);
310 mStateAnimation.play(pageDrift);
311
312 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(contentView, "alpha", 0f, 1f);
313 itemsAlpha.setDuration(revealDuration);
314 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
315 itemsAlpha.setStartDelay(itemsAlphaStagger);
316 mStateAnimation.play(itemsAlpha);
317
318 if (material) {
319 // Animate the all apps button
320 float startRadius = pCb.getMaterialRevealViewStartFinalRadius();
321 AnimatorListenerAdapter listener = pCb.getMaterialRevealViewAnimatorListener(
322 revealView, allAppsButtonView);
323 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
324 height / 2, startRadius, revealRadius);
325 reveal.setDuration(revealDuration);
326 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
327 if (listener != null) {
328 reveal.addListener(listener);
329 }
330 mStateAnimation.play(reveal);
331 }
332
333 mStateAnimation.addListener(new AnimatorListenerAdapter() {
334 @Override
335 public void onAnimationEnd(Animator animation) {
336 dispatchOnLauncherTransitionEnd(fromView, animated, false);
337 dispatchOnLauncherTransitionEnd(toView, animated, false);
338
339 // Hide the reveal view
340 revealView.setVisibility(View.INVISIBLE);
341 pCb.onAnimationComplete(revealView, contentView, allAppsButtonView);
342
343 // Disable all necessary layers
344 for (View v : layerViews.keySet()) {
345 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
346 v.setLayerType(View.LAYER_TYPE_NONE, null);
347 }
348 }
349
Winson Chung0f785722015-04-08 10:27:49 -0700350 if (hideSearchBar) {
351 mCb.onStateTransitionHideSearchBar();
352 }
Winson Chungb745afb2015-03-02 11:51:23 -0800353
354 // This can hold unnecessary references to views.
355 mStateAnimation = null;
356 }
357
358 });
359
360 // Play the workspace animation
361 if (workspaceAnim != null) {
362 mStateAnimation.play(workspaceAnim);
363 }
364
365 // Dispatch the prepare transition signal
366 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
367 dispatchOnLauncherTransitionPrepare(toView, animated, false);
368
369
370 final AnimatorSet stateAnimation = mStateAnimation;
371 final Runnable startAnimRunnable = new Runnable() {
372 public void run() {
373 // Check that mStateAnimation hasn't changed while
374 // we waited for a layout/draw pass
375 if (mStateAnimation != stateAnimation)
376 return;
377 dispatchOnLauncherTransitionStart(fromView, animated, false);
378 dispatchOnLauncherTransitionStart(toView, animated, false);
379
380 // Enable all necessary layers
Winson Chung11509ad2015-05-12 18:55:26 -0700381 boolean isLmpOrAbove = Utilities.isLmpOrAbove();
Winson Chungb745afb2015-03-02 11:51:23 -0800382 for (View v : layerViews.keySet()) {
383 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
384 v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
385 }
Winson Chung11509ad2015-05-12 18:55:26 -0700386 if (isLmpOrAbove && Utilities.isViewAttachedToWindow(v)) {
Winson Chungb745afb2015-03-02 11:51:23 -0800387 v.buildLayer();
388 }
389 }
390
391 // Focus the new view
392 toView.requestFocus();
393
394 mStateAnimation.start();
395 }
396 };
Winson Chungb745afb2015-03-02 11:51:23 -0800397 toView.bringToFront();
398 toView.setVisibility(View.VISIBLE);
399 toView.post(startAnimRunnable);
400 } else {
401 toView.setTranslationX(0.0f);
402 toView.setTranslationY(0.0f);
403 toView.setScaleX(1.0f);
404 toView.setScaleY(1.0f);
405 toView.setVisibility(View.VISIBLE);
406 toView.bringToFront();
407
408 // Show the content view
409 contentView.setVisibility(View.VISIBLE);
410
Winson Chung0f785722015-04-08 10:27:49 -0700411 if (hideSearchBar) {
412 mCb.onStateTransitionHideSearchBar();
413 }
Winson Chungb745afb2015-03-02 11:51:23 -0800414
415 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
416 dispatchOnLauncherTransitionStart(fromView, animated, false);
417 dispatchOnLauncherTransitionEnd(fromView, animated, false);
418 dispatchOnLauncherTransitionPrepare(toView, animated, false);
419 dispatchOnLauncherTransitionStart(toView, animated, false);
420 dispatchOnLauncherTransitionEnd(toView, animated, false);
421 }
422 }
423
424 /**
425 * Starts and animation to the workspace from the apps view.
426 */
427 private void startAnimationToWorkspaceFromAllApps(final Launcher.State fromState,
Winson Chungdc61c4d2015-04-20 18:26:57 -0700428 final Workspace.State toWorkspaceState, final int toWorkspacePage,
429 final boolean animated, final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -0800430 AppsContainerView appsView = mLauncher.getAppsView();
431 PrivateTransitionCallbacks cb = new PrivateTransitionCallbacks() {
432 int[] mAllAppsToPanelDelta;
433
434 @Override
435 public void onRevealViewVisible(View revealView, View contentView,
436 View allAppsButtonView) {
437 // Get the y delta between the center of the page and the center of the all apps
438 // button
439 mAllAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
440 allAppsButtonView, null);
441 }
442 @Override
443 public float getMaterialRevealViewFinalXDrift(View revealView) {
444 return mAllAppsToPanelDelta[0];
445 }
446 @Override
447 public float getMaterialRevealViewFinalYDrift(View revealView) {
448 return mAllAppsToPanelDelta[1];
449 }
450 @Override
451 float getMaterialRevealViewFinalAlpha(View revealView) {
452 // No alpha anim from all apps
453 return 1f;
454 }
455 @Override
456 float getMaterialRevealViewStartFinalRadius() {
457 int allAppsButtonSize = LauncherAppState.getInstance().
458 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
459 return allAppsButtonSize / 2;
460 }
461 @Override
462 public AnimatorListenerAdapter getMaterialRevealViewAnimatorListener(
463 final View revealView, final View allAppsButtonView) {
464 return new AnimatorListenerAdapter() {
465 public void onAnimationStart(Animator animation) {
466 // We set the alpha instead of visibility to ensure that the focus does not
467 // get taken from the all apps view
468 allAppsButtonView.setVisibility(View.VISIBLE);
469 allAppsButtonView.setAlpha(0f);
470 }
471 public void onAnimationEnd(Animator animation) {
472 // Hide the reveal view
473 revealView.setVisibility(View.INVISIBLE);
474
475 // Show the all apps button, and focus it
476 allAppsButtonView.setAlpha(1f);
477 }
478 };
479 }
480 };
Winson Chungdc61c4d2015-04-20 18:26:57 -0700481 startAnimationToWorkspaceFromOverlay(toWorkspaceState, toWorkspacePage, appsView,
482 appsView.getContentView(), appsView.getRevealView(), animated, onCompleteRunnable,
483 cb);
Winson Chungb745afb2015-03-02 11:51:23 -0800484 }
485
486 /**
487 * Starts and animation to the workspace from the widgets view.
488 */
489 private void startAnimationToWorkspaceFromWidgets(final Launcher.State fromState,
Winson Chungdc61c4d2015-04-20 18:26:57 -0700490 final Workspace.State toWorkspaceState, final int toWorkspacePage,
491 final boolean animated, final Runnable onCompleteRunnable) {
Hyunyoung Song4cea4c82015-04-17 19:02:30 -0700492 final WidgetsContainerView widgetsView = mLauncher.getWidgetsView();
493 final Resources res = mLauncher.getResources();
Winson Chungb745afb2015-03-02 11:51:23 -0800494 PrivateTransitionCallbacks cb = new PrivateTransitionCallbacks() {
495 @Override
Hyunyoung Song4cea4c82015-04-17 19:02:30 -0700496 public void onRevealViewVisible(View revealView, View contentView,
497 View allAppsButtonView) {
498 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
499 }
500 @Override
Winson Chungb745afb2015-03-02 11:51:23 -0800501 public float getMaterialRevealViewFinalYDrift(View revealView) {
502 return revealView.getMeasuredHeight() / 2;
503 }
504 @Override
505 float getMaterialRevealViewFinalAlpha(View revealView) {
506 return 0.4f;
507 }
508 @Override
509 public AnimatorListenerAdapter getMaterialRevealViewAnimatorListener(
510 final View revealView, final View allAppsButtonView) {
511 return new AnimatorListenerAdapter() {
512 public void onAnimationEnd(Animator animation) {
513 // Hide the reveal view
514 revealView.setVisibility(View.INVISIBLE);
515 }
516 };
517 }
518 };
Winson Chungdc61c4d2015-04-20 18:26:57 -0700519 startAnimationToWorkspaceFromOverlay(toWorkspaceState, toWorkspacePage, widgetsView,
Winson Chung0f785722015-04-08 10:27:49 -0700520 widgetsView.getContentView(), widgetsView.getRevealView(), animated,
521 onCompleteRunnable, cb);
Winson Chungb745afb2015-03-02 11:51:23 -0800522 }
523
524 /**
525 * Creates and starts a new animation to the workspace.
526 */
527 private void startAnimationToWorkspaceFromOverlay(final Workspace.State toWorkspaceState,
Winson Chungdc61c4d2015-04-20 18:26:57 -0700528 final int toWorkspacePage, final View fromView, final View contentView,
529 final View revealView, final boolean animated, final Runnable onCompleteRunnable,
Winson Chung0f785722015-04-08 10:27:49 -0700530 final PrivateTransitionCallbacks pCb) {
Winson Chungb745afb2015-03-02 11:51:23 -0800531 final Resources res = mLauncher.getResources();
532 final boolean material = Utilities.isLmpOrAbove();
533 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
534 final int itemsAlphaStagger =
535 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
536
537 final View allAppsButtonView = mLauncher.getAllAppsButton();
538 final View toView = mLauncher.getWorkspace();
539
540 final HashMap<View, Integer> layerViews = new HashMap<>();
541
542 // If for some reason our views aren't initialized, don't animate
543 boolean initialized = allAppsButtonView != null;
544
545 // Cancel the current animation
546 cancelAnimation();
547
548 // Create the workspace animation.
549 // NOTE: this call apparently also sets the state for the workspace if !animated
Winson Chungcd99cd32015-04-29 11:03:24 -0700550 Animator workspaceAnim = mLauncher.startWorkspaceStateChangeAnimation(toWorkspaceState,
551 toWorkspacePage, animated, layerViews);
Winson Chungb745afb2015-03-02 11:51:23 -0800552
553 if (animated && initialized) {
554 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
555
556 // Play the workspace animation
557 if (workspaceAnim != null) {
558 mStateAnimation.play(workspaceAnim);
559 }
560
561 // hideAppsCustomizeHelper is called in some cases when it is already hidden
562 // don't perform all these no-op animations. In particularly, this was causing
563 // the all-apps button to pop in and out.
564 if (fromView.getVisibility() == View.VISIBLE) {
565 int width = revealView.getMeasuredWidth();
566 int height = revealView.getMeasuredHeight();
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700567 float revealRadius = (float) Math.hypot(width / 2, height / 2);
Winson Chungb745afb2015-03-02 11:51:23 -0800568 revealView.setVisibility(View.VISIBLE);
569 revealView.setAlpha(1f);
570 revealView.setTranslationY(0);
571 layerViews.put(revealView, BUILD_AND_SET_LAYER);
572 pCb.onRevealViewVisible(revealView, contentView, allAppsButtonView);
573
574 // Calculate the final animation values
575 final float revealViewToXDrift;
576 final float revealViewToYDrift;
577 if (material) {
578 revealViewToYDrift = pCb.getMaterialRevealViewFinalYDrift(revealView);
579 revealViewToXDrift = pCb.getMaterialRevealViewFinalXDrift(revealView);
580 } else {
581 revealViewToYDrift = 2 * height / 3;
582 revealViewToXDrift = 0;
583 }
584
585 // The vertical motion of the apps panel should be delayed by one frame
586 // from the conceal animation in order to give the right feel. We correspondingly
587 // shorten the duration so that the slide and conceal end at the same time.
588 TimeInterpolator decelerateInterpolator = material ?
589 new LogDecelerateInterpolator(100, 0) :
590 new DecelerateInterpolator(1f);
591 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
592 0, revealViewToYDrift);
593 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
594 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
595 panelDriftY.setInterpolator(decelerateInterpolator);
596 mStateAnimation.play(panelDriftY);
597
598 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
599 0, revealViewToXDrift);
600 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
601 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
602 panelDriftX.setInterpolator(decelerateInterpolator);
603 mStateAnimation.play(panelDriftX);
604
605 // Setup animation for the reveal panel alpha
606 final float revealViewToAlpha = !material ? 0f :
607 pCb.getMaterialRevealViewFinalAlpha(revealView);
608 if (revealViewToAlpha != 1f) {
609 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
610 1f, revealViewToAlpha);
611 panelAlpha.setDuration(material ? revealDuration : 150);
612 panelAlpha.setStartDelay(material ? 0 : itemsAlphaStagger + SINGLE_FRAME_DELAY);
613 panelAlpha.setInterpolator(decelerateInterpolator);
614 mStateAnimation.play(panelAlpha);
615 }
616
617 // Setup the animation for the content view
618 layerViews.put(contentView, BUILD_AND_SET_LAYER);
619
620 // Create the individual animators
621 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(contentView, "translationY",
622 0, revealViewToYDrift);
623 contentView.setTranslationY(0);
624 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
625 pageDrift.setInterpolator(decelerateInterpolator);
626 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
627 mStateAnimation.play(pageDrift);
628
629 contentView.setAlpha(1f);
630 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(contentView, "alpha", 1f, 0f);
631 itemsAlpha.setDuration(100);
632 itemsAlpha.setInterpolator(decelerateInterpolator);
633 mStateAnimation.play(itemsAlpha);
634
Winson Chungb745afb2015-03-02 11:51:23 -0800635 if (material) {
636 // Animate the all apps button
637 float finalRadius = pCb.getMaterialRevealViewStartFinalRadius();
638 AnimatorListenerAdapter listener =
639 pCb.getMaterialRevealViewAnimatorListener(revealView, allAppsButtonView);
640 Animator reveal =
641 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
642 height / 2, revealRadius, finalRadius);
643 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
644 reveal.setDuration(revealDuration);
645 reveal.setStartDelay(itemsAlphaStagger);
646 if (listener != null) {
647 reveal.addListener(listener);
648 }
649 mStateAnimation.play(reveal);
650 }
651
652 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
653 dispatchOnLauncherTransitionPrepare(toView, animated, true);
654 }
655
656 mStateAnimation.addListener(new AnimatorListenerAdapter() {
657 @Override
658 public void onAnimationEnd(Animator animation) {
659 fromView.setVisibility(View.GONE);
660 dispatchOnLauncherTransitionEnd(fromView, animated, true);
661 dispatchOnLauncherTransitionEnd(toView, animated, true);
662
663 // Run any queued runnables
664 if (onCompleteRunnable != null) {
665 onCompleteRunnable.run();
666 }
667
668 // Animation complete callback
669 pCb.onAnimationComplete(revealView, contentView, allAppsButtonView);
670
671 // Disable all necessary layers
672 for (View v : layerViews.keySet()) {
673 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
674 v.setLayerType(View.LAYER_TYPE_NONE, null);
675 }
676 }
677
678 // Reset page transforms
679 if (contentView != null) {
680 contentView.setTranslationX(0);
681 contentView.setTranslationY(0);
682 contentView.setAlpha(1);
683 }
684
685 // This can hold unnecessary references to views.
686 mStateAnimation = null;
687 }
688 });
689
690 final AnimatorSet stateAnimation = mStateAnimation;
691 final Runnable startAnimRunnable = new Runnable() {
692 public void run() {
693 // Check that mStateAnimation hasn't changed while
694 // we waited for a layout/draw pass
695 if (mStateAnimation != stateAnimation)
696 return;
697 dispatchOnLauncherTransitionStart(fromView, animated, false);
698 dispatchOnLauncherTransitionStart(toView, animated, false);
699
700 // Enable all necessary layers
Winson Chung11509ad2015-05-12 18:55:26 -0700701 boolean isLmpOrAbove = Utilities.isLmpOrAbove();
Winson Chungb745afb2015-03-02 11:51:23 -0800702 for (View v : layerViews.keySet()) {
703 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
704 v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
705 }
Winson Chung11509ad2015-05-12 18:55:26 -0700706 if (isLmpOrAbove && Utilities.isViewAttachedToWindow(v)) {
Winson Chungb745afb2015-03-02 11:51:23 -0800707 v.buildLayer();
708 }
709 }
710 mStateAnimation.start();
711 }
712 };
713 fromView.post(startAnimRunnable);
714 } else {
715 fromView.setVisibility(View.GONE);
716 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
717 dispatchOnLauncherTransitionStart(fromView, animated, true);
718 dispatchOnLauncherTransitionEnd(fromView, animated, true);
719 dispatchOnLauncherTransitionPrepare(toView, animated, true);
720 dispatchOnLauncherTransitionStart(toView, animated, true);
721 dispatchOnLauncherTransitionEnd(toView, animated, true);
722
723 // Run any queued runnables
724 if (onCompleteRunnable != null) {
725 onCompleteRunnable.run();
726 }
727 }
728 }
729
730
731 /**
732 * Dispatches the prepare-transition event to suitable views.
733 */
734 void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
735 if (v instanceof LauncherTransitionable) {
736 ((LauncherTransitionable) v).onLauncherTransitionPrepare(mLauncher, animated,
737 toWorkspace);
738 }
739 }
740
741 /**
742 * Dispatches the start-transition event to suitable views.
743 */
744 void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
745 if (v instanceof LauncherTransitionable) {
746 ((LauncherTransitionable) v).onLauncherTransitionStart(mLauncher, animated,
747 toWorkspace);
748 }
749
750 // Update the workspace transition step as well
751 dispatchOnLauncherTransitionStep(v, 0f);
752 }
753
754 /**
755 * Dispatches the step-transition event to suitable views.
756 */
757 void dispatchOnLauncherTransitionStep(View v, float t) {
758 if (v instanceof LauncherTransitionable) {
759 ((LauncherTransitionable) v).onLauncherTransitionStep(mLauncher, t);
760 }
761 }
762
763 /**
764 * Dispatches the end-transition event to suitable views.
765 */
766 void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
767 if (v instanceof LauncherTransitionable) {
768 ((LauncherTransitionable) v).onLauncherTransitionEnd(mLauncher, animated,
769 toWorkspace);
770 }
771
772 // Update the workspace transition step as well
773 dispatchOnLauncherTransitionStep(v, 1f);
774 }
775
776 /**
777 * Cancels the current animation.
778 */
779 private void cancelAnimation() {
780 if (mStateAnimation != null) {
781 mStateAnimation.setDuration(0);
782 mStateAnimation.cancel();
783 mStateAnimation = null;
784 }
785 }
786}