Search in sources :

Example 31 with FolderAdaptiveIcon

use of com.android.launcher3.dragndrop.FolderAdaptiveIcon in project Neo-Launcher by NeoApplications.

the class DragView method setItemInfo.

/**
 * Initialize {@code #mIconDrawable} if the item can be represented using
 * an {@link AdaptiveIconDrawable} or {@link FolderAdaptiveIcon}.
 */
@TargetApi(Build.VERSION_CODES.O)
public void setItemInfo(final ItemInfo info) {
    if (!Utilities.ATLEAST_OREO) {
        return;
    }
    if (info.itemType != LauncherSettings.Favorites.ITEM_TYPE_APPLICATION && info.itemType != LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT && info.itemType != LauncherSettings.Favorites.ITEM_TYPE_FOLDER) {
        return;
    }
    // Load the adaptive icon on a background thread and add the view in ui thread.
    MODEL_EXECUTOR.getHandler().postAtFrontOfQueue(new Runnable() {

        @Override
        public void run() {
            Object[] outObj = new Object[1];
            int w = mBitmap.getWidth();
            int h = mBitmap.getHeight();
            Drawable dr = Utilities.getFullDrawable(mLauncher, info, w, h, false, /* flattenDrawable */
            outObj);
            if (dr instanceof AdaptiveIconDrawable) {
                int blurMargin = (int) mLauncher.getResources().getDimension(R.dimen.blur_size_medium_outline) / 2;
                Rect bounds = new Rect(0, 0, w, h);
                bounds.inset(blurMargin, blurMargin);
                // Badge is applied after icon normalization so the bounds for badge should not
                // be scaled down due to icon normalization.
                Rect badgeBounds = new Rect(bounds);
                mBadge = getBadge(mLauncher, info, outObj[0]);
                mBadge.setBounds(badgeBounds);
                // Do not draw the background in case of folder as its translucent
                mDrawBitmap = !(dr instanceof FolderAdaptiveIcon);
                try (LauncherIcons li = LauncherIcons.obtain(mLauncher)) {
                    // drawable to be normalized
                    Drawable nDr;
                    if (mDrawBitmap) {
                        nDr = dr;
                    } else {
                        // Since we just want the scale, avoid heavy drawing operations
                        nDr = new AdaptiveIconDrawable(new ColorDrawable(Color.BLACK), null);
                    }
                    Utilities.scaleRectAboutCenter(bounds, li.getNormalizer().getScale(nDr, null, null, null));
                }
                AdaptiveIconDrawable adaptiveIcon = (AdaptiveIconDrawable) dr;
                // Shrink very tiny bit so that the clip path is smaller than the original bitmap
                // that has anti aliased edges and shadows.
                Rect shrunkBounds = new Rect(bounds);
                Utilities.scaleRectAboutCenter(shrunkBounds, 0.98f);
                adaptiveIcon.setBounds(shrunkBounds);
                final Path mask = adaptiveIcon.getIconMask();
                mTranslateX = new SpringFloatValue(DragView.this, w * AdaptiveIconDrawable.getExtraInsetFraction());
                mTranslateY = new SpringFloatValue(DragView.this, h * AdaptiveIconDrawable.getExtraInsetFraction());
                bounds.inset((int) (-bounds.width() * AdaptiveIconDrawable.getExtraInsetFraction()), (int) (-bounds.height() * AdaptiveIconDrawable.getExtraInsetFraction()));
                mBgSpringDrawable = adaptiveIcon.getBackground();
                if (mBgSpringDrawable == null) {
                    mBgSpringDrawable = new ColorDrawable(Color.TRANSPARENT);
                }
                mBgSpringDrawable.setBounds(bounds);
                mFgSpringDrawable = adaptiveIcon.getForeground();
                if (mFgSpringDrawable == null) {
                    mFgSpringDrawable = new ColorDrawable(Color.TRANSPARENT);
                }
                mFgSpringDrawable.setBounds(bounds);
                new Handler(Looper.getMainLooper()).post(new Runnable() {

                    @Override
                    public void run() {
                        // Assign the variable on the UI thread to avoid race conditions.
                        mScaledMaskPath = mask;
                        if (info.isDisabled()) {
                            FastBitmapDrawable d = new FastBitmapDrawable((Bitmap) null);
                            d.setIsDisabled(true);
                            mBaseFilter = (ColorMatrixColorFilter) d.getColorFilter();
                        }
                        updateColorFilter();
                    }
                });
            }
        }
    });
}
Also used : Path(android.graphics.Path) FastBitmapDrawable(com.android.launcher3.FastBitmapDrawable) Rect(android.graphics.Rect) ColorDrawable(android.graphics.drawable.ColorDrawable) LauncherIcons(com.android.launcher3.icons.LauncherIcons) AdaptiveIconDrawable(android.graphics.drawable.AdaptiveIconDrawable) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) FastBitmapDrawable(com.android.launcher3.FastBitmapDrawable) Handler(android.os.Handler) AdaptiveIconDrawable(android.graphics.drawable.AdaptiveIconDrawable) TargetApi(android.annotation.TargetApi)

Example 32 with FolderAdaptiveIcon

use of com.android.launcher3.dragndrop.FolderAdaptiveIcon in project Neo-Launcher by NeoApplications.

the class FolderAdaptiveIcon method createDrawableOnUiThread.

/**
 * Initializes various bitmaps on the UI thread and returns the final drawable.
 */
private static FolderAdaptiveIcon createDrawableOnUiThread(FolderIcon icon, Bitmap badgeBitmap, Point dragViewSize) {
    Preconditions.assertUIThread();
    float margin = icon.getResources().getDimension(R.dimen.blur_size_medium_outline) / 2;
    Canvas c = new Canvas();
    PreviewBackground bg = icon.getFolderBackground();
    // Initialize badge
    c.setBitmap(badgeBitmap);
    bg.drawShadow(c);
    bg.drawBackgroundStroke(c);
    icon.drawDot(c);
    // Initialize preview
    final float sizeScaleFactor = 1 + 2 * AdaptiveIconDrawable.getExtraInsetFraction();
    final int previewWidth = (int) (dragViewSize.x * sizeScaleFactor);
    final int previewHeight = (int) (dragViewSize.y * sizeScaleFactor);
    final float shiftFactor = AdaptiveIconDrawable.getExtraInsetFraction() / sizeScaleFactor;
    final float previewShiftX = shiftFactor * previewWidth;
    final float previewShiftY = shiftFactor * previewHeight;
    Bitmap previewBitmap = BitmapRenderer.createHardwareBitmap(previewWidth, previewHeight, (canvas) -> {
        int count = canvas.save();
        canvas.translate(previewShiftX, previewShiftY);
        icon.getPreviewItemManager().draw(canvas);
        canvas.restoreToCount(count);
    });
    // Initialize mask
    Path mask = new Path();
    Matrix m = new Matrix();
    m.setTranslate(margin, margin);
    bg.getClipPath().transform(m, mask);
    ShiftedBitmapDrawable badge = new ShiftedBitmapDrawable(badgeBitmap, margin, margin);
    ShiftedBitmapDrawable foreground = new ShiftedBitmapDrawable(previewBitmap, margin - previewShiftX, margin - previewShiftY);
    return new FolderAdaptiveIcon(new ColorDrawable(bg.getBgColor()), foreground, badge, mask);
}
Also used : Path(android.graphics.Path) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) ColorDrawable(android.graphics.drawable.ColorDrawable) PreviewBackground(com.android.launcher3.folder.PreviewBackground) Canvas(android.graphics.Canvas) ShiftedBitmapDrawable(com.android.launcher3.graphics.ShiftedBitmapDrawable) Point(android.graphics.Point)

Example 33 with FolderAdaptiveIcon

