blob: 285aef8c85c61e96a6e8c752ee4db6336587a391 [file] [log] [blame]
Jon Mirandaa0233f72017-06-22 18:34:45 -07001/*
2 * Copyright (C) 2017 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.folder;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ObjectAnimator;
22import android.animation.ValueAnimator;
23import android.graphics.Canvas;
24import android.graphics.Color;
25import android.graphics.Matrix;
26import android.graphics.Paint;
27import android.graphics.Path;
28import android.graphics.PorterDuff;
29import android.graphics.PorterDuffXfermode;
30import android.graphics.RadialGradient;
31import android.graphics.Region;
32import android.graphics.Shader;
33import android.support.v4.graphics.ColorUtils;
34import android.util.Property;
35import android.view.View;
36
37import com.android.launcher3.CellLayout;
38import com.android.launcher3.DeviceProfile;
39import com.android.launcher3.Launcher;
40import com.android.launcher3.LauncherAnimUtils;
41import com.android.launcher3.util.Themes;
42
43/**
44 * This object represents a FolderIcon preview background. It stores drawing / measurement
45 * information, handles drawing, and animation (accept state <--> rest state).
46 */
47public class PreviewBackground {
48
49 private static final int CONSUMPTION_ANIMATION_DURATION = 100;
50
51 private final PorterDuffXfermode mClipPorterDuffXfermode
52 = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
53 // Create a RadialGradient such that it draws a black circle and then extends with
54 // transparent. To achieve this, we keep the gradient to black for the range [0, 1) and
55 // just at the edge quickly change it to transparent.
56 private final RadialGradient mClipShader = new RadialGradient(0, 0, 1,
57 new int[] {Color.BLACK, Color.BLACK, Color.TRANSPARENT },
58 new float[] {0, 0.999f, 1},
59 Shader.TileMode.CLAMP);
60
61 private final PorterDuffXfermode mShadowPorterDuffXfermode
62 = new PorterDuffXfermode(PorterDuff.Mode.DST_OUT);
63 private RadialGradient mShadowShader = null;
64
65 private final Matrix mShaderMatrix = new Matrix();
66 private final Path mPath = new Path();
67
68 private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
69
70 float mScale = 1f;
71 private float mColorMultiplier = 1f;
72 private int mBgColor;
73 private float mStrokeWidth;
74 private int mStrokeAlpha = MAX_BG_OPACITY;
75 private int mShadowAlpha = 255;
76 private View mInvalidateDelegate;
77
78 int previewSize;
79 int basePreviewOffsetX;
80 int basePreviewOffsetY;
81
82 private CellLayout mDrawingDelegate;
83 public int delegateCellX;
84 public int delegateCellY;
85
86 // When the PreviewBackground is drawn under an icon (for creating a folder) the border
87 // should not occlude the icon
88 public boolean isClipping = true;
89
90 // Drawing / animation configurations
Jon Miranda72b5fd12017-06-25 18:06:23 -070091 private static final float ACCEPT_SCALE_FACTOR = 1.20f;
Jon Mirandaa0233f72017-06-22 18:34:45 -070092 private static final float ACCEPT_COLOR_MULTIPLIER = 1.5f;
93
94 // Expressed on a scale from 0 to 255.
95 private static final int BG_OPACITY = 160;
96 private static final int MAX_BG_OPACITY = 225;
97 private static final int SHADOW_OPACITY = 40;
98
99 private ValueAnimator mScaleAnimator;
100 private ObjectAnimator mStrokeAlphaAnimator;
101 private ObjectAnimator mShadowAnimator;
102
103 private static final Property<PreviewBackground, Integer> STROKE_ALPHA =
104 new Property<PreviewBackground, Integer>(Integer.class, "strokeAlpha") {
105 @Override
106 public Integer get(PreviewBackground previewBackground) {
107 return previewBackground.mStrokeAlpha;
108 }
109
110 @Override
111 public void set(PreviewBackground previewBackground, Integer alpha) {
112 previewBackground.mStrokeAlpha = alpha;
113 previewBackground.invalidate();
114 }
115 };
116
117 private static final Property<PreviewBackground, Integer> SHADOW_ALPHA =
118 new Property<PreviewBackground, Integer>(Integer.class, "shadowAlpha") {
119 @Override
120 public Integer get(PreviewBackground previewBackground) {
121 return previewBackground.mShadowAlpha;
122 }
123
124 @Override
125 public void set(PreviewBackground previewBackground, Integer alpha) {
126 previewBackground.mShadowAlpha = alpha;
127 previewBackground.invalidate();
128 }
129 };
130
131 public void setup(Launcher launcher, View invalidateDelegate,
132 int availableSpace, int topPadding) {
133 mInvalidateDelegate = invalidateDelegate;
134 mBgColor = Themes.getAttrColor(launcher, android.R.attr.colorPrimary);
135
136 DeviceProfile grid = launcher.getDeviceProfile();
137 final int previewSize = grid.folderIconSizePx;
138 final int previewPadding = grid.folderIconPreviewPadding;
139
140 this.previewSize = (previewSize - 2 * previewPadding);
141
142 basePreviewOffsetX = (availableSpace - this.previewSize) / 2;
143 basePreviewOffsetY = previewPadding + grid.folderBackgroundOffset + topPadding;
144
145 // Stroke width is 1dp
146 mStrokeWidth = launcher.getResources().getDisplayMetrics().density;
147
148 float radius = getScaledRadius();
149 float shadowRadius = radius + mStrokeWidth;
150 int shadowColor = Color.argb(SHADOW_OPACITY, 0, 0, 0);
151 mShadowShader = new RadialGradient(0, 0, 1,
152 new int[] {shadowColor, Color.TRANSPARENT},
153 new float[] {radius / shadowRadius, 1},
154 Shader.TileMode.CLAMP);
155
156 invalidate();
157 }
158
159 int getRadius() {
160 return previewSize / 2;
161 }
162
163 int getScaledRadius() {
164 return (int) (mScale * getRadius());
165 }
166
167 int getOffsetX() {
168 return basePreviewOffsetX - (getScaledRadius() - getRadius());
169 }
170
171 int getOffsetY() {
172 return basePreviewOffsetY - (getScaledRadius() - getRadius());
173 }
174
175 /**
176 * Returns the progress of the scale animation, where 0 means the scale is at 1f
177 * and 1 means the scale is at ACCEPT_SCALE_FACTOR.
178 */
179 float getScaleProgress() {
180 return (mScale - 1f) / (ACCEPT_SCALE_FACTOR - 1f);
181 }
182
183 void invalidate() {
184 if (mInvalidateDelegate != null) {
185 mInvalidateDelegate.invalidate();
186 }
187
188 if (mDrawingDelegate != null) {
189 mDrawingDelegate.invalidate();
190 }
191 }
192
193 void setInvalidateDelegate(View invalidateDelegate) {
194 mInvalidateDelegate = invalidateDelegate;
195 invalidate();
196 }
197
Sunny Goyale29897f2017-07-20 10:09:42 -0700198 public int getBgColor() {
199 int alpha = (int) Math.min(MAX_BG_OPACITY, BG_OPACITY * mColorMultiplier);
200 return ColorUtils.setAlphaComponent(mBgColor, alpha);
201 }
202
Sunny Goyal179249d2017-12-19 16:49:24 -0800203 public int getBadgeColor() {
204 return mBgColor;
205 }
206
Jon Mirandaa0233f72017-06-22 18:34:45 -0700207 public void drawBackground(Canvas canvas) {
208 mPaint.setStyle(Paint.Style.FILL);
Sunny Goyale29897f2017-07-20 10:09:42 -0700209 mPaint.setColor(getBgColor());
Jon Mirandaa0233f72017-06-22 18:34:45 -0700210
211 drawCircle(canvas, 0 /* deltaRadius */);
212
Sunny Goyale29897f2017-07-20 10:09:42 -0700213 drawShadow(canvas);
214 }
215
216 public void drawShadow(Canvas canvas) {
Jon Mirandaa0233f72017-06-22 18:34:45 -0700217 if (mShadowShader == null) {
218 return;
219 }
Sunny Goyale29897f2017-07-20 10:09:42 -0700220
Jon Mirandaa0233f72017-06-22 18:34:45 -0700221 float radius = getScaledRadius();
222 float shadowRadius = radius + mStrokeWidth;
Sunny Goyale29897f2017-07-20 10:09:42 -0700223 mPaint.setStyle(Paint.Style.FILL);
Jon Mirandaa0233f72017-06-22 18:34:45 -0700224 mPaint.setColor(Color.BLACK);
225 int offsetX = getOffsetX();
226 int offsetY = getOffsetY();
227 final int saveCount;
228 if (canvas.isHardwareAccelerated()) {
229 saveCount = canvas.saveLayer(offsetX - mStrokeWidth, offsetY,
230 offsetX + radius + shadowRadius, offsetY + shadowRadius + shadowRadius,
231 null, Canvas.CLIP_TO_LAYER_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
232
233 } else {
234 saveCount = canvas.save(Canvas.CLIP_SAVE_FLAG);
Sunny Goyale29897f2017-07-20 10:09:42 -0700235 canvas.clipPath(getClipPath(), Region.Op.DIFFERENCE);
Jon Mirandaa0233f72017-06-22 18:34:45 -0700236 }
237
238 mShaderMatrix.setScale(shadowRadius, shadowRadius);
239 mShaderMatrix.postTranslate(radius + offsetX, shadowRadius + offsetY);
240 mShadowShader.setLocalMatrix(mShaderMatrix);
241 mPaint.setAlpha(mShadowAlpha);
242 mPaint.setShader(mShadowShader);
243 canvas.drawPaint(mPaint);
244 mPaint.setAlpha(255);
245 mPaint.setShader(null);
246 if (canvas.isHardwareAccelerated()) {
247 mPaint.setXfermode(mShadowPorterDuffXfermode);
248 canvas.drawCircle(radius + offsetX, radius + offsetY, radius, mPaint);
249 mPaint.setXfermode(null);
250 }
251
252 canvas.restoreToCount(saveCount);
253 }
254
255 public void fadeInBackgroundShadow() {
256 if (mShadowAnimator != null) {
257 mShadowAnimator.cancel();
258 }
259 mShadowAnimator = ObjectAnimator
260 .ofInt(this, SHADOW_ALPHA, 0, 255)
261 .setDuration(100);
262 mShadowAnimator.addListener(new AnimatorListenerAdapter() {
263 @Override
264 public void onAnimationEnd(Animator animation) {
265 mShadowAnimator = null;
266 }
267 });
268 mShadowAnimator.start();
269 }
270
271 public void animateBackgroundStroke() {
272 if (mStrokeAlphaAnimator != null) {
273 mStrokeAlphaAnimator.cancel();
274 }
275 mStrokeAlphaAnimator = ObjectAnimator
276 .ofInt(this, STROKE_ALPHA, MAX_BG_OPACITY / 2, MAX_BG_OPACITY)
277 .setDuration(100);
278 mStrokeAlphaAnimator.addListener(new AnimatorListenerAdapter() {
279 @Override
280 public void onAnimationEnd(Animator animation) {
281 mStrokeAlphaAnimator = null;
282 }
283 });
284 mStrokeAlphaAnimator.start();
285 }
286
287 public void drawBackgroundStroke(Canvas canvas) {
288 mPaint.setColor(ColorUtils.setAlphaComponent(mBgColor, mStrokeAlpha));
289 mPaint.setStyle(Paint.Style.STROKE);
290 mPaint.setStrokeWidth(mStrokeWidth);
291 drawCircle(canvas, 1 /* deltaRadius */);
292 }
293
294 public void drawLeaveBehind(Canvas canvas) {
295 float originalScale = mScale;
296 mScale = 0.5f;
297
298 mPaint.setStyle(Paint.Style.FILL);
299 mPaint.setColor(Color.argb(160, 245, 245, 245));
300 drawCircle(canvas, 0 /* deltaRadius */);
301
302 mScale = originalScale;
303 }
304
305 private void drawCircle(Canvas canvas,float deltaRadius) {
306 float radius = getScaledRadius();
307 canvas.drawCircle(radius + getOffsetX(), radius + getOffsetY(),
308 radius - deltaRadius, mPaint);
309 }
310
Sunny Goyale29897f2017-07-20 10:09:42 -0700311 public Path getClipPath() {
Jon Mirandaa0233f72017-06-22 18:34:45 -0700312 mPath.reset();
313 float r = getScaledRadius();
314 mPath.addCircle(r + getOffsetX(), r + getOffsetY(), r, Path.Direction.CW);
Sunny Goyale29897f2017-07-20 10:09:42 -0700315 return mPath;
Jon Mirandaa0233f72017-06-22 18:34:45 -0700316 }
317
318 // It is the callers responsibility to save and restore the canvas layers.
319 void clipCanvasHardware(Canvas canvas) {
320 mPaint.setColor(Color.BLACK);
Sunny Goyal5247f5b2017-07-04 12:07:46 -0700321 mPaint.setStyle(Paint.Style.FILL);
Jon Mirandaa0233f72017-06-22 18:34:45 -0700322 mPaint.setXfermode(mClipPorterDuffXfermode);
323
324 float radius = getScaledRadius();
325 mShaderMatrix.setScale(radius, radius);
326 mShaderMatrix.postTranslate(radius + getOffsetX(), radius + getOffsetY());
327 mClipShader.setLocalMatrix(mShaderMatrix);
328 mPaint.setShader(mClipShader);
329 canvas.drawPaint(mPaint);
330 mPaint.setXfermode(null);
331 mPaint.setShader(null);
332 }
333
334 private void delegateDrawing(CellLayout delegate, int cellX, int cellY) {
335 if (mDrawingDelegate != delegate) {
336 delegate.addFolderBackground(this);
337 }
338
339 mDrawingDelegate = delegate;
340 delegateCellX = cellX;
341 delegateCellY = cellY;
342
343 invalidate();
344 }
345
346 private void clearDrawingDelegate() {
347 if (mDrawingDelegate != null) {
348 mDrawingDelegate.removeFolderBackground(this);
349 }
350
351 mDrawingDelegate = null;
Sunny Goyal5247f5b2017-07-04 12:07:46 -0700352 isClipping = true;
Jon Mirandaa0233f72017-06-22 18:34:45 -0700353 invalidate();
354 }
355
356 boolean drawingDelegated() {
357 return mDrawingDelegate != null;
358 }
359
360 private void animateScale(float finalScale, float finalMultiplier,
361 final Runnable onStart, final Runnable onEnd) {
362 final float scale0 = mScale;
363 final float scale1 = finalScale;
364
365 final float bgMultiplier0 = mColorMultiplier;
366 final float bgMultiplier1 = finalMultiplier;
367
368 if (mScaleAnimator != null) {
369 mScaleAnimator.cancel();
370 }
371
372 mScaleAnimator = LauncherAnimUtils.ofFloat(0f, 1.0f);
373
374 mScaleAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
375 @Override
376 public void onAnimationUpdate(ValueAnimator animation) {
377 float prog = animation.getAnimatedFraction();
378 mScale = prog * scale1 + (1 - prog) * scale0;
379 mColorMultiplier = prog * bgMultiplier1 + (1 - prog) * bgMultiplier0;
380 invalidate();
381 }
382 });
383 mScaleAnimator.addListener(new AnimatorListenerAdapter() {
384 @Override
385 public void onAnimationStart(Animator animation) {
386 if (onStart != null) {
387 onStart.run();
388 }
389 }
390
391 @Override
392 public void onAnimationEnd(Animator animation) {
393 if (onEnd != null) {
394 onEnd.run();
395 }
396 mScaleAnimator = null;
397 }
398 });
399
400 mScaleAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
401 mScaleAnimator.start();
402 }
403
404 public void animateToAccept(final CellLayout cl, final int cellX, final int cellY) {
405 Runnable onStart = new Runnable() {
406 @Override
407 public void run() {
408 delegateDrawing(cl, cellX, cellY);
409 }
410 };
411 animateScale(ACCEPT_SCALE_FACTOR, ACCEPT_COLOR_MULTIPLIER, onStart, null);
412 }
413
414 public void animateToRest() {
415 // This can be called multiple times -- we need to make sure the drawing delegate
416 // is saved and restored at the beginning of the animation, since cancelling the
417 // existing animation can clear the delgate.
418 final CellLayout cl = mDrawingDelegate;
419 final int cellX = delegateCellX;
420 final int cellY = delegateCellY;
421
422 Runnable onStart = new Runnable() {
423 @Override
424 public void run() {
425 delegateDrawing(cl, cellX, cellY);
426 }
427 };
428 Runnable onEnd = new Runnable() {
429 @Override
430 public void run() {
431 clearDrawingDelegate();
432 }
433 };
434 animateScale(1f, 1f, onStart, onEnd);
435 }
436
437 public int getBackgroundAlpha() {
438 return (int) Math.min(MAX_BG_OPACITY, BG_OPACITY * mColorMultiplier);
439 }
440
441 public float getStrokeWidth() {
442 return mStrokeWidth;
443 }
444}