1157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal/* 2157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal * Copyright (C) 2016 The Android Open Source Project 3157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal * 4157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal * Licensed under the Apache License, Version 2.0 (the "License"); 5157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal * you may not use this file except in compliance with the License. 6157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal * You may obtain a copy of the License at 7157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal * 8157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal * http://www.apache.org/licenses/LICENSE-2.0 9157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal * 10157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal * Unless required by applicable law or agreed to in writing, software 11157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal * distributed under the License is distributed on an "AS IS" BASIS, 12157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal * See the License for the specific language governing permissions and 14157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal * limitations under the License. 15157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal */ 16157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal 17a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyalpackage com.android.launcher3.shortcuts; 18157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal 19157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyalimport android.graphics.Bitmap; 20157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyalimport android.graphics.Canvas; 21a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyalimport android.graphics.Point; 22157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyalimport android.graphics.Rect; 23157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyalimport android.graphics.drawable.Drawable; 24157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyalimport android.view.View; 25157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal 26157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyalimport com.android.launcher3.Launcher; 27a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyalimport com.android.launcher3.Utilities; 28157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyalimport com.android.launcher3.graphics.DragPreviewProvider; 29c06af333cbcebb183d13d004cccf5c9d69a70af0Mario Bertschlerimport com.android.launcher3.graphics.HolographicOutlineHelper; 30157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal 31157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal/** 32a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal * Extension of {@link DragPreviewProvider} which generates bitmaps scaled to the default icon size. 33157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal */ 34a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyalpublic class ShortcutDragPreviewProvider extends DragPreviewProvider { 35a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal 36a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal private final Point mPositionShift; 37157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal 38a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal public ShortcutDragPreviewProvider(View icon, Point shift) { 39a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal super(icon); 40a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal mPositionShift = shift; 41157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal } 42157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal 43157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal @Override 44157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal public Bitmap createDragOutline(Canvas canvas) { 45f28e6afafdc8b5afbdf99910668a38f9252bfb47Sunny Goyal Bitmap b = drawScaledPreview(canvas, Bitmap.Config.ALPHA_8); 46a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal 4710629b077c65b648be6a8603a092322d8a2c2c50Sunny Goyal HolographicOutlineHelper.getInstance(mView.getContext()) 48f28e6afafdc8b5afbdf99910668a38f9252bfb47Sunny Goyal .applyExpensiveOutlineWithBlur(b, canvas); 49a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal canvas.setBitmap(null); 50a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal return b; 51157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal } 52157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal 53157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal @Override 54157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal public Bitmap createDragBitmap(Canvas canvas) { 55f28e6afafdc8b5afbdf99910668a38f9252bfb47Sunny Goyal Bitmap b = drawScaledPreview(canvas, Bitmap.Config.ARGB_8888); 56a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal canvas.setBitmap(null); 57a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal return b; 58157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal } 59157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal 60f28e6afafdc8b5afbdf99910668a38f9252bfb47Sunny Goyal private Bitmap drawScaledPreview(Canvas canvas, Bitmap.Config config) { 61a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal Drawable d = mView.getBackground(); 62157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal Rect bounds = getDrawableBounds(d); 63157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal 64157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal int size = Launcher.getLauncher(mView.getContext()).getDeviceProfile().iconSizePx; 65157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal 66157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal final Bitmap b = Bitmap.createBitmap( 67c9c57631a985a8e1a4eac17ef0952bd344e281a2Jon Miranda size + blurSizeOutline, 68c9c57631a985a8e1a4eac17ef0952bd344e281a2Jon Miranda size + blurSizeOutline, 69f28e6afafdc8b5afbdf99910668a38f9252bfb47Sunny Goyal config); 70157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal 71157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal canvas.setBitmap(b); 72157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal canvas.save(Canvas.MATRIX_SAVE_FLAG); 73c9c57631a985a8e1a4eac17ef0952bd344e281a2Jon Miranda canvas.translate(blurSizeOutline / 2, blurSizeOutline / 2); 74157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal canvas.scale(((float) size) / bounds.width(), ((float) size) / bounds.height(), 0, 0); 75157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal canvas.translate(bounds.left, bounds.top); 76157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal d.draw(canvas); 77157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal canvas.restore(); 78157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal return b; 79157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal } 80a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal 81a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal @Override 82a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal public float getScaleAndPosition(Bitmap preview, int[] outPos) { 83a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal Launcher launcher = Launcher.getLauncher(mView.getContext()); 84a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal int iconSize = getDrawableBounds(mView.getBackground()).width(); 85a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal float scale = launcher.getDragLayer().getLocationInDragLayer(mView, outPos); 86a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal 87a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal int iconLeft = mView.getPaddingStart(); 88a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal if (Utilities.isRtl(mView.getResources())) { 89a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal iconLeft = mView.getWidth() - iconSize - iconLeft; 90a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal } 91a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal 92a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal outPos[0] += Math.round(scale * iconLeft + (scale * iconSize - preview.getWidth()) / 2 + 93a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal mPositionShift.x); 94a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal outPos[1] += Math.round((scale * mView.getHeight() - preview.getHeight()) / 2 95a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal + mPositionShift.y); 96a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal float size = launcher.getDeviceProfile().iconSizePx; 97a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal return scale * iconSize / size; 98a2454ad2d8dcffa94f670853eb464726c73597f1Sunny Goyal } 99157793dda450b69da388b859d1c1a7a1083c4ec9Sunny Goyal} 100