Call the callback for the ImageSaveTask on the ui thread after onSuccess or onFail is called.

Properly delete the destination when saving fails

Maybe this fixes issue #151
multisite
Floens 9 years ago
parent 5ae20bfbe6
commit 5e84e53b79
  1. 35
      Clover/app/src/main/java/org/floens/chan/core/saver/ImageSaveTask.java

@ -104,8 +104,8 @@ public class ImageSaveTask implements Runnable, FileCache.DownloadedCallback {
onDestination(); onDestination();
} else { } else {
FileCache.FileCacheDownloader fileCacheDownloader = Chan.getFileCache().downloadFile(postImage.imageUrl, this); FileCache.FileCacheDownloader fileCacheDownloader = Chan.getFileCache().downloadFile(postImage.imageUrl, this);
// If the fileCacheDownloader is null then the callbacks were already executed here, // If the fileCacheDownloader is null then the destination already existed and onSuccess() has been called.
// else wait for the download to finish to avoid that the next task is immediately executed. // Wait otherwise for the download to finish to avoid that the next task is immediately executed.
if (fileCacheDownloader != null) { if (fileCacheDownloader != null) {
// If the file is now downloading // If the file is now downloading
fileCacheDownloader.getFuture().get(); fileCacheDownloader.getFuture().get();
@ -115,8 +115,6 @@ public class ImageSaveTask implements Runnable, FileCache.DownloadedCallback {
onInterrupted(); onInterrupted();
} catch (Exception e) { } catch (Exception e) {
Logger.e(TAG, "Uncaught exception", e); Logger.e(TAG, "Uncaught exception", e);
} finally {
postFinished(success);
} }
} }
@ -126,15 +124,24 @@ public class ImageSaveTask implements Runnable, FileCache.DownloadedCallback {
@Override @Override
public void onFail(boolean notFound) { public void onFail(boolean notFound) {
postFinished(success);
} }
@Override @Override
public void onSuccess(File file) { public void onSuccess(File file) {
copyToDestination(file); if (copyToDestination(file)) {
onDestination(); onDestination();
} else {
deleteDestination();
}
postFinished(success);
} }
private void onInterrupted() { private void onInterrupted() {
deleteDestination();
}
private void deleteDestination() {
if (destination.exists()) { if (destination.exists()) {
if (!destination.delete()) { if (!destination.delete()) {
Logger.e(TAG, "Could not delete destination after an interrupt"); Logger.e(TAG, "Could not delete destination after an interrupt");
@ -143,13 +150,16 @@ public class ImageSaveTask implements Runnable, FileCache.DownloadedCallback {
} }
private void onDestination() { private void onDestination() {
success = true;
scanDestination(); scanDestination();
if (makeBitmap) { if (makeBitmap) {
bitmap = ImageDecoder.decodeFile(destination, dp(512), dp(256)); bitmap = ImageDecoder.decodeFile(destination, dp(512), dp(256));
} }
} }
private void copyToDestination(File source) { private boolean copyToDestination(File source) {
boolean result = false;
InputStream is = null; InputStream is = null;
OutputStream os = null; OutputStream os = null;
try { try {
@ -166,13 +176,15 @@ public class ImageSaveTask implements Runnable, FileCache.DownloadedCallback {
os = new FileOutputStream(destination); os = new FileOutputStream(destination);
IOUtils.copy(is, os); IOUtils.copy(is, os);
success = true; result = true;
} catch (IOException e) { } catch (IOException e) {
Logger.e(TAG, "Error writing to file", e); Logger.e(TAG, "Error writing to file", e);
} finally { } finally {
IOUtils.closeQuietly(is); IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os); IOUtils.closeQuietly(os);
} }
return result;
} }
private void scanDestination() { private void scanDestination() {
@ -202,12 +214,7 @@ public class ImageSaveTask implements Runnable, FileCache.DownloadedCallback {
} }
private void postFinished(final boolean success) { private void postFinished(final boolean success) {
AndroidUtils.runOnUiThread(new Runnable() { callback.imageSaveTaskFinished(ImageSaveTask.this, success);
@Override
public void run() {
callback.imageSaveTaskFinished(ImageSaveTask.this, success);
}
});
} }
public interface ImageSaveTaskCallback { public interface ImageSaveTaskCallback {

Loading…
Cancel
Save