summaryrefslogtreecommitdiffstats
path: root/packages/Keyguard/src/com/android/keyguard/KeyguardWidgetCarousel.java
blob: 98b31b79ae7805313695d71d6db886fe6eeebf9b (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
286
287
288
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.keyguard;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;

import java.util.ArrayList;

public class KeyguardWidgetCarousel extends KeyguardWidgetPager {

    private float mAdjacentPagesAngle;
    private static float MAX_SCROLL_PROGRESS = 1.3f;
    private static float CAMERA_DISTANCE = 10000;
    protected AnimatorSet mChildrenTransformsAnimator;
    float[] mTmpTransform = new float[3];

    public KeyguardWidgetCarousel(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public KeyguardWidgetCarousel(Context context) {
        this(context, null, 0);
    }

    public KeyguardWidgetCarousel(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mAdjacentPagesAngle = context.getResources().getInteger(R.integer.kg_carousel_angle);
    }

    protected float getMaxScrollProgress() {
        return MAX_SCROLL_PROGRESS;
    }

    public float getAlphaForPage(int screenCenter, int index, boolean showSidePages) {
        View child = getChildAt(index);
        if (child == null) return 0f;

        boolean inVisibleRange = index >= getNextPage() - 1 && index <= getNextPage() + 1;
        float scrollProgress = getScrollProgress(screenCenter, child, index);

        if (isOverScrollChild(index, scrollProgress)) {
            return 1.0f;
        } else if ((showSidePages && inVisibleRange) || index == getNextPage()) {
            scrollProgress = getBoundedScrollProgress(screenCenter, child, index);
            float alpha = 1.0f - 1.0f * Math.abs(scrollProgress / MAX_SCROLL_PROGRESS);
            return alpha;
        } else {
            return 0f;
        }
    }

    public float getOutlineAlphaForPage(int screenCenter, int index, boolean showSidePages) {
        boolean inVisibleRange = index >= getNextPage() - 1 && index <= getNextPage() + 1;
        if (inVisibleRange) {
            return super.getOutlineAlphaForPage(screenCenter, index, showSidePages);
        } else {
            return 0f;
        }
    }

    private void updatePageAlphaValues(int screenCenter) {
        if (mChildrenOutlineFadeAnimation != null) {
            mChildrenOutlineFadeAnimation.cancel();
            mChildrenOutlineFadeAnimation = null;
        }
        boolean showSidePages = mShowingInitialHints || isPageMoving();
        if (!isReordering(false)) {
            for (int i = 0; i < getChildCount(); i++) {
                KeyguardWidgetFrame child = getWidgetPageAt(i);
                if (child != null) {
                    float outlineAlpha = getOutlineAlphaForPage(screenCenter, i, showSidePages);
                    float contentAlpha = getAlphaForPage(screenCenter, i,showSidePages);
                    child.setBackgroundAlpha(outlineAlpha);
                    child.setContentAlpha(contentAlpha);
                }
            }
        }
    }

    public void showInitialPageHints() {
        mShowingInitialHints = true;
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            boolean inVisibleRange = i >= getNextPage() - 1 && i <= getNextPage() + 1;
            KeyguardWidgetFrame child = getWidgetPageAt(i);
            if (inVisibleRange) {
                child.setBackgroundAlpha(KeyguardWidgetFrame.OUTLINE_ALPHA_MULTIPLIER);
                child.setContentAlpha(1f);
            } else {
                child.setBackgroundAlpha(0f);
                child.setContentAlpha(0f);
            }
        }
    }

    @Override
    protected void screenScrolled(int screenCenter) {
        mScreenCenter = screenCenter;
        updatePageAlphaValues(screenCenter);
        if (isReordering(false)) return;
        for (int i = 0; i < getChildCount(); i++) {
            KeyguardWidgetFrame v = getWidgetPageAt(i);
            float scrollProgress = getScrollProgress(screenCenter, v, i);
            float boundedProgress = getBoundedScrollProgress(screenCenter, v, i);
            if (v == mDragView || v == null) continue;
            v.setCameraDistance(CAMERA_DISTANCE);

            if (isOverScrollChild(i, scrollProgress)) {
                v.setRotationY(- OVERSCROLL_MAX_ROTATION * scrollProgress);
                v.setOverScrollAmount(Math.abs(scrollProgress), scrollProgress < 0);
            } else {
                int width = v.getMeasuredWidth();
                float pivotX = (width / 2f) + boundedProgress * (width / 2f);
                float pivotY = v.getMeasuredHeight() / 2;
                float rotationY = - mAdjacentPagesAngle * boundedProgress;
                v.setPivotX(pivotX);
                v.setPivotY(pivotY);
                v.setRotationY(rotationY);
                v.setOverScrollAmount(0f, false);
            }
            float alpha = v.getAlpha();
            // If the view has 0 alpha, we set it to be invisible so as to prevent
            // it from accepting touches
            if (alpha == 0) {
                v.setVisibility(INVISIBLE);
            } else if (v.getVisibility() != VISIBLE) {
                v.setVisibility(VISIBLE);
            }
        }
    }

    void animatePagesToNeutral() {
        if (mChildrenTransformsAnimator != null) {
            mChildrenTransformsAnimator.cancel();
            mChildrenTransformsAnimator = null;
        }

        int count = getChildCount();
        PropertyValuesHolder alpha;
        PropertyValuesHolder outlineAlpha;
        PropertyValuesHolder rotationY;
        ArrayList<Animator> anims = new ArrayList<Animator>();

        for (int i = 0; i < count; i++) {
            KeyguardWidgetFrame child = getWidgetPageAt(i);
            boolean inVisibleRange = (i >= mCurrentPage - 1 && i <= mCurrentPage + 1);
            if (!inVisibleRange) {
                child.setRotationY(0f);
            }
            alpha = PropertyValuesHolder.ofFloat("contentAlpha", 1.0f);
            outlineAlpha = PropertyValuesHolder.ofFloat("backgroundAlpha",
                    KeyguardWidgetFrame.OUTLINE_ALPHA_MULTIPLIER);
            rotationY = PropertyValuesHolder.ofFloat("rotationY", 0f);
            ObjectAnimator a = ObjectAnimator.ofPropertyValuesHolder(child, alpha, outlineAlpha, rotationY);
            child.setVisibility(VISIBLE);
            if (!inVisibleRange) {
                a.setInterpolator(mSlowFadeInterpolator);
            }
            anims.add(a);
        }

        int duration = REORDERING_ZOOM_IN_OUT_DURATION;
        mChildrenTransformsAnimator = new AnimatorSet();
        mChildrenTransformsAnimator.playTogether(anims);

        mChildrenTransformsAnimator.setDuration(duration);
        mChildrenTransformsAnimator.start();
    }

    private void getTransformForPage(int screenCenter, int index, float[] transform) {
        View child = getChildAt(index);
        float boundedProgress = getBoundedScrollProgress(screenCenter, child, index);
        float rotationY = - mAdjacentPagesAngle * boundedProgress;
        int width = child.getMeasuredWidth();
        float pivotX = (width / 2f) + boundedProgress * (width / 2f);
        float pivotY = child.getMeasuredHeight() / 2;

        transform[0] = pivotX;
        transform[1] = pivotY;
        transform[2] = rotationY;
    }

    Interpolator mFastFadeInterpolator = new Interpolator() {
        Interpolator mInternal = new DecelerateInterpolator(1.5f);
        float mFactor = 2.5f;
        @Override
        public float getInterpolation(float input) {
            return mInternal.getInterpolation(Math.min(mFactor * input, 1f));
        }
    };

    Interpolator mSlowFadeInterpolator = new Interpolator() {
        Interpolator mInternal = new AccelerateInterpolator(1.5f);
        float mFactor = 1.3f;
        @Override
        public float getInterpolation(float input) {
            input -= (1 - 1 / mFactor);
            input = mFactor * Math.max(input, 0f);
            return mInternal.getInterpolation(input);
        }
    };

    void animatePagesToCarousel() {
        if (mChildrenTransformsAnimator != null) {
            mChildrenTransformsAnimator.cancel();
            mChildrenTransformsAnimator = null;
        }

        int count = getChildCount();
        PropertyValuesHolder alpha;
        PropertyValuesHolder outlineAlpha;
        PropertyValuesHolder rotationY;
        PropertyValuesHolder pivotX;
        PropertyValuesHolder pivotY;
        ArrayList<Animator> anims = new ArrayList<Animator>();

        for (int i = 0; i < count; i++) {
            KeyguardWidgetFrame child = getWidgetPageAt(i);
            float finalAlpha = getAlphaForPage(mScreenCenter, i, true);
            float finalOutlineAlpha = getOutlineAlphaForPage(mScreenCenter, i, true);
            getTransformForPage(mScreenCenter, i, mTmpTransform);

            boolean inVisibleRange = (i >= mCurrentPage - 1 && i <= mCurrentPage + 1);

            ObjectAnimator a;
            alpha = PropertyValuesHolder.ofFloat("contentAlpha", finalAlpha);
            outlineAlpha = PropertyValuesHolder.ofFloat("backgroundAlpha", finalOutlineAlpha);
            pivotX = PropertyValuesHolder.ofFloat("pivotX", mTmpTransform[0]);
            pivotY = PropertyValuesHolder.ofFloat("pivotY", mTmpTransform[1]);
            rotationY = PropertyValuesHolder.ofFloat("rotationY", mTmpTransform[2]);

            if (inVisibleRange) {
                // for the central pages we animate into a rotated state
                a = ObjectAnimator.ofPropertyValuesHolder(child, alpha, outlineAlpha,
                        pivotX, pivotY, rotationY);
            } else {
                a = ObjectAnimator.ofPropertyValuesHolder(child, alpha, outlineAlpha);
                a.setInterpolator(mFastFadeInterpolator);
            }
            anims.add(a);
        }

        int duration = REORDERING_ZOOM_IN_OUT_DURATION;
        mChildrenTransformsAnimator = new AnimatorSet();
        mChildrenTransformsAnimator.playTogether(anims);

        mChildrenTransformsAnimator.setDuration(duration);
        mChildrenTransformsAnimator.start();
    }

    protected void reorderStarting() {
        mViewStateManager.fadeOutSecurity(REORDERING_ZOOM_IN_OUT_DURATION);
        animatePagesToNeutral();
    }

    protected boolean zoomIn(final Runnable onCompleteRunnable) {
        animatePagesToCarousel();
        return super.zoomIn(onCompleteRunnable);
    }

    @Override
    protected void onEndReordering() {
        super.onEndReordering();
        mViewStateManager.fadeInSecurity(REORDERING_ZOOM_IN_OUT_DURATION);
    }
}