blob: f42d37e62b59b7a0e7e21f8e560ee50cdeba912c [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
27import static com.android.launcher3.Workspace.State.NORMAL;
28import static com.android.launcher3.Workspace.State.OVERVIEW;
29
30/**
31 * Manages the animations that play as the user pinches to/from overview mode.
32 *
33 * It will look like this pinching in:
34 * - Workspace scales down
35 * - At some threshold 1, hotseat and QSB fade out (full animation)
36 * - At a later threshold 2, panel buttons fade in and scrim fades in
37 * - At a final threshold 3, snap to overview
38 *
39 * Pinching out:
40 * - Workspace scales up
41 * - At threshold 1, panel buttons fade out
42 * - At threshold 2, hotseat and QSB fade in and scrim fades out
43 * - At threshold 3, snap to workspace
44 *
45 * @see PinchToOverviewListener
46 * @see PinchThresholdManager
47 */
48public class PinchAnimationManager {
49 private static final String TAG = "PinchAnimationManager";
50
51 private static final int THRESHOLD_ANIM_DURATION = 150;
Sunny Goyaled268c22016-03-24 15:27:42 -070052 private static final LinearInterpolator INTERPOLATOR = new LinearInterpolator();
Tony Wickhamdadb3042016-02-24 11:07:00 -080053
Sunny Goyaled268c22016-03-24 15:27:42 -070054 private static int INDEX_PAGE_INDICATOR = 0;
55 private static int INDEX_HOTSEAT = 1;
56 private static int INDEX_OVERVIEW_PANEL_BUTTONS = 2;
57 private static int INDEX_SCRIM = 3;
58
59 private final Animator[] mAnimators = new Animator[4];
60
61 private final int[] mVisiblePageRange = new int[2];
Tony Wickhamdadb3042016-02-24 11:07:00 -080062 private Launcher mLauncher;
63 private Workspace mWorkspace;
64
65 private float mOverviewScale;
66 private float mOverviewTranslationY;
67 private int mNormalOverviewTransitionDuration;
Tony Wickhamdadb3042016-02-24 11:07:00 -080068 private boolean mIsAnimating;
69
Tony Wickhamdadb3042016-02-24 11:07:00 -080070 public PinchAnimationManager(Launcher launcher) {
71 mLauncher = launcher;
72 mWorkspace = launcher.mWorkspace;
73
74 mOverviewScale = mWorkspace.getOverviewModeShrinkFactor();
75 mOverviewTranslationY = mWorkspace.getOverviewModeTranslationY();
76 mNormalOverviewTransitionDuration = mWorkspace.getStateTransitionAnimation()
77 .mOverviewTransitionTime;
Tony Wickhamdadb3042016-02-24 11:07:00 -080078 }
79
80 public int getNormalOverviewTransitionDuration() {
81 return mNormalOverviewTransitionDuration;
82 }
83
84 /**
85 * Interpolate from {@param currentProgress} to {@param toProgress}, calling
86 * {@link #setAnimationProgress(float)} throughout the duration. If duration is -1,
87 * the default overview transition duration is used.
88 */
89 public void animateToProgress(float currentProgress, float toProgress, int duration,
90 final PinchThresholdManager thresholdManager) {
91 if (duration == -1) {
92 duration = mNormalOverviewTransitionDuration;
93 }
94 ValueAnimator animator = ValueAnimator.ofFloat(currentProgress, toProgress);
95 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
96 @Override
97 public void onAnimationUpdate(ValueAnimator animation) {
98 float pinchProgress = (Float) animation.getAnimatedValue();
99 setAnimationProgress(pinchProgress);
100 thresholdManager.updateAndAnimatePassedThreshold(pinchProgress,
101 PinchAnimationManager.this);
102 }
103 }
104 );
105 animator.addListener(new AnimatorListenerAdapter() {
106 @Override
107 public void onAnimationEnd(Animator animation) {
108 mIsAnimating = false;
109 thresholdManager.reset();
110 }
111 });
112 animator.setDuration(duration).start();
113 mIsAnimating = true;
114 }
115
116 public boolean isAnimating() {
117 return mIsAnimating;
118 }
119
120 /**
121 * Animates to the specified progress. This should be called repeatedly throughout the pinch
122 * gesture to run animations that interpolate throughout the gesture.
123 * @param interpolatedProgress The progress from 0 to 1, where 0 is overview and 1 is workspace.
124 */
125 public void setAnimationProgress(float interpolatedProgress) {
126 float interpolatedScale = interpolatedProgress * (1f - mOverviewScale) + mOverviewScale;
127 float interpolatedTranslationY = (1f - interpolatedProgress) * mOverviewTranslationY;
128 mWorkspace.setScaleX(interpolatedScale);
129 mWorkspace.setScaleY(interpolatedScale);
130 mWorkspace.setTranslationY(interpolatedTranslationY);
131 setOverviewPanelsAlpha(1f - interpolatedProgress, 0);
132
133 // Make sure adjacent pages, except custom content page, are visible while scaling.
134 mWorkspace.setCustomContentVisibility(View.INVISIBLE);
135 mWorkspace.invalidate();
136 }
137
138 /**
139 * Animates certain properties based on which threshold was passed, and in what direction. The
140 * starting state must also be taken into account because the thresholds mean different things
141 * when going from workspace to overview and vice versa.
142 * @param threshold One of {@link PinchThresholdManager#THRESHOLD_ONE},
143 * {@link PinchThresholdManager#THRESHOLD_TWO}, or
144 * {@link PinchThresholdManager#THRESHOLD_THREE}
145 * @param startState {@link Workspace.State#NORMAL} or {@link Workspace.State#OVERVIEW}.
146 * @param goingTowards {@link Workspace.State#NORMAL} or {@link Workspace.State#OVERVIEW}.
Sunny Goyaled268c22016-03-24 15:27:42 -0700147 * Note that this doesn't have to be the opposite of startState;
Tony Wickhamdadb3042016-02-24 11:07:00 -0800148 */
149 public void animateThreshold(float threshold, Workspace.State startState,
150 Workspace.State goingTowards) {
151 if (threshold == PinchThresholdManager.THRESHOLD_ONE) {
152 if (startState == OVERVIEW) {
153 animateOverviewPanelButtons(goingTowards == OVERVIEW);
154 } else if (startState == NORMAL) {
155 animateHotseatAndPageIndicator(goingTowards == NORMAL);
156 animateQsb(goingTowards == NORMAL);
157 }
158 } else if (threshold == PinchThresholdManager.THRESHOLD_TWO) {
159 if (startState == OVERVIEW) {
160 animateHotseatAndPageIndicator(goingTowards == NORMAL);
161 animateQsb(goingTowards == NORMAL);
162 animateScrim(goingTowards == OVERVIEW);
163 } else if (startState == NORMAL) {
164 animateOverviewPanelButtons(goingTowards == OVERVIEW);
165 animateScrim(goingTowards == OVERVIEW);
166 }
167 } else if (threshold == PinchThresholdManager.THRESHOLD_THREE) {
168 // Passing threshold 3 ends the pinch and snaps to the new state.
169 if (startState == OVERVIEW && goingTowards == NORMAL) {
170 mLauncher.showWorkspace(true);
171 mWorkspace.snapToPage(mWorkspace.getPageNearestToCenterOfScreen());
172 } else if (startState == NORMAL && goingTowards == OVERVIEW) {
173 mLauncher.showOverviewMode(true);
174 }
175 } else {
176 Log.e(TAG, "Received unknown threshold to animate: " + threshold);
177 }
178 }
179
180 private void setOverviewPanelsAlpha(float alpha, int duration) {
181 mWorkspace.getVisiblePages(mVisiblePageRange);
182 for (int i = mVisiblePageRange[0]; i <= mVisiblePageRange[1]; i++) {
183 View page = mWorkspace.getPageAt(i);
184 if (!mWorkspace.shouldDrawChild(page)) {
185 continue;
186 }
187 if (duration == 0) {
188 ((CellLayout) page).setBackgroundAlpha(alpha);
189 } else {
190 ObjectAnimator.ofFloat(page, "backgroundAlpha", alpha)
191 .setDuration(duration).start();
192 }
193 }
194 }
195
196 private void animateHotseatAndPageIndicator(boolean show) {
Sunny Goyaled268c22016-03-24 15:27:42 -0700197 animateShowHideView(INDEX_HOTSEAT, mLauncher.getHotseat(), show);
198 if (mWorkspace.getPageIndicator() != null) {
199 // There aren't page indicators in landscape mode on phones, hence the null check.
200 animateShowHideView(INDEX_PAGE_INDICATOR, mWorkspace.getPageIndicator(), show);
Tony Wickhamdadb3042016-02-24 11:07:00 -0800201 }
202 }
203
204 private void animateQsb(boolean show) {
205 SearchDropTargetBar.State searchBarState = show ? SearchDropTargetBar.State.SEARCH_BAR
206 : SearchDropTargetBar.State.INVISIBLE;
207 mLauncher.getSearchDropTargetBar().animateToState(searchBarState, THRESHOLD_ANIM_DURATION);
208 }
209
210 private void animateOverviewPanelButtons(boolean show) {
Sunny Goyaled268c22016-03-24 15:27:42 -0700211 animateShowHideView(INDEX_OVERVIEW_PANEL_BUTTONS, mLauncher.getOverviewPanel(), show);
Tony Wickhamdadb3042016-02-24 11:07:00 -0800212 }
213
214 private void animateScrim(boolean show) {
Sunny Goyaled268c22016-03-24 15:27:42 -0700215 float endValue = show ? mWorkspace.getStateTransitionAnimation().mWorkspaceScrimAlpha : 0;
216 startAnimator(INDEX_SCRIM,
217 ObjectAnimator.ofFloat(mLauncher.getDragLayer(), "backgroundAlpha", endValue),
218 mNormalOverviewTransitionDuration);
219 }
220
221 private void animateShowHideView(int index, final View view, boolean show) {
222 Animator animator = new LauncherViewPropertyAnimator(view).alpha(show ? 1 : 0).withLayer();
Tony Wickhamdadb3042016-02-24 11:07:00 -0800223 if (show) {
Sunny Goyaled268c22016-03-24 15:27:42 -0700224 view.setVisibility(View.VISIBLE);
Tony Wickhamdadb3042016-02-24 11:07:00 -0800225 } else {
Sunny Goyaled268c22016-03-24 15:27:42 -0700226 animator.addListener(new AnimatorListenerAdapter() {
227 @Override
228 public void onAnimationEnd(Animator animation) {
229 view.setVisibility(View.INVISIBLE);
230 }
231 });
Tony Wickhamdadb3042016-02-24 11:07:00 -0800232 }
Sunny Goyaled268c22016-03-24 15:27:42 -0700233 startAnimator(index, animator, THRESHOLD_ANIM_DURATION);
234 }
235
236 private void startAnimator(int index, Animator animator, long duration) {
237 if (mAnimators[index] != null) {
238 mAnimators[index].removeAllListeners();
239 mAnimators[index].cancel();
240 }
241 mAnimators[index] = animator;
242 mAnimators[index].setInterpolator(INTERPOLATOR);
243 mAnimators[index].setDuration(duration).start();
Tony Wickhamdadb3042016-02-24 11:07:00 -0800244 }
245}