Search in sources :

Example 66 with Workspace

use of com.android.launcher3.tapl.Workspace in project android_packages_apps_Launcher3 by AOSPA.

the class WidgetsPredicationUpdateTaskTest method widgetsRecommendationRan_shouldOnlyReturnNotAddedWidgetsInAppPredictionOrder.

@Test
public void widgetsRecommendationRan_shouldOnlyReturnNotAddedWidgetsInAppPredictionOrder() throws Exception {
    // WHEN newPredicationTask is executed with app predication of 5 apps.
    AppTarget app1 = new AppTarget(new AppTargetId("app1"), "app1", "className", mUserHandle);
    AppTarget app2 = new AppTarget(new AppTargetId("app2"), "app2", "className", mUserHandle);
    AppTarget app3 = new AppTarget(new AppTargetId("app3"), "app3", "className", mUserHandle);
    AppTarget app4 = new AppTarget(new AppTargetId("app4"), "app4", "className", mUserHandle);
    AppTarget app5 = new AppTarget(new AppTargetId("app5"), "app5", "className", mUserHandle);
    mModelHelper.executeTaskForTest(newWidgetsPredicationTask(List.of(app5, app3, app2, app4, app1))).forEach(Runnable::run);
    // THEN only 3 widgets are returned because
    // 1. app5/provider1 & app4/provider1 have already been added to workspace. They are
    // excluded from the result.
    // 2. app3 doesn't have a widget.
    // 3. only 1 widget is picked from app1 because we only want to promote one widget per app.
    List<PendingAddWidgetInfo> recommendedWidgets = mCallback.mRecommendedWidgets.items.stream().map(itemInfo -> (PendingAddWidgetInfo) itemInfo).collect(Collectors.toList());
    assertThat(recommendedWidgets).hasSize(3);
    assertWidgetInfo(recommendedWidgets.get(0).info, mApp2Provider1);
    assertWidgetInfo(recommendedWidgets.get(1).info, mApp4Provider2);
    assertWidgetInfo(recommendedWidgets.get(2).info, mApp1Provider1);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CONTAINER_WIDGETS_PREDICTION(com.android.launcher3.LauncherSettings.Favorites.CONTAINER_WIDGETS_PREDICTION) Arrays(java.util.Arrays) MODEL_EXECUTOR(com.android.launcher3.util.Executors.MODEL_EXECUTOR) AppTargetId(android.app.prediction.AppTargetId) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Mock(org.mockito.Mock) PendingAddWidgetInfo(com.android.launcher3.widget.PendingAddWidgetInfo) RunWith(org.junit.runner.RunWith) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) IconCache(com.android.launcher3.icons.IconCache) AppTarget(android.app.prediction.AppTarget) MockitoAnnotations(org.mockito.MockitoAnnotations) Process.myUserHandle(android.os.Process.myUserHandle) Mockito.doAnswer(org.mockito.Mockito.doAnswer) UserHandle(android.os.UserHandle) After(org.junit.After) MAIN_EXECUTOR(com.android.launcher3.util.Executors.MAIN_EXECUTOR) WidgetUtils.createAppWidgetProviderInfo(com.android.launcher3.util.WidgetUtils.createAppWidgetProviderInfo) Mockito.doReturn(org.mockito.Mockito.doReturn) Log(android.util.Log) Before(org.junit.Before) SmallTest(androidx.test.filters.SmallTest) LauncherModelHelper(com.android.launcher3.util.LauncherModelHelper) ComponentName(android.content.ComponentName) LauncherAppState(com.android.launcher3.LauncherAppState) TextUtils(android.text.TextUtils) FeatureFlags(com.android.launcher3.config.FeatureFlags) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) Collectors(java.util.stream.Collectors) AppWidgetManager(android.appwidget.AppWidgetManager) List(java.util.List) LauncherAppWidgetProviderInfo(com.android.launcher3.widget.LauncherAppWidgetProviderInfo) FixedContainerItems(com.android.launcher3.model.BgDataModel.FixedContainerItems) ComponentWithLabel(com.android.launcher3.icons.ComponentWithLabel) PredictorState(com.android.launcher3.model.QuickstepModelDelegate.PredictorState) AppTarget(android.app.prediction.AppTarget) PendingAddWidgetInfo(com.android.launcher3.widget.PendingAddWidgetInfo) AppTargetId(android.app.prediction.AppTargetId) SmallTest(androidx.test.filters.SmallTest) Test(org.junit.Test)

