diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2012-08-01 11:56:14 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2012-08-01 11:56:14 +0200 |
commit | 460b77ea0a5b6867b9efd5371276a62fd5ae68ba (patch) | |
tree | 181392f09928a8138f8b9089911eff00a8aaa1b9 /src/de/danoeh/antennapod | |
parent | 9396769e17a021a16217c167814f506d4408b862 (diff) | |
download | AntennaPod-460b77ea0a5b6867b9efd5371276a62fd5ae68ba.zip |
Improved stability when DownloadObserver changes it's content
Diffstat (limited to 'src/de/danoeh/antennapod')
-rw-r--r-- | src/de/danoeh/antennapod/asynctask/DownloadObserver.java | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/src/de/danoeh/antennapod/asynctask/DownloadObserver.java b/src/de/danoeh/antennapod/asynctask/DownloadObserver.java index 9da0ad90c..08353add7 100644 --- a/src/de/danoeh/antennapod/asynctask/DownloadObserver.java +++ b/src/de/danoeh/antennapod/asynctask/DownloadObserver.java @@ -3,11 +3,13 @@ package de.danoeh.antennapod.asynctask; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; import android.app.DownloadManager; import android.content.Context; import android.database.Cursor; import android.os.AsyncTask; +import android.os.Handler; import android.util.Log; import de.danoeh.antennapod.feed.FeedFile; import de.danoeh.antennapod.storage.DownloadRequester; @@ -31,16 +33,18 @@ public class DownloadObserver extends AsyncTask<Void, Void, Void> { private DownloadRequester requester; private Context context; - private ArrayList<DownloadStatus> statusList; + private List<DownloadStatus> statusList; private List<DownloadObserver.Callback> observer; + private Handler contentChanger; public DownloadObserver(Context context) { super(); this.context = context; requester = DownloadRequester.getInstance(); - statusList = new ArrayList<DownloadStatus>(); + statusList = new CopyOnWriteArrayList<DownloadStatus>(); observer = Collections .synchronizedList(new ArrayList<DownloadObserver.Callback>()); + contentChanger = new Handler(); } @Override @@ -88,7 +92,7 @@ public class DownloadObserver extends AsyncTask<Void, Void, Void> { } private void refreshStatuslist() { - ArrayList<DownloadStatus> unhandledItems = new ArrayList<DownloadStatus>( + final ArrayList<DownloadStatus> unhandledItems = new ArrayList<DownloadStatus>( statusList); Cursor cursor = getDownloadCursor(); @@ -102,7 +106,14 @@ public class DownloadObserver extends AsyncTask<Void, Void, Void> { if (status == null) { status = new DownloadStatus(feedFile); - statusList.add(status); + final DownloadStatus statusToAdd = status; + contentChanger.post(new Runnable() { + @Override + public void run() { + statusList.add(statusToAdd); + publishProgress(); + } + }); } else { unhandledItems.remove(status); } @@ -138,11 +149,18 @@ public class DownloadObserver extends AsyncTask<Void, Void, Void> { } while (cursor.moveToNext()); } cursor.close(); + contentChanger.post(new Runnable() { + + @Override + public void run() { + // remove unhandled items from statuslist + for (DownloadStatus status : unhandledItems) { + statusList.remove(status); + } + publishProgress(); + } + }); - // remove unhandled items from statuslist - for (DownloadStatus status : unhandledItems) { - statusList.remove(status); - } } /** Request a cursor with all running Feedfile downloads */ @@ -195,7 +213,7 @@ public class DownloadObserver extends AsyncTask<Void, Void, Void> { return null; } - public ArrayList<DownloadStatus> getStatusList() { + public List<DownloadStatus> getStatusList() { return statusList; } |