Home | History | Annotate | Download | only in widget
      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.widget;
     18 
     19 import android.graphics.Bitmap;
     20 import android.graphics.Canvas;
     21 import android.graphics.Rect;
     22 import android.view.View;
     23 
     24 import com.android.launcher3.HolographicOutlineHelper;
     25 import com.android.launcher3.Launcher;
     26 import com.android.launcher3.PendingAddItemInfo;
     27 import com.android.launcher3.Workspace;
     28 import com.android.launcher3.graphics.DragPreviewProvider;
     29 
     30 /**
     31  * Extension of {@link DragPreviewProvider} with logic specific to pending widgets/shortcuts
     32  * dragged from the widget tray.
     33  */
     34 public class PendingItemPreviewProvider extends DragPreviewProvider {
     35 
     36     private final PendingAddItemInfo mAddInfo;
     37     private final Bitmap mPreviewBitmap;
     38 
     39     public PendingItemPreviewProvider(View view, PendingAddItemInfo addInfo, Bitmap preview) {
     40         super(view);
     41         mAddInfo = addInfo;
     42         mPreviewBitmap = preview;
     43     }
     44 
     45     @Override
     46     public Bitmap createDragOutline(Canvas canvas) {
     47         Workspace workspace = Launcher.getLauncher(mView.getContext()).getWorkspace();
     48         int[] size = workspace.estimateItemSize(mAddInfo, false);
     49 
     50         int w = size[0];
     51         int h = size[1];
     52         final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ALPHA_8);
     53         canvas.setBitmap(b);
     54 
     55         Rect src = new Rect(0, 0, mPreviewBitmap.getWidth(), mPreviewBitmap.getHeight());
     56         float scaleFactor = Math.min((w - DRAG_BITMAP_PADDING) / (float) mPreviewBitmap.getWidth(),
     57                 (h - DRAG_BITMAP_PADDING) / (float) mPreviewBitmap.getHeight());
     58         int scaledWidth = (int) (scaleFactor * mPreviewBitmap.getWidth());
     59         int scaledHeight = (int) (scaleFactor * mPreviewBitmap.getHeight());
     60         Rect dst = new Rect(0, 0, scaledWidth, scaledHeight);
     61 
     62         // center the image
     63         dst.offset((w - scaledWidth) / 2, (h - scaledHeight) / 2);
     64 
     65         canvas.drawBitmap(mPreviewBitmap, src, dst, null);
     66 
     67         // Don't clip alpha values for the drag outline if we're using the default widget preview
     68         boolean clipAlpha = !(mAddInfo instanceof PendingAddWidgetInfo &&
     69                 (((PendingAddWidgetInfo) mAddInfo).previewImage == 0));
     70         HolographicOutlineHelper.obtain(mView.getContext())
     71                 .applyExpensiveOutlineWithBlur(b, canvas, clipAlpha);
     72         canvas.setBitmap(null);
     73 
     74         return b;
     75     }
     76 }
     77