Example 67 with Workspace

use of com.android.launcher3.tapl.Workspace in project android_packages_apps_Launcher3 by AOSPA.

the class CellLayout method applyColorExtractionOnWidget.

/**
 * Applies the local color extraction to a dragging widget object.
 */
private void applyColorExtractionOnWidget(DropTarget.DragObject dragObject, int[] targetCell, int spanX, int spanY) {
    // Apply local extracted color if the DragView is an AppWidgetHostViewDrawable.
    View view = dragObject.dragView.getContentView();
    if (view instanceof LauncherAppWidgetHostView) {
        Launcher launcher = Launcher.getLauncher(getContext());
        Workspace workspace = launcher.getWorkspace();
        int screenId = workspace.getIdForScreen(this);
        cellToRect(targetCell[0], targetCell[1], spanX, spanY, mTempRect);
        ((LauncherAppWidgetHostView) view).handleDrag(mTempRect, this, screenId);
    }
}
Also used : LauncherAppWidgetHostView(com.android.launcher3.widget.LauncherAppWidgetHostView) DraggableView(com.android.launcher3.dragndrop.DraggableView) View(android.view.View) LauncherAppWidgetHostView(com.android.launcher3.widget.LauncherAppWidgetHostView) Paint(android.graphics.Paint) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point)

Example 68 with Workspace

use of com.android.launcher3.tapl.Workspace in project android_packages_apps_Launcher3 by AOSPA.

the class DatabaseWidgetPreviewLoader method generateWidgetPreview.

/**
 * Generates the widget preview from either the {@link WidgetManagerHelper} or cache
 * and add badge at the bottom right corner.
 *
 * @param info                        information about the widget
 * @param maxPreviewWidth             width of the preview on either workspace or tray
 * @param preScaledWidthOut           return the width of the returned bitmap
 */
