Merge "Remove intrinsic padding from widget previews" into sc-dev

This commit is contained in:
Stevie Kideckel 2021-05-26 09:27:11 +00:00 committed by Android (Google) Code Review
commit fa7c98ee45
2 changed files with 40 additions and 14 deletions

View File

@ -385,27 +385,29 @@ public class WidgetPreviewLoader {
previewHeight = Math.max((int)(scale * previewHeight), 1); previewHeight = Math.max((int)(scale * previewHeight), 1);
} }
// If a bitmap is passed in, we use it; otherwise, we create a bitmap of the right size
final Canvas c = new Canvas(); final Canvas c = new Canvas();
if (preview == null) { if (preview == null) {
// If no bitmap was provided, then allocate a new one with the right size.
preview = Bitmap.createBitmap(previewWidth, previewHeight, Config.ARGB_8888); preview = Bitmap.createBitmap(previewWidth, previewHeight, Config.ARGB_8888);
c.setBitmap(preview); c.setBitmap(preview);
} else { } else {
// We use the preview bitmap height to determine where the badge will be drawn in the // If a bitmap was passed in, attempt to reconfigure the bitmap to the same dimensions
// UI. If its larger than what we need, resize the preview bitmap so that there are // as the preview.
// no transparent pixels between the preview and the badge. try {
if (preview.getHeight() > previewHeight) { preview.reconfigure(previewWidth, previewHeight, preview.getConfig());
preview.reconfigure(preview.getWidth(), previewHeight, preview.getConfig()); } catch (IllegalArgumentException e) {
// This occurs if the preview can't be reconfigured for any reason. In this case,
// allocate a new bitmap with the right size.
preview = Bitmap.createBitmap(previewWidth, previewHeight, Config.ARGB_8888);
} }
// Reusing bitmap. Clear it.
c.setBitmap(preview); c.setBitmap(preview);
c.drawColor(0, PorterDuff.Mode.CLEAR); c.drawColor(0, PorterDuff.Mode.CLEAR);
} }
// Draw the scaled preview into the final bitmap // Draw the scaled preview into the final bitmap
int x = (preview.getWidth() - previewWidth) / 2;
if (widgetPreviewExists) { if (widgetPreviewExists) {
drawable.setBounds(x, 0, x + previewWidth, previewHeight); drawable.setBounds(0, 0, previewWidth, previewHeight);
drawable.draw(c); drawable.draw(c);
} else { } else {
RectF boxRect; RectF boxRect;
@ -565,6 +567,7 @@ public class WidgetPreviewLoader {
@Thunk long[] mVersions; @Thunk long[] mVersions;
@Thunk Bitmap mBitmapToRecycle; @Thunk Bitmap mBitmapToRecycle;
@Nullable private Bitmap mUnusedPreviewBitmap;
private boolean mSaveToDB = false; private boolean mSaveToDB = false;
PreviewLoadTask(WidgetCacheKey key, WidgetItem info, int previewWidth, PreviewLoadTask(WidgetCacheKey key, WidgetItem info, int previewWidth,
@ -625,6 +628,11 @@ public class WidgetPreviewLoader {
Pair<Bitmap, Boolean> pair = generatePreview(mActivity, mInfo, unusedBitmap, Pair<Bitmap, Boolean> pair = generatePreview(mActivity, mInfo, unusedBitmap,
mPreviewWidth, mPreviewHeight); mPreviewWidth, mPreviewHeight);
preview = pair.first; preview = pair.first;
if (preview != unusedBitmap) {
mUnusedPreviewBitmap = unusedBitmap;
}
this.mSaveToDB = pair.second; this.mSaveToDB = pair.second;
} }
return preview; return preview;
@ -639,6 +647,14 @@ public class WidgetPreviewLoader {
MODEL_EXECUTOR.post(new Runnable() { MODEL_EXECUTOR.post(new Runnable() {
@Override @Override
public void run() { public void run() {
if (mUnusedPreviewBitmap != null) {
// If we didn't end up using the bitmap, it can be added back into the
// recycled set.
synchronized (mUnusedBitmaps) {
mUnusedBitmaps.add(mUnusedPreviewBitmap);
}
}
if (!isCancelled() && mSaveToDB) { if (!isCancelled() && mSaveToDB) {
// If we are still using this preview, then write it to the DB and then // If we are still using this preview, then write it to the DB and then
// let the normal clear mechanism recycle the bitmap // let the normal clear mechanism recycle the bitmap

View File

@ -95,6 +95,7 @@ public class WidgetCell extends LinearLayout implements OnLayoutChangeListener {
protected final BaseActivity mActivity; protected final BaseActivity mActivity;
private final CheckLongPressHelper mLongPressHelper; private final CheckLongPressHelper mLongPressHelper;
private final float mEnforcedCornerRadius; private final float mEnforcedCornerRadius;
private final int mPreviewPadding;
private RemoteViews mRemoteViewsPreview; private RemoteViews mRemoteViewsPreview;
private NavigableAppWidgetHostView mAppWidgetHostViewPreview; private NavigableAppWidgetHostView mAppWidgetHostViewPreview;
@ -119,6 +120,8 @@ public class WidgetCell extends LinearLayout implements OnLayoutChangeListener {
setClipToPadding(false); setClipToPadding(false);
setAccessibilityDelegate(mActivity.getAccessibilityDelegate()); setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
mEnforcedCornerRadius = RoundedCornerEnforcement.computeEnforcedRadius(context); mEnforcedCornerRadius = RoundedCornerEnforcement.computeEnforcedRadius(context);
mPreviewPadding =
2 * getResources().getDimensionPixelSize(R.dimen.widget_preview_shortcut_padding);
} }
private void setContainerWidth() { private void setContainerWidth() {
@ -281,7 +284,16 @@ public class WidgetCell extends LinearLayout implements OnLayoutChangeListener {
return; return;
} }
if (drawable != null) { if (drawable != null) {
setContainerSize(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); float scale = 1f;
if (getWidth() > 0 && getHeight() > 0) {
// Scale down the preview size if it's wider than the cell.
float maxWidth = getWidth() - mPreviewPadding;
float previewWidth = drawable.getIntrinsicWidth() * mPreviewScale;
scale = Math.min(maxWidth / previewWidth, 1);
}
setContainerSize(
Math.round(drawable.getIntrinsicWidth() * scale),
Math.round(drawable.getIntrinsicHeight() * scale));
mWidgetImage.setDrawable(drawable); mWidgetImage.setDrawable(drawable);
mWidgetImage.setVisibility(View.VISIBLE); mWidgetImage.setVisibility(View.VISIBLE);
if (mAppWidgetHostViewPreview != null) { if (mAppWidgetHostViewPreview != null) {
@ -330,11 +342,9 @@ public class WidgetCell extends LinearLayout implements OnLayoutChangeListener {
/** Sets the widget preview image size, in number of cells, and preview scale. */ /** Sets the widget preview image size, in number of cells, and preview scale. */
public void setPreviewSize(int spanX, int spanY, float previewScale) { public void setPreviewSize(int spanX, int spanY, float previewScale) {
int padding = 2 * getResources()
.getDimensionPixelSize(R.dimen.widget_preview_shortcut_padding);
DeviceProfile deviceProfile = mActivity.getDeviceProfile(); DeviceProfile deviceProfile = mActivity.getDeviceProfile();
mPreviewWidth = deviceProfile.cellWidthPx * spanX + padding; mPreviewWidth = deviceProfile.cellWidthPx * spanX + mPreviewPadding;
mPreviewHeight = deviceProfile.cellHeightPx * spanY + padding; mPreviewHeight = deviceProfile.cellHeightPx * spanY + mPreviewPadding;
mPreviewScale = previewScale; mPreviewScale = previewScale;
} }