Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a2441e88 authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Updating widget preview generation to not embed any badge.

Instead drawing badge in the view directly.

This will allow us to reuse the same preview when we do not want to draw
badges. This also simplified badging of shortcut config activities.

Change-Id: I9c07c33bf9c2b8ef81a00026b1d42587868c906f
parent 27835953
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -15,7 +15,6 @@
-->
<com.android.launcher3.widget.WidgetCell
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:launcher="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
@@ -25,7 +24,7 @@
    android:gravity="center_horizontal">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/widget_preview_label_vertical_padding"
        android:paddingBottom="@dimen/widget_preview_label_vertical_padding"
@@ -36,7 +35,7 @@
        <!-- The name of the widget. -->
        <TextView
            android:id="@+id/widget_name"
            android:layout_width="wrap_content"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ellipsize="end"
+1 −1
Original line number Diff line number Diff line
@@ -83,7 +83,7 @@

<!-- Widget tray -->
    <dimen name="widget_preview_label_vertical_padding">8dp</dimen>
    <dimen name="widget_preview_label_horizontal_padding">8dp</dimen>
    <dimen name="widget_preview_label_horizontal_padding">16dp</dimen>

    <dimen name="widget_section_height">56dp</dimen>
    <dimen name="widget_section_icon_size">40dp</dimen>
