blob: b2c0ca7213195c847062544e6493d57cd4286b84 [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
Sunny Goyal905262c2019-05-03 16:50:43 -070019import static com.android.launcher3.graphics.IconShape.getShape;
Sunny Goyal066ace12018-11-05 11:08:31 -080020import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound;
Sunny Goyald0ae4922018-10-11 10:37:53 -070021
Jon Mirandaa0233f72017-06-22 18:34:45 -070022import android.animation.Animator;
23import android.animation.AnimatorListenerAdapter;
24import android.animation.ObjectAnimator;
25import android.animation.ValueAnimator;
Sunny Goyalab770a12018-11-14 15:17:26 -080026import android.content.Context;
Sunny Goyalbcadb7f2019-02-05 14:45:31 -080027import android.content.res.TypedArray;
Jon Mirandaa0233f72017-06-22 18:34:45 -070028import android.graphics.Canvas;
29import android.graphics.Color;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.Path;
33import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
35import android.graphics.RadialGradient;
Jon Miranda69b35e02019-03-04 20:16:18 -080036import android.graphics.Rect;
Jon Mirandaa0233f72017-06-22 18:34:45 -070037import android.graphics.Region;
38import android.graphics.Shader;
Jon Mirandaa0233f72017-06-22 18:34:45 -070039import android.util.Property;
40import android.view.View;
41
42import com.android.launcher3.CellLayout;
43import com.android.launcher3.DeviceProfile;
Tony05d98c22018-07-23 08:01:15 -070044import com.android.launcher3.R;
Sunny Goyalab770a12018-11-14 15:17:26 -080045import com.android.launcher3.views.ActivityContext;
Jon Mirandaa0233f72017-06-22 18:34:45 -070046
47/**
48 * This object represents a FolderIcon preview background. It stores drawing / measurement
49 * information, handles drawing, and animation (accept state <--> rest state).
50 */
51public class PreviewBackground {
52
53 private static final int CONSUMPTION_ANIMATION_DURATION = 100;
54
Jon Mirandaa0233f72017-06-22 18:34:45 -070055 private final PorterDuffXfermode mShadowPorterDuffXfermode
56 = new PorterDuffXfermode(PorterDuff.Mode.DST_OUT);
57 private RadialGradient mShadowShader = null;
58
59 private final Matrix mShaderMatrix = new Matrix();
60 private final Path mPath = new Path();
61
62 private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
63
64 float mScale = 1f;
65 private float mColorMultiplier = 1f;
66 private int mBgColor;
Sunny Goyalbcadb7f2019-02-05 14:45:31 -080067 private int mStrokeColor;
Tony Wickhamf34bee82018-12-03 18:11:39 -080068 private int mDotColor;
Jon Mirandaa0233f72017-06-22 18:34:45 -070069 private float mStrokeWidth;
70 private int mStrokeAlpha = MAX_BG_OPACITY;
71 private int mShadowAlpha = 255;
72 private View mInvalidateDelegate;
73
74 int previewSize;
75 int basePreviewOffsetX;
76 int basePreviewOffsetY;
77
78 private CellLayout mDrawingDelegate;
79 public int delegateCellX;
80 public int delegateCellY;
81
82 // When the PreviewBackground is drawn under an icon (for creating a folder) the border
83 // should not occlude the icon
84 public boolean isClipping = true;
85
86 // Drawing / animation configurations
Jon Miranda72b5fd12017-06-25 18:06:23 -070087 private static final float ACCEPT_SCALE_FACTOR = 1.20f;
Jon Mirandaa0233f72017-06-22 18:34:45 -070088 private static final float ACCEPT_COLOR_MULTIPLIER = 1.5f;
89
90 // Expressed on a scale from 0 to 255.
91 private static final int BG_OPACITY = 160;
92 private static final int MAX_BG_OPACITY = 225;
93 private static final int SHADOW_OPACITY = 40;
94
95 private ValueAnimator mScaleAnimator;
96 private ObjectAnimator mStrokeAlphaAnimator;
97 private ObjectAnimator mShadowAnimator;
98
99 private static final Property<PreviewBackground, Integer> STROKE_ALPHA =
100 new Property<PreviewBackground, Integer>(Integer.class, "strokeAlpha") {
101 @Override
102 public Integer get(PreviewBackground previewBackground) {
103 return previewBackground.mStrokeAlpha;
104 }
105
106 @Override
107 public void set(PreviewBackground previewBackground, Integer alpha) {
108 previewBackground.mStrokeAlpha = alpha;
109 previewBackground.invalidate();
110 }
111 };
112
113 private static final Property<PreviewBackground, Integer> SHADOW_ALPHA =
114 new Property<PreviewBackground, Integer>(Integer.class, "shadowAlpha") {
115 @Override
116 public Integer get(PreviewBackground previewBackground) {
117 return previewBackground.mShadowAlpha;
118 }
119
120 @Override
121 public void set(PreviewBackground previewBackground, Integer alpha) {
122 previewBackground.mShadowAlpha = alpha;
123 previewBackground.invalidate();
124 }
125 };
126
Sunny Goyalab770a12018-11-14 15:17:26 -0800127 public void setup(Context context, ActivityContext activity, View invalidateDelegate,
Jon Miranda591e3602018-03-28 11:37:00 -0700128 int availableSpaceX, int topPadding) {
Jon Mirandaa0233f72017-06-22 18:34:45 -0700129 mInvalidateDelegate = invalidateDelegate;
Sunny Goyalbcadb7f2019-02-05 14:45:31 -0800130
131 TypedArray ta = context.getTheme().obtainStyledAttributes(R.styleable.FolderIconPreview);
132 mDotColor = ta.getColor(R.styleable.FolderIconPreview_folderDotColor, 0);
133 mStrokeColor = ta.getColor(R.styleable.FolderIconPreview_folderIconBorderColor, 0);
Hyunyoung Songe751d902019-05-13 23:23:05 -0700134 mBgColor = ta.getColor(R.styleable.FolderIconPreview_folderFillColor, 0);
Sunny Goyalbcadb7f2019-02-05 14:45:31 -0800135 ta.recycle();
Jon Mirandaa0233f72017-06-22 18:34:45 -0700136
Sunny Goyalae6e3182019-04-30 12:04:37 -0700137 DeviceProfile grid = activity.getWallpaperDeviceProfile();
Jon Miranda591e3602018-03-28 11:37:00 -0700138 previewSize = grid.folderIconSizePx;
Jon Mirandaa0233f72017-06-22 18:34:45 -0700139
Jon Miranda591e3602018-03-28 11:37:00 -0700140 basePreviewOffsetX = (availableSpaceX - previewSize) / 2;
141 basePreviewOffsetY = topPadding + grid.folderIconOffsetYPx;
Jon Mirandaa0233f72017-06-22 18:34:45 -0700142
143 // Stroke width is 1dp
Sunny Goyalab770a12018-11-14 15:17:26 -0800144 mStrokeWidth = context.getResources().getDisplayMetrics().density;
Jon Mirandaa0233f72017-06-22 18:34:45 -0700145
146 float radius = getScaledRadius();
147 float shadowRadius = radius + mStrokeWidth;
148 int shadowColor = Color.argb(SHADOW_OPACITY, 0, 0, 0);
149 mShadowShader = new RadialGradient(0, 0, 1,
150 new int[] {shadowColor, Color.TRANSPARENT},
151 new float[] {radius / shadowRadius, 1},
152 Shader.TileMode.CLAMP);
153
154 invalidate();
155 }
156
Jon Miranda69b35e02019-03-04 20:16:18 -0800157 void getBounds(Rect outBounds) {
158 int top = basePreviewOffsetY;
159 int left = basePreviewOffsetX;
160 int right = left + previewSize;
161 int bottom = top + previewSize;
162 outBounds.set(left, top, right, bottom);
163 }
164
Jon Mirandaa0233f72017-06-22 18:34:45 -0700165 int getRadius() {
166 return previewSize / 2;
167 }
168
169 int getScaledRadius() {
170 return (int) (mScale * getRadius());
171 }
172
173 int getOffsetX() {
174 return basePreviewOffsetX - (getScaledRadius() - getRadius());
175 }
176
177 int getOffsetY() {
178 return basePreviewOffsetY - (getScaledRadius() - getRadius());
179 }
180
181 /**
182 * Returns the progress of the scale animation, where 0 means the scale is at 1f
183 * and 1 means the scale is at ACCEPT_SCALE_FACTOR.
184 */
185 float getScaleProgress() {
186 return (mScale - 1f) / (ACCEPT_SCALE_FACTOR - 1f);
187 }
188
189 void invalidate() {
190 if (mInvalidateDelegate != null) {
191 mInvalidateDelegate.invalidate();
192 }
193
194 if (mDrawingDelegate != null) {
195 mDrawingDelegate.invalidate();
196 }
197 }
198
199 void setInvalidateDelegate(View invalidateDelegate) {
200 mInvalidateDelegate = invalidateDelegate;
201 invalidate();
202 }
203
Sunny Goyale29897f2017-07-20 10:09:42 -0700204 public int getBgColor() {
205 int alpha = (int) Math.min(MAX_BG_OPACITY, BG_OPACITY * mColorMultiplier);
Sunny Goyal066ace12018-11-05 11:08:31 -0800206 return setColorAlphaBound(mBgColor, alpha);
Sunny Goyale29897f2017-07-20 10:09:42 -0700207 }
208
Tony Wickhamf34bee82018-12-03 18:11:39 -0800209 public int getDotColor() {
210 return mDotColor;
Sunny Goyal179249d2017-12-19 16:49:24 -0800211 }
212
Jon Mirandaa0233f72017-06-22 18:34:45 -0700213 public void drawBackground(Canvas canvas) {
214 mPaint.setStyle(Paint.Style.FILL);
Sunny Goyale29897f2017-07-20 10:09:42 -0700215 mPaint.setColor(getBgColor());
Jon Mirandaa0233f72017-06-22 18:34:45 -0700216
Sunny Goyald0ae4922018-10-11 10:37:53 -0700217 getShape().drawShape(canvas, getOffsetX(), getOffsetY(), getScaledRadius(), mPaint);
Sunny Goyale29897f2017-07-20 10:09:42 -0700218 drawShadow(canvas);
219 }
220
221 public void drawShadow(Canvas canvas) {
Jon Mirandaa0233f72017-06-22 18:34:45 -0700222 if (mShadowShader == null) {
223 return;
224 }
Sunny Goyale29897f2017-07-20 10:09:42 -0700225
Jon Mirandaa0233f72017-06-22 18:34:45 -0700226 float radius = getScaledRadius();
227 float shadowRadius = radius + mStrokeWidth;
Sunny Goyale29897f2017-07-20 10:09:42 -0700228 mPaint.setStyle(Paint.Style.FILL);
Jon Mirandaa0233f72017-06-22 18:34:45 -0700229 mPaint.setColor(Color.BLACK);
230 int offsetX = getOffsetX();
231 int offsetY = getOffsetY();
232 final int saveCount;
233 if (canvas.isHardwareAccelerated()) {
234 saveCount = canvas.saveLayer(offsetX - mStrokeWidth, offsetY,
Derek Sollenberger26a1b452018-02-23 13:37:25 -0500235 offsetX + radius + shadowRadius, offsetY + shadowRadius + shadowRadius, null);
Jon Mirandaa0233f72017-06-22 18:34:45 -0700236
237 } else {
Derek Sollenberger26a1b452018-02-23 13:37:25 -0500238 saveCount = canvas.save();
Sunny Goyale29897f2017-07-20 10:09:42 -0700239 canvas.clipPath(getClipPath(), Region.Op.DIFFERENCE);
Jon Mirandaa0233f72017-06-22 18:34:45 -0700240 }
241
242 mShaderMatrix.setScale(shadowRadius, shadowRadius);
243 mShaderMatrix.postTranslate(radius + offsetX, shadowRadius + offsetY);
244 mShadowShader.setLocalMatrix(mShaderMatrix);
245 mPaint.setAlpha(mShadowAlpha);
246 mPaint.setShader(mShadowShader);
247 canvas.drawPaint(mPaint);
248 mPaint.setAlpha(255);
249 mPaint.setShader(null);
250 if (canvas.isHardwareAccelerated()) {
251 mPaint.setXfermode(mShadowPorterDuffXfermode);
Sunny Goyald0ae4922018-10-11 10:37:53 -0700252 getShape().drawShape(canvas, offsetX, offsetY, radius, mPaint);
Jon Mirandaa0233f72017-06-22 18:34:45 -0700253 mPaint.setXfermode(null);
254 }
255
256 canvas.restoreToCount(saveCount);
257 }
258
259 public void fadeInBackgroundShadow() {
260 if (mShadowAnimator != null) {
261 mShadowAnimator.cancel();
262 }
263 mShadowAnimator = ObjectAnimator
264 .ofInt(this, SHADOW_ALPHA, 0, 255)
265 .setDuration(100);
266 mShadowAnimator.addListener(new AnimatorListenerAdapter() {
267 @Override
268 public void onAnimationEnd(Animator animation) {
269 mShadowAnimator = null;
270 }
271 });
272 mShadowAnimator.start();
273 }
274
275 public void animateBackgroundStroke() {
276 if (mStrokeAlphaAnimator != null) {
277 mStrokeAlphaAnimator.cancel();
278 }
279 mStrokeAlphaAnimator = ObjectAnimator
280 .ofInt(this, STROKE_ALPHA, MAX_BG_OPACITY / 2, MAX_BG_OPACITY)
281 .setDuration(100);
282 mStrokeAlphaAnimator.addListener(new AnimatorListenerAdapter() {
283 @Override
284 public void onAnimationEnd(Animator animation) {
285 mStrokeAlphaAnimator = null;
286 }
287 });
288 mStrokeAlphaAnimator.start();
289 }
290
291 public void drawBackgroundStroke(Canvas canvas) {
Sunny Goyalbcadb7f2019-02-05 14:45:31 -0800292 mPaint.setColor(setColorAlphaBound(mStrokeColor, mStrokeAlpha));
Jon Mirandaa0233f72017-06-22 18:34:45 -0700293 mPaint.setStyle(Paint.Style.STROKE);
294 mPaint.setStrokeWidth(mStrokeWidth);
Sunny Goyald0ae4922018-10-11 10:37:53 -0700295
296 float inset = 1f;
297 getShape().drawShape(canvas,
298 getOffsetX() + inset, getOffsetY() + inset, getScaledRadius() - inset, mPaint);
Jon Mirandaa0233f72017-06-22 18:34:45 -0700299 }
300
301 public void drawLeaveBehind(Canvas canvas) {
302 float originalScale = mScale;
303 mScale = 0.5f;
304
305 mPaint.setStyle(Paint.Style.FILL);
306 mPaint.setColor(Color.argb(160, 245, 245, 245));
Sunny Goyald0ae4922018-10-11 10:37:53 -0700307 getShape().drawShape(canvas, getOffsetX(), getOffsetY(), getScaledRadius(), mPaint);
Jon Mirandaa0233f72017-06-22 18:34:45 -0700308
309 mScale = originalScale;
310 }
311
Sunny Goyale29897f2017-07-20 10:09:42 -0700312 public Path getClipPath() {
Jon Mirandaa0233f72017-06-22 18:34:45 -0700313 mPath.reset();
Sunny Goyal905262c2019-05-03 16:50:43 -0700314 getShape().addToPath(mPath, getOffsetX(), getOffsetY(), getScaledRadius());
Sunny Goyale29897f2017-07-20 10:09:42 -0700315 return mPath;
Jon Mirandaa0233f72017-06-22 18:34:45 -0700316 }
317
Jon Mirandaa0233f72017-06-22 18:34:45 -0700318 private void delegateDrawing(CellLayout delegate, int cellX, int cellY) {
319 if (mDrawingDelegate != delegate) {
320 delegate.addFolderBackground(this);
321 }
322
323 mDrawingDelegate = delegate;
324 delegateCellX = cellX;
325 delegateCellY = cellY;
326
327 invalidate();
328 }
329
330 private void clearDrawingDelegate() {
331 if (mDrawingDelegate != null) {
332 mDrawingDelegate.removeFolderBackground(this);
333 }
334
335 mDrawingDelegate = null;
Sunny Goyal5247f5b2017-07-04 12:07:46 -0700336 isClipping = true;
Jon Mirandaa0233f72017-06-22 18:34:45 -0700337 invalidate();
338 }
339
340 boolean drawingDelegated() {
341 return mDrawingDelegate != null;
342 }
343
344 private void animateScale(float finalScale, float finalMultiplier,
345 final Runnable onStart, final Runnable onEnd) {
346 final float scale0 = mScale;
347 final float scale1 = finalScale;
348
349 final float bgMultiplier0 = mColorMultiplier;
350 final float bgMultiplier1 = finalMultiplier;
351
352 if (mScaleAnimator != null) {
353 mScaleAnimator.cancel();
354 }
355
Sunny Goyal849c6a22018-08-08 16:33:46 -0700356 mScaleAnimator = ValueAnimator.ofFloat(0f, 1.0f);
Jon Mirandaa0233f72017-06-22 18:34:45 -0700357
358 mScaleAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
359 @Override
360 public void onAnimationUpdate(ValueAnimator animation) {
361 float prog = animation.getAnimatedFraction();
362 mScale = prog * scale1 + (1 - prog) * scale0;
363 mColorMultiplier = prog * bgMultiplier1 + (1 - prog) * bgMultiplier0;
364 invalidate();
365 }
366 });
367 mScaleAnimator.addListener(new AnimatorListenerAdapter() {
368 @Override
369 public void onAnimationStart(Animator animation) {
370 if (onStart != null) {
371 onStart.run();
372 }
373 }
374
375 @Override
376 public void onAnimationEnd(Animator animation) {
377 if (onEnd != null) {
378 onEnd.run();
379 }
380 mScaleAnimator = null;
381 }
382 });
383
384 mScaleAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
385 mScaleAnimator.start();
386 }
387
Sunny Goyal849c6a22018-08-08 16:33:46 -0700388 public void animateToAccept(CellLayout cl, int cellX, int cellY) {
389 animateScale(ACCEPT_SCALE_FACTOR, ACCEPT_COLOR_MULTIPLIER,
390 () -> delegateDrawing(cl, cellX, cellY), null);
Jon Mirandaa0233f72017-06-22 18:34:45 -0700391 }
392
393 public void animateToRest() {
394 // This can be called multiple times -- we need to make sure the drawing delegate
395 // is saved and restored at the beginning of the animation, since cancelling the
396 // existing animation can clear the delgate.
Sunny Goyal849c6a22018-08-08 16:33:46 -0700397 CellLayout cl = mDrawingDelegate;
398 int cellX = delegateCellX;
399 int cellY = delegateCellY;
400 animateScale(1f, 1f, () -> delegateDrawing(cl, cellX, cellY), this::clearDrawingDelegate);
Jon Mirandaa0233f72017-06-22 18:34:45 -0700401 }
402
403 public int getBackgroundAlpha() {
404 return (int) Math.min(MAX_BG_OPACITY, BG_OPACITY * mColorMultiplier);
405 }
406
407 public float getStrokeWidth() {
408 return mStrokeWidth;
409 }
410}