Search in sources :

Example 1 with WidgetImageView

use of com.android.launcher3.widget.WidgetImageView in project android_packages_apps_Trebuchet by LineageOS.

the class PendingItemDragHelper method startDrag.

/**
 * Starts the drag for the pending item associated with the view.
 *
 * @param previewBounds The bounds where the image was displayed,
 *                      {@link WidgetImageView#getBitmapBounds()}
 * @param previewBitmapWidth The actual width of the bitmap displayed in the view.
 * @param previewViewWidth The width of {@link WidgetImageView} displaying the preview
 * @param screenPos Position of {@link WidgetImageView} on the screen
 */
public void startDrag(Rect previewBounds, int previewBitmapWidth, int previewViewWidth, Point screenPos, DragSource source, DragOptions options) {
    final Launcher launcher = Launcher.getLauncher(mView.getContext());
    LauncherAppState app = LauncherAppState.getInstance(launcher);
    Bitmap preview = null;
    final float scale;
    final Point dragOffset;
    final Rect dragRegion;
    mEstimatedCellSize = launcher.getWorkspace().estimateItemSize(mAddInfo);
    DraggableView draggableView;
    if (mAddInfo instanceof PendingAddWidgetInfo) {
        PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) mAddInfo;
        int maxWidth = Math.min((int) (previewBitmapWidth * MAX_WIDGET_SCALE), mEstimatedCellSize[0]);
        int[] previewSizeBeforeScale = new int[1];
        if (mPreview != null) {
            preview = LivePreviewWidgetCell.generateFromRemoteViews(launcher, mPreview, createWidgetInfo.info, maxWidth, previewSizeBeforeScale);
        }
        if (preview == null) {
            preview = app.getWidgetCache().generateWidgetPreview(launcher, createWidgetInfo.info, maxWidth, null, previewSizeBeforeScale).first;
        }
        if (previewSizeBeforeScale[0] < previewBitmapWidth) {
            // The icon has extra padding around it.
            int padding = (previewBitmapWidth - previewSizeBeforeScale[0]) / 2;
            if (previewBitmapWidth > previewViewWidth) {
                padding = padding * previewViewWidth / previewBitmapWidth;
            }
            previewBounds.left += padding;
            previewBounds.right -= padding;
        }
        scale = previewBounds.width() / (float) preview.getWidth();
        launcher.getDragController().addDragListener(new WidgetHostViewLoader(launcher, mView));
        dragOffset = null;
        dragRegion = null;
        draggableView = DraggableView.ofType(DraggableView.DRAGGABLE_WIDGET);
    } else {
        PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) mAddInfo;
        Drawable icon = createShortcutInfo.activityInfo.getFullResIcon(app.getIconCache());
        LauncherIcons li = LauncherIcons.obtain(launcher);
        preview = li.createScaledBitmapWithoutShadow(icon, 0);
        li.recycle();
        scale = ((float) launcher.getDeviceProfile().iconSizePx) / preview.getWidth();
        dragOffset = new Point(previewPadding / 2, previewPadding / 2);
        // Create a preview same as the workspace cell size and draw the icon at the
        // appropriate position.
        DeviceProfile dp = launcher.getDeviceProfile();
        int iconSize = dp.iconSizePx;
        int padding = launcher.getResources().getDimensionPixelSize(R.dimen.widget_preview_shortcut_padding);
        previewBounds.left += padding;
        previewBounds.top += padding;
        dragRegion = new Rect();
        dragRegion.left = (mEstimatedCellSize[0] - iconSize) / 2;
        dragRegion.right = dragRegion.left + iconSize;
        dragRegion.top = (mEstimatedCellSize[1] - iconSize - dp.iconTextSizePx - dp.iconDrawablePaddingPx) / 2;
        dragRegion.bottom = dragRegion.top + iconSize;
        draggableView = DraggableView.ofType(DraggableView.DRAGGABLE_ICON);
    }
    // Since we are not going through the workspace for starting the drag, set drag related
    // information on the workspace before starting the drag.
    launcher.getWorkspace().prepareDragWithProvider(this);
    int dragLayerX = screenPos.x + previewBounds.left + (int) ((scale * preview.getWidth() - preview.getWidth()) / 2);
    int dragLayerY = screenPos.y + previewBounds.top + (int) ((scale * preview.getHeight() - preview.getHeight()) / 2);
    // Start the drag
    launcher.getDragController().startDrag(preview, draggableView, dragLayerX, dragLayerY, source, mAddInfo, dragOffset, dragRegion, scale, scale, options);
}
Also used : Rect(android.graphics.Rect) LauncherAppState(com.android.launcher3.LauncherAppState) Drawable(android.graphics.drawable.Drawable) Point(android.graphics.Point) DraggableView(com.android.launcher3.dragndrop.DraggableView) Point(android.graphics.Point) Paint(android.graphics.Paint) DeviceProfile(com.android.launcher3.DeviceProfile) Bitmap(android.graphics.Bitmap) LauncherIcons(com.android.launcher3.icons.LauncherIcons) Launcher(com.android.launcher3.Launcher)

