summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/de/danoeh/antennapod/activity/MiroGuideMainActivity.java2
-rw-r--r--src/de/danoeh/antennapod/adapter/MiroGuideChannelListAdapter.java5
-rw-r--r--src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java43
-rw-r--r--src/de/danoeh/antennapod/asynctask/FeedImageLoader.java68
-rw-r--r--src/de/danoeh/antennapod/asynctask/MiroGuideThumbnailDownloader.java19
-rw-r--r--src/de/danoeh/antennapod/fragment/MiroGuideChannellistFragment.java2
-rw-r--r--src/de/danoeh/antennapod/miroguide/con/MiroGuideConnector.java8
-rw-r--r--src/de/danoeh/antennapod/miroguide/con/MiroGuideService.java4
8 files changed, 93 insertions, 58 deletions
diff --git a/src/de/danoeh/antennapod/activity/MiroGuideMainActivity.java b/src/de/danoeh/antennapod/activity/MiroGuideMainActivity.java
index ff37b4e76..56014b404 100644
--- a/src/de/danoeh/antennapod/activity/MiroGuideMainActivity.java
+++ b/src/de/danoeh/antennapod/activity/MiroGuideMainActivity.java
@@ -103,6 +103,8 @@ public class MiroGuideMainActivity extends SherlockListActivity {
} catch (MiroGuideException e) {
e.printStackTrace();
exception = e;
+ } finally {
+ service.close();
}
return null;
}
diff --git a/src/de/danoeh/antennapod/adapter/MiroGuideChannelListAdapter.java b/src/de/danoeh/antennapod/adapter/MiroGuideChannelListAdapter.java
index 1249b7905..4c097c00c 100644
--- a/src/de/danoeh/antennapod/adapter/MiroGuideChannelListAdapter.java
+++ b/src/de/danoeh/antennapod/adapter/MiroGuideChannelListAdapter.java
@@ -10,6 +10,7 @@ import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import de.danoeh.antennapod.R;
+import de.danoeh.antennapod.asynctask.FeedImageLoader;
import de.danoeh.antennapod.miroguide.model.MiroChannel;
public class MiroGuideChannelListAdapter extends ArrayAdapter<MiroChannel> {
@@ -41,9 +42,9 @@ public class MiroGuideChannelListAdapter extends ArrayAdapter<MiroChannel> {
holder = (Holder) convertView.getTag();
}
- holder.cover.setVisibility(View.GONE);
holder.title.setText(channel.getName());
-
+ holder.cover.setTag(channel);
+ FeedImageLoader.getInstance().loadMiroGuideThumbnail(channel, holder.cover);
return convertView;
}
diff --git a/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java b/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java
index c75bc09e1..586e20deb 100644
--- a/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java
+++ b/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java
@@ -6,6 +6,7 @@ import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
+import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;
import de.danoeh.antennapod.AppConfig;
@@ -13,8 +14,7 @@ import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.util.BitmapDecoder;
-public abstract class BitmapDecodeWorkerTask extends
- AsyncTask<Void, Void, Void> {
+public abstract class BitmapDecodeWorkerTask extends Thread {
protected int PREFERRED_LENGTH;
@@ -30,8 +30,12 @@ public abstract class BitmapDecodeWorkerTask extends
protected String fileUrl;
- public BitmapDecodeWorkerTask(ImageView target, String fileUrl, int length) {
+ private Handler handler;
+
+ public BitmapDecodeWorkerTask(Handler handler, ImageView target,
+ String fileUrl, int length) {
super();
+ this.handler = handler;
this.target = target;
this.fileUrl = fileUrl;
this.baseLength = length;
@@ -44,9 +48,7 @@ public abstract class BitmapDecodeWorkerTask extends
*/
abstract protected boolean tagsMatching(ImageView target);
- @Override
- protected void onPostExecute(Void result) {
- super.onPostExecute(result);
+ protected void onPostExecute() {
// check if imageview is still supposed to display this image
if (tagsMatching(target)) {
target.setImageBitmap(bitmap);
@@ -57,12 +59,7 @@ public abstract class BitmapDecodeWorkerTask extends
}
@Override
- protected void onPreExecute() {
- super.onPreExecute();
- }
-
- @Override
- protected Void doInBackground(Void... params) {
+ public void run() {
File f = null;
if (fileUrl != null) {
f = new File(fileUrl);
@@ -81,7 +78,18 @@ public abstract class BitmapDecodeWorkerTask extends
} else {
onInvalidFileUrl();
}
- return null;
+ endBackgroundTask();
+ }
+
+ protected final void endBackgroundTask() {
+ handler.post(new Runnable() {
+
+ @Override
+ public void run() {
+ onPostExecute();
+ }
+
+ });
}
protected void onInvalidFileUrl() {
@@ -98,13 +106,4 @@ public abstract class BitmapDecodeWorkerTask extends
loader.addBitmapToThumbnailCache(fileUrl, bitmap);
}
}
-
- @SuppressLint("NewApi")
- public void executeAsync() {
- if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
- executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
- } else {
- execute();
- }
- }
}
diff --git a/src/de/danoeh/antennapod/asynctask/FeedImageLoader.java b/src/de/danoeh/antennapod/asynctask/FeedImageLoader.java
index c7430f90f..bed6a2fbb 100644
--- a/src/de/danoeh/antennapod/asynctask/FeedImageLoader.java
+++ b/src/de/danoeh/antennapod/asynctask/FeedImageLoader.java
@@ -1,6 +1,9 @@
package de.danoeh.antennapod.asynctask;
import java.io.IOException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
@@ -8,6 +11,7 @@ import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
+import android.os.Handler;
import android.support.v4.util.LruCache;
import android.util.Log;
import android.widget.ImageView;
@@ -34,6 +38,9 @@ public class FeedImageLoader {
private static final int CACHE_SIZE = 20 * 1024 * 1024;
private static final int VALUE_SIZE = 500 * 1024;
+ private Handler handler;
+ private ExecutorService executor;
+
/**
* Stores references to loaded bitmaps. Bitmaps can be accessed by the id of
* the FeedImage the bitmap belongs to.
@@ -50,6 +57,18 @@ public class FeedImageLoader {
private LruCache<String, Bitmap> thumbnailCache;
private FeedImageLoader() {
+ handler = new Handler();
+ executor = Executors.newFixedThreadPool(Runtime.getRuntime()
+ .availableProcessors(), new ThreadFactory() {
+
+ @Override
+ public Thread newThread(Runnable r) {
+ Thread t = new Thread(r);
+ t.setPriority(Thread.MIN_PRIORITY);
+ return t;
+ }
+ });
+
coverCache = new LruCache<String, Bitmap>(coverCacheSize) {
@SuppressLint("NewApi")
@@ -110,8 +129,8 @@ public class FeedImageLoader {
} else {
target.setImageResource(R.drawable.default_cover);
FeedImageDecodeWorkerTask worker = new FeedImageDecodeWorkerTask(
- target, image, LENGTH_BASE_COVER);
- worker.executeAsync();
+ handler, target, image, LENGTH_BASE_COVER);
+ executor.submit(worker);
}
} else {
target.setImageResource(R.drawable.default_cover);
@@ -126,30 +145,36 @@ public class FeedImageLoader {
} else {
target.setImageResource(R.drawable.default_cover);
FeedImageDecodeWorkerTask worker = new FeedImageDecodeWorkerTask(
- target, image, LENGTH_BASE_THUMBNAIL);
- worker.executeAsync();
+ handler, target, image, LENGTH_BASE_THUMBNAIL);
+ executor.submit(worker);
}
} else {
target.setImageResource(R.drawable.default_cover);
}
}
-
+
public void loadMiroGuideThumbnail(MiroChannel channel, ImageView target) {
if (channel.getThumbnailUrl() != null) {
- Bitmap bitmap = getBitmapFromThumbnailCache(channel.getThumbnailUrl());
- if (bitmap != null) {
+ Bitmap bitmap = getBitmapFromThumbnailCache(channel
+ .getThumbnailUrl());
+ if (bitmap == null) {
boolean isInDiskCache = false;
try {
- isInDiskCache = isInThumbnailDiskCache(channel.getThumbnailUrl());
+ isInDiskCache = isInThumbnailDiskCache(channel
+ .getThumbnailUrl());
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "Error when trying to read disk cache");
}
if (isInDiskCache) {
- new MiroGuideDiskCacheLoader(target, channel, LENGTH_BASE_THUMBNAIL).executeAsync();
+ executor.submit(new MiroGuideDiskCacheLoader(handler,
+ target, channel, LENGTH_BASE_THUMBNAIL));
} else {
- new MiroGuideThumbnailDownloader(target, channel, LENGTH_BASE_THUMBNAIL).executeAsync();
+ executor.submit(new MiroGuideThumbnailDownloader(handler,
+ target, channel, LENGTH_BASE_THUMBNAIL));
}
+ } else {
+ target.setImageBitmap(bitmap);
}
} else {
target.setImageResource(R.drawable.default_cover);
@@ -184,7 +209,7 @@ public class FeedImageLoader {
public void addBitmapToCoverCache(String key, Bitmap bitmap) {
coverCache.put(key, bitmap);
}
-
+
public boolean isInThumbnailDiskCache(String key) throws IOException {
DiskLruCache cache = openThubmnailDiskCache();
return cache.get(key) != null;
@@ -196,9 +221,9 @@ public class FeedImageLoader {
protected FeedImage image;
- public FeedImageDecodeWorkerTask(ImageView target, FeedImage image,
- int length) {
- super(target, image.getFile_url(), length);
+ public FeedImageDecodeWorkerTask(Handler handler, ImageView target,
+ FeedImage image, int length) {
+ super(handler, target, image.getFile_url(), length);
this.image = image;
}
@@ -227,14 +252,13 @@ public class FeedImageLoader {
private MiroChannel channel;
- public MiroGuideDiskCacheLoader(ImageView target, MiroChannel channel,
- int length) {
- super(target, channel.getThumbnailUrl(), length);
+ public MiroGuideDiskCacheLoader(Handler handler, ImageView target,
+ MiroChannel channel, int length) {
+ super(handler, target, channel.getThumbnailUrl(), length);
this.channel = channel;
}
- @Override
- protected Void doInBackground(Void... params) {
+ public void run() {
try {
DiskLruCache cache = openThubmnailDiskCache();
Snapshot snapshot = cache.get(fileUrl);
@@ -244,13 +268,13 @@ public class FeedImageLoader {
e.printStackTrace();
exception = e;
}
- return null;
+ endBackgroundTask();
}
@Override
- protected void onPostExecute(Void result) {
+ protected void onPostExecute() {
if (exception != null) {
- super.onPostExecute(result);
+ super.onPostExecute();
} else {
Log.e(TAG, "Failed to load bitmap from disk cache");
}
diff --git a/src/de/danoeh/antennapod/asynctask/MiroGuideThumbnailDownloader.java b/src/de/danoeh/antennapod/asynctask/MiroGuideThumbnailDownloader.java
index 98b7ba04c..87eca833b 100644
--- a/src/de/danoeh/antennapod/asynctask/MiroGuideThumbnailDownloader.java
+++ b/src/de/danoeh/antennapod/asynctask/MiroGuideThumbnailDownloader.java
@@ -19,6 +19,7 @@ import de.danoeh.antennapod.miroguide.model.MiroChannel;
import de.danoeh.antennapod.util.BitmapDecoder;
import android.graphics.Bitmap;
+import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;
@@ -30,30 +31,29 @@ public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
private MiroChannel miroChannel;
- public MiroGuideThumbnailDownloader(ImageView target,
+ public MiroGuideThumbnailDownloader(Handler handler, ImageView target,
MiroChannel miroChannel, int length) {
- super(target, miroChannel.getThumbnailUrl(), length);
+ super(handler, target, miroChannel.getThumbnailUrl(), length);
this.miroChannel = miroChannel;
}
@Override
- protected void onPostExecute(Void result) {
+ protected void onPostExecute() {
if (exception != null) {
- super.onPostExecute(result);
+ super.onPostExecute();
} else {
Log.e(TAG, "Failed to download thumbnail");
}
}
- @Override
- protected Void doInBackground(Void... params) {
+ public void run() {
// Download file to cache folder
URL url = null;
try {
url = new URL(fileUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
- return null;
+ endBackgroundTask();
}
File destination = new File(PodcastApp.getInstance().getCacheDir(),
Integer.toString(fileUrl.hashCode()));
@@ -73,6 +73,7 @@ public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
output.write(inputBuffer, 0, count);
}
output.close();
+ connection.disconnect();
if (AppConfig.DEBUG) Log.d(TAG, "MiroGuide thumbnail downloaded");
// Get a smaller version of the bitmap and store it inside the
// LRU
@@ -91,13 +92,13 @@ public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
}
} catch (IOException e) {
e.printStackTrace();
- return null;
+ endBackgroundTask();
} finally {
if (destination.exists()) {
destination.delete();
}
}
- return null;
+ endBackgroundTask();
}
@Override
diff --git a/src/de/danoeh/antennapod/fragment/MiroGuideChannellistFragment.java b/src/de/danoeh/antennapod/fragment/MiroGuideChannellistFragment.java
index 6817ab4d8..c0f25cc9b 100644
--- a/src/de/danoeh/antennapod/fragment/MiroGuideChannellistFragment.java
+++ b/src/de/danoeh/antennapod/fragment/MiroGuideChannellistFragment.java
@@ -227,6 +227,8 @@ public class MiroGuideChannellistFragment extends SherlockListFragment {
} catch (MiroGuideException e) {
exception = e;
e.printStackTrace();
+ } finally {
+ service.close();
}
return null;
}
diff --git a/src/de/danoeh/antennapod/miroguide/con/MiroGuideConnector.java b/src/de/danoeh/antennapod/miroguide/con/MiroGuideConnector.java
index cccfff4d5..4d137a56f 100644
--- a/src/de/danoeh/antennapod/miroguide/con/MiroGuideConnector.java
+++ b/src/de/danoeh/antennapod/miroguide/con/MiroGuideConnector.java
@@ -15,10 +15,11 @@ import org.json.JSONException;
import org.json.JSONObject;
import android.net.Uri;
+import android.net.http.AndroidHttpClient;
/** Executes HTTP requests and returns the results. */
public class MiroGuideConnector {
- private HttpClient httpClient;
+ private AndroidHttpClient httpClient;
private static final String HOST_URL = "https://www.miroguide.com/api/";
private static final String PATH_GET_CHANNELS = "get_channels";
@@ -26,11 +27,11 @@ public class MiroGuideConnector {
private static final String PATH_GET_CHANNEL = "get_channel";
public MiroGuideConnector() {
- httpClient = new DefaultHttpClient();
+ httpClient = AndroidHttpClient.newInstance(null);
}
public void shutdown() {
- httpClient.getConnectionManager().shutdown();
+ httpClient.close();
}
private Uri.Builder getBaseURIBuilder(String path) {
@@ -78,6 +79,7 @@ public class MiroGuideConnector {
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
result = reader.readLine();
+ in.close();
}
} else {
throw new MiroGuideException(response.getStatusLine()
diff --git a/src/de/danoeh/antennapod/miroguide/con/MiroGuideService.java b/src/de/danoeh/antennapod/miroguide/con/MiroGuideService.java
index a3ecf6e06..be2679229 100644
--- a/src/de/danoeh/antennapod/miroguide/con/MiroGuideService.java
+++ b/src/de/danoeh/antennapod/miroguide/con/MiroGuideService.java
@@ -40,6 +40,10 @@ public class MiroGuideService {
public MiroGuideService() {
connector = new MiroGuideConnector();
}
+
+ public void close() {
+ connector.shutdown();
+ }
public String[] getCategories() throws MiroGuideException {
JSONArray resultArray = connector.getArrayResponse(connector