kopia lustrzana https://github.com/ryukoposting/Signal-Android
Fix NPE in ThumbnailsTask.
The async task was being cancelled, but there was still a race condition in how the thumbnails list was being managed. This attempts to fix that.fork-5.53.8
rodzic
e4755b298f
commit
7da47c9586
|
@ -21,16 +21,17 @@ import java.io.IOException;
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@RequiresApi(api = 23)
|
@RequiresApi(api = 23)
|
||||||
public class VideoThumbnailsView extends View {
|
public class VideoThumbnailsView extends View {
|
||||||
|
|
||||||
private static final String TAG = Log.tag(VideoThumbnailsView.class);
|
private static final String TAG = Log.tag(VideoThumbnailsView.class);
|
||||||
|
|
||||||
private MediaInput input;
|
private MediaInput input;
|
||||||
private ArrayList<Bitmap> thumbnails;
|
private volatile ArrayList<Bitmap> thumbnails;
|
||||||
private AsyncTask<Void, Bitmap, Void> thumbnailsTask;
|
private AsyncTask<Void, Bitmap, Void> thumbnailsTask;
|
||||||
private OnDurationListener durationListener;
|
private OnDurationListener durationListener;
|
||||||
|
|
||||||
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||||
private final RectF tempRect = new RectF();
|
private final RectF tempRect = new RectF();
|
||||||
|
@ -100,7 +101,7 @@ public class VideoThumbnailsView extends View {
|
||||||
|
|
||||||
if (thumbnails == null) {
|
if (thumbnails == null) {
|
||||||
if (thumbnailsTask == null) {
|
if (thumbnailsTask == null) {
|
||||||
final int thumbnailCount = drawRect.width() / drawRect.height();
|
final int thumbnailCount = drawRect.width() / drawRect.height();
|
||||||
final float thumbnailWidth = (float) drawRect.width() / thumbnailCount;
|
final float thumbnailWidth = (float) drawRect.width() / thumbnailCount;
|
||||||
final float thumbnailHeight = drawRect.height();
|
final float thumbnailHeight = drawRect.height();
|
||||||
|
|
||||||
|
@ -109,7 +110,7 @@ public class VideoThumbnailsView extends View {
|
||||||
thumbnailsTask.execute();
|
thumbnailsTask.execute();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
final int thumbnailCount = drawRect.width() / drawRect.height();
|
final int thumbnailCount = drawRect.width() / drawRect.height();
|
||||||
final float thumbnailWidth = (float) drawRect.width() / thumbnailCount;
|
final float thumbnailWidth = (float) drawRect.width() / thumbnailCount;
|
||||||
final float thumbnailHeight = drawRect.height();
|
final float thumbnailHeight = drawRect.height();
|
||||||
|
|
||||||
|
@ -119,7 +120,7 @@ public class VideoThumbnailsView extends View {
|
||||||
for (int i = 0; i < thumbnails.size(); i++) {
|
for (int i = 0; i < thumbnails.size(); i++) {
|
||||||
tempRect.left = drawRect.left + i * thumbnailWidth;
|
tempRect.left = drawRect.left + i * thumbnailWidth;
|
||||||
tempRect.right = tempRect.left + thumbnailWidth;
|
tempRect.right = tempRect.left + thumbnailWidth;
|
||||||
|
|
||||||
final Bitmap thumbnailBitmap = thumbnails.get(i);
|
final Bitmap thumbnailBitmap = thumbnails.get(i);
|
||||||
if (thumbnailBitmap != null) {
|
if (thumbnailBitmap != null) {
|
||||||
canvas.save();
|
canvas.save();
|
||||||
|
@ -128,11 +129,11 @@ public class VideoThumbnailsView extends View {
|
||||||
if (tempDrawRect.width() * thumbnailHeight > tempDrawRect.height() * thumbnailWidth) {
|
if (tempDrawRect.width() * thumbnailHeight > tempDrawRect.height() * thumbnailWidth) {
|
||||||
float w = tempDrawRect.height() * thumbnailWidth / thumbnailHeight;
|
float w = tempDrawRect.height() * thumbnailWidth / thumbnailHeight;
|
||||||
tempDrawRect.left = tempDrawRect.centerX() - (int) (w / 2);
|
tempDrawRect.left = tempDrawRect.centerX() - (int) (w / 2);
|
||||||
tempDrawRect.right = tempDrawRect.left + (int) w;
|
tempDrawRect.right = tempDrawRect.left + (int) w;
|
||||||
} else {
|
} else {
|
||||||
float h = tempDrawRect.width() * thumbnailHeight / thumbnailWidth;
|
float h = tempDrawRect.width() * thumbnailHeight / thumbnailWidth;
|
||||||
tempDrawRect.top = tempDrawRect.centerY() - (int) (h / 2);
|
tempDrawRect.top = tempDrawRect.centerY() - (int) (h / 2);
|
||||||
tempDrawRect.bottom = tempDrawRect.top + (int) h;
|
tempDrawRect.bottom = tempDrawRect.top + (int) h;
|
||||||
}
|
}
|
||||||
canvas.drawBitmap(thumbnailBitmap, tempDrawRect, tempRect, paint);
|
canvas.drawBitmap(thumbnailBitmap, tempDrawRect, tempRect, paint);
|
||||||
canvas.restore();
|
canvas.restore();
|
||||||
|
@ -149,10 +150,10 @@ public class VideoThumbnailsView extends View {
|
||||||
if (durationListener != null) {
|
if (durationListener != null) {
|
||||||
durationListener.onDurationKnown(duration);
|
durationListener.onDurationKnown(duration);
|
||||||
}
|
}
|
||||||
if (this.duration != duration) {
|
if (this.duration != duration) {
|
||||||
this.duration = duration;
|
this.duration = duration;
|
||||||
afterDurationChange(duration);
|
afterDurationChange(duration);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void afterDurationChange(long duration) {
|
protected void afterDurationChange(long duration) {
|
||||||
|
@ -169,7 +170,8 @@ public class VideoThumbnailsView extends View {
|
||||||
final float thumbnailWidth;
|
final float thumbnailWidth;
|
||||||
final float thumbnailHeight;
|
final float thumbnailHeight;
|
||||||
final int thumbnailCount;
|
final int thumbnailCount;
|
||||||
long duration;
|
|
||||||
|
long duration;
|
||||||
|
|
||||||
ThumbnailsTask(final @NonNull VideoThumbnailsView view, final @NonNull MediaInput input, final float thumbnailWidth, final float thumbnailHeight, final int thumbnailCount) {
|
ThumbnailsTask(final @NonNull VideoThumbnailsView view, final @NonNull MediaInput input, final float thumbnailWidth, final float thumbnailHeight, final int thumbnailCount) {
|
||||||
this.viewReference = new WeakReference<>(view);
|
this.viewReference = new WeakReference<>(view);
|
||||||
|
@ -191,8 +193,11 @@ public class VideoThumbnailsView extends View {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean publishProgress(int index, Bitmap thumbnail) {
|
public boolean publishProgress(int index, Bitmap thumbnail) {
|
||||||
ThumbnailsTask.this.publishProgress(thumbnail);
|
boolean notCanceled = !isCancelled();
|
||||||
return !isCancelled();
|
if (notCanceled) {
|
||||||
|
ThumbnailsTask.this.publishProgress(thumbnail);
|
||||||
|
}
|
||||||
|
return notCanceled;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -205,20 +210,26 @@ public class VideoThumbnailsView extends View {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onProgressUpdate(Bitmap... values) {
|
protected void onProgressUpdate(Bitmap... values) {
|
||||||
final VideoThumbnailsView view = viewReference.get();
|
if (isCancelled()) {
|
||||||
if (view != null) {
|
return;
|
||||||
view.thumbnails.addAll(Arrays.asList(values));
|
}
|
||||||
|
|
||||||
|
VideoThumbnailsView view = viewReference.get();
|
||||||
|
List<Bitmap> thumbnails = view != null ? view.thumbnails : null;
|
||||||
|
if (thumbnails != null) {
|
||||||
|
thumbnails.addAll(Arrays.asList(values));
|
||||||
view.invalidate();
|
view.invalidate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(Void result) {
|
protected void onPostExecute(Void result) {
|
||||||
final VideoThumbnailsView view = viewReference.get();
|
VideoThumbnailsView view = viewReference.get();
|
||||||
|
List<Bitmap> thumbnails = view != null ? view.thumbnails : null;
|
||||||
if (view != null) {
|
if (view != null) {
|
||||||
view.setDuration(duration);
|
view.setDuration(duration);
|
||||||
view.invalidate();
|
view.invalidate();
|
||||||
Log.i(TAG, "onPostExecute, we have " + view.thumbnails.size() + " thumbs");
|
Log.i(TAG, "onPostExecute, we have " + (thumbnails != null ? thumbnails.size() : "null") + " thumbs");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue