2016-07-20 14:55:26 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.android.launcher3.graphics;
|
|
|
|
|
|
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
|
import android.graphics.Rect;
|
|
|
|
|
import android.graphics.Region.Op;
|
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
2016-12-05 12:04:44 -08:00
|
|
|
import com.android.launcher3.DeviceProfile;
|
2016-07-22 10:50:11 -07:00
|
|
|
import com.android.launcher3.Launcher;
|
2016-12-05 12:04:44 -08:00
|
|
|
import com.android.launcher3.LauncherAppWidgetHostView;
|
2016-07-20 14:55:26 -07:00
|
|
|
import com.android.launcher3.PreloadIconDrawable;
|
|
|
|
|
import com.android.launcher3.Workspace;
|
2016-08-11 16:02:02 -07:00
|
|
|
import com.android.launcher3.config.ProviderConfig;
|
2016-07-20 14:55:26 -07:00
|
|
|
import com.android.launcher3.folder.FolderIcon;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A utility class to generate preview bitmap for dragging.
|
|
|
|
|
*/
|
|
|
|
|
public class DragPreviewProvider {
|
|
|
|
|
|
|
|
|
|
public static final int DRAG_BITMAP_PADDING = 2;
|
|
|
|
|
|
|
|
|
|
private final Rect mTempRect = new Rect();
|
|
|
|
|
|
|
|
|
|
protected final View mView;
|
|
|
|
|
|
|
|
|
|
// The padding added to the drag view during the preview generation.
|
|
|
|
|
public final int previewPadding;
|
|
|
|
|
|
2016-10-05 16:27:48 -07:00
|
|
|
public Bitmap generatedDragOutline;
|
2016-08-11 16:02:02 -07:00
|
|
|
|
2016-07-20 14:55:26 -07:00
|
|
|
public DragPreviewProvider(View view) {
|
|
|
|
|
mView = view;
|
|
|
|
|
|
|
|
|
|
if (mView instanceof TextView) {
|
|
|
|
|
Drawable d = Workspace.getTextViewIcon((TextView) mView);
|
|
|
|
|
Rect bounds = getDrawableBounds(d);
|
|
|
|
|
previewPadding = DRAG_BITMAP_PADDING - bounds.left - bounds.top;
|
|
|
|
|
} else {
|
|
|
|
|
previewPadding = DRAG_BITMAP_PADDING;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-07-22 10:50:11 -07:00
|
|
|
* Draws the {@link #mView} into the given {@param destCanvas}.
|
2016-07-20 14:55:26 -07:00
|
|
|
*/
|
|
|
|
|
private void drawDragView(Canvas destCanvas) {
|
|
|
|
|
destCanvas.save();
|
|
|
|
|
if (mView instanceof TextView) {
|
|
|
|
|
Drawable d = Workspace.getTextViewIcon((TextView) mView);
|
|
|
|
|
Rect bounds = getDrawableBounds(d);
|
|
|
|
|
destCanvas.translate(DRAG_BITMAP_PADDING / 2 - bounds.left,
|
|
|
|
|
DRAG_BITMAP_PADDING / 2 - bounds.top);
|
|
|
|
|
d.draw(destCanvas);
|
|
|
|
|
} else {
|
|
|
|
|
final Rect clipRect = mTempRect;
|
|
|
|
|
mView.getDrawingRect(clipRect);
|
|
|
|
|
|
|
|
|
|
boolean textVisible = false;
|
|
|
|
|
if (mView instanceof FolderIcon) {
|
|
|
|
|
// For FolderIcons the text can bleed into the icon area, and so we need to
|
|
|
|
|
// hide the text completely (which can't be achieved by clipping).
|
|
|
|
|
if (((FolderIcon) mView).getTextVisible()) {
|
|
|
|
|
((FolderIcon) mView).setTextVisible(false);
|
|
|
|
|
textVisible = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
destCanvas.translate(-mView.getScrollX() + DRAG_BITMAP_PADDING / 2,
|
|
|
|
|
-mView.getScrollY() + DRAG_BITMAP_PADDING / 2);
|
|
|
|
|
destCanvas.clipRect(clipRect, Op.REPLACE);
|
|
|
|
|
mView.draw(destCanvas);
|
|
|
|
|
|
|
|
|
|
// Restore text visibility of FolderIcon if necessary
|
|
|
|
|
if (textVisible) {
|
|
|
|
|
((FolderIcon) mView).setTextVisible(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
destCanvas.restore();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-07-22 10:50:11 -07:00
|
|
|
* Returns a new bitmap to show when the {@link #mView} is being dragged around.
|
2016-07-20 14:55:26 -07:00
|
|
|
* Responsibility for the bitmap is transferred to the caller.
|
|
|
|
|
*/
|
|
|
|
|
public Bitmap createDragBitmap(Canvas canvas) {
|
2016-12-05 12:04:44 -08:00
|
|
|
float scale = 1f;
|
|
|
|
|
int width = mView.getWidth();
|
|
|
|
|
int height = mView.getHeight();
|
2016-07-20 14:55:26 -07:00
|
|
|
|
|
|
|
|
if (mView instanceof TextView) {
|
|
|
|
|
Drawable d = Workspace.getTextViewIcon((TextView) mView);
|
|
|
|
|
Rect bounds = getDrawableBounds(d);
|
2016-12-05 12:04:44 -08:00
|
|
|
width = bounds.width();
|
|
|
|
|
height = bounds.height();
|
|
|
|
|
} else if (mView instanceof LauncherAppWidgetHostView) {
|
|
|
|
|
DeviceProfile profile = Launcher.getLauncher(mView.getContext()).getDeviceProfile();
|
|
|
|
|
scale = Math.min(profile.appWidgetScale.x, profile.appWidgetScale.y);
|
|
|
|
|
width = (int) (mView.getWidth() * scale);
|
|
|
|
|
height = (int) (mView.getHeight() * scale);
|
2016-07-20 14:55:26 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-05 12:04:44 -08:00
|
|
|
Bitmap b = Bitmap.createBitmap(width + DRAG_BITMAP_PADDING, height + DRAG_BITMAP_PADDING,
|
|
|
|
|
Bitmap.Config.ARGB_8888);
|
2016-07-20 14:55:26 -07:00
|
|
|
canvas.setBitmap(b);
|
2016-12-05 12:04:44 -08:00
|
|
|
|
|
|
|
|
canvas.save();
|
|
|
|
|
canvas.scale(scale, scale);
|
2016-07-20 14:55:26 -07:00
|
|
|
drawDragView(canvas);
|
2016-12-05 12:04:44 -08:00
|
|
|
canvas.restore();
|
|
|
|
|
|
2016-07-20 14:55:26 -07:00
|
|
|
canvas.setBitmap(null);
|
|
|
|
|
|
|
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-11 16:02:02 -07:00
|
|
|
public final void generateDragOutline(Canvas canvas) {
|
2016-10-05 16:27:48 -07:00
|
|
|
if (ProviderConfig.IS_DOGFOOD_BUILD && generatedDragOutline != null) {
|
2016-08-11 16:02:02 -07:00
|
|
|
throw new RuntimeException("Drag outline generated twice");
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-05 16:27:48 -07:00
|
|
|
generatedDragOutline = createDragOutline(canvas);
|
2016-08-11 16:02:02 -07:00
|
|
|
}
|
|
|
|
|
|
2016-07-20 14:55:26 -07:00
|
|
|
/**
|
|
|
|
|
* Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
|
|
|
|
|
* Responsibility for the bitmap is transferred to the caller.
|
|
|
|
|
*/
|
|
|
|
|
public Bitmap createDragOutline(Canvas canvas) {
|
2016-12-05 12:04:44 -08:00
|
|
|
float scale = 1f;
|
|
|
|
|
int width = mView.getWidth();
|
|
|
|
|
int height = mView.getHeight();
|
|
|
|
|
|
|
|
|
|
if (mView instanceof LauncherAppWidgetHostView) {
|
|
|
|
|
DeviceProfile profile = Launcher.getLauncher(mView.getContext()).getDeviceProfile();
|
|
|
|
|
scale = Math.min(profile.appWidgetScale.x, profile.appWidgetScale.y);
|
|
|
|
|
width = (int) Math.floor(mView.getWidth() * scale);
|
|
|
|
|
height = (int) Math.floor(mView.getHeight() * scale);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Bitmap b = Bitmap.createBitmap(width + DRAG_BITMAP_PADDING, height + DRAG_BITMAP_PADDING,
|
|
|
|
|
Bitmap.Config.ALPHA_8);
|
2016-07-20 14:55:26 -07:00
|
|
|
canvas.setBitmap(b);
|
2016-12-05 12:04:44 -08:00
|
|
|
|
|
|
|
|
canvas.save();
|
|
|
|
|
canvas.scale(scale, scale);
|
2016-07-20 14:55:26 -07:00
|
|
|
drawDragView(canvas);
|
2016-12-05 12:04:44 -08:00
|
|
|
canvas.restore();
|
|
|
|
|
|
2016-09-01 12:50:11 -07:00
|
|
|
HolographicOutlineHelper.getInstance(mView.getContext())
|
2016-08-31 16:02:40 -07:00
|
|
|
.applyExpensiveOutlineWithBlur(b, canvas);
|
2016-12-05 12:04:44 -08:00
|
|
|
|
2016-07-20 14:55:26 -07:00
|
|
|
canvas.setBitmap(null);
|
|
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static Rect getDrawableBounds(Drawable d) {
|
|
|
|
|
Rect bounds = new Rect();
|
|
|
|
|
d.copyBounds(bounds);
|
|
|
|
|
if (bounds.width() == 0 || bounds.height() == 0) {
|
|
|
|
|
bounds.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
|
|
|
|
|
} else {
|
|
|
|
|
bounds.offsetTo(0, 0);
|
|
|
|
|
}
|
|
|
|
|
if (d instanceof PreloadIconDrawable) {
|
|
|
|
|
int inset = -((PreloadIconDrawable) d).getOutset();
|
|
|
|
|
bounds.inset(inset, inset);
|
|
|
|
|
}
|
|
|
|
|
return bounds;
|
|
|
|
|
}
|
2016-07-22 10:50:11 -07:00
|
|
|
|
|
|
|
|
public float getScaleAndPosition(Bitmap preview, int[] outPos) {
|
|
|
|
|
float scale = Launcher.getLauncher(mView.getContext())
|
|
|
|
|
.getDragLayer().getLocationInDragLayer(mView, outPos);
|
2016-12-05 12:04:44 -08:00
|
|
|
DeviceProfile profile = Launcher.getLauncher(mView.getContext()).getDeviceProfile();
|
|
|
|
|
if (mView instanceof LauncherAppWidgetHostView) {
|
|
|
|
|
// App widgets are technically scaled, but are drawn at their expected size -- so the
|
|
|
|
|
// app widget scale should not affect the scale of the preview.
|
|
|
|
|
scale /= Math.min(profile.appWidgetScale.x, profile.appWidgetScale.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
outPos[0] = Math.round(outPos[0] -
|
|
|
|
|
(preview.getWidth() - scale * mView.getWidth() * mView.getScaleX()) / 2);
|
|
|
|
|
outPos[1] = Math.round(outPos[1] - (1 - scale) * preview.getHeight() / 2
|
|
|
|
|
- previewPadding / 2);
|
2016-07-22 10:50:11 -07:00
|
|
|
return scale;
|
|
|
|
|
}
|
2016-07-20 14:55:26 -07:00
|
|
|
}
|