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