Example 2 with WidgetImageView

use of com.android.launcher3.widget.WidgetImageView in project Neo-Launcher by NeoApplications.

the class BaseWidgetSheet method beginDraggingWidget.

private boolean beginDraggingWidget(WidgetCell v) {
    // Get the widget preview as the drag representation
    WidgetImageView image = v.getWidgetView();
    // we abort the drag.
    if (image.getBitmap() == null) {
        return false;
    }
    int[] loc = new int[2];
    getPopupContainer().getLocationInDragLayer(image, loc);
    new PendingItemDragHelper(v).startDrag(image.getBitmapBounds(), image.getBitmap().getWidth(), image.getWidth(), new Point(loc[0], loc[1]), this, new DragOptions());
    close(true);
    return true;
}
Also used : DragOptions(com.android.launcher3.dragndrop.DragOptions) Point(android.graphics.Point)

Example 3 with WidgetImageView

use of com.android.launcher3.widget.WidgetImageView in project android_packages_apps_Launcher3 by AOSPA.

the class PendingItemDragHelper method startDrag.

/**
 * Starts the drag for the pending item associated with the view.
 *
 * @param previewBounds The bounds where the image was displayed,
 *                      {@link WidgetImageView#getBitmapBounds()}
 * @param previewBitmapWidth The actual width of the bitmap displayed in the view.
 * @param previewViewWidth The width of {@link WidgetImageView} displaying the preview
 * @param screenPos Position of {@link WidgetImageView} on the screen
 */
