Search in sources :

Example 1 with AppWidgetHostViewDragListener

use of com.android.launcher3.widget.dragndrop.AppWidgetHostViewDragListener in project android_packages_apps_Launcher3 by crdroidandroid.

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);
    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);
            int width = deviceProfile.cellWidthPx * mAddInfo.spanX + padding.left + padding.right;
            int height = deviceProfile.cellHeightPx * mAddInfo.spanY + padding.top + padding.bottom;
            mAppWidgetHostViewPreview.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
        }
        if (mAppWidgetHostViewPreview != null) {
            previewSizeBeforeScale[0] = mAppWidgetHostViewPreview.getMeasuredWidth();
            launcher.getDragController().addDragListener(new AppWidgetHostViewDragListener(launcher));
        }
        if (preview == null && mAppWidgetHostViewPreview == null) {
            Drawable p = new FastBitmapDrawable(app.getWidgetCache().generateWidgetPreview(launcher, createWidgetInfo.info, maxWidth, null, previewSizeBeforeScale).first);
            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);
    }
    // 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 * 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) FastBitmapDrawable(com.android.launcher3.icons.FastBitmapDrawable) Drawable(android.graphics.drawable.Drawable) 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 2 with AppWidgetHostViewDragListener

use of com.android.launcher3.widget.dragndrop.AppWidgetHostViewDragListener in project android_packages_apps_Launcher3 by crdroidandroid.

the class Workspace method beginDragShared.

/**
 * Core functionality for beginning a drag operation for an item that will be dropped within
 * the workspace
 */
public DragView beginDragShared(View child, DraggableView draggableView, DragSource source, ItemInfo dragObject, DragPreviewProvider previewProvider, DragOptions dragOptions) {
    float iconScale = 1f;
    if (child instanceof BubbleTextView) {
        Drawable icon = ((BubbleTextView) child).getIcon();
        if (icon instanceof FastBitmapDrawable) {
            iconScale = ((FastBitmapDrawable) icon).getAnimatedScale();
        }
    }
    // Clear the pressed state if necessary
    child.clearFocus();
    child.setPressed(false);
    if (child instanceof BubbleTextView) {
        BubbleTextView icon = (BubbleTextView) child;
        icon.clearPressedBackground();
    }
    mOutlineProvider = previewProvider;
    if (draggableView == null && child instanceof DraggableView) {
        draggableView = (DraggableView) child;
    }
    final View contentView = previewProvider.getContentView();
    final float scale;
    // The draggable drawable follows the touch point around on the screen
    final Drawable drawable;
    if (contentView == null) {
        drawable = previewProvider.createDrawable();
        scale = previewProvider.getScaleAndPosition(drawable, mTempXY);
    } else {
        drawable = null;
        scale = previewProvider.getScaleAndPosition(contentView, mTempXY);
    }
    int halfPadding = previewProvider.previewPadding / 2;
    int dragLayerX = mTempXY[0];
    int dragLayerY = mTempXY[1];
    Point dragVisualizeOffset = null;
    Rect dragRect = new Rect();
    if (draggableView != null) {
        draggableView.getSourceVisualDragBounds(dragRect);
        dragLayerY += dragRect.top;
        dragVisualizeOffset = new Point(-halfPadding, halfPadding);
    }
    if (child.getParent() instanceof ShortcutAndWidgetContainer) {
        mDragSourceInternal = (ShortcutAndWidgetContainer) child.getParent();
    }
    if (child instanceof BubbleTextView && !dragOptions.isAccessibleDrag) {
        PopupContainerWithArrow popupContainer = PopupContainerWithArrow.showForIcon((BubbleTextView) child);
        if (popupContainer != null) {
            dragOptions.preDragCondition = popupContainer.createPreDragCondition();
        }
    }
    final DragView dv;
    if (contentView instanceof View) {
        if (contentView instanceof LauncherAppWidgetHostView) {
            mDragController.addDragListener(new AppWidgetHostViewDragListener(mLauncher));
        }
        dv = mDragController.startDrag(contentView, draggableView, dragLayerX, dragLayerY, source, dragObject, dragVisualizeOffset, dragRect, scale * iconScale, scale, dragOptions);
    } else {
        dv = mDragController.startDrag(drawable, draggableView, dragLayerX, dragLayerY, source, dragObject, dragVisualizeOffset, dragRect, scale * iconScale, scale, dragOptions);
    }
    return dv;
}
Also used : Rect(android.graphics.Rect) Drawable(android.graphics.drawable.Drawable) PreloadIconDrawable(com.android.launcher3.graphics.PreloadIconDrawable) FastBitmapDrawable(com.android.launcher3.icons.FastBitmapDrawable) DragView(com.android.launcher3.dragndrop.DragView) Point(android.graphics.Point) DraggableView(com.android.launcher3.dragndrop.DraggableView) DraggableView(com.android.launcher3.dragndrop.DraggableView) LauncherAppWidgetHostView(com.android.launcher3.widget.LauncherAppWidgetHostView) AppWidgetHostView(android.appwidget.AppWidgetHostView) View(android.view.View) PendingAppWidgetHostView(com.android.launcher3.widget.PendingAppWidgetHostView) DragView(com.android.launcher3.dragndrop.DragView) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point) FastBitmapDrawable(com.android.launcher3.icons.FastBitmapDrawable) AppWidgetHostViewDragListener(com.android.launcher3.widget.dragndrop.AppWidgetHostViewDragListener) PopupContainerWithArrow(com.android.launcher3.popup.PopupContainerWithArrow) LauncherAppWidgetHostView(com.android.launcher3.widget.LauncherAppWidgetHostView)

Aggregations

Point (android.graphics.Point)2 Rect (android.graphics.Rect)2 Drawable (android.graphics.drawable.Drawable)2 DraggableView (com.android.launcher3.dragndrop.DraggableView)2 FastBitmapDrawable (com.android.launcher3.icons.FastBitmapDrawable)2 AppWidgetHostViewDragListener (com.android.launcher3.widget.dragndrop.AppWidgetHostViewDragListener)2 SuppressLint (android.annotation.SuppressLint)1 AppWidgetHostView (android.appwidget.AppWidgetHostView)1 Paint (android.graphics.Paint)1 View (android.view.View)1 DeviceProfile (com.android.launcher3.DeviceProfile)1 Launcher (com.android.launcher3.Launcher)1 LauncherAppState (com.android.launcher3.LauncherAppState)1 DragView (com.android.launcher3.dragndrop.DragView)1 PreloadIconDrawable (com.android.launcher3.graphics.PreloadIconDrawable)1 LauncherIcons (com.android.launcher3.icons.LauncherIcons)1 RoundDrawableWrapper (com.android.launcher3.icons.RoundDrawableWrapper)1 PopupContainerWithArrow (com.android.launcher3.popup.PopupContainerWithArrow)1 LauncherAppWidgetHostView (com.android.launcher3.widget.LauncherAppWidgetHostView)1 PendingAppWidgetHostView (com.android.launcher3.widget.PendingAppWidgetHostView)1