aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/network/HtmlImage.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/network/HtmlImage.java')
-rw-r--r--main/src/cgeo/geocaching/network/HtmlImage.java33
1 files changed, 8 insertions, 25 deletions
diff --git a/main/src/cgeo/geocaching/network/HtmlImage.java b/main/src/cgeo/geocaching/network/HtmlImage.java
index 5d713eb..8ee2ba7 100644
--- a/main/src/cgeo/geocaching/network/HtmlImage.java
+++ b/main/src/cgeo/geocaching/network/HtmlImage.java
@@ -10,9 +10,9 @@ import cgeo.geocaching.utils.CancellableHandler;
import cgeo.geocaching.utils.FileUtils;
import cgeo.geocaching.utils.ImageUtils;
import cgeo.geocaching.utils.Log;
+import cgeo.geocaching.utils.RxUtils;
import ch.boye.httpclientandroidlib.HttpResponse;
-import ch.boye.httpclientandroidlib.androidextra.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
@@ -42,9 +42,6 @@ import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
@@ -127,9 +124,8 @@ public class HtmlImage implements Html.ImageGetter {
return drawable.toBlockingObservable().lastOrDefault(null);
}
- // Caches are loaded from disk on Schedulers.computation() to avoid using more threads than processors
- // on the phone while decoding the image. Downloads happen on downloadScheduler, in parallel with image
- // decoding.
+ // Caches are loaded from disk on a computation scheduler to avoid using more threads than cores while decoding
+ // the image. Downloads happen on downloadScheduler, in parallel with image decoding.
public Observable<BitmapDrawable> fetchDrawable(final String url) {
if (StringUtils.isBlank(url) || ImageUtils.containsPattern(url, BLOCKED)) {
@@ -143,7 +139,7 @@ public class HtmlImage implements Html.ImageGetter {
@Override
public void call(final Subscriber<? super BitmapDrawable> subscriber) {
subscription.add(subscriber);
- subscriber.add(Schedulers.computation().schedule(new Action1<Inner>() {
+ subscriber.add(RxUtils.computationScheduler.schedule(new Action1<Inner>() {
@Override
public void call(final Inner inner) {
final Pair<BitmapDrawable, Boolean> loaded = loadFromDisk();
@@ -172,14 +168,15 @@ public class HtmlImage implements Html.ImageGetter {
return new ImmutablePair<BitmapDrawable, Boolean>(bitmap != null ?
ImageUtils.scaleBitmapToFitDisplay(bitmap) :
null,
- loadResult.getRight());
+ loadResult.getRight()
+ );
}
private void downloadAndSave(final Subscriber<? super BitmapDrawable> subscriber) {
final File file = LocalStorage.getStorageFile(pseudoGeocode, url, true, true);
if (url.startsWith("data:image/")) {
if (url.contains(";base64,")) {
- saveBase64ToFile(url, file);
+ ImageUtils.decodeBase64ToFile(StringUtils.substringAfter(url, ";base64,"), file);
} else {
Log.e("HtmlImage.getDrawable: unable to decode non-base64 inline image");
subscriber.onCompleted();
@@ -195,7 +192,7 @@ public class HtmlImage implements Html.ImageGetter {
if (onlySave) {
subscriber.onCompleted();
} else {
- Schedulers.computation().schedule(new Action1<Inner>() {
+ RxUtils.computationScheduler.schedule(new Action1<Inner>() {
@Override
public void call(final Inner inner) {
final Pair<BitmapDrawable, Boolean> loaded = loadFromDisk();
@@ -254,20 +251,6 @@ public class HtmlImage implements Html.ImageGetter {
return false;
}
- private static void saveBase64ToFile(final String url, final File file) {
- // TODO: when we use SDK level 8 or above, we can use the streaming version of the base64
- // Android utilities.
- OutputStream out = null;
- try {
- out = new FileOutputStream(file);
- out.write(Base64.decode(StringUtils.substringAfter(url, ";base64,"), Base64.DEFAULT));
- } catch (final IOException e) {
- Log.e("HtmlImage.saveBase64ToFile: cannot write file for decoded inline image", e);
- } finally {
- IOUtils.closeQuietly(out);
- }
- }
-
/**
* Make a fresh copy of the file to reset its timestamp. On some storage, it is impossible
* to modify the modified time after the fact, in which case a brand new file must be