diff --git a/Clover/app/src/main/java/org/floens/chan/ui/view/ThumbnailImageView.java b/Clover/app/src/main/java/org/floens/chan/ui/view/ThumbnailImageView.java index 323f3828..95259214 100644 --- a/Clover/app/src/main/java/org/floens/chan/ui/view/ThumbnailImageView.java +++ b/Clover/app/src/main/java/org/floens/chan/ui/view/ThumbnailImageView.java @@ -31,19 +31,15 @@ import com.android.volley.VolleyError; import com.android.volley.toolbox.ImageLoader.ImageContainer; import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; import com.koushikdutta.async.future.Future; -import com.koushikdutta.async.future.FutureCallback; -import com.koushikdutta.ion.Ion; -import com.koushikdutta.ion.ProgressCallback; -import com.koushikdutta.ion.Response; import org.floens.chan.ChanApplication; import org.floens.chan.R; +import org.floens.chan.utils.FileCache; import org.floens.chan.utils.Logger; import org.floens.chan.utils.Utils; import java.io.File; import java.io.IOException; -import java.util.concurrent.CancellationException; import pl.droidsonroids.gif.GifDrawable; import pl.droidsonroids.gif.GifImageView; @@ -121,64 +117,36 @@ public class ThumbnailImageView extends LoadView implements View.OnClickListener } callback.setProgress(true); + ionRequest = ChanApplication.getFileCache().downloadFile(getContext(), imageUrl, new FileCache.DownloadedCallback() { + @Override + public void onProgress(long downloaded, long total, boolean done) { + if (done) { + callback.setProgress(false); + callback.setLinearProgress(0, 0, true); + thumbnailNeeded = false; + } else { + callback.setLinearProgress(downloaded, total, false); + } + } - File file = ChanApplication.getFileCache().get(imageUrl); - if (file.exists()) { - onBigImage(file); - } else { - ionRequest = Ion.with(getContext()) - .load(imageUrl) - .progress(new ProgressCallback() { - @Override - public void onProgress(final long downloaded, final long total) { - Utils.runOnUiThread(new Runnable() { - @Override - public void run() { - callback.setLinearProgress(downloaded, total, false); - } - }); - } - }) - .write(file) - .withResponse() - .setCallback(new FutureCallback>() { - @Override - public void onCompleted(Exception e, Response result) { - if (result != null && result.getHeaders() != null && result.getHeaders().getResponseCode() / 100 != 2) { - if (result.getResult() != null) { - ChanApplication.getFileCache().delete(result.getResult()); - } - onNotFoundError(); - return; - } - - if (e != null && !(e instanceof CancellationException)) { - e.printStackTrace(); - if (result != null && result.getResult() != null) { - ChanApplication.getFileCache().delete(result.getResult()); - } - onError(); - return; - } - - if (result != null && result.getResult() != null) { - ChanApplication.getFileCache().put(result.getResult()); - onBigImage(result.getResult()); - } - } - }); - } - } + @Override + public void onSuccess(File file) { + SubsamplingScaleImageView image = new SubsamplingScaleImageView(getContext()); + image.setImageFile(file.getAbsolutePath()); + image.setOnClickListener(ThumbnailImageView.this); - private void onBigImage(File file) { - SubsamplingScaleImageView image = new SubsamplingScaleImageView(getContext()); - image.setImageFile(file.getAbsolutePath()); - image.setOnClickListener(this); + setView(image, false); + } - setView(image, false); - callback.setProgress(false); - callback.setLinearProgress(0, 0, true); - thumbnailNeeded = false; + @Override + public void onFail(boolean notFound) { + if (notFound) { + onNotFoundError(); + } else { + onError(); + } + } + }); } public void setGif(String gifUrl) { @@ -188,152 +156,95 @@ public class ThumbnailImageView extends LoadView implements View.OnClickListener } callback.setProgress(true); + ionRequest = ChanApplication.getFileCache().downloadFile(getContext(), gifUrl, new FileCache.DownloadedCallback() { + @Override + public void onProgress(long downloaded, long total, boolean done) { + if (done) { + callback.setProgress(false); + callback.setLinearProgress(0, 0, true); + thumbnailNeeded = false; + } else { + callback.setLinearProgress(downloaded, total, false); + } + } - File file = ChanApplication.getFileCache().get(gifUrl); - if (file.exists()) { - onGif(file); - } else { - ionRequest = Ion.with(getContext()) - .load(gifUrl) - .progress(new ProgressCallback() { - @Override - public void onProgress(final long downloaded, final long total) { - Utils.runOnUiThread(new Runnable() { - @Override - public void run() { - callback.setLinearProgress(downloaded, total, false); - } - }); - } - }) - .write(file) - .withResponse() - .setCallback(new FutureCallback>() { - @Override - public void onCompleted(Exception e, Response result) { - if (result != null && result.getHeaders() != null && result.getHeaders().getResponseCode() / 100 != 2) { - if (result.getResult() != null) { - ChanApplication.getFileCache().delete(result.getResult()); - } - onNotFoundError(); - return; - } - - if (e != null && !(e instanceof CancellationException)) { - e.printStackTrace(); - if (result != null && result.getResult() != null) { - ChanApplication.getFileCache().delete(result.getResult()); - } - onError(); - return; - } - - if (result != null && result.getResult() != null) { - ChanApplication.getFileCache().put(result.getResult()); - onGif(result.getResult()); - } - } - }); - } - } - - private void onGif(File file) { - GifDrawable drawable; - try { - drawable = new GifDrawable(file.getAbsolutePath()); - } catch (IOException e) { - e.printStackTrace(); - onError(); - return; - } + @Override + public void onSuccess(File file) { + GifDrawable drawable; + try { + drawable = new GifDrawable(file.getAbsolutePath()); + } catch (IOException e) { + e.printStackTrace(); + onError(); + return; + } - GifImageView view = new GifImageView(getContext()); - view.setImageDrawable(drawable); - view.setLayoutParams(Utils.MATCH_PARAMS); - setView(view, false); + GifImageView view = new GifImageView(getContext()); + view.setImageDrawable(drawable); + view.setLayoutParams(Utils.MATCH_PARAMS); + setView(view, false); + } - callback.setProgress(false); - callback.setLinearProgress(0, 0, true); - thumbnailNeeded = false; + @Override + public void onFail(boolean notFound) { + if (notFound) { + onNotFoundError(); + } else { + onError(); + } + } + }); } public void setVideo(String videoUrl) { callback.setProgress(true); + ionRequest = ChanApplication.getFileCache().downloadFile(getContext(), videoUrl, new FileCache.DownloadedCallback() { + @Override + public void onProgress(long downloaded, long total, boolean done) { + if (done) { + callback.setProgress(false); + callback.setLinearProgress(0, 0, true); + thumbnailNeeded = false; + } else { + callback.setLinearProgress(downloaded, total, false); + } + } - File file = ChanApplication.getFileCache().get(videoUrl); - if (file.exists()) { - onVideo(file); - } else { - ionRequest = Ion.with(getContext()) - .load(videoUrl) - .progress(new ProgressCallback() { - @Override - public void onProgress(final long downloaded, final long total) { - Utils.runOnUiThread(new Runnable() { - @Override - public void run() { - callback.setLinearProgress(downloaded, total, false); - } - }); - } - }) - .write(file) - .withResponse() - .setCallback(new FutureCallback>() { - @Override - public void onCompleted(Exception e, Response result) { - if (result != null && result.getHeaders() != null && result.getHeaders().getResponseCode() / 100 != 2) { - if (result.getResult() != null) { - ChanApplication.getFileCache().delete(result.getResult()); - } - onNotFoundError(); - return; - } - - if (e != null && !(e instanceof CancellationException)) { - e.printStackTrace(); - if (result != null && result.getResult() != null) { - ChanApplication.getFileCache().delete(result.getResult()); - } - onError(); - return; - } - - if (result != null && result.getResult() != null) { - ChanApplication.getFileCache().put(result.getResult()); - onVideo(result.getResult()); - } - } - }); - } - } + @Override + public void onSuccess(File file) { + videoView = new VideoView(getContext()); + videoView.setZOrderOnTop(true); + videoView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, + LayoutParams.MATCH_PARENT)); + videoView.setLayoutParams(Utils.MATCH_PARAMS); + LayoutParams par = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); + par.gravity = Gravity.CENTER; + videoView.setLayoutParams(par); + + videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { + @Override + public void onPrepared(MediaPlayer mp) { + mp.setLooping(true); + callback.onVideoLoaded(); + } + }); + + videoView.setVideoPath(file.getAbsolutePath()); + + setView(videoView, false); + + videoView.start(); + } - private void onVideo(File file) { - videoView = new VideoView(getContext()); - videoView.setZOrderOnTop(true); - videoView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, - LayoutParams.MATCH_PARENT)); - videoView.setLayoutParams(Utils.MATCH_PARAMS); - LayoutParams par = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); - par.gravity = Gravity.CENTER; - videoView.setLayoutParams(par); - - videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override - public void onPrepared(MediaPlayer mp) { - mp.setLooping(true); - callback.onVideoLoaded(); + public void onFail(boolean notFound) { + if (notFound) { + onNotFoundError(); + } else { + onError(); + } } }); - - videoView.setVideoPath(file.getAbsolutePath()); - - setView(videoView, false); - callback.setProgress(false); - callback.setLinearProgress(0, 0, true); - thumbnailNeeded = false; - - videoView.start(); } @Override diff --git a/Clover/app/src/main/java/org/floens/chan/utils/FileCache.java b/Clover/app/src/main/java/org/floens/chan/utils/FileCache.java index e49a66c5..cec0767b 100644 --- a/Clover/app/src/main/java/org/floens/chan/utils/FileCache.java +++ b/Clover/app/src/main/java/org/floens/chan/utils/FileCache.java @@ -1,8 +1,18 @@ package org.floens.chan.utils; +import android.content.Context; import android.util.Log; +import com.koushikdutta.async.future.Future; +import com.koushikdutta.async.future.FutureCallback; +import com.koushikdutta.ion.Ion; +import com.koushikdutta.ion.ProgressCallback; +import com.koushikdutta.ion.Response; + +import org.floens.chan.ChanApplication; + import java.io.File; +import java.util.concurrent.CancellationException; public class FileCache { private static final String TAG = "FileCache"; @@ -39,6 +49,60 @@ public class FileCache { return file.delete(); } + public Future> downloadFile(Context context, String url, final DownloadedCallback callback) { + File file = get(url); + if (file.exists()) { + file.setLastModified(Time.get()); + callback.onProgress(0, 0, true); + callback.onSuccess(file); + return null; + } else { + return Ion.with(context) + .load(url) + .progress(new ProgressCallback() { + @Override + public void onProgress(final long downloaded, final long total) { + Utils.runOnUiThread(new Runnable() { + @Override + public void run() { + callback.onProgress(downloaded, total, false); + } + }); + } + }) + .write(file) + .withResponse() + .setCallback(new FutureCallback>() { + @Override + public void onCompleted(Exception e, Response result) { + callback.onProgress(0, 0, true); + + if (result != null && result.getHeaders() != null && result.getHeaders().getResponseCode() / 100 != 2) { + if (result.getResult() != null) { + delete(result.getResult()); + } + callback.onFail(true); + return; + } + + if (e != null && !(e instanceof CancellationException)) { + e.printStackTrace(); + if (result != null && result.getResult() != null) { + delete(result.getResult()); + } + callback.onFail(false); + return; + } + + if (result != null && result.getResult() != null) { + ChanApplication.getFileCache().put(result.getResult()); + callback.onSuccess(result.getResult()); + } + } + }); + } + } + private void trim() { int tries = 0; while (size > maxSize && tries++ < 10) { @@ -83,4 +147,12 @@ public class FileCache { } } } + + public interface DownloadedCallback { + public void onProgress(long downloaded, long total, boolean done); + + public void onSuccess(File file); + + public void onFail(boolean notFound); + } }