• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 
17 package com.android.launcher3.shortcuts;
18 
19 import android.graphics.Bitmap;
20 import android.graphics.Canvas;
21 import android.graphics.Point;
22 import android.graphics.Rect;
23 import android.graphics.drawable.Drawable;
24 import android.view.View;
25 
26 import com.android.launcher3.Launcher;
27 import com.android.launcher3.Utilities;
28 import com.android.launcher3.config.FeatureFlags;
29 import com.android.launcher3.graphics.DragPreviewProvider;
30 import com.android.launcher3.icons.BitmapRenderer;
31 import com.android.launcher3.icons.FastBitmapDrawable;
32 
33 /**
34  * Extension of {@link DragPreviewProvider} which generates bitmaps scaled to the default icon size.
35  */
36 public class ShortcutDragPreviewProvider extends DragPreviewProvider {
37 
38     private final Point mPositionShift;
39 
ShortcutDragPreviewProvider(View icon, Point shift)40     public ShortcutDragPreviewProvider(View icon, Point shift) {
41         super(icon);
42         mPositionShift = shift;
43     }
44 
45     @Override
createDrawable()46     public Drawable createDrawable() {
47         if (FeatureFlags.ENABLE_DEEP_SHORTCUT_ICON_CACHE.get()) {
48             int size = Launcher.getLauncher(mView.getContext()).getDeviceProfile().iconSizePx;
49             return new FastBitmapDrawable(
50                     BitmapRenderer.createHardwareBitmap(
51                             size + blurSizeOutline,
52                             size + blurSizeOutline,
53                             (c) -> drawDragViewOnBackground(c, size)));
54         } else {
55             return new FastBitmapDrawable(createDragBitmapLegacy());
56         }
57     }
58 
createDragBitmapLegacy()59     private Bitmap createDragBitmapLegacy() {
60         Drawable d = mView.getBackground();
61         Rect bounds = getDrawableBounds(d);
62         int size = Launcher.getLauncher(mView.getContext()).getDeviceProfile().iconSizePx;
63         final Bitmap b = Bitmap.createBitmap(
64                 size + blurSizeOutline,
65                 size + blurSizeOutline,
66                 Bitmap.Config.ARGB_8888);
67         Canvas canvas = new Canvas(b);
68         canvas.translate(blurSizeOutline / 2, blurSizeOutline / 2);
69         canvas.scale(((float) size) / bounds.width(), ((float) size) / bounds.height(), 0, 0);
70         canvas.translate(bounds.left, bounds.top);
71         d.draw(canvas);
72         return b;
73     }
74 
drawDragViewOnBackground(Canvas canvas, float size)75     private void drawDragViewOnBackground(Canvas canvas, float size) {
76         Drawable d = mView.getBackground();
77         Rect bounds = getDrawableBounds(d);
78 
79         canvas.translate(blurSizeOutline / 2, blurSizeOutline / 2);
80         canvas.scale(size / bounds.width(), size / bounds.height(), 0, 0);
81         canvas.translate(bounds.left, bounds.top);
82         d.draw(canvas);
83     }
84 
85     @Override
getScaleAndPosition(Drawable preview, int[] outPos)86     public float getScaleAndPosition(Drawable preview, int[] outPos) {
87         Launcher launcher = Launcher.getLauncher(mView.getContext());
88         int iconSize = getDrawableBounds(mView.getBackground()).width();
89         float scale = launcher.getDragLayer().getLocationInDragLayer(mView, outPos);
90 
91         int iconLeft = mView.getPaddingStart();
92         if (Utilities.isRtl(mView.getResources())) {
93             iconLeft = mView.getWidth() - iconSize - iconLeft;
94         }
95 
96         outPos[0] += Math.round(
97                 scale * iconLeft + (scale * iconSize - preview.getIntrinsicWidth()) / 2
98                         + mPositionShift.x);
99         outPos[1] += Math.round((scale * mView.getHeight() - preview.getIntrinsicHeight()) / 2
100                 + mPositionShift.y);
101         float size = launcher.getDeviceProfile().iconSizePx;
102         return scale * iconSize / size;
103     }
104 }
105