Search in sources :

Example 1 with Utilities.getFullDrawable

use of com.android.launcher3.Utilities.getFullDrawable in project android_packages_apps_Launcher3 by crdroidandroid.

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 (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(() -> {
        Object[] outObj = new Object[1];
        int w = mWidth;
        int h = mHeight;
        Drawable dr = Utilities.getFullDrawable(mLauncher, info, w, h, 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
            final boolean shouldDrawBackground = !(dr instanceof FolderAdaptiveIcon);
            try (LauncherIcons li = LauncherIcons.obtain(mLauncher)) {
                // drawable to be normalized
                Drawable nDr;
                if (shouldDrawBackground) {
                    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(() -> mOnDragStartCallback.add(() -> {
                // TODO: Consider fade-in animation
                // Assign the variable on the UI thread to avoid race conditions.
                mScaledMaskPath = mask;
                // Avoid relayout as we do not care about children affecting layout
                removeAllViewsInLayout();
                if (info.isDisabled()) {
                    FastBitmapDrawable d = new FastBitmapDrawable((Bitmap) null);
                    d.setIsDisabled(true);
                    mBgSpringDrawable.setColorFilter(d.getColorFilter());
                    mFgSpringDrawable.setColorFilter(d.getColorFilter());
                    mBadge.setColorFilter(d.getColorFilter());
                }
                invalidate();
            }));
        }
    });
}
Also used : Path(android.graphics.Path) Rect(android.graphics.Rect) AdaptiveIconDrawable(android.graphics.drawable.AdaptiveIconDrawable) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) FastBitmapDrawable(com.android.launcher3.icons.FastBitmapDrawable) PictureDrawable(android.graphics.drawable.PictureDrawable) Handler(android.os.Handler) Point(android.graphics.Point) FastBitmapDrawable(com.android.launcher3.icons.FastBitmapDrawable) Bitmap(android.graphics.Bitmap) ColorDrawable(android.graphics.drawable.ColorDrawable) LauncherIcons(com.android.launcher3.icons.LauncherIcons) AdaptiveIconDrawable(android.graphics.drawable.AdaptiveIconDrawable) TargetApi(android.annotation.TargetApi)

Aggregations

TargetApi (android.annotation.TargetApi)1 Bitmap (android.graphics.Bitmap)1 Path (android.graphics.Path)1 Point (android.graphics.Point)1 Rect (android.graphics.Rect)1 AdaptiveIconDrawable (android.graphics.drawable.AdaptiveIconDrawable)1 ColorDrawable (android.graphics.drawable.ColorDrawable)1 Drawable (android.graphics.drawable.Drawable)1 PictureDrawable (android.graphics.drawable.PictureDrawable)1 Handler (android.os.Handler)1 FastBitmapDrawable (com.android.launcher3.icons.FastBitmapDrawable)1 LauncherIcons (com.android.launcher3.icons.LauncherIcons)1