use of com.android.launcher3.dragndrop.FolderAdaptiveIcon in project Neo-Launcher by NeoApplications.

the class FolderAdaptiveIcon method createFolderAdaptiveIcon.

@Nullable
public static FolderAdaptiveIcon createFolderAdaptiveIcon(Launcher launcher, int folderId, Point dragViewSize) {
    Preconditions.assertNonUiThread();
    int margin = launcher.getResources().getDimensionPixelSize(R.dimen.blur_size_medium_outline);
    // Allocate various bitmaps on the background thread, because why not!
    int width = dragViewSize.x - margin;
    int height = dragViewSize.y - margin;
    if (width <= 0 || height <= 0) {
        return null;
    }
    final Bitmap badge = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    // FolderIcon draw pass
    try {
        return MAIN_EXECUTOR.submit(() -> {
            FolderIcon icon = launcher.findFolderIcon(folderId);
            return icon == null ? null : createDrawableOnUiThread(icon, badge, dragViewSize);
        }).get();
    } catch (Exception e) {
        Log.e(TAG, "Unable to create folder icon", e);
        return null;
    }
}
Also used : Bitmap(android.graphics.Bitmap) FolderIcon(com.android.launcher3.folder.FolderIcon) Point(android.graphics.Point) Nullable(androidx.annotation.Nullable)

Example 34 with FolderAdaptiveIcon

use of com.android.launcher3.dragndrop.FolderAdaptiveIcon in project android_packages_apps_Trebuchet by LineageOS.

the class ClipIconView method setIcon.

/**
 * Sets the icon for this view as part of initial setup
 */
public void setIcon(@Nullable Drawable drawable, int iconOffset, MarginLayoutParams lp, boolean isOpening, boolean isVerticalBarLayout, DeviceProfile dp) {
    mIsAdaptiveIcon = drawable instanceof AdaptiveIconDrawable;
    if (mIsAdaptiveIcon) {
        boolean isFolderIcon = drawable instanceof FolderAdaptiveIcon;
        AdaptiveIconDrawable adaptiveIcon = (AdaptiveIconDrawable) drawable;
        Drawable background = adaptiveIcon.getBackground();
        if (background == null) {
            background = new ColorDrawable(Color.TRANSPARENT);
        }
        mBackground = background;
        Drawable foreground = adaptiveIcon.getForeground();
        if (foreground == null) {
            foreground = new ColorDrawable(Color.TRANSPARENT);
        }
        mForeground = foreground;
        final int originalHeight = lp.height;
        final int originalWidth = lp.width;
        int blurMargin = mBlurSizeOutline / 2;
        mFinalDrawableBounds.set(0, 0, originalWidth, originalHeight);
        if (!isFolderIcon) {
            mFinalDrawableBounds.inset(iconOffset - blurMargin, iconOffset - blurMargin);
        }
        mForeground.setBounds(mFinalDrawableBounds);
        mBackground.setBounds(mFinalDrawableBounds);
        mStartRevealRect.set(0, 0, originalWidth, originalHeight);
        if (!isFolderIcon) {
            Utilities.scaleRectAboutCenter(mStartRevealRect, IconShape.getNormalizationScale());
        }
        if (isVerticalBarLayout) {
            lp.width = (int) Math.max(lp.width, lp.height * dp.aspectRatio);
        } else {
            lp.height = (int) Math.max(lp.height, lp.width * dp.aspectRatio);
        }
        int left = mIsRtl ? dp.widthPx - lp.getMarginStart() - lp.width : lp.leftMargin;
        layout(left, lp.topMargin, left + lp.width, lp.topMargin + lp.height);
        float scale = Math.max((float) lp.height / originalHeight, (float) lp.width / originalWidth);
        float bgDrawableStartScale;
        if (isOpening) {
            bgDrawableStartScale = 1f;
            mOutline.set(0, 0, originalWidth, originalHeight);
        } else {
            bgDrawableStartScale = scale;
            mOutline.set(0, 0, lp.width, lp.height);
        }
        setBackgroundDrawableBounds(bgDrawableStartScale, isVerticalBarLayout);
        mEndRevealRect.set(0, 0, lp.width, lp.height);
        setOutlineProvider(new ViewOutlineProvider() {

            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(mOutline, mTaskCornerRadius);
            }
        });
        setClipToOutline(true);
    } else {
        setBackground(drawable);
        setClipToOutline(false);
    }
    invalidate();
    invalidateOutline();
}
Also used : FolderAdaptiveIcon(com.android.launcher3.dragndrop.FolderAdaptiveIcon) ColorDrawable(android.graphics.drawable.ColorDrawable) AdaptiveIconDrawable(android.graphics.drawable.AdaptiveIconDrawable) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) Outline(android.graphics.Outline) AdaptiveIconDrawable(android.graphics.drawable.AdaptiveIconDrawable) ViewOutlineProvider(android.view.ViewOutlineProvider) View(android.view.View)

