Stop trying to draw a view not attached to the view tree

The behavior of the framework when we try to do so is undefined. In our
case, it almost work, but no clipping is applied, which is a problem for
Android S (before that, widget couldn't use clipping in the first
place).

Instead of drawing the view through a drawable, this really add the view
and adds also a badge ImageView for badges instead of drawing them
indirectly.

Note that, temporarily, we have to re-allow drawing the view after it
has been attached, but the underlying framework bug being fixed, this
should be fine (I tested it and it really seems to be).

Bug: 183609936
Test: Using hand designed app (see bug)
Change-Id: I929ef8fc81c98c49406f2d940cd5efc28319886d
This commit is contained in:
Pierre Barbier de Reuille committed 2021-04-07 15:51:43 +01:00
1 parent ce271c1067
commit ad41a56166
8 files changed
+137 -85

No files matched your search

@@ -19,6 +19,7 @@ import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
@@ -108,7 +109,7 @@ public abstract class BaseWidgetSheet extends AbstractSlideInView
// If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
// we abort the drag.
if (image.getDrawable() == null) {
if (image.getDrawable() == null && v.getAppWidgetHostViewPreview() == null) {
return false;
}
@@ -116,11 +117,21 @@ public abstract class BaseWidgetSheet extends AbstractSlideInView
dragHelper.setRemoteViewsPreview(v.getPreview());
dragHelper.setAppWidgetHostViewPreview(v.getAppWidgetHostViewPreview());
int[] loc = new int[2];
getPopupContainer().getLocationInDragLayer(image, loc);
if (image.getDrawable() != null) {
int[] loc = new int[2];
getPopupContainer().getLocationInDragLayer(image, loc);
dragHelper.startDrag(image.getBitmapBounds(), image.getDrawable().getIntrinsicWidth(),
image.getWidth(), new Point(loc[0], loc[1]), this, new DragOptions());
dragHelper.startDrag(image.getBitmapBounds(), image.getDrawable().getIntrinsicWidth(),
image.getWidth(), new Point(loc[0], loc[1]), this, new DragOptions());
} else {
View preview = v.getAppWidgetHostViewPreview();
int[] loc = new int[2];
getPopupContainer().getLocationInDragLayer(preview, loc);
Rect r = new Rect(0, 0, preview.getWidth(), preview.getHeight());
dragHelper.startDrag(r, preview.getMeasuredWidth(), preview.getMeasuredWidth(),
new Point(loc[0], loc[1]), this, new DragOptions());
}
close(true);
return true;
}