diff options
author | Daniel Oeh <daniel@danielpc.(none)> | 2012-05-28 13:22:23 +0200 |
---|---|---|
committer | Daniel Oeh <daniel@danielpc.(none)> | 2012-05-28 13:22:23 +0200 |
commit | 0cecd8255082ef32908dc9b1440b15218aca3e66 (patch) | |
tree | 7cdd0ef24ddf44b50ff53da21f4c021dd048ab9e /src/de/podfetcher | |
parent | 4caa46e03f16a3dcbc8e8fe383c4d64ed533980a (diff) | |
download | AntennaPod-0cecd8255082ef32908dc9b1440b15218aca3e66.zip |
Implemented FeedMedia Download
Diffstat (limited to 'src/de/podfetcher')
-rw-r--r-- | src/de/podfetcher/activity/ItemviewActivity.java | 50 | ||||
-rw-r--r-- | src/de/podfetcher/feed/FeedManager.java | 6 | ||||
-rw-r--r-- | src/de/podfetcher/service/DownloadService.java | 12 | ||||
-rw-r--r-- | src/de/podfetcher/storage/DownloadRequester.java | 10 |
4 files changed, 77 insertions, 1 deletions
diff --git a/src/de/podfetcher/activity/ItemviewActivity.java b/src/de/podfetcher/activity/ItemviewActivity.java index c2d1a5564..b3c42397b 100644 --- a/src/de/podfetcher/activity/ItemviewActivity.java +++ b/src/de/podfetcher/activity/ItemviewActivity.java @@ -1,6 +1,7 @@ package de.podfetcher.activity; import java.io.File; +import java.util.concurrent.Callable; import android.net.Uri; import android.graphics.BitmapFactory; import com.actionbarsherlock.app.SherlockActivity; @@ -10,16 +11,20 @@ import android.os.Bundle; import de.podfetcher.feed.*; import android.util.Log; import android.content.Intent; +import android.content.Context; import android.widget.Button; import android.widget.TextView; import android.widget.ImageView; import de.podfetcher.R; +import de.podfetcher.service.DownloadObserver; +import de.podfetcher.storage.DownloadRequester; /** Displays a single FeedItem and provides various actions */ public class ItemviewActivity extends SherlockActivity { private static final String TAG = "ItemviewActivity"; private FeedManager manager; + private DownloadRequester requester; private FeedItem item; // Widgets @@ -36,6 +41,16 @@ public class ItemviewActivity extends SherlockActivity { manager = FeedManager.getInstance(); extractFeeditem(); populateUI(); + getDownloadStatus(); + + butDownload.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + requester = DownloadRequester.getInstance(); + requester.downloadMedia(v.getContext(), item.getMedia()); + getDownloadStatus(); + } + }); } /** Extracts FeedItem object the activity is supposed to display */ @@ -68,6 +83,41 @@ public class ItemviewActivity extends SherlockActivity { imgvImage.setImageBitmap(item.getFeed().getImage().getImageBitmap()); } } + + private void getDownloadStatus() { + if(item.getMedia().getFile_url() == null) { + butPlay.setEnabled(false); + butDownload.setEnabled(true); + butRemove.setEnabled(false); + } else { + final DownloadObserver observer = new DownloadObserver( + item.getMedia().getDownloadId(), this); + + final Callable client = new Callable() { + public Object call() { + runOnUiThread(new Runnable() { + public void run() { + if(observer.getDone() && + // Download successful + observer.getResult() == R.string.download_successful) { + butDownload.setEnabled(false); + butPlay.setEnabled(true); + butRemove.setEnabled(true); + } else { + // Download running + butDownload.setEnabled(false); + butPlay.setEnabled(false); + butRemove.setEnabled(false); + } + } + }); + return null; + } + }; + observer.setClient(client); + observer.start(); + } + } } diff --git a/src/de/podfetcher/feed/FeedManager.java b/src/de/podfetcher/feed/FeedManager.java index 83e718310..8baee5a1d 100644 --- a/src/de/podfetcher/feed/FeedManager.java +++ b/src/de/podfetcher/feed/FeedManager.java @@ -136,6 +136,12 @@ public class FeedManager { return adapter.setImage(image); } + /** Updates information of an existing FeedMedia object. */ + public long setFeedMedia(Context context, FeedMedia media) { + PodDBAdapter adapter = new PodDBAdapter(context); + return adapter.setMedia(media); + } + /** Get a Feed by its id */ public Feed getFeed(long id) { for(Feed f : feeds) { diff --git a/src/de/podfetcher/service/DownloadService.java b/src/de/podfetcher/service/DownloadService.java index 21dfe9c67..074da4694 100644 --- a/src/de/podfetcher/service/DownloadService.java +++ b/src/de/podfetcher/service/DownloadService.java @@ -102,6 +102,11 @@ public class DownloadService extends Service { FeedImage image = requester.getFeedImage(downloadId); if(image != null) { handleCompletedImageDownload(context, image); + } else { + FeedMedia media = requester.getFeedMedia(downloadId); + if (media != null) { + handleCompletedFeedMediaDownload(context, media); + } } } queryDownloads(); @@ -131,6 +136,13 @@ public class DownloadService extends Service { manager.setFeedImage(this, image); } + /** Is called whenever a FeedMedia is downloaded. */ + private void handleCompletedFeedMediaDownload(Context context, FeedMedia media) { + Log.d(TAG, "Handling completed FeedMedia Download"); + requester.removeFeedMedia(media); + manager.setFeedMedia(this, media); + } + class IncomingHandler extends Handler { @Override public void handleMessage(Message msg) { diff --git a/src/de/podfetcher/storage/DownloadRequester.java b/src/de/podfetcher/storage/DownloadRequester.java index 3c44bd427..c10b15a92 100644 --- a/src/de/podfetcher/storage/DownloadRequester.java +++ b/src/de/podfetcher/storage/DownloadRequester.java @@ -87,7 +87,7 @@ public class DownloadRequester { public void downloadMedia(Context context, FeedMedia feedmedia) { download(context, media, feedmedia, - new File(context.getExternalFilesDir(MEDIA_DOWNLOADPATH), "media-" + media.size()), + new File(getMediafilePath(context, feedmedia), getMediafilename(feedmedia)), true); } @@ -183,6 +183,14 @@ public class DownloadRequester { return "image-" + NumberGenerator.generateLong(image.getDownload_url()); } + public String getMediafilePath(Context context, FeedMedia media) { + return context.getExternalFilesDir(MEDIA_DOWNLOADPATH).toString() + "/"; + } + + public String getMediafilename(FeedMedia media) { + return "media-" + NumberGenerator.generateLong(media.getDownload_url()); + } + /* ------------ Methods for communicating with the DownloadService ------------- */ private Messenger mService = null; |