+12 −11
Original line number Diff line number Diff line
@@ -70,7 +70,6 @@ public class WidgetPreviewLoader {
    private final UserManagerCompat mUserManager;
    private final AppWidgetManagerCompat mWidgetManager;
    private final CacheDb mDb;
    private final int mProfileBadgeMargin;

    private final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor();
    @Thunk final Handler mWorkerHandler;
@@ -82,8 +81,6 @@ public class WidgetPreviewLoader {
        mUserManager = UserManagerCompat.getInstance(context);
        mDb = new CacheDb(context);
        mWorkerHandler = new Handler(LauncherModel.getWorkerLooper());
        mProfileBadgeMargin = context.getResources()
                .getDimensionPixelSize(R.dimen.profile_badge_margin);
    }

    /**
@@ -107,7 +104,7 @@ public class WidgetPreviewLoader {
     * sizes (landscape vs portrait).
     */
    private static class CacheDb extends SQLiteCacheHelper {
        private static final int DB_VERSION = 4;
        private static final int DB_VERSION = 5;

        private static final String TABLE_NAME = "shortcut_and_widget_previews";
        private static final String COLUMN_COMPONENT = "componentName";
@@ -344,7 +341,7 @@ public class WidgetPreviewLoader {
            preScaledWidthOut[0] = previewWidth;
        }
        if (previewWidth > maxPreviewWidth) {
            scale = (maxPreviewWidth - 2 * mProfileBadgeMargin) / (float) (previewWidth);
            scale = maxPreviewWidth / (float) (previewWidth);
        }
        if (scale != 1f) {
            previewWidth = (int) (scale * previewWidth);
@@ -357,6 +354,12 @@ public class WidgetPreviewLoader {
            preview = Bitmap.createBitmap(previewWidth, previewHeight, Config.ARGB_8888);
            c.setBitmap(preview);
        } else {
            // We use the preview bitmap height to determine where the badge will be drawn in the
            // UI. If its larger than what we need, resize the preview bitmap so that there are
            // no transparent pixels between the preview and the badge.
            if (preview.getHeight() > previewHeight) {
                preview.reconfigure(preview.getWidth(), previewHeight, preview.getConfig());
            }
            // Reusing bitmap. Clear it.
            c.setBitmap(preview);
            c.drawColor(0, PorterDuff.Mode.CLEAR);
@@ -409,9 +412,7 @@ public class WidgetPreviewLoader {
            }
            c.setBitmap(null);
        }
        int imageWidth = Math.min(preview.getWidth(), previewWidth + mProfileBadgeMargin);
        int imageHeight = Math.min(preview.getHeight(), previewHeight + mProfileBadgeMargin);
        return mWidgetManager.getBadgeBitmap(info, preview, imageWidth, imageHeight);
        return preview;
    }

    private Bitmap generateShortcutPreview(
@@ -538,6 +539,7 @@ public class WidgetPreviewLoader {
        private final int mPreviewHeight;
        private final int mPreviewWidth;
        private final WidgetCell mCaller;
        private final BaseActivity mActivity;
        @Thunk long[] mVersions;
        @Thunk Bitmap mBitmapToRecycle;

@@ -548,6 +550,7 @@ public class WidgetPreviewLoader {
            mPreviewHeight = previewHeight;
            mPreviewWidth = previewWidth;
            mCaller = caller;
            mActivity = BaseActivity.fromContext(mCaller.getContext());
            if (DEBUG) {
                Log.d(TAG, String.format("%s, %s, %d, %d",
                        mKey, mInfo, mPreviewHeight, mPreviewWidth));
@@ -591,10 +594,8 @@ public class WidgetPreviewLoader {
                // which would gets re-written next time.
                mVersions = getPackageVersion(mKey.componentName.getPackageName());

                BaseActivity launcher = BaseActivity.fromContext(mCaller.getContext());

                // it's not in the db... we need to generate it
                preview = generatePreview(launcher, mInfo, unusedBitmap, mPreviewWidth, mPreviewHeight);
                preview = generatePreview(mActivity, mInfo, unusedBitmap, mPreviewWidth, mPreviewHeight);
            }
            return preview;
        }
+0 −3
Original line number Diff line number Diff line
@@ -71,9 +71,6 @@ public abstract class AppWidgetManagerCompat {
    public abstract void startConfigActivity(AppWidgetProviderInfo info, int widgetId,
            Activity activity, AppWidgetHost host, int requestCode);

    public abstract Bitmap getBadgeBitmap(LauncherAppWidgetProviderInfo info, Bitmap bitmap,
            int imageWidth, int imageHeight);

    public abstract LauncherAppWidgetProviderInfo findProvider(
            ComponentName provider, UserHandle user);

+0 −49
Original line number Diff line number Diff line
@@ -23,17 +23,9 @@ import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.view.View;
import android.widget.Toast;

import com.android.launcher3.LauncherAppWidgetProviderInfo;
@@ -81,47 +73,6 @@ class AppWidgetManagerCompatVL extends AppWidgetManagerCompat {
        }
    }

    @Override
    public Bitmap getBadgeBitmap(LauncherAppWidgetProviderInfo info, Bitmap bitmap,
            int imageWidth, int imageHeight) {
        if (info.isCustomWidget || info.getProfile().equals(android.os.Process.myUserHandle())) {
            return bitmap;
        }

        // Add a user badge in the bottom right of the image.
        final Resources res = mContext.getResources();
        final int badgeMinTop = res.getDimensionPixelSize(R.dimen.profile_badge_minimum_top);

        // choose min between badge size defined for widget tray versus width, height of the image.
        // Width, height of the image can be smaller than widget tray badge size when being dropped
        // to the workspace.
        final int badgeSize = Math.min(res.getDimensionPixelSize(R.dimen.profile_badge_size),
                Math.min(imageWidth, imageHeight - badgeMinTop));
        final Rect badgeLocation = new Rect(0, 0, badgeSize, badgeSize);

        final int top = Math.max(imageHeight - badgeSize, badgeMinTop);

        if (res.getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
            badgeLocation.offset(0, top);
        } else {
            badgeLocation.offset(bitmap.getWidth() - badgeSize, top);
        }

        Drawable drawable = mPm.getUserBadgedDrawableForDensity(
                new BitmapDrawable(res, bitmap), info.getProfile(), badgeLocation, 0);

        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        bitmap.eraseColor(Color.TRANSPARENT);
        Canvas c = new Canvas(bitmap);
        drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
        drawable.draw(c);
        c.setBitmap(null);
        return bitmap;
    }

    @Override
    public LauncherAppWidgetProviderInfo findProvider(ComponentName provider, UserHandle user) {
        for (AppWidgetProviderInfo info : mAppWidgetManager
Loading