Example 35 with FolderAdaptiveIcon

use of com.android.launcher3.dragndrop.FolderAdaptiveIcon in project android_packages_apps_Trebuchet by LineageOS.

the class FloatingIconView method getOffsetForIconBounds.

@WorkerThread
@SuppressWarnings("WrongThread")
private static int getOffsetForIconBounds(Launcher l, Drawable drawable, RectF position) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || !(drawable instanceof AdaptiveIconDrawable) || (drawable instanceof FolderAdaptiveIcon)) {
        return 0;
    }
    int blurSizeOutline = l.getResources().getDimensionPixelSize(R.dimen.blur_size_medium_outline);
    Rect bounds = new Rect(0, 0, (int) position.width() + blurSizeOutline, (int) position.height() + blurSizeOutline);
    bounds.inset(blurSizeOutline / 2, blurSizeOutline / 2);
    try (LauncherIcons li = LauncherIcons.obtain(l)) {
        Utilities.scaleRectAboutCenter(bounds, li.getNormalizer().getScale(drawable, null, null, null));
    }
    bounds.inset((int) (-bounds.width() * AdaptiveIconDrawable.getExtraInsetFraction()), (int) (-bounds.height() * AdaptiveIconDrawable.getExtraInsetFraction()));
    return bounds.left;
}
Also used : FolderAdaptiveIcon(com.android.launcher3.dragndrop.FolderAdaptiveIcon) Rect(android.graphics.Rect) LauncherIcons(com.android.launcher3.icons.LauncherIcons) AdaptiveIconDrawable(android.graphics.drawable.AdaptiveIconDrawable) WorkerThread(androidx.annotation.WorkerThread)

Aggregations

Point (android.graphics.Point)29 Bitmap (android.graphics.Bitmap)26 AdaptiveIconDrawable (android.graphics.drawable.AdaptiveIconDrawable)24 FolderAdaptiveIcon (com.android.launcher3.dragndrop.FolderAdaptiveIcon)20 PendingAddShortcutInfo (com.android.launcher3.widget.PendingAddShortcutInfo)20 Rect (android.graphics.Rect)19 TargetApi (android.annotation.TargetApi)18 ColorDrawable (android.graphics.drawable.ColorDrawable)16 Drawable (android.graphics.drawable.Drawable)16 Path (android.graphics.Path)14 FastBitmapDrawable (com.android.launcher3.icons.FastBitmapDrawable)14 LauncherIcons (com.android.launcher3.icons.LauncherIcons)14 View (android.view.View)13 Paint (android.graphics.Paint)12 List (java.util.List)11 Handler (android.os.Handler)9 PreviewBackground (com.android.launcher3.folder.PreviewBackground)9 FolderIcon (com.android.launcher3.folder.FolderIcon)8 ShortcutInfo (android.content.pm.ShortcutInfo)7 Matrix (android.graphics.Matrix)7