• 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.Canvas;
20 import android.graphics.Point;
21 import android.graphics.Rect;
22 import android.graphics.drawable.Drawable;
23 import android.view.View;
24 
25 import com.android.launcher3.Utilities;
26 import com.android.launcher3.graphics.DragPreviewProvider;
27 import com.android.launcher3.icons.BitmapRenderer;
28 import com.android.launcher3.icons.FastBitmapDrawable;
29 import com.android.launcher3.views.ActivityContext;
30 
31 /**
32  * Extension of {@link DragPreviewProvider} which generates bitmaps scaled to the default icon size.
33  */
34 public class ShortcutDragPreviewProvider extends DragPreviewProvider {
35 
36     private final Point mPositionShift;
37 
ShortcutDragPreviewProvider(View icon, Point shift)38     public ShortcutDragPreviewProvider(View icon, Point shift) {
39         super(icon);
40         mPositionShift = shift;
41     }
42 
43     @Override
createDrawable()44     public Drawable createDrawable() {
45         int size = ActivityContext.lookupContext(mView.getContext())
46                 .getDeviceProfile().iconSizePx;
47         return new FastBitmapDrawable(
48                 BitmapRenderer.createHardwareBitmap(
49                         size + blurSizeOutline,
50                         size + blurSizeOutline,
51                         (c) -> drawDragViewOnBackground(c, size)));
52     }
53 
drawDragViewOnBackground(Canvas canvas, float size)54     private void drawDragViewOnBackground(Canvas canvas, float size) {
55         Drawable d = mView.getBackground();
56         Rect bounds = getDrawableBounds(d);
57 
58         canvas.translate(blurSizeOutline / 2, blurSizeOutline / 2);
59         canvas.scale(size / bounds.width(), size / bounds.height(), 0, 0);
60         canvas.translate(bounds.left, bounds.top);
61         d.draw(canvas);
62     }
63 
64     @Override
getScaleAndPosition(Drawable preview, int[] outPos)65     public float getScaleAndPosition(Drawable preview, int[] outPos) {
66         ActivityContext context = ActivityContext.lookupContext(mView.getContext());
67         int iconSize = getDrawableBounds(mView.getBackground()).width();
68         float scale = context.getDragLayer().getLocationInDragLayer(mView, outPos);
69 
70         int iconLeft = mView.getPaddingStart();
71         if (Utilities.isRtl(mView.getResources())) {
72             iconLeft = mView.getWidth() - iconSize - iconLeft;
73         }
74 
75         outPos[0] += Math.round(
76                 scale * iconLeft + (scale * iconSize - preview.getIntrinsicWidth()) / 2
77                         + mPositionShift.x);
78         outPos[1] += Math.round((scale * mView.getHeight() - preview.getIntrinsicHeight()) / 2
79                 + mPositionShift.y);
80         float size = context.getDeviceProfile().iconSizePx;
81         return scale * iconSize / size;
82     }
83 }
84