1/*
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.util;
18
19import android.graphics.Canvas;
20import android.graphics.Paint;
21import android.graphics.Rect;
22import android.view.animation.AnimationUtils;
23import android.view.animation.DecelerateInterpolator;
24import android.view.animation.Interpolator;
25
26/**
27 * This class differs from the framework {@link android.widget.EdgeEffect}:
28 *   1) It does not use PorterDuffXfermode
29 *   2) The width to radius factor is smaller (0.5 instead of 0.75)
30 */
31public class LauncherEdgeEffect {
32
33    // Time it will take the effect to fully recede in ms
34    private static final int RECEDE_TIME = 600;
35
36    // Time it will take before a pulled glow begins receding in ms
37    private static final int PULL_TIME = 167;
38
39    // Time it will take in ms for a pulled glow to decay to partial strength before release
40    private static final int PULL_DECAY_TIME = 2000;
41
42    private static final float MAX_ALPHA = 0.5f;
43
44    private static final float MAX_GLOW_SCALE = 2.f;
45
46    private static final float PULL_GLOW_BEGIN = 0.f;
47
48    // Minimum velocity that will be absorbed
49    private static final int MIN_VELOCITY = 100;
50    // Maximum velocity, clamps at this value
51    private static final int MAX_VELOCITY = 10000;
52
53    private static final float EPSILON = 0.001f;
54
55    private static final double ANGLE = Math.PI / 6;
56    private static final float SIN = (float) Math.sin(ANGLE);
57    private static final float COS = (float) Math.cos(ANGLE);
58
59    private float mGlowAlpha;
60    private float mGlowScaleY;
61
62    private float mGlowAlphaStart;
63    private float mGlowAlphaFinish;
64    private float mGlowScaleYStart;
65    private float mGlowScaleYFinish;
66
67    private long mStartTime;
68    private float mDuration;
69
70    private final Interpolator mInterpolator;
71
72    private static final int STATE_IDLE = 0;
73    private static final int STATE_PULL = 1;
74    private static final int STATE_ABSORB = 2;
75    private static final int STATE_RECEDE = 3;
76    private static final int STATE_PULL_DECAY = 4;
77
78    private static final float PULL_DISTANCE_ALPHA_GLOW_FACTOR = 0.8f;
79
80    private static final int VELOCITY_GLOW_FACTOR = 6;
81
82    private int mState = STATE_IDLE;
83
84    private float mPullDistance;
85
86    private final Rect mBounds = new Rect();
87    private final Paint mPaint = new Paint();
88    private float mRadius;
89    private float mBaseGlowScale;
90    private float mDisplacement = 0.5f;
91    private float mTargetDisplacement = 0.5f;
92
93    /**
94     * Construct a new EdgeEffect with a theme appropriate for the provided context.
95     */
96    public LauncherEdgeEffect() {
97        mPaint.setAntiAlias(true);
98        mPaint.setStyle(Paint.Style.FILL);
99        mInterpolator = new DecelerateInterpolator();
100    }
101
102    /**
103     * Set the size of this edge effect in pixels.
104     *
105     * @param width Effect width in pixels
106     * @param height Effect height in pixels
107     */
108    public void setSize(int width, int height) {
109        final float r = width * 0.5f / SIN;
110        final float y = COS * r;
111        final float h = r - y;
112        final float or = height * 0.75f / SIN;
113        final float oy = COS * or;
114        final float oh = or - oy;
115
116        mRadius = r;
117        mBaseGlowScale = h > 0 ? Math.min(oh / h, 1.f) : 1.f;
118
119        mBounds.set(mBounds.left, mBounds.top, width, (int) Math.min(height, h));
120    }
121
122    /**
123     * Reports if this EdgeEffect's animation is finished. If this method returns false
124     * after a call to {@link #draw(Canvas)} the host widget should schedule another
125     * drawing pass to continue the animation.
126     *
127     * @return true if animation is finished, false if drawing should continue on the next frame.
128     */
129    public boolean isFinished() {
130        return mState == STATE_IDLE;
131    }
132
133    /**
134     * Immediately finish the current animation.
135     * After this call {@link #isFinished()} will return true.
136     */
137    public void finish() {
138        mState = STATE_IDLE;
139    }
140
141    /**
142     * A view should call this when content is pulled away from an edge by the user.
143     * This will update the state of the current visual effect and its associated animation.
144     * The host view should always {@link android.view.View#invalidate()} after this
145     * and draw the results accordingly.
146     *
147     * <p>Views using EdgeEffect should favor {@link #onPull(float, float)} when the displacement
148     * of the pull point is known.</p>
149     *
150     * @param deltaDistance Change in distance since the last call. Values may be 0 (no change) to
151     *                      1.f (full length of the view) or negative values to express change
152     *                      back toward the edge reached to initiate the effect.
153     */
154    public void onPull(float deltaDistance) {
155        onPull(deltaDistance, 0.5f);
156    }
157
158    /**
159     * A view should call this when content is pulled away from an edge by the user.
160     * This will update the state of the current visual effect and its associated animation.
161     * The host view should always {@link android.view.View#invalidate()} after this
162     * and draw the results accordingly.
163     *
164     * @param deltaDistance Change in distance since the last call. Values may be 0 (no change) to
165     *                      1.f (full length of the view) or negative values to express change
166     *                      back toward the edge reached to initiate the effect.
167     * @param displacement The displacement from the starting side of the effect of the point
168     *                     initiating the pull. In the case of touch this is the finger position.
169     *                     Values may be from 0-1.
170     */
171    public void onPull(float deltaDistance, float displacement) {
172        final long now = AnimationUtils.currentAnimationTimeMillis();
173        mTargetDisplacement = displacement;
174        if (mState == STATE_PULL_DECAY && now - mStartTime < mDuration) {
175            return;
176        }
177        if (mState != STATE_PULL) {
178            mGlowScaleY = Math.max(PULL_GLOW_BEGIN, mGlowScaleY);
179        }
180        mState = STATE_PULL;
181
182        mStartTime = now;
183        mDuration = PULL_TIME;
184
185        mPullDistance += deltaDistance;
186
187        final float absdd = Math.abs(deltaDistance);
188        mGlowAlpha = mGlowAlphaStart = Math.min(MAX_ALPHA,
189                mGlowAlpha + (absdd * PULL_DISTANCE_ALPHA_GLOW_FACTOR));
190
191        if (mPullDistance == 0) {
192            mGlowScaleY = mGlowScaleYStart = 0;
193        } else {
194            final float scale = (float) (Math.max(0, 1 - 1 /
195                    Math.sqrt(Math.abs(mPullDistance) * mBounds.height()) - 0.3d) / 0.7d);
196
197            mGlowScaleY = mGlowScaleYStart = scale;
198        }
199
200        mGlowAlphaFinish = mGlowAlpha;
201        mGlowScaleYFinish = mGlowScaleY;
202    }
203
204    /**
205     * Call when the object is released after being pulled.
206     * This will begin the "decay" phase of the effect. After calling this method
207     * the host view should {@link android.view.View#invalidate()} and thereby
208     * draw the results accordingly.
209     */
210    public void onRelease() {
211        mPullDistance = 0;
212
213        if (mState != STATE_PULL && mState != STATE_PULL_DECAY) {
214            return;
215        }
216
217        mState = STATE_RECEDE;
218        mGlowAlphaStart = mGlowAlpha;
219        mGlowScaleYStart = mGlowScaleY;
220
221        mGlowAlphaFinish = 0.f;
222        mGlowScaleYFinish = 0.f;
223
224        mStartTime = AnimationUtils.currentAnimationTimeMillis();
225        mDuration = RECEDE_TIME;
226    }
227
228    /**
229     * Call when the effect absorbs an impact at the given velocity.
230     * Used when a fling reaches the scroll boundary.
231     *
232     * <p>When using a {@link android.widget.Scroller} or {@link android.widget.OverScroller},
233     * the method <code>getCurrVelocity</code> will provide a reasonable approximation
234     * to use here.</p>
235     *
236     * @param velocity Velocity at impact in pixels per second.
237     */
238    public void onAbsorb(int velocity) {
239        mState = STATE_ABSORB;
240        velocity = Math.min(Math.max(MIN_VELOCITY, Math.abs(velocity)), MAX_VELOCITY);
241
242        mStartTime = AnimationUtils.currentAnimationTimeMillis();
243        mDuration = 0.15f + (velocity * 0.02f);
244
245        // The glow depends more on the velocity, and therefore starts out
246        // nearly invisible.
247        mGlowAlphaStart = 0.3f;
248        mGlowScaleYStart = Math.max(mGlowScaleY, 0.f);
249
250
251        // Growth for the size of the glow should be quadratic to properly
252        // respond
253        // to a user's scrolling speed. The faster the scrolling speed, the more
254        // intense the effect should be for both the size and the saturation.
255        mGlowScaleYFinish = Math.min(0.025f + (velocity * (velocity / 100) * 0.00015f) / 2, 1.f);
256        // Alpha should change for the glow as well as size.
257        mGlowAlphaFinish = Math.max(
258                mGlowAlphaStart, Math.min(velocity * VELOCITY_GLOW_FACTOR * .00001f, MAX_ALPHA));
259        mTargetDisplacement = 0.5f;
260    }
261
262    /**
263     * Set the color of this edge effect in argb.
264     *
265     * @param color Color in argb
266     */
267    public void setColor(int color) {
268        mPaint.setColor(color);
269    }
270
271    /**
272     * Return the color of this edge effect in argb.
273     * @return The color of this edge effect in argb
274     */
275    public int getColor() {
276        return mPaint.getColor();
277    }
278
279    /**
280     * Draw into the provided canvas. Assumes that the canvas has been rotated
281     * accordingly and the size has been set. The effect will be drawn the full
282     * width of X=0 to X=width, beginning from Y=0 and extending to some factor <
283     * 1.f of height.
284     *
285     * @param canvas Canvas to draw into
286     * @return true if drawing should continue beyond this frame to continue the
287     *         animation
288     */
289    public boolean draw(Canvas canvas) {
290        update();
291
292        final float centerX = mBounds.centerX();
293        final float centerY = mBounds.height() - mRadius;
294
295        canvas.scale(1.f, Math.min(mGlowScaleY, 1.f) * mBaseGlowScale, centerX, 0);
296
297        final float displacement = Math.max(0, Math.min(mDisplacement, 1.f)) - 0.5f;
298        float translateX = mBounds.width() * displacement / 2;
299        mPaint.setAlpha((int) (0xff * mGlowAlpha));
300        canvas.drawCircle(centerX + translateX, centerY, mRadius, mPaint);
301
302        boolean oneLastFrame = false;
303        if (mState == STATE_RECEDE && mGlowScaleY == 0) {
304            mState = STATE_IDLE;
305            oneLastFrame = true;
306        }
307
308        return mState != STATE_IDLE || oneLastFrame;
309    }
310
311    /**
312     * Return the maximum height that the edge effect will be drawn at given the original
313     * {@link #setSize(int, int) input size}.
314     * @return The maximum height of the edge effect
315     */
316    public int getMaxHeight() {
317        return (int) (mBounds.height() * MAX_GLOW_SCALE + 0.5f);
318    }
319
320    private void update() {
321        final long time = AnimationUtils.currentAnimationTimeMillis();
322        final float t = Math.min((time - mStartTime) / mDuration, 1.f);
323
324        final float interp = mInterpolator.getInterpolation(t);
325
326        mGlowAlpha = mGlowAlphaStart + (mGlowAlphaFinish - mGlowAlphaStart) * interp;
327        mGlowScaleY = mGlowScaleYStart + (mGlowScaleYFinish - mGlowScaleYStart) * interp;
328        mDisplacement = (mDisplacement + mTargetDisplacement) / 2;
329
330        if (t >= 1.f - EPSILON) {
331            switch (mState) {
332                case STATE_ABSORB:
333                    mState = STATE_RECEDE;
334                    mStartTime = AnimationUtils.currentAnimationTimeMillis();
335                    mDuration = RECEDE_TIME;
336
337                    mGlowAlphaStart = mGlowAlpha;
338                    mGlowScaleYStart = mGlowScaleY;
339
340                    // After absorb, the glow should fade to nothing.
341                    mGlowAlphaFinish = 0.f;
342                    mGlowScaleYFinish = 0.f;
343                    break;
344                case STATE_PULL:
345                    mState = STATE_PULL_DECAY;
346                    mStartTime = AnimationUtils.currentAnimationTimeMillis();
347                    mDuration = PULL_DECAY_TIME;
348
349                    mGlowAlphaStart = mGlowAlpha;
350                    mGlowScaleYStart = mGlowScaleY;
351
352                    // After pull, the glow should fade to nothing.
353                    mGlowAlphaFinish = 0.f;
354                    mGlowScaleYFinish = 0.f;
355                    break;
356                case STATE_PULL_DECAY:
357                    mState = STATE_RECEDE;
358                    break;
359                case STATE_RECEDE:
360                    mState = STATE_IDLE;
361                    break;
362            }
363        }
364    }
365}
366