summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/PinchAnimationManager.java
blob: 3302cb65275e58899ec4d1d874a74a2b3312ac1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package com.android.launcher3;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.util.Log;
import android.view.View;

import static com.android.launcher3.Workspace.State.NORMAL;
import static com.android.launcher3.Workspace.State.OVERVIEW;

/**
 * Manages the animations that play as the user pinches to/from overview mode.
 *
 *  It will look like this pinching in:
 * - Workspace scales down
 * - At some threshold 1, hotseat and QSB fade out (full animation)
 * - At a later threshold 2, panel buttons fade in and scrim fades in
 * - At a final threshold 3, snap to overview
 *
 * Pinching out:
 * - Workspace scales up
 * - At threshold 1, panel buttons fade out
 * - At threshold 2, hotseat and QSB fade in and scrim fades out
 * - At threshold 3, snap to workspace
 *
 * @see PinchToOverviewListener
 * @see PinchThresholdManager
 */
public class PinchAnimationManager {
    private static final String TAG = "PinchAnimationManager";

    private static final int THRESHOLD_ANIM_DURATION = 150;

    private Launcher mLauncher;
    private Workspace mWorkspace;

    private float mOverviewScale;
    private float mOverviewTranslationY;
    private int mNormalOverviewTransitionDuration;
    private final int[] mVisiblePageRange = new int[2];
    private boolean mIsAnimating;

    // Animators
    private Animator mShowPageIndicatorAnimator;
    private Animator mShowHotseatAnimator;
    private Animator mShowOverviewPanelButtonsAnimator;
    private Animator mShowScrimAnimator;
    private Animator mHidePageIndicatorAnimator;
    private Animator mHideHotseatAnimator;
    private Animator mHideOverviewPanelButtonsAnimator;
    private Animator mHideScrimAnimator;

    public PinchAnimationManager(Launcher launcher) {
        mLauncher = launcher;
        mWorkspace = launcher.mWorkspace;

        mOverviewScale = mWorkspace.getOverviewModeShrinkFactor();
        mOverviewTranslationY = mWorkspace.getOverviewModeTranslationY();
        mNormalOverviewTransitionDuration = mWorkspace.getStateTransitionAnimation()
                .mOverviewTransitionTime;

        initializeAnimators();
    }

