Merge "Adding back the badges on widgets for widget recommendations." into sc-v2-dev am: 160d85c815

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Launcher3/+/16279659

Change-Id: I2f163917e70b9cb9fc6d49dc2b60cb4d0da73373
This commit is contained in:
TreeHugger Robot
2021-11-18 17:45:14 +00:00
committed by Automerger Merge Worker
4 changed files with 84 additions and 0 deletions

View File

@@ -33,6 +33,14 @@
android:layout_height="match_parent"
android:importantForAccessibility="no"
android:layout_gravity="fill"/>
<ImageView
android:id="@+id/widget_badge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:importantForAccessibility="no"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/profile_badge_margin"/>
</com.android.launcher3.widget.WidgetCellPreview>
<!-- The name of the widget. -->

View File

@@ -25,11 +25,15 @@ import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Process;
import android.os.UserHandle;
import android.util.ArrayMap;
import android.util.Log;
import android.util.Size;
@@ -40,6 +44,7 @@ import com.android.launcher3.LauncherAppState;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.icons.BitmapRenderer;
import com.android.launcher3.icons.FastBitmapDrawable;
import com.android.launcher3.icons.LauncherIcons;
import com.android.launcher3.icons.ShadowGenerator;
import com.android.launcher3.icons.cache.HandlerRunnable;
@@ -60,6 +65,9 @@ public class DatabaseWidgetPreviewLoader {
private final Context mContext;
private final float mPreviewBoxCornerRadius;
private final UserHandle mMyUser = Process.myUserHandle();
private final ArrayMap<UserHandle, Bitmap> mUserBadges = new ArrayMap<>();
public DatabaseWidgetPreviewLoader(Context context) {
mContext = context;
float previewCornerRadius = RoundedCornerEnforcement.computeEnforcedRadius(context);
@@ -100,6 +108,52 @@ public class DatabaseWidgetPreviewLoader {
}
}
/**
* Returns a drawable that can be used as a badge for the user or null.
*/
// @UiThread
public Drawable getBadgeForUser(UserHandle user, int badgeSize) {
if (mMyUser.equals(user)) {
return null;
}
Bitmap badgeBitmap = getUserBadge(user, badgeSize);
FastBitmapDrawable d = new FastBitmapDrawable(badgeBitmap);
d.setFilterBitmap(true);
d.setBounds(0, 0, badgeBitmap.getWidth(), badgeBitmap.getHeight());
return d;
}
private Bitmap getUserBadge(UserHandle user, int badgeSize) {
synchronized (mUserBadges) {
Bitmap badgeBitmap = mUserBadges.get(user);
if (badgeBitmap != null) {
return badgeBitmap;
}
final Resources res = mContext.getResources();
badgeBitmap = Bitmap.createBitmap(badgeSize, badgeSize, Bitmap.Config.ARGB_8888);
Drawable drawable = mContext.getPackageManager().getUserBadgedDrawableForDensity(
new BitmapDrawable(res, badgeBitmap), user,
new Rect(0, 0, badgeSize, badgeSize),
0);
if (drawable instanceof BitmapDrawable) {
badgeBitmap = ((BitmapDrawable) drawable).getBitmap();
} else {
badgeBitmap.eraseColor(Color.TRANSPARENT);
Canvas c = new Canvas(badgeBitmap);
drawable.setBounds(0, 0, badgeSize, badgeSize);
drawable.draw(c);
c.setBitmap(null);
}
mUserBadges.put(user, badgeBitmap);
return badgeBitmap;
}
}
/**
* Generates the widget preview from either the {@link WidgetManagerHelper} or cache
* and add badge at the bottom right corner.

View File

@@ -36,6 +36,7 @@ import android.view.ViewGroup;
import android.view.ViewPropertyAnimator;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RemoteViews;
import android.widget.TextView;
@@ -47,6 +48,7 @@ import com.android.launcher3.CheckLongPressHelper;
import com.android.launcher3.DeviceProfile;
import com.android.launcher3.Launcher;
import com.android.launcher3.R;
import com.android.launcher3.icons.BaseIconFactory;
import com.android.launcher3.icons.FastBitmapDrawable;
import com.android.launcher3.icons.RoundDrawableWrapper;
import com.android.launcher3.icons.cache.HandlerRunnable;
@@ -111,6 +113,7 @@ public class WidgetCell extends LinearLayout {
private FrameLayout mWidgetImageContainer;
private WidgetImageView mWidgetImage;
private ImageView mWidgetBadge;
private TextView mWidgetName;
private TextView mWidgetDims;
private TextView mWidgetDescription;
@@ -166,6 +169,7 @@ public class WidgetCell extends LinearLayout {
mWidgetImageContainer = findViewById(R.id.widget_preview_container);
mWidgetImage = findViewById(R.id.widget_preview);
mWidgetBadge = findViewById(R.id.widget_badge);
mWidgetName = findViewById(R.id.widget_name);
mWidgetDims = findViewById(R.id.widget_dims);
mWidgetDescription = findViewById(R.id.widget_description);
@@ -195,6 +199,8 @@ public class WidgetCell extends LinearLayout {
mWidgetImage.animate().cancel();
mWidgetImage.setDrawable(null);
mWidgetImage.setVisibility(View.VISIBLE);
mWidgetBadge.setImageDrawable(null);
mWidgetBadge.setVisibility(View.GONE);
mWidgetName.setText(null);
mWidgetDims.setText(null);
mWidgetDescription.setText(null);
@@ -349,6 +355,7 @@ public class WidgetCell extends LinearLayout {
mAppWidgetHostViewPreview = null;
}
}
if (mAnimatePreview) {
mWidgetImageContainer.setAlpha(0f);
ViewPropertyAnimator anim = mWidgetImageContainer.animate();
@@ -362,6 +369,20 @@ public class WidgetCell extends LinearLayout {
}
}
/** Used to show the badge when the widget is in the recommended section
*/
public void showBadge() {
Drawable badge = mWidgetPreviewLoader.getBadgeForUser(mItem.user,
BaseIconFactory.getBadgeSizeForIconSize(
mActivity.getDeviceProfile().allAppsIconSizePx));
if (badge == null) {
mWidgetBadge.setVisibility(View.GONE);
} else {
mWidgetBadge.setVisibility(View.VISIBLE);
mWidgetBadge.setImageDrawable(badge);
}
}
private void setContainerSize(int width, int height) {
LayoutParams layoutParams = (LayoutParams) mWidgetImageContainer.getLayoutParams();
layoutParams.width = width;

View File

@@ -109,6 +109,7 @@ public final class WidgetsRecommendationTableLayout extends TableLayout {
for (WidgetItem widgetItem : widgetItems) {
WidgetCell widgetCell = addItemCell(tableRow);
widgetCell.applyFromCellItem(widgetItem, data.mPreviewScale);
widgetCell.showBadge();
}
addView(tableRow);
}