public void startDrag(Rect previewBounds, int previewBitmapWidth, int previewViewWidth, Point screenPos, DragSource source, DragOptions options) {
    if (TestProtocol.sDebugTracing) {
        Log.d(TestProtocol.NO_DROP_TARGET, "3");
    }
    final Launcher launcher = Launcher.getLauncher(mView.getContext());
    LauncherAppState app = LauncherAppState.getInstance(launcher);
    Drawable preview = null;
    final int previewWidth;
    final int previewHeight;
    final float scale;
    final Point dragOffset;
    final Rect dragRegion;
    mEstimatedCellSize = launcher.getWorkspace().estimateItemSize(mAddInfo);
    DraggableView draggableView;
    if (mAddInfo instanceof PendingAddWidgetInfo) {
        PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) mAddInfo;
        int maxWidth = Math.min((int) (previewBitmapWidth * MAX_WIDGET_SCALE), mEstimatedCellSize[0]);
        int[] previewSizeBeforeScale = new int[1];
        if (mRemoteViewsPreview != null) {
            mAppWidgetHostViewPreview = new LauncherAppWidgetHostView(launcher);
            mAppWidgetHostViewPreview.setAppWidget(/* appWidgetId= */
            -1, ((PendingAddWidgetInfo) mAddInfo).info);
            DeviceProfile deviceProfile = launcher.getDeviceProfile();
            Rect padding = new Rect();
            mAppWidgetHostViewPreview.getWidgetInset(deviceProfile, padding);
            mAppWidgetHostViewPreview.setPadding(padding.left, padding.top, padding.right, padding.bottom);
            mAppWidgetHostViewPreview.updateAppWidget(/* remoteViews= */
            mRemoteViewsPreview);
            Size widgetSizes = WidgetSizes.getWidgetPaddedSizePx(launcher, mAddInfo.componentName, deviceProfile, mAddInfo.spanX, mAddInfo.spanY);
            mAppWidgetHostViewPreview.measure(MeasureSpec.makeMeasureSpec(widgetSizes.getWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(widgetSizes.getHeight(), MeasureSpec.EXACTLY));
            mAppWidgetHostViewPreview.setClipChildren(false);
            mAppWidgetHostViewPreview.setClipToPadding(false);
            mAppWidgetHostViewPreview.setScaleToFit(mRemoteViewsPreviewScale);
        }
        if (mAppWidgetHostViewPreview != null) {
            previewSizeBeforeScale[0] = mAppWidgetHostViewPreview.getMeasuredWidth();
            launcher.getDragController().addDragListener(new AppWidgetHostViewDragListener(launcher));
        }
        if (preview == null && mAppWidgetHostViewPreview == null) {
            Drawable p = new FastBitmapDrawable(new DatabaseWidgetPreviewLoader(launcher).generateWidgetPreview(createWidgetInfo.info, maxWidth, previewSizeBeforeScale));
            if (RoundedCornerEnforcement.isRoundedCornerEnabled()) {
                p = new RoundDrawableWrapper(p, mEnforcedRoundedCornersForWidget);
            }
            preview = p;
        }
        if (previewSizeBeforeScale[0] < previewBitmapWidth) {
            // The icon has extra padding around it.
            int padding = (previewBitmapWidth - previewSizeBeforeScale[0]) / 2;
            if (previewBitmapWidth > previewViewWidth) {
                padding = padding * previewViewWidth / previewBitmapWidth;
            }
            previewBounds.left += padding;
            previewBounds.right -= padding;
        }
        if (mAppWidgetHostViewPreview != null) {
            previewWidth = mAppWidgetHostViewPreview.getMeasuredWidth();
            previewHeight = mAppWidgetHostViewPreview.getMeasuredHeight();
        } else {
            previewWidth = preview.getIntrinsicWidth();
            previewHeight = preview.getIntrinsicHeight();
        }
        scale = previewBounds.width() / (float) previewWidth;
        launcher.getDragController().addDragListener(new WidgetHostViewLoader(launcher, mView));
        dragOffset = null;
        dragRegion = null;
        draggableView = DraggableView.ofType(DraggableView.DRAGGABLE_WIDGET);
    } else {
        PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) mAddInfo;
        Drawable icon = createShortcutInfo.activityInfo.getFullResIcon(app.getIconCache());
        LauncherIcons li = LauncherIcons.obtain(launcher);
        preview = new FastBitmapDrawable(li.createScaledBitmapWithoutShadow(icon, 0));
        previewWidth = preview.getIntrinsicWidth();
        previewHeight = preview.getIntrinsicHeight();
        li.recycle();
        scale = ((float) launcher.getDeviceProfile().iconSizePx) / previewWidth;
        dragOffset = new Point(previewPadding / 2, previewPadding / 2);
        // Create a preview same as the workspace cell size and draw the icon at the
        // appropriate position.
        DeviceProfile dp = launcher.getDeviceProfile();
        int iconSize = dp.iconSizePx;
        int padding = launcher.getResources().getDimensionPixelSize(R.dimen.widget_preview_shortcut_padding);
        previewBounds.left += padding;
        previewBounds.top += padding;
        dragRegion = new Rect();
        dragRegion.left = (mEstimatedCellSize[0] - iconSize) / 2;
        dragRegion.right = dragRegion.left + iconSize;
        dragRegion.top = (mEstimatedCellSize[1] - iconSize - dp.iconTextSizePx - dp.iconDrawablePaddingPx) / 2;
        dragRegion.bottom = dragRegion.top + iconSize;
        draggableView = DraggableView.ofType(DraggableView.DRAGGABLE_ICON);
    }
    int dragLayerX = screenPos.x + previewBounds.left + (int) ((scale * previewWidth - previewWidth) / 2);
    int dragLayerY = screenPos.y + previewBounds.top + (int) ((scale * previewHeight - previewHeight) / 2);
    // Start the drag
    if (mAppWidgetHostViewPreview != null) {
        launcher.getDragController().startDrag(mAppWidgetHostViewPreview, draggableView, dragLayerX, dragLayerY, source, mAddInfo, dragOffset, dragRegion, scale, scale, options);
    } else {
        launcher.getDragController().startDrag(preview, draggableView, dragLayerX, dragLayerY, source, mAddInfo, dragOffset, dragRegion, scale, scale, options);
    }
}
Also used : Rect(android.graphics.Rect) LauncherAppState(com.android.launcher3.LauncherAppState) Size(android.util.Size) Drawable(android.graphics.drawable.Drawable) FastBitmapDrawable(com.android.launcher3.icons.FastBitmapDrawable) RoundDrawableWrapper(com.android.launcher3.icons.RoundDrawableWrapper) Point(android.graphics.Point) DraggableView(com.android.launcher3.dragndrop.DraggableView) Point(android.graphics.Point) Paint(android.graphics.Paint) FastBitmapDrawable(com.android.launcher3.icons.FastBitmapDrawable) DeviceProfile(com.android.launcher3.DeviceProfile) AppWidgetHostViewDragListener(com.android.launcher3.widget.dragndrop.AppWidgetHostViewDragListener) LauncherIcons(com.android.launcher3.icons.LauncherIcons) Launcher(com.android.launcher3.Launcher)

