blob: c1d60fd0219ac1dc379284392f18e46d586c2464 [file] [log] [blame]
Tony Wickham1743ac42016-03-17 11:53:26 -07001/*
2 * Copyright (C) 2016 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
Tony Wickhamdadb3042016-02-24 11:07:00 -080017package com.android.launcher3;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ObjectAnimator;
22import android.animation.ValueAnimator;
23import android.util.Log;
24import android.view.View;
Sunny Goyaled268c22016-03-24 15:27:42 -070025import android.view.animation.LinearInterpolator;
Tony Wickhamdadb3042016-02-24 11:07:00 -080026
Jon Mirandac4de1372016-10-05 15:40:51 -070027import com.android.launcher3.userevent.nano.LauncherLogProto;
28
Tony Wickhamdadb3042016-02-24 11:07:00 -080029import static com.android.launcher3.Workspace.State.NORMAL;
30import static com.android.launcher3.Workspace.State.OVERVIEW;
31
32/**
33 * Manages the animations that play as the user pinches to/from overview mode.
34 *
35 * It will look like this pinching in:
36 * - Workspace scales down
37 * - At some threshold 1, hotseat and QSB fade out (full animation)
38 * - At a later threshold 2, panel buttons fade in and scrim fades in
39 * - At a final threshold 3, snap to overview
40 *
41 * Pinching out:
42 * - Workspace scales up
43 * - At threshold 1, panel buttons fade out
44 * - At threshold 2, hotseat and QSB fade in and scrim fades out
45 * - At threshold 3, snap to workspace
46 *
47 * @see PinchToOverviewListener
48 * @see PinchThresholdManager
49 */
50public class PinchAnimationManager {
51 private static final String TAG = "PinchAnimationManager";
52
53 private static final int THRESHOLD_ANIM_DURATION = 150;
Sunny Goyaled268c22016-03-24 15:27:42 -070054 private static final LinearInterpolator INTERPOLATOR = new LinearInterpolator();
Tony Wickhamdadb3042016-02-24 11:07:00 -080055
Sunny Goyal6178f132016-07-11 17:30:03 -070056 private static final int INDEX_HOTSEAT = 0;
57 private static final int INDEX_QSB = 1;
Tony Wickhamf898b972016-04-05 18:36:36 -070058 private static final int INDEX_OVERVIEW_PANEL_BUTTONS = 2;
59 private static final int INDEX_SCRIM = 3;
Sunny Goyaled268c22016-03-24 15:27:42 -070060
61 private final Animator[] mAnimators = new Animator[4];
62
Tony Wickhamdadb3042016-02-24 11:07:00 -080063 private Launcher mLauncher;
64 private Workspace mWorkspace;
65
66 private float mOverviewScale;
67 private float mOverviewTranslationY;
68 private int mNormalOverviewTransitionDuration;
Tony Wickhamdadb3042016-02-24 11:07:00 -080069 private boolean mIsAnimating;
70
Tony Wickhamdadb3042016-02-24 11:07:00 -080071 public PinchAnimationManager(Launcher launcher) {
72 mLauncher = launcher;
73 mWorkspace = launcher.mWorkspace;
74
75 mOverviewScale = mWorkspace.getOverviewModeShrinkFactor();
76 mOverviewTranslationY = mWorkspace.getOverviewModeTranslationY();
77 mNormalOverviewTransitionDuration = mWorkspace.getStateTransitionAnimation()
78 .mOverviewTransitionTime;
Tony Wickhamdadb3042016-02-24 11:07:00 -080079 }
80
81 public int getNormalOverviewTransitionDuration() {
82 return mNormalOverviewTransitionDuration;
83 }
84
85 /**
86 * Interpolate from {@param currentProgress} to {@param toProgress}, calling
87 * {@link #setAnimationProgress(float)} throughout the duration. If duration is -1,
88 * the default overview transition duration is used.
89 */
90 public void animateToProgress(float currentProgress, float toProgress, int duration,
91 final PinchThresholdManager thresholdManager) {
92 if (duration == -1) {
93 duration = mNormalOverviewTransitionDuration;
94 }
95 ValueAnimator animator = ValueAnimator.ofFloat(currentProgress, toProgress);
96 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
Tony Wickhamf898b972016-04-05 18:36:36 -070097 @Override
98 public void onAnimationUpdate(ValueAnimator animation) {
99 float pinchProgress = (Float) animation.getAnimatedValue();
100 setAnimationProgress(pinchProgress);
101 thresholdManager.updateAndAnimatePassedThreshold(pinchProgress,
102 PinchAnimationManager.this);
103 }
104 }
Tony Wickhamdadb3042016-02-24 11:07:00 -0800105 );
106 animator.addListener(new AnimatorListenerAdapter() {
107 @Override
108 public void onAnimationEnd(Animator animation) {
109 mIsAnimating = false;
110 thresholdManager.reset();
Sunny Goyaldb364372016-10-26 19:12:47 -0700111 mWorkspace.onEndStateTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -0800112 }
113 });
114 animator.setDuration(duration).start();
115 mIsAnimating = true;
116 }
117
118 public boolean isAnimating() {
119 return mIsAnimating;
120 }
121
122 /**
123 * Animates to the specified progress. This should be called repeatedly throughout the pinch
124 * gesture to run animations that interpolate throughout the gesture.
125 * @param interpolatedProgress The progress from 0 to 1, where 0 is overview and 1 is workspace.
126 */
127 public void setAnimationProgress(float interpolatedProgress) {
128 float interpolatedScale = interpolatedProgress * (1f - mOverviewScale) + mOverviewScale;
129 float interpolatedTranslationY = (1f - interpolatedProgress) * mOverviewTranslationY;
130 mWorkspace.setScaleX(interpolatedScale);
131 mWorkspace.setScaleY(interpolatedScale);
132 mWorkspace.setTranslationY(interpolatedTranslationY);
133 setOverviewPanelsAlpha(1f - interpolatedProgress, 0);
Tony Wickhamdadb3042016-02-24 11:07:00 -0800134 }
135
136 /**
137 * Animates certain properties based on which threshold was passed, and in what direction. The
138 * starting state must also be taken into account because the thresholds mean different things
139 * when going from workspace to overview and vice versa.
140 * @param threshold One of {@link PinchThresholdManager#THRESHOLD_ONE},
141 * {@link PinchThresholdManager#THRESHOLD_TWO}, or
142 * {@link PinchThresholdManager#THRESHOLD_THREE}
143 * @param startState {@link Workspace.State#NORMAL} or {@link Workspace.State#OVERVIEW}.
144 * @param goingTowards {@link Workspace.State#NORMAL} or {@link Workspace.State#OVERVIEW}.
Sunny Goyaled268c22016-03-24 15:27:42 -0700145 * Note that this doesn't have to be the opposite of startState;
Tony Wickhamdadb3042016-02-24 11:07:00 -0800146 */
147 public void animateThreshold(float threshold, Workspace.State startState,
148 Workspace.State goingTowards) {
149 if (threshold == PinchThresholdManager.THRESHOLD_ONE) {
150 if (startState == OVERVIEW) {
151 animateOverviewPanelButtons(goingTowards == OVERVIEW);
152 } else if (startState == NORMAL) {
Sunny Goyal857bfcf2016-07-14 15:30:24 -0700153 animateHotseatAndQsb(goingTowards == NORMAL);
Tony Wickhamdadb3042016-02-24 11:07:00 -0800154 }
155 } else if (threshold == PinchThresholdManager.THRESHOLD_TWO) {
156 if (startState == OVERVIEW) {
Sunny Goyal857bfcf2016-07-14 15:30:24 -0700157 animateHotseatAndQsb(goingTowards == NORMAL);
Tony Wickhamdadb3042016-02-24 11:07:00 -0800158 animateScrim(goingTowards == OVERVIEW);
159 } else if (startState == NORMAL) {
160 animateOverviewPanelButtons(goingTowards == OVERVIEW);
161 animateScrim(goingTowards == OVERVIEW);
162 }
163 } else if (threshold == PinchThresholdManager.THRESHOLD_THREE) {
164 // Passing threshold 3 ends the pinch and snaps to the new state.
165 if (startState == OVERVIEW && goingTowards == NORMAL) {
Jon Mirandac4de1372016-10-05 15:40:51 -0700166 mLauncher.getUserEventDispatcher().logActionOnContainer(
167 LauncherLogProto.Action.PINCH, LauncherLogProto.Action.NONE,
168 LauncherLogProto.OVERVIEW, mWorkspace.getCurrentPage());
Tony Wickhamdadb3042016-02-24 11:07:00 -0800169 mLauncher.showWorkspace(true);
Tony Wickhamf898b972016-04-05 18:36:36 -0700170 mWorkspace.snapToPage(mWorkspace.getCurrentPage());
Tony Wickhamdadb3042016-02-24 11:07:00 -0800171 } else if (startState == NORMAL && goingTowards == OVERVIEW) {
Jon Mirandac4de1372016-10-05 15:40:51 -0700172 mLauncher.getUserEventDispatcher().logActionOnContainer(
173 LauncherLogProto.Action.PINCH, LauncherLogProto.Action.NONE,
174 LauncherLogProto.WORKSPACE, mWorkspace.getCurrentPage());
Tony Wickhamdadb3042016-02-24 11:07:00 -0800175 mLauncher.showOverviewMode(true);
176 }
177 } else {
178 Log.e(TAG, "Received unknown threshold to animate: " + threshold);
179 }
180 }
181
182 private void setOverviewPanelsAlpha(float alpha, int duration) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700183 int childCount = mWorkspace.getChildCount();
184 for (int i = 0; i < childCount; i++) {
185 final CellLayout cl = (CellLayout) mWorkspace.getChildAt(i);
Tony Wickhamdadb3042016-02-24 11:07:00 -0800186 if (duration == 0) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700187 cl.setBackgroundAlpha(alpha);
Tony Wickhamdadb3042016-02-24 11:07:00 -0800188 } else {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700189 ObjectAnimator.ofFloat(cl, "backgroundAlpha", alpha).setDuration(duration).start();
Tony Wickhamdadb3042016-02-24 11:07:00 -0800190 }
191 }
192 }
193
Sunny Goyal857bfcf2016-07-14 15:30:24 -0700194 private void animateHotseatAndQsb(boolean show) {
Sunny Goyal6178f132016-07-11 17:30:03 -0700195 startAnimator(INDEX_HOTSEAT,
196 mWorkspace.createHotseatAlphaAnimator(show ? 1 : 0), THRESHOLD_ANIM_DURATION);
197 startAnimator(INDEX_QSB, mWorkspace.mQsbAlphaController.animateAlphaAtIndex(
198 show ? 1 : 0, Workspace.QSB_ALPHA_INDEX_STATE_CHANGE), THRESHOLD_ANIM_DURATION);
Tony Wickhamdadb3042016-02-24 11:07:00 -0800199 }
200
Tony Wickhamdadb3042016-02-24 11:07:00 -0800201 private void animateOverviewPanelButtons(boolean show) {
Sunny Goyaled268c22016-03-24 15:27:42 -0700202 animateShowHideView(INDEX_OVERVIEW_PANEL_BUTTONS, mLauncher.getOverviewPanel(), show);
Tony Wickhamdadb3042016-02-24 11:07:00 -0800203 }
204
205 private void animateScrim(boolean show) {
Sunny Goyaled268c22016-03-24 15:27:42 -0700206 float endValue = show ? mWorkspace.getStateTransitionAnimation().mWorkspaceScrimAlpha : 0;
207 startAnimator(INDEX_SCRIM,
208 ObjectAnimator.ofFloat(mLauncher.getDragLayer(), "backgroundAlpha", endValue),
209 mNormalOverviewTransitionDuration);
210 }
211
212 private void animateShowHideView(int index, final View view, boolean show) {
213 Animator animator = new LauncherViewPropertyAnimator(view).alpha(show ? 1 : 0).withLayer();
Tony Wickhamdadb3042016-02-24 11:07:00 -0800214 if (show) {
Sunny Goyaled268c22016-03-24 15:27:42 -0700215 view.setVisibility(View.VISIBLE);
Tony Wickhamdadb3042016-02-24 11:07:00 -0800216 } else {
Sunny Goyaled268c22016-03-24 15:27:42 -0700217 animator.addListener(new AnimatorListenerAdapter() {
218 @Override
219 public void onAnimationEnd(Animator animation) {
220 view.setVisibility(View.INVISIBLE);
221 }
222 });
Tony Wickhamdadb3042016-02-24 11:07:00 -0800223 }
Sunny Goyaled268c22016-03-24 15:27:42 -0700224 startAnimator(index, animator, THRESHOLD_ANIM_DURATION);
225 }
226
227 private void startAnimator(int index, Animator animator, long duration) {
228 if (mAnimators[index] != null) {
229 mAnimators[index].removeAllListeners();
230 mAnimators[index].cancel();
231 }
232 mAnimators[index] = animator;
233 mAnimators[index].setInterpolator(INTERPOLATOR);
234 mAnimators[index].setDuration(duration).start();
Tony Wickhamdadb3042016-02-24 11:07:00 -0800235 }
236}