    private void initializeAnimators() {
        mShowPageIndicatorAnimator = new LauncherViewPropertyAnimator(
                mWorkspace.getPageIndicator()).alpha(1f).withLayer();
        mShowPageIndicatorAnimator.setInterpolator(null);

        mShowHotseatAnimator = new LauncherViewPropertyAnimator(mLauncher.getHotseat())
                .alpha(1f).withLayer();
        mShowHotseatAnimator.setInterpolator(null);

        mShowOverviewPanelButtonsAnimator = new LauncherViewPropertyAnimator(
                mLauncher.getOverviewPanel()).alpha(1f).withLayer();
        mShowOverviewPanelButtonsAnimator.setInterpolator(null);

        mShowScrimAnimator = ObjectAnimator.ofFloat(mLauncher.getDragLayer(), "backgroundAlpha",
                mWorkspace.getStateTransitionAnimation().mWorkspaceScrimAlpha);
        mShowScrimAnimator.setInterpolator(null);

        mHidePageIndicatorAnimator = new LauncherViewPropertyAnimator(
                mWorkspace.getPageIndicator()).alpha(0f).withLayer();
        mHidePageIndicatorAnimator.setInterpolator(null);
        mHidePageIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (mWorkspace.getPageIndicator() != null) {
                    mWorkspace.getPageIndicator().setVisibility(View.INVISIBLE);
                }
            }
        });

        mHideHotseatAnimator = new LauncherViewPropertyAnimator(mLauncher.getHotseat())
                .alpha(0f).withLayer();
        mHideHotseatAnimator.setInterpolator(null);
        mHideHotseatAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mLauncher.getHotseat().setVisibility(View.INVISIBLE);
            }
        });

        mHideOverviewPanelButtonsAnimator = new LauncherViewPropertyAnimator(
                mLauncher.getOverviewPanel()).alpha(0f).withLayer();
        mHideOverviewPanelButtonsAnimator.setInterpolator(null);
        mHideOverviewPanelButtonsAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mLauncher.getOverviewPanel().setVisibility(View.INVISIBLE);
            }
        });

        mHideScrimAnimator = ObjectAnimator.ofFloat(mLauncher.getDragLayer(), "backgroundAlpha", 0f);
        mHideScrimAnimator.setInterpolator(null);
    }

    public int getNormalOverviewTransitionDuration() {
        return mNormalOverviewTransitionDuration;
    }

    /**
     * Interpolate from {@param currentProgress} to {@param toProgress}, calling
     * {@link #setAnimationProgress(float)} throughout the duration. If duration is -1,
     * the default overview transition duration is used.
     */
    public void animateToProgress(float currentProgress, float toProgress, int duration,
            final PinchThresholdManager thresholdManager) {
        if (duration == -1) {
            duration = mNormalOverviewTransitionDuration;
        }
        ValueAnimator animator = ValueAnimator.ofFloat(currentProgress, toProgress);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
               @Override
               public void onAnimationUpdate(ValueAnimator animation) {
                   float pinchProgress = (Float) animation.getAnimatedValue();
                   setAnimationProgress(pinchProgress);
                   thresholdManager.updateAndAnimatePassedThreshold(pinchProgress,
                           PinchAnimationManager.this);
               }
           }
        );
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mIsAnimating = false;
                thresholdManager.reset();
            }
        });
        animator.setDuration(duration).start();
        mIsAnimating = true;
    }

    public boolean isAnimating() {
        return mIsAnimating;
    }

    /**
     * Animates to the specified progress. This should be called repeatedly throughout the pinch
     * gesture to run animations that interpolate throughout the gesture.
     * @param interpolatedProgress The progress from 0 to 1, where 0 is overview and 1 is workspace.
     */
    public void setAnimationProgress(float interpolatedProgress) {
        float interpolatedScale = interpolatedProgress * (1f - mOverviewScale) + mOverviewScale;
        float interpolatedTranslationY = (1f - interpolatedProgress) * mOverviewTranslationY;
        mWorkspace.setScaleX(interpolatedScale);
        mWorkspace.setScaleY(interpolatedScale);
        mWorkspace.setTranslationY(interpolatedTranslationY);
        setOverviewPanelsAlpha(1f - interpolatedProgress, 0);

        // Make sure adjacent pages, except custom content page, are visible while scaling.
        mWorkspace.setCustomContentVisibility(View.INVISIBLE);
        mWorkspace.invalidate();
    }

    /**
     * Animates certain properties based on which threshold was passed, and in what direction. The
     * starting state must also be taken into account because the thresholds mean different things
     * when going from workspace to overview and vice versa.
     * @param threshold One of {@link PinchThresholdManager#THRESHOLD_ONE},
     *                  {@link PinchThresholdManager#THRESHOLD_TWO}, or
     *                  {@link PinchThresholdManager#THRESHOLD_THREE}
     * @param startState {@link Workspace.State#NORMAL} or {@link Workspace.State#OVERVIEW}.
     * @param goingTowards {@link Workspace.State#NORMAL} or {@link Workspace.State#OVERVIEW}.
 *                     Note that this doesn't have to be the opposite of startState;
     */
    public void animateThreshold(float threshold, Workspace.State startState,
            Workspace.State goingTowards) {
        if (threshold == PinchThresholdManager.THRESHOLD_ONE) {
            if (startState == OVERVIEW) {
                animateOverviewPanelButtons(goingTowards == OVERVIEW);
            } else if (startState == NORMAL) {
                animateHotseatAndPageIndicator(goingTowards == NORMAL);
                animateQsb(goingTowards == NORMAL);
            }
        } else if (threshold == PinchThresholdManager.THRESHOLD_TWO) {
            if (startState == OVERVIEW) {
                animateHotseatAndPageIndicator(goingTowards == NORMAL);
                animateQsb(goingTowards == NORMAL);
                animateScrim(goingTowards == OVERVIEW);
            } else if (startState == NORMAL) {
                animateOverviewPanelButtons(goingTowards == OVERVIEW);
                animateScrim(goingTowards == OVERVIEW);
            }
        } else if (threshold == PinchThresholdManager.THRESHOLD_THREE) {
            // Passing threshold 3 ends the pinch and snaps to the new state.
            if (startState == OVERVIEW && goingTowards == NORMAL) {
                mLauncher.showWorkspace(true);
                mWorkspace.snapToPage(mWorkspace.getPageNearestToCenterOfScreen());
            } else if (startState == NORMAL && goingTowards == OVERVIEW) {
                mLauncher.showOverviewMode(true);
            }
        } else {
            Log.e(TAG, "Received unknown threshold to animate: " + threshold);
        }
    }

    private void setOverviewPanelsAlpha(float alpha, int duration) {
        mWorkspace.getVisiblePages(mVisiblePageRange);
        for (int i = mVisiblePageRange[0]; i <= mVisiblePageRange[1]; i++) {
            View page = mWorkspace.getPageAt(i);
            if (!mWorkspace.shouldDrawChild(page)) {
                continue;
            }
            if (duration == 0) {
                ((CellLayout) page).setBackgroundAlpha(alpha);
            } else {
                ObjectAnimator.ofFloat(page, "backgroundAlpha", alpha)
                        .setDuration(duration).start();
            }
        }
    }

    private void animateHotseatAndPageIndicator(boolean show) {
        if (show) {
            mLauncher.getHotseat().setVisibility(View.VISIBLE);
            mShowHotseatAnimator.setDuration(THRESHOLD_ANIM_DURATION).start();
            if (mWorkspace.getPageIndicator() != null) {
                // There aren't page indicators in landscape mode on phones, hence the null check.
                mWorkspace.getPageIndicator().setVisibility(View.VISIBLE);
                mShowPageIndicatorAnimator.setDuration(THRESHOLD_ANIM_DURATION).start();
            }
        } else {
            mHideHotseatAnimator.setDuration(THRESHOLD_ANIM_DURATION).start();
            if (mWorkspace.getPageIndicator() != null) {
                // There aren't page indicators in landscape mode on phones, hence the null check.
                mHidePageIndicatorAnimator.setDuration(THRESHOLD_ANIM_DURATION).start();
            }
        }
    }

    private void animateQsb(boolean show) {
        SearchDropTargetBar.State searchBarState = show ? SearchDropTargetBar.State.SEARCH_BAR
                : SearchDropTargetBar.State.INVISIBLE;
        mLauncher.getSearchDropTargetBar().animateToState(searchBarState, THRESHOLD_ANIM_DURATION);
    }

    private void animateOverviewPanelButtons(boolean show) {
        if (show) {
            mLauncher.getOverviewPanel().setVisibility(View.VISIBLE);
            mShowOverviewPanelButtonsAnimator.setDuration(THRESHOLD_ANIM_DURATION).start();
        } else {
            mHideOverviewPanelButtonsAnimator.setDuration(THRESHOLD_ANIM_DURATION).start();
        }
    }

    private void animateScrim(boolean show) {
        // We reninitialize the animators here so that they have the correct start values.
        if (show) {
            mShowScrimAnimator = ObjectAnimator.ofFloat(mLauncher.getDragLayer(), "backgroundAlpha",
                    mWorkspace.getStateTransitionAnimation().mWorkspaceScrimAlpha);
            mShowScrimAnimator.setInterpolator(null);
            mShowScrimAnimator.setDuration(mNormalOverviewTransitionDuration).start();
        } else {
            mHideScrimAnimator.setupStartValues();
            mHideScrimAnimator = ObjectAnimator.ofFloat(mLauncher.getDragLayer(), "backgroundAlpha",
                    0f);
            mHideScrimAnimator.setInterpolator(null);
            mHideScrimAnimator.setDuration(mNormalOverviewTransitionDuration).start();
            mHideScrimAnimator.setDuration(mNormalOverviewTransitionDuration).start();
        }
    }
}