Example 4 with WidgetImageView

use of com.android.launcher3.widget.WidgetImageView in project android_packages_apps_Launcher3 by ArrowOS.

the class BaseWidgetSheet method beginDraggingWidget.

private boolean beginDraggingWidget(WidgetCell v) {
    if (TestProtocol.sDebugTracing) {
        Log.d(TestProtocol.NO_DROP_TARGET, "2");
    }
    // Get the widget preview as the drag representation
    WidgetImageView image = v.getWidgetView();
    // we abort the drag.
    if (image.getDrawable() == null && v.getAppWidgetHostViewPreview() == null) {
        return false;
    }
    PendingItemDragHelper dragHelper = new PendingItemDragHelper(v);
    // RemoteViews are being rendered in AppWidgetHostView in WidgetCell. And thus, the scale of
    // RemoteViews is equivalent to the AppWidgetHostView scale.
    dragHelper.setRemoteViewsPreview(v.getRemoteViewsPreview(), v.getAppWidgetHostViewScale());
    dragHelper.setAppWidgetHostViewPreview(v.getAppWidgetHostViewPreview());
    if (image.getDrawable() != null) {
        int[] loc = new int[2];
        getPopupContainer().getLocationInDragLayer(image, loc);
        dragHelper.startDrag(image.getBitmapBounds(), image.getDrawable().getIntrinsicWidth(), image.getWidth(), new Point(loc[0], loc[1]), this, new DragOptions());
    } else {
        NavigableAppWidgetHostView preview = v.getAppWidgetHostViewPreview();
        int[] loc = new int[2];
        getPopupContainer().getLocationInDragLayer(preview, loc);
        Rect r = new Rect();
        preview.getWorkspaceVisualDragBounds(r);
        dragHelper.startDrag(r, preview.getMeasuredWidth(), preview.getMeasuredWidth(), new Point(loc[0], loc[1]), this, new DragOptions());
    }
    close(true);
    return true;
}
Also used : DragOptions(com.android.launcher3.dragndrop.DragOptions) Rect(android.graphics.Rect) Point(android.graphics.Point)

Example 5 with WidgetImageView

use of com.android.launcher3.widget.WidgetImageView in project android_packages_apps_Launcher3 by ProtonAOSP.

the class AddItemActivity method onLongClick.

@Override
public boolean onLongClick(View view) {
    // Find the position of the preview relative to the touch location.
    WidgetImageView img = mWidgetCell.getWidgetView();
    NavigableAppWidgetHostView appWidgetHostView = mWidgetCell.getAppWidgetHostViewPreview();
    // we abort the drag.
    if (img.getDrawable() == null && appWidgetHostView == null) {
        return false;
    }
    final Rect bounds;
    // Start home and pass the draw request params
    final PinItemDragListener listener;
    if (appWidgetHostView != null) {
        bounds = new Rect();
        appWidgetHostView.getSourceVisualDragBounds(bounds);
        float appWidgetHostViewScale = mWidgetCell.getAppWidgetHostViewScale();
        int xOffset = appWidgetHostView.getLeft() - (int) (mLastTouchPos.x * appWidgetHostViewScale);
        int yOffset = appWidgetHostView.getTop() - (int) (mLastTouchPos.y * appWidgetHostViewScale);
        bounds.offset(xOffset, yOffset);
        listener = new PinItemDragListener(mRequest, bounds, appWidgetHostView.getMeasuredWidth(), appWidgetHostView.getMeasuredWidth(), appWidgetHostViewScale);
    } else {
        bounds = img.getBitmapBounds();
        bounds.offset(img.getLeft() - (int) mLastTouchPos.x, img.getTop() - (int) mLastTouchPos.y);
        listener = new PinItemDragListener(mRequest, bounds, img.getDrawable().getIntrinsicWidth(), img.getWidth());
    }
    // Start a system drag and drop. We use a transparent bitmap as preview for system drag
    // as the preview is handled internally by launcher.
    ClipDescription description = new ClipDescription("", new String[] { listener.getMimeType() });
    ClipData data = new ClipData(description, new ClipData.Item(""));
    view.startDragAndDrop(data, new DragShadowBuilder(view) {

        @Override
        public void onDrawShadow(Canvas canvas) {
        }

        @Override
        public void onProvideShadowMetrics(Point outShadowSize, Point outShadowTouchPoint) {
            outShadowSize.set(SHADOW_SIZE, SHADOW_SIZE);
            outShadowTouchPoint.set(SHADOW_SIZE / 2, SHADOW_SIZE / 2);
        }
    }, null, View.DRAG_FLAG_GLOBAL);
    Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME).setPackage(getPackageName()).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Launcher.ACTIVITY_TRACKER.registerCallback(listener);
    startActivity(homeIntent, ActivityOptions.makeCustomAnimation(this, 0, android.R.anim.fade_out).toBundle());
    logCommand(LAUNCHER_ADD_EXTERNAL_ITEM_DRAGGED);
    mFinishOnPause = true;
    return false;
}
Also used : Rect(android.graphics.Rect) DragShadowBuilder(android.view.View.DragShadowBuilder) Canvas(android.graphics.Canvas) NavigableAppWidgetHostView(com.android.launcher3.widget.NavigableAppWidgetHostView) Intent(android.content.Intent) Point(android.graphics.Point) Point(android.graphics.Point) WidgetImageView(com.android.launcher3.widget.WidgetImageView) ClipData(android.content.ClipData) ClipDescription(android.content.ClipDescription)

Aggregations

Point (android.graphics.Point)18 Rect (android.graphics.Rect)16 ClipData (android.content.ClipData)6 ClipDescription (android.content.ClipDescription)6 Intent (android.content.Intent)6 Canvas (android.graphics.Canvas)6 Paint (android.graphics.Paint)6 Drawable (android.graphics.drawable.Drawable)6 DragShadowBuilder (android.view.View.DragShadowBuilder)6 DeviceProfile (com.android.launcher3.DeviceProfile)6 Launcher (com.android.launcher3.Launcher)6 LauncherAppState (com.android.launcher3.LauncherAppState)6 DragOptions (com.android.launcher3.dragndrop.DragOptions)6 LauncherIcons (com.android.launcher3.icons.LauncherIcons)6 WidgetImageView (com.android.launcher3.widget.WidgetImageView)6 DraggableView (com.android.launcher3.dragndrop.DraggableView)5 FastBitmapDrawable (com.android.launcher3.icons.FastBitmapDrawable)4 RoundDrawableWrapper (com.android.launcher3.icons.RoundDrawableWrapper)4 NavigableAppWidgetHostView (com.android.launcher3.widget.NavigableAppWidgetHostView)4 AppWidgetHostViewDragListener (com.android.launcher3.widget.dragndrop.AppWidgetHostViewDragListener)4