diff options
author | Daniel Oeh <daniel@danielpc.(none)> | 2012-06-02 23:50:12 +0200 |
---|---|---|
committer | Daniel Oeh <daniel@danielpc.(none)> | 2012-06-02 23:50:12 +0200 |
commit | 72e608a04999bf37b0768465602889498b0daf06 (patch) | |
tree | 917bd7e0224956729c59280fe1b5a04acf9e75c8 /src | |
parent | 8f9db702911b1ad0a82f6ef4ceb2c47569b201d3 (diff) | |
download | AntennaPod-72e608a04999bf37b0768465602889498b0daf06.zip |
Eclipse transfer, Made sure that DownloadObserver cancels when Activity
stops
Diffstat (limited to 'src')
-rw-r--r-- | src/de/podfetcher/activity/AddFeedActivity.java | 68 | ||||
-rw-r--r-- | src/de/podfetcher/activity/DownloadActivity.java | 8 | ||||
-rw-r--r-- | src/de/podfetcher/activity/ItemviewActivity.java | 7 | ||||
-rw-r--r-- | src/de/podfetcher/service/DownloadObserver.java | 2 | ||||
-rw-r--r-- | src/de/podfetcher/storage/DownloadRequester.java | 26 |
5 files changed, 79 insertions, 32 deletions
diff --git a/src/de/podfetcher/activity/AddFeedActivity.java b/src/de/podfetcher/activity/AddFeedActivity.java index 65202f2a4..2013d204e 100644 --- a/src/de/podfetcher/activity/AddFeedActivity.java +++ b/src/de/podfetcher/activity/AddFeedActivity.java @@ -21,16 +21,37 @@ import java.util.concurrent.Callable; public class AddFeedActivity extends SherlockActivity { private static final String TAG = "AddFeedActivity"; + private DownloadRequester requester; + private EditText etxtFeedurl; private Button butConfirm; private Button butCancel; + private long downloadId; - + + private DownloadObserver observer; + + private ProgressDialog progDialog; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.addfeed); + requester = DownloadRequester.getInstance(); + + createObserver(); + progDialog = new ProgressDialog(this) { + @Override + public void onBackPressed() { + requester.cancelDownload(getContext(), downloadId); + observer.cancel(true); + createObserver(); + dismiss(); + } + + }; + etxtFeedurl = (EditText) findViewById(R.id.etxtFeedurl); butConfirm = (Button) findViewById(R.id.butConfirm); butCancel = (Button) findViewById(R.id.butCancel); @@ -49,9 +70,30 @@ public class AddFeedActivity extends SherlockActivity { finish(); } }); + } + + private void createObserver() { + observer = new DownloadObserver(this) { + @Override + protected void onPostExecute(Boolean result) { + progDialog.dismiss(); + finish(); + } - - + @Override + protected void onProgressUpdate(DownloadObserver.DownloadStatus... values) { + DownloadObserver.DownloadStatus progr = values[0]; + progDialog.setMessage(getContext().getString(progr.getStatusMsg()) + + " (" + progr.getProgressPercent() + "%)"); + } + }; + } + + @Override + protected void onStop() { + super.onStop(); + Log.d(TAG, "Stopping Activity"); + observer.cancel(true); } private void addNewFeed() { @@ -60,29 +102,13 @@ public class AddFeedActivity extends SherlockActivity { if(url != null) { Feed feed = new Feed(url); - DownloadRequester req = DownloadRequester.getInstance(); - req.downloadFeed(this, feed); + downloadId = requester.downloadFeed(this, feed); observeDownload(feed); } } private void observeDownload(Feed feed) { - final ProgressDialog dialog = new ProgressDialog(this); - final DownloadObserver observer = new DownloadObserver(this) { - @Override - protected void onPostExecute(Boolean result) { - dialog.dismiss(); - finish(); - } - - @Override - protected void onProgressUpdate(DownloadObserver.DownloadStatus... values) { - DownloadObserver.DownloadStatus progr = values[0]; - dialog.setMessage(getContext().getString(progr.getStatusMsg()) - + " (" + progr.getProgressPercent() + "%)"); - } - }; - dialog.show(); + progDialog.show(); observer.execute(feed); } diff --git a/src/de/podfetcher/activity/DownloadActivity.java b/src/de/podfetcher/activity/DownloadActivity.java index 3a5a57e5b..48a18eb0f 100644 --- a/src/de/podfetcher/activity/DownloadActivity.java +++ b/src/de/podfetcher/activity/DownloadActivity.java @@ -9,6 +9,7 @@ import de.podfetcher.feed.FeedFile; import com.actionbarsherlock.app.SherlockListActivity; import android.os.Bundle; +import android.util.Log; public class DownloadActivity extends SherlockListActivity { private static final String TAG = "DownloadActivity"; @@ -25,6 +26,13 @@ public class DownloadActivity extends SherlockListActivity { new FeedFile[requester.getMediaDownloads().size()])); } + + @Override + protected void onStop() { + super.onStop(); + Log.d(TAG, "Stopping Activity"); + observer.cancel(true); + } private final DownloadObserver observer = new DownloadObserver(this) { @Override diff --git a/src/de/podfetcher/activity/ItemviewActivity.java b/src/de/podfetcher/activity/ItemviewActivity.java index 89bdcf8af..eae35ac5b 100644 --- a/src/de/podfetcher/activity/ItemviewActivity.java +++ b/src/de/podfetcher/activity/ItemviewActivity.java @@ -64,6 +64,13 @@ public class ItemviewActivity extends SherlockActivity { } }); } + + @Override + public void onStop() { + super.onStop(); + Log.d(TAG, "Stopping Activity"); + downloadObserver.cancel(true); + } /** Extracts FeedItem object the activity is supposed to display */ private void extractFeeditem() { diff --git a/src/de/podfetcher/service/DownloadObserver.java b/src/de/podfetcher/service/DownloadObserver.java index 3abd21a38..7e1803015 100644 --- a/src/de/podfetcher/service/DownloadObserver.java +++ b/src/de/podfetcher/service/DownloadObserver.java @@ -61,7 +61,7 @@ public class DownloadObserver extends AsyncTask<FeedFile, DownloadObserver.Downl } - while(downloadsLeft()) { + while(downloadsLeft() && !isCancelled()) { for (DownloadStatus status : statusList) { if (status.done == false) { Cursor cursor = getDownloadCursor(status.feedfile); diff --git a/src/de/podfetcher/storage/DownloadRequester.java b/src/de/podfetcher/storage/DownloadRequester.java index cfa91d832..572eabe93 100644 --- a/src/de/podfetcher/storage/DownloadRequester.java +++ b/src/de/podfetcher/storage/DownloadRequester.java @@ -60,7 +60,7 @@ public class DownloadRequester { return downloader; } - private void download(Context context, ArrayList<FeedFile> type, FeedFile item, File dest, boolean visibleInUI) { + private long download(Context context, ArrayList<FeedFile> type, FeedFile item, File dest, boolean visibleInUI) { Log.d(TAG, "Requesting download of url "+ item.getDownload_url()); type.add(item); DownloadManager.Request request = new DownloadManager.Request(Uri.parse(item.getDownload_url())); @@ -71,27 +71,33 @@ public class DownloadRequester { // TODO Set Allowed Network Types DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); context.startService(new Intent(context, DownloadService.class)); - item.setDownloadId(manager.enqueue(request)); - item.setFile_url(dest.toString()); + long downloadId = manager.enqueue(request); + item.setDownloadId(downloadId); + item.setFile_url(dest.toString()); + return downloadId; } - public void downloadFeed(Context context, Feed feed) { - download(context, feeds, feed, + public long downloadFeed(Context context, Feed feed) { + return download(context, feeds, feed, new File(getFeedfilePath(context), getFeedfileName(feed)), true); } - public void downloadImage(Context context, FeedImage image) { - download(context, images, image, + public long downloadImage(Context context, FeedImage image) { + return download(context, images, image, new File(getImagefilePath(context), getImagefileName(image)), true); } - public void downloadMedia(Context context, FeedMedia feedmedia) { - download(context, media, feedmedia, + public long downloadMedia(Context context, FeedMedia feedmedia) { + return download(context, media, feedmedia, new File(getMediafilePath(context, feedmedia), getMediafilename(feedmedia)), true); } - + + /** Cancels a running download. + * @param context A context needed to get the DownloadManager service + * @param id ID of the download to cancel + * */ public void cancelDownload(final Context context, final long id) { Log.d(TAG, "Cancelling download with id " + id); DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); |