public Bitmap generateWidgetPreview(LauncherAppWidgetProviderInfo info, int maxPreviewWidth, int[] preScaledWidthOut) {
    // Load the preview image if possible
    if (maxPreviewWidth < 0)
        maxPreviewWidth = Integer.MAX_VALUE;
    Drawable drawable = null;
    if (info.previewImage != 0) {
        try {
            drawable = info.loadPreviewImage(mContext, 0);
        } catch (OutOfMemoryError e) {
            Log.w(TAG, "Error loading widget preview for: " + info.provider, e);
            // During OutOfMemoryError, the previous heap stack is not affected. Catching
            // an OOM error here should be safe & not affect other parts of launcher.
            drawable = null;
        }
        if (drawable != null) {
            drawable = mutateOnMainThread(drawable);
        } else {
            Log.w(TAG, "Can't load widget preview drawable 0x" + Integer.toHexString(info.previewImage) + " for provider: " + info.provider);
        }
    }
    final boolean widgetPreviewExists = (drawable != null);
    final int spanX = info.spanX;
    final int spanY = info.spanY;
    int previewWidth;
    int previewHeight;
    DeviceProfile dp = ActivityContext.lookupContext(mContext).getDeviceProfile();
    if (widgetPreviewExists && drawable.getIntrinsicWidth() > 0 && drawable.getIntrinsicHeight() > 0) {
        previewWidth = drawable.getIntrinsicWidth();
        previewHeight = drawable.getIntrinsicHeight();
    } else {
        Size widgetSize = WidgetSizes.getWidgetPaddedSizePx(mContext, info.provider, dp, spanX, spanY);
        previewWidth = widgetSize.getWidth();
        previewHeight = widgetSize.getHeight();
    }
    if (preScaledWidthOut != null) {
        preScaledWidthOut[0] = previewWidth;
    }
    // Scale to fit width only - let the widget preview be clipped in the
    // vertical dimension
    final float scale = previewWidth > maxPreviewWidth ? (maxPreviewWidth / (float) (previewWidth)) : 1f;
    if (scale != 1f) {
        previewWidth = Math.max((int) (scale * previewWidth), 1);
        previewHeight = Math.max((int) (scale * previewHeight), 1);
    }
    final int previewWidthF = previewWidth;
    final int previewHeightF = previewHeight;
    final Drawable drawableF = drawable;
    return BitmapRenderer.createHardwareBitmap(previewWidth, previewHeight, c -> {
        // Draw the scaled preview into the final bitmap
        if (widgetPreviewExists) {
            drawableF.setBounds(0, 0, previewWidthF, previewHeightF);
            drawableF.draw(c);
        } else {
            RectF boxRect;
            // Draw horizontal and vertical lines to represent individual columns.
            final Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
            if (Utilities.ATLEAST_S) {
                boxRect = new RectF(/* left= */
                0, /* top= */
                0, /* right= */
                previewWidthF, /* bottom= */
                previewHeightF);
                p.setStyle(Paint.Style.FILL);
                p.setColor(Color.WHITE);
                float roundedCorner = mContext.getResources().getDimension(android.R.dimen.system_app_widget_background_radius);
                c.drawRoundRect(boxRect, roundedCorner, roundedCorner, p);
            } else {
                boxRect = drawBoxWithShadow(c, previewWidthF, previewHeightF);
            }
            p.setStyle(Paint.Style.STROKE);
            p.setStrokeWidth(mContext.getResources().getDimension(R.dimen.widget_preview_cell_divider_width));
            p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            float t = boxRect.left;
            float tileSize = boxRect.width() / spanX;
            for (int i = 1; i < spanX; i++) {
                t += tileSize;
                c.drawLine(t, 0, t, previewHeightF, p);
            }
            t = boxRect.top;
            tileSize = boxRect.height() / spanY;
            for (int i = 1; i < spanY; i++) {
                t += tileSize;
                c.drawLine(0, t, previewWidthF, t, p);
            }
            // Draw icon in the center.
            try {
                Drawable icon = LauncherAppState.getInstance(mContext).getIconCache().getFullResIcon(info.provider.getPackageName(), info.icon);
                if (icon != null) {
                    int appIconSize = dp.iconSizePx;
                    int iconSize = (int) Math.min(appIconSize * scale, Math.min(boxRect.width(), boxRect.height()));
                    icon = mutateOnMainThread(icon);
                    int hoffset = (previewWidthF - iconSize) / 2;
                    int yoffset = (previewHeightF - iconSize) / 2;
                    icon.setBounds(hoffset, yoffset, hoffset + iconSize, yoffset + iconSize);
                    icon.draw(c);
                }
            } catch (Resources.NotFoundException e) {
            }
        }
    });
}
Also used : RectF(android.graphics.RectF) DeviceProfile(com.android.launcher3.DeviceProfile) Size(android.util.Size) PorterDuffXfermode(android.graphics.PorterDuffXfermode) Drawable(android.graphics.drawable.Drawable) FastBitmapDrawable(com.android.launcher3.icons.FastBitmapDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Paint(android.graphics.Paint) Resources(android.content.res.Resources) Paint(android.graphics.Paint)

Example 69 with Workspace

use of com.android.launcher3.tapl.Workspace 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 70 with Workspace

use of com.android.launcher3.tapl.Workspace in project android_packages_apps_Launcher3 by AOSPA.

the class StaggeredWorkspaceAnim method addStaggeredAnimationForView.

/**
 * Adds an alpha/trans animator for {@param v}, with a start delay based on the view's row.
 *
 * @param v A view on the workspace.
 * @param row The bottom-most row that contains the view.
 * @param totalRows Total number of rows.
 */
private void addStaggeredAnimationForView(View v, int row, int totalRows) {
    if (mIgnoredView != null && mIgnoredView == v)
        return;
    // Invert the rows, because we stagger starting from the bottom of the screen.
    int invertedRow = totalRows - row;
    // Add 1 to the inverted row so that the bottom most row has a start delay.
    long startDelay = (long) ((invertedRow + 1) * APP_CLOSE_ROW_START_DELAY_MS);
    v.setTranslationY(mSpringTransY);
    ResourceProvider rp = DynamicResource.provider(v.getContext());
    float stiffness = rp.getFloat(R.dimen.staggered_stiffness);
    float damping = rp.getFloat(R.dimen.staggered_damping_ratio);
    float endTransY = 0;
    float springVelocity = Math.abs(mVelocity) * Math.signum(endTransY - mSpringTransY);
    ValueAnimator springTransY = new SpringAnimationBuilder(v.getContext()).setStiffness(stiffness).setDampingRatio(damping).setMinimumVisibleChange(1f).setStartValue(mSpringTransY).setEndValue(endTransY).setStartVelocity(springVelocity).build(v, VIEW_TRANSLATE_Y);
    springTransY.setStartDelay(startDelay);
    springTransY.addListener(new AnimatorListenerAdapter() {

        @Override
        public void onAnimationEnd(Animator animation) {
            v.setTranslationY(0f);
        }
    });
    mAnimators.play(springTransY);
    v.setAlpha(0);
    ObjectAnimator alpha = ObjectAnimator.ofFloat(v, View.ALPHA, 0f, 1f);
    alpha.setInterpolator(LINEAR);
    alpha.setDuration(ALPHA_DURATION_MS);
    alpha.setStartDelay(startDelay);
    alpha.addListener(new AnimatorListenerAdapter() {

        @Override
        public void onAnimationEnd(Animator animation) {
            v.setAlpha(1f);
        }
    });
    mAnimators.play(alpha);
}
Also used : Animator(android.animation.Animator) ObjectAnimator(android.animation.ObjectAnimator) ValueAnimator(android.animation.ValueAnimator) AnimatorListenerAdapter(android.animation.AnimatorListenerAdapter) ObjectAnimator(android.animation.ObjectAnimator) ResourceProvider(com.android.systemui.plugins.ResourceProvider) SpringAnimationBuilder(com.android.launcher3.anim.SpringAnimationBuilder) ValueAnimator(android.animation.ValueAnimator)

Aggregations

View (android.view.View)143 WorkspaceItemInfo (com.android.launcher3.model.data.WorkspaceItemInfo)124 Point (android.graphics.Point)115 LauncherAppWidgetHostView (com.android.launcher3.widget.LauncherAppWidgetHostView)97 Rect (android.graphics.Rect)93 ArrayList (java.util.ArrayList)91 ItemInfo (com.android.launcher3.model.data.ItemInfo)86 DragView (com.android.launcher3.dragndrop.DragView)77 PendingAppWidgetHostView (com.android.launcher3.widget.PendingAppWidgetHostView)73 AppWidgetHostView (android.appwidget.AppWidgetHostView)72 Workspace (com.android.launcher3.Workspace)63 SuppressLint (android.annotation.SuppressLint)58 DraggableView (com.android.launcher3.dragndrop.DraggableView)58 Test (org.junit.Test)57 FolderInfo (com.android.launcher3.model.data.FolderInfo)55 CellLayout (com.android.launcher3.CellLayout)51 LauncherAppWidgetInfo (com.android.launcher3.model.data.LauncherAppWidgetInfo)47 IntArray (com.android.launcher3.util.IntArray)45 Intent (android.content.Intent)43 ValueAnimator (android.animation.ValueAnimator)41