Adding support for showing the widget preview based on the provided RemoteViews

Bug: 35811129
Change-Id: I336e48cd00cfec2e617ac73bd8a81419b0944aa7
This commit is contained in:
Sunny Goyal committed 2017-03-17 11:25:29 -07:00
1 parent efd5cbf55c
commit c65a0085d7
13 files changed
+320 -112

No files matched your search

@@ -22,6 +22,7 @@ import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.CancellationSignal;
import android.os.Handler;
import android.os.UserHandle;
import android.support.v4.graphics.ColorUtils;
@@ -89,14 +90,17 @@ public class WidgetPreviewLoader {
*
* @return a request id which can be used to cancel the request.
*/
public PreviewLoadRequest getPreview(WidgetItem item, int previewWidth,
public CancellationSignal getPreview(WidgetItem item, int previewWidth,
int previewHeight, WidgetCell caller) {
String size = previewWidth + "x" + previewHeight;
WidgetCacheKey key = new WidgetCacheKey(item.componentName, item.user, size);
PreviewLoadTask task = new PreviewLoadTask(key, item, previewWidth, previewHeight, caller);
task.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
return new PreviewLoadRequest(task);
CancellationSignal signal = new CancellationSignal();
signal.setOnCancelListener(task);
return signal;
}
/**
@@ -510,42 +514,8 @@ public class WidgetPreviewLoader {
}
}
/**
* A request Id which can be used by the client to cancel any request.
*/
public class PreviewLoadRequest {
@Thunk final PreviewLoadTask mTask;
public PreviewLoadRequest(PreviewLoadTask task) {
mTask = task;
}
public void cleanup() {
if (mTask != null) {
mTask.cancel(true);
}
// This only handles the case where the PreviewLoadTask is cancelled after the task has
// successfully completed (including having written to disk when necessary). In the
// other cases where it is cancelled while the task is running, it will be cleaned up
// in the tasks's onCancelled() call, and if cancelled while the task is writing to
// disk, it will be cancelled in the task's onPostExecute() call.
if (mTask.mBitmapToRecycle != null) {
mWorkerHandler.post(new Runnable() {
@Override
public void run() {
synchronized (mUnusedBitmaps) {
mUnusedBitmaps.add(mTask.mBitmapToRecycle);
}
mTask.mBitmapToRecycle = null;
}
});
}
}
}
public class PreviewLoadTask extends AsyncTask<Void, Void, Bitmap> {
public class PreviewLoadTask extends AsyncTask<Void, Void, Bitmap>
implements CancellationSignal.OnCancelListener {
@Thunk final WidgetCacheKey mKey;
private final WidgetItem mInfo;
private final int mPreviewHeight;
@@ -661,6 +631,28 @@ public class WidgetPreviewLoader {
});
}
}
@Override
public void onCancel() {
cancel(true);
// This only handles the case where the PreviewLoadTask is cancelled after the task has
// successfully completed (including having written to disk when necessary). In the
// other cases where it is cancelled while the task is running, it will be cleaned up
// in the tasks's onCancelled() call, and if cancelled while the task is writing to
// disk, it will be cancelled in the task's onPostExecute() call.
if (mBitmapToRecycle != null) {
mWorkerHandler.post(new Runnable() {
@Override
public void run() {
synchronized (mUnusedBitmaps) {
mUnusedBitmaps.add(mBitmapToRecycle);
}
mBitmapToRecycle = null;
}
});
}
}
}
private static final class WidgetCacheKey extends ComponentKey {