image decoding tasks managed as LIFO

This commit is contained in:
Thibault Deckers 2019-09-04 00:47:19 +09:00
parent 7aeae543c4
commit d919cd6022
3 changed files with 34 additions and 12 deletions

View file

@ -1,34 +1,45 @@
package deckers.thibault.aves.channelhandlers; package deckers.thibault.aves.channelhandlers;
import android.app.Activity; import android.app.Activity;
import android.os.AsyncTask; import android.util.Log;
import java.util.HashMap; import java.util.concurrent.LinkedBlockingDeque;
import deckers.thibault.aves.model.ImageEntry; import deckers.thibault.aves.model.ImageEntry;
import deckers.thibault.aves.utils.Utils;
import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel;
public class ImageDecodeTaskManager { public class ImageDecodeTaskManager {
private Activity activity; private static final String LOG_TAG = Utils.createLogTag(ImageDecodeTaskManager.class);
private HashMap<String, AsyncTask> taskMap = new HashMap<>();
private LinkedBlockingDeque<ImageDecodeTask.Params> taskParamsQueue;
private boolean running = true;
ImageDecodeTaskManager(Activity activity) { ImageDecodeTaskManager(Activity activity) {
this.activity = activity; taskParamsQueue = new LinkedBlockingDeque<>();
new Thread(() -> {
try {
while (running) {
ImageDecodeTask.Params params = taskParamsQueue.take();
new ImageDecodeTask(activity).execute(params);
Thread.sleep(10);
}
} catch (InterruptedException ex) {
Log.w(LOG_TAG, ex);
}
}).start();
} }
void fetch(MethodChannel.Result result, ImageEntry entry, Integer width, Integer height) { void fetch(MethodChannel.Result result, ImageEntry entry, Integer width, Integer height) {
ImageDecodeTask.Params params = new ImageDecodeTask.Params(entry, width, height, result, this::complete); taskParamsQueue.addFirst(new ImageDecodeTask.Params(entry, width, height, result, this::complete));
AsyncTask task = new ImageDecodeTask(activity).execute(params);
taskMap.put(entry.getUri().toString(), task);
} }
void cancel(String uri) { void cancel(String uri) {
AsyncTask task = taskMap.get(uri); boolean removed = taskParamsQueue.removeIf(p -> uri.equals(p.entry.getUri().toString()));
if (task != null) task.cancel(true); if (removed) Log.d(LOG_TAG, "cancelled uri=" + uri);
taskMap.remove(uri);
} }
private void complete(String uri) { private void complete(String uri) {
taskMap.remove(uri); // nothing for now
} }
} }

View file

@ -31,6 +31,16 @@ class ImageFileService {
return Uint8List(0); return Uint8List(0);
} }
static cancelGetImageBytes(String uri) async {
try {
await platform.invokeMethod('cancelGetImageBytes', <String, dynamic>{
'uri': uri,
});
} on PlatformException catch (e) {
debugPrint('cancelGetImageBytes failed with exception=${e.message}');
}
}
static Future<bool> delete(ImageEntry entry) async { static Future<bool> delete(ImageEntry entry) async {
try { try {
await platform.invokeMethod('delete', <String, dynamic>{ await platform.invokeMethod('delete', <String, dynamic>{

View file

@ -59,6 +59,7 @@ class InfoPageState extends State<InfoPage> {
InfoRow('Resolution', resolutionText), InfoRow('Resolution', resolutionText),
InfoRow('Size', formatFilesize(entry.sizeBytes)), InfoRow('Size', formatFilesize(entry.sizeBytes)),
InfoRow('Path', entry.path), InfoRow('Path', entry.path),
InfoRow('Uri', entry.uri),
LocationSection(entry: entry), LocationSection(entry: entry),
XmpTagSection(collection: widget.collection, entry: entry), XmpTagSection(collection: widget.collection, entry: entry),
MetadataSection(entry: entry), MetadataSection(entry: entry),