Search in sources :

Example 16 with LauncherAppWidgetHostView

use of com.android.launcher3.widget.LauncherAppWidgetHostView 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 17 with LauncherAppWidgetHostView

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

the class Workspace method onDrop.

@Override
public void onDrop(final DragObject d, DragOptions options) {
    mDragViewVisualCenter = d.getVisualCenter(mDragViewVisualCenter);
    CellLayout dropTargetLayout = mDropToLayout;
    // We want the point to be mapped to the dragTarget.
    if (dropTargetLayout != null) {
        mapPointFromDropLayout(dropTargetLayout, mDragViewVisualCenter);
    }
    boolean droppedOnOriginalCell = false;
    int snapScreen = -1;
    boolean resizeOnDrop = false;
    if (d.dragSource != this || mDragInfo == null) {
        final int[] touchXY = new int[] { (int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1] };
        onDropExternal(touchXY, dropTargetLayout, d);
    } else {
        final View cell = mDragInfo.cell;
        boolean droppedOnOriginalCellDuringTransition = false;
        Runnable onCompleteRunnable = null;
        if (dropTargetLayout != null && !d.cancelled) {
            // Move internally
            boolean hasMovedLayouts = (getParentCellLayoutForView(cell) != dropTargetLayout);
            boolean hasMovedIntoHotseat = mLauncher.isHotseatLayout(dropTargetLayout);
            int container = hasMovedIntoHotseat ? LauncherSettings.Favorites.CONTAINER_HOTSEAT : LauncherSettings.Favorites.CONTAINER_DESKTOP;
            int screenId = (mTargetCell[0] < 0) ? mDragInfo.screenId : getIdForScreen(dropTargetLayout);
            int spanX = mDragInfo != null ? mDragInfo.spanX : 1;
            int spanY = mDragInfo != null ? mDragInfo.spanY : 1;
            // First we find the cell nearest to point at which the item is
            // dropped, without any consideration to whether there is an item there.
            mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], spanX, spanY, dropTargetLayout, mTargetCell);
            float distance = dropTargetLayout.getDistanceFromCell(mDragViewVisualCenter[0], mDragViewVisualCenter[1], mTargetCell);
            // cell also contains a shortcut, then create a folder with the two shortcuts.
            if (createUserFolderIfNecessary(cell, container, dropTargetLayout, mTargetCell, distance, false, d) || addToExistingFolderIfNecessary(cell, dropTargetLayout, mTargetCell, distance, d, false)) {
                mLauncher.getStateManager().goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
                return;
            }
            // Aside from the special case where we're dropping a shortcut onto a shortcut,
            // we need to find the nearest cell location that is vacant
            ItemInfo item = d.dragInfo;
            int minSpanX = item.spanX;
            int minSpanY = item.spanY;
            if (item.minSpanX > 0 && item.minSpanY > 0) {
                minSpanX = item.minSpanX;
                minSpanY = item.minSpanY;
            }
            droppedOnOriginalCell = item.screenId == screenId && item.container == container && item.cellX == mTargetCell[0] && item.cellY == mTargetCell[1];
            droppedOnOriginalCellDuringTransition = droppedOnOriginalCell && mIsSwitchingState;
            // When quickly moving an item, a user may accidentally rearrange their
            // workspace. So instead we move the icon back safely to its original position.
            boolean returnToOriginalCellToPreventShuffling = !isFinishedSwitchingState() && !droppedOnOriginalCellDuringTransition && !dropTargetLayout.isRegionVacant(mTargetCell[0], mTargetCell[1], spanX, spanY);
            int[] resultSpan = new int[2];
            if (returnToOriginalCellToPreventShuffling) {
                mTargetCell[0] = mTargetCell[1] = -1;
            } else {
                mTargetCell = dropTargetLayout.performReorder((int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY, cell, mTargetCell, resultSpan, CellLayout.MODE_ON_DROP);
            }
            boolean foundCell = mTargetCell[0] >= 0 && mTargetCell[1] >= 0;
            // if the widget resizes on drop
            if (foundCell && (cell instanceof AppWidgetHostView) && (resultSpan[0] != item.spanX || resultSpan[1] != item.spanY)) {
                resizeOnDrop = true;
                item.spanX = resultSpan[0];
                item.spanY = resultSpan[1];
                AppWidgetHostView awhv = (AppWidgetHostView) cell;
                AppWidgetResizeFrame.updateWidgetSizeRanges(awhv, mLauncher, resultSpan[0], resultSpan[1]);
            }
            if (foundCell) {
                if (getScreenIdForPageIndex(mCurrentPage) != screenId && !hasMovedIntoHotseat) {
                    snapScreen = getPageIndexForScreenId(screenId);
                    snapToPage(snapScreen);
                }
                final ItemInfo info = (ItemInfo) cell.getTag();
                if (hasMovedLayouts) {
                    // Reparent the view
                    CellLayout parentCell = getParentCellLayoutForView(cell);
                    if (parentCell != null) {
                        parentCell.removeView(cell);
                    } else if (FeatureFlags.IS_STUDIO_BUILD) {
                        throw new NullPointerException("mDragInfo.cell has null parent");
                    }
                    addInScreen(cell, container, screenId, mTargetCell[0], mTargetCell[1], info.spanX, info.spanY);
                }
                // update the item's position after drop
                CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams();
                lp.cellX = lp.tmpCellX = mTargetCell[0];
                lp.cellY = lp.tmpCellY = mTargetCell[1];
                lp.cellHSpan = item.spanX;
                lp.cellVSpan = item.spanY;
                lp.isLockedToGrid = true;
                if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT && cell instanceof LauncherAppWidgetHostView) {
                    final CellLayout cellLayout = dropTargetLayout;
                    // We post this call so that the widget has a chance to be placed
                    // in its final location
                    final LauncherAppWidgetHostView hostView = (LauncherAppWidgetHostView) cell;
                    AppWidgetProviderInfo pInfo = hostView.getAppWidgetInfo();
                    if (pInfo != null && !options.isAccessibleDrag) {
                        onCompleteRunnable = () -> {
                            if (!isPageInTransition()) {
                                AppWidgetResizeFrame.showForWidget(hostView, cellLayout);
                            }
                        };
                    }
                }
                mLauncher.getModelWriter().modifyItemInDatabase(info, container, screenId, lp.cellX, lp.cellY, item.spanX, item.spanY);
            } else {
                if (!returnToOriginalCellToPreventShuffling) {
                    onNoCellFound(dropTargetLayout);
                }
                // If we can't find a drop location, we return the item to its original position
                CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams();
                mTargetCell[0] = lp.cellX;
                mTargetCell[1] = lp.cellY;
                CellLayout layout = (CellLayout) cell.getParent().getParent();
                layout.markCellsAsOccupiedForView(cell);
            }
        }
        final CellLayout parent = (CellLayout) cell.getParent().getParent();
        if (d.dragView.hasDrawn()) {
            if (droppedOnOriginalCellDuringTransition) {
                // Animate the item to its original position, while simultaneously exiting
                // spring-loaded mode so the page meets the icon where it was picked up.
                mLauncher.getDragController().animateDragViewToOriginalPosition(onCompleteRunnable, cell, SPRING_LOADED.getTransitionDuration(mLauncher));
                mLauncher.getStateManager().goToState(NORMAL);
                mLauncher.getDropTargetBar().onDragEnd();
                parent.onDropChild(cell);
                return;
            }
            final ItemInfo info = (ItemInfo) cell.getTag();
            boolean isWidget = info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET || info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
            if (isWidget) {
                int animationType = resizeOnDrop ? ANIMATE_INTO_POSITION_AND_RESIZE : ANIMATE_INTO_POSITION_AND_DISAPPEAR;
                animateWidgetDrop(info, parent, d.dragView, null, animationType, cell, false);
            } else {
                int duration = snapScreen < 0 ? -1 : ADJACENT_SCREEN_DROP_DURATION;
                mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, cell, duration, this);
            }
        } else {
            d.deferDragViewCleanupPostAnimation = false;
            cell.setVisibility(VISIBLE);
        }
        parent.onDropChild(cell);
        mLauncher.getStateManager().goToState(NORMAL, SPRING_LOADED_EXIT_DELAY, onCompleteRunnable);
        mStatsLogManager.logger().withItemInfo(d.dragInfo).withInstanceId(d.logInstanceId).log(LauncherEvent.LAUNCHER_ITEM_DROP_COMPLETED);
    }
    if (d.stateAnnouncer != null && !droppedOnOriginalCell) {
        d.stateAnnouncer.completeAction(R.string.item_moved);
    }
}
Also used : WorkspaceItemInfo(com.android.launcher3.model.data.WorkspaceItemInfo) ItemInfo(com.android.launcher3.model.data.ItemInfo) 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) LauncherAppWidgetHostView(com.android.launcher3.widget.LauncherAppWidgetHostView) AppWidgetHostView(android.appwidget.AppWidgetHostView) PendingAppWidgetHostView(com.android.launcher3.widget.PendingAppWidgetHostView) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) LauncherAppWidgetHostView(com.android.launcher3.widget.LauncherAppWidgetHostView)

Example 18 with LauncherAppWidgetHostView

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

the class LauncherAccessibilityDelegate method getSupportedResizeActions.

private IntArray getSupportedResizeActions(View host, LauncherAppWidgetInfo info) {
    IntArray actions = new IntArray();
    AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo();
    if (providerInfo == null) {
        return actions;
    }
    CellLayout layout = (CellLayout) host.getParent().getParent();
    if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) {
        if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) || layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) {
            actions.add(R.string.action_increase_width);
        }
        if (info.spanX > info.minSpanX && info.spanX > 1) {
            actions.add(R.string.action_decrease_width);
        }
    }
    if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) {
        if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) || layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) {
            actions.add(R.string.action_increase_height);
        }
        if (info.spanY > info.minSpanY && info.spanY > 1) {
            actions.add(R.string.action_decrease_height);
        }
    }
    return actions;
}
Also used : IntArray(com.android.launcher3.util.IntArray) CellLayout(com.android.launcher3.CellLayout) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) LauncherAppWidgetHostView(com.android.launcher3.widget.LauncherAppWidgetHostView)

Example 19 with LauncherAppWidgetHostView

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

the class LauncherAppWidgetHost method createView.

public AppWidgetHostView createView(Context context, int appWidgetId, LauncherAppWidgetProviderInfo appWidget) {
    if (appWidget.isCustomWidget()) {
        LauncherAppWidgetHostView lahv = new LauncherAppWidgetHostView(context);
        lahv.setAppWidget(0, appWidget);
        CustomWidgetManager.INSTANCE.get(context).onViewCreated(lahv);
        return lahv;
    } else if ((mFlags & FLAG_LISTENING) == 0) {
        DeferredAppWidgetHostView view = new DeferredAppWidgetHostView(context);
        view.setAppWidget(appWidgetId, appWidget);
        mViews.put(appWidgetId, view);
        return view;
    } else {
        try {
            return super.createView(context, appWidgetId, appWidget);
        } catch (Exception e) {
            if (!Utilities.isBinderSizeError(e)) {
                throw new RuntimeException(e);
            }
            // If the exception was thrown while fetching the remote views, let the view stay.
            // This will ensure that if the widget posts a valid update later, the view
            // will update.
            LauncherAppWidgetHostView view = mViews.get(appWidgetId);
            if (view == null) {
                view = onCreateView(mContext, appWidgetId, appWidget);
            }
            view.setAppWidget(appWidgetId, appWidget);
            view.switchToErrorView();
            return view;
        }
    }
}
Also used : LauncherAppWidgetHostView(com.android.launcher3.widget.LauncherAppWidgetHostView) DeferredAppWidgetHostView(com.android.launcher3.widget.DeferredAppWidgetHostView) ActivityNotFoundException(android.content.ActivityNotFoundException)

Example 20 with LauncherAppWidgetHostView

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

the class AppWidgetResizeFrame method showForWidget.

public static void showForWidget(LauncherAppWidgetHostView widget, CellLayout cellLayout) {
    Launcher launcher = Launcher.getLauncher(cellLayout.getContext());
    AbstractFloatingView.closeAllOpenViews(launcher);
    DragLayer dl = launcher.getDragLayer();
    AppWidgetResizeFrame frame = (AppWidgetResizeFrame) launcher.getLayoutInflater().inflate(R.layout.app_widget_resize_frame, dl, false);
    frame.setupForWidget(widget, cellLayout, dl);
    ((DragLayer.LayoutParams) frame.getLayoutParams()).customPosition = true;
    dl.addView(frame);
    frame.mIsOpen = true;
    frame.snapToWidget(false);
}
Also used : DragLayer(com.android.launcher3.dragndrop.DragLayer)

Aggregations

LauncherAppWidgetHostView (com.android.launcher3.widget.LauncherAppWidgetHostView)89 Point (android.graphics.Point)46 View (android.view.View)44 DraggableView (com.android.launcher3.dragndrop.DraggableView)34 SuppressLint (android.annotation.SuppressLint)32 PendingAppWidgetHostView (com.android.launcher3.widget.PendingAppWidgetHostView)30 Rect (android.graphics.Rect)28 DragView (com.android.launcher3.dragndrop.DragView)27 ItemInfo (com.android.launcher3.model.data.ItemInfo)26 LauncherAppWidgetInfo (com.android.launcher3.model.data.LauncherAppWidgetInfo)25 AppWidgetHostView (android.appwidget.AppWidgetHostView)22 Drawable (android.graphics.drawable.Drawable)21 Paint (android.graphics.Paint)17 FastBitmapDrawable (com.android.launcher3.icons.FastBitmapDrawable)15 AppWidgetProviderInfo (android.appwidget.AppWidgetProviderInfo)14 CellLayout (com.android.launcher3.CellLayout)14 Launcher (com.android.launcher3.Launcher)12 DragLayer (com.android.launcher3.dragndrop.DragLayer)12 Thunk (com.android.launcher3.util.Thunk)12 BubbleTextView (com.android.launcher3.BubbleTextView)11