summaryrefslogtreecommitdiff
path: root/src/de
diff options
context:
space:
mode:
authordaniel oeh <daniel.oeh@gmail.com>2012-08-03 14:25:05 +0200
committerdaniel oeh <daniel.oeh@gmail.com>2012-08-03 14:25:05 +0200
commit7ada4c2efb411d0eddcca315a164da0d7c3984a6 (patch)
tree95e777eb08d27d0a903a66468d1292350ca5febe /src/de
parentfadb0de18c6beb263324e8771bb54a496f55e9a6 (diff)
downloadAntennaPod-7ada4c2efb411d0eddcca315a164da0d7c3984a6.zip
Created AsnycTask for downloading thumbnails from the miroguide
Diffstat (limited to 'src/de')
-rw-r--r--src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java31
-rw-r--r--src/de/danoeh/antennapod/asynctask/MiroGuideThumbnailDownloader.java123
2 files changed, 128 insertions, 26 deletions
diff --git a/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java b/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java
index bfd43a08c..3ab4be684 100644
--- a/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java
+++ b/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java
@@ -16,8 +16,8 @@ import de.danoeh.antennapod.util.BitmapDecoder;
public abstract class BitmapDecodeWorkerTask extends
AsyncTask<Void, Void, Void> {
- private int PREFERRED_LENGTH;
-
+ protected int PREFERRED_LENGTH;
+
public static final int LENGTH_BASE_COVER = 200;
public static final int LENGTH_BASE_THUMBNAIL = 100;
@@ -28,7 +28,7 @@ public abstract class BitmapDecodeWorkerTask extends
protected int baseLength;
- private String fileUrl;
+ protected String fileUrl;
public BitmapDecodeWorkerTask(ImageView target, String fileUrl, int length) {
super();
@@ -61,27 +61,6 @@ public abstract class BitmapDecodeWorkerTask extends
super.onPreExecute();
}
- private int calculateSampleSize(int width, int height) {
- int max = Math.max(width, height);
- if (max < PREFERRED_LENGTH) {
- return 1;
- } else {
- // find first sample size where max / sampleSize <
- // PREFERRED_LENGTH
- for (int sampleSize = 1, power = 0;; power++, sampleSize = (int) Math
- .pow(2, power)) {
- int newLength = max / sampleSize;
- if (newLength <= PREFERRED_LENGTH) {
- if (newLength > 0) {
- return sampleSize;
- } else {
- return sampleSize - 1;
- }
- }
- }
- }
- }
-
@Override
protected Void doInBackground(Void... params) {
File f = null;
@@ -104,13 +83,13 @@ public abstract class BitmapDecodeWorkerTask extends
}
return null;
}
-
+
protected void onInvalidFileUrl() {
Log.e(TAG, "FeedImage has no valid file url. Using default image");
bitmap = BitmapFactory.decodeResource(target.getResources(),
R.drawable.default_cover);
}
-
+
protected abstract void storeBitmapInCache(Bitmap bitmap);
@SuppressLint("NewApi")
diff --git a/src/de/danoeh/antennapod/asynctask/MiroGuideThumbnailDownloader.java b/src/de/danoeh/antennapod/asynctask/MiroGuideThumbnailDownloader.java
new file mode 100644
index 000000000..c1a87b1ec
--- /dev/null
+++ b/src/de/danoeh/antennapod/asynctask/MiroGuideThumbnailDownloader.java
@@ -0,0 +1,123 @@
+package de.danoeh.antennapod.asynctask;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import com.jakewharton.DiskLruCache;
+import com.jakewharton.DiskLruCache.Editor;
+
+import de.danoeh.antennapod.AppConfig;
+import de.danoeh.antennapod.PodcastApp;
+import de.danoeh.antennapod.miroguide.model.MiroChannel;
+import de.danoeh.antennapod.util.BitmapDecoder;
+
+import android.content.Context;
+import android.content.pm.PackageManager.NameNotFoundException;
+import android.graphics.Bitmap;
+import android.os.AsyncTask;
+import android.util.Log;
+import android.widget.ImageView;
+
+/** Downlods thumbnails from the MiroGuide and stores them in a DiskLruCache */
+public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
+ private static final String TAG = "MiroGuideThumbnailDownloader";
+
+ private static final String CACHE_DIR = "miroguide_thumbnails";
+ private static final int CACHE_SIZE = 20 * 1024 * 1024;
+ private static final int VALUE_SIZE = 500 * 1024;
+
+ private Exception exception;
+
+ private MiroChannel miroChannel;
+
+ public MiroGuideThumbnailDownloader(ImageView target,
+ MiroChannel miroChannel, int length) {
+ super(target, miroChannel.getThumbnailUrl(), length);
+ this.miroChannel = miroChannel;
+ }
+
+ private static DiskLruCache openThubmnailDiskCache()
+ throws NameNotFoundException, IOException {
+ Context appContext = PodcastApp.getInstance();
+ return DiskLruCache.open(
+ appContext.getExternalFilesDir(CACHE_DIR),
+ appContext.getPackageManager().getPackageInfo(
+ appContext.getPackageName(), 0).versionCode,
+ VALUE_SIZE, CACHE_SIZE);
+ }
+
+ @Override
+ protected Void doInBackground(Void... params) {
+ // Download file to cache folder
+ URL url = null;
+ try {
+ url = new URL(fileUrl);
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ return null;
+ }
+ File destination = new File(PodcastApp.getInstance().getCacheDir(),
+ Integer.toString(fileUrl.hashCode()));
+ try {
+ DiskLruCache diskCache = openThubmnailDiskCache();
+ Editor editor = diskCache.edit(fileUrl);
+ if (editor != null) {
+ HttpURLConnection connection = (HttpURLConnection) url
+ .openConnection();
+ byte inputBuffer[] = new byte[10 * 1024];
+ InputStream input = new BufferedInputStream(
+ connection.getInputStream());
+ FileOutputStream output = new FileOutputStream(destination);
+
+ int count = 0;
+ while ((count = input.read(inputBuffer, 0, 10 * 1024)) != -1) {
+ output.write(inputBuffer, 0, count);
+ }
+ output.close();
+
+ // Get a smaller version of the bitmap and store it inside the
+ // LRU
+ // Cache
+ Bitmap bitmap = BitmapDecoder.decodeBitmap(PREFERRED_LENGTH,
+ fileUrl);
+ if (bitmap != null) {
+ OutputStream imageOut = editor.newOutputStream(0);
+ bitmap.compress(Bitmap.CompressFormat.PNG, 80, imageOut);
+ editor.commit();
+ storeBitmapInCache(bitmap);
+ }
+ } else {
+ if (AppConfig.DEBUG)
+ Log.d(TAG, "No editor object available");
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ return null;
+ } catch (NameNotFoundException e) {
+ e.printStackTrace();
+ return null;
+ } finally {
+ if (destination.exists()) {
+ destination.delete();
+ }
+ }
+ return null;
+ }
+
+ @Override
+ protected boolean tagsMatching(ImageView target) {
+ return target.getTag() == null || target.getTag() == miroChannel;
+ }
+
+ @Override
+ protected void storeBitmapInCache(Bitmap bitmap) {
+
+ }
+}