diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2012-06-16 13:21:41 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2012-06-16 13:21:41 +0200 |
commit | 26c8772e0a60aa916dceff4ec4d4eb7af2a19460 (patch) | |
tree | 6d48ff67f4a5e207eca4111ca69da2084fe4353e /src | |
parent | c41605ef3b9a21025d3625d577b53d63f64f1074 (diff) | |
download | AntennaPod-26c8772e0a60aa916dceff4ec4d4eb7af2a19460.zip |
Cleaned up the Download requester
Diffstat (limited to 'src')
-rw-r--r-- | src/de/podfetcher/activity/DownloadActivity.java | 4 | ||||
-rw-r--r-- | src/de/podfetcher/adapter/DownloadlistAdapter.java | 17 | ||||
-rw-r--r-- | src/de/podfetcher/service/DownloadService.java | 31 | ||||
-rw-r--r-- | src/de/podfetcher/storage/DownloadRequester.java | 134 |
4 files changed, 62 insertions, 124 deletions
diff --git a/src/de/podfetcher/activity/DownloadActivity.java b/src/de/podfetcher/activity/DownloadActivity.java index 48a18eb0f..0da47053a 100644 --- a/src/de/podfetcher/activity/DownloadActivity.java +++ b/src/de/podfetcher/activity/DownloadActivity.java @@ -22,8 +22,8 @@ public class DownloadActivity extends SherlockListActivity { super.onCreate(savedInstanceState); requester = DownloadRequester.getInstance(); - observer.execute(requester.getMediaDownloads().toArray( - new FeedFile[requester.getMediaDownloads().size()])); + observer.execute(requester.getDownloads().toArray( + new FeedFile[requester.getDownloads().size()])); } diff --git a/src/de/podfetcher/adapter/DownloadlistAdapter.java b/src/de/podfetcher/adapter/DownloadlistAdapter.java index 53196c0e4..a13e3be8b 100644 --- a/src/de/podfetcher/adapter/DownloadlistAdapter.java +++ b/src/de/podfetcher/adapter/DownloadlistAdapter.java @@ -9,6 +9,9 @@ import android.content.Context; import de.podfetcher.R; import de.podfetcher.util.Converter; +import de.podfetcher.feed.Feed; +import de.podfetcher.feed.FeedFile; +import de.podfetcher.feed.FeedImage; import de.podfetcher.feed.FeedMedia; import de.podfetcher.service.DownloadObserver; @@ -23,7 +26,7 @@ public class DownloadlistAdapter extends ArrayAdapter<DownloadObserver.DownloadS public View getView(int position, View convertView, ViewGroup parent) { Holder holder; DownloadObserver.DownloadStatus status = getItem(position); - + FeedFile feedFile = status.getFeedFile(); // Inflate layout if (convertView == null) { holder = new Holder(); @@ -38,8 +41,16 @@ public class DownloadlistAdapter extends ArrayAdapter<DownloadObserver.DownloadS } else { holder = (Holder) convertView.getTag(); } - - holder.title.setText( ((FeedMedia) status.getFeedFile()).getItem().getTitle()); + + String titleText = null; + if (feedFile.getClass() == FeedMedia.class) { + titleText = ((FeedMedia) feedFile).getItem().getTitle(); + } else if (feedFile.getClass() == Feed.class) { + titleText = ((Feed) feedFile).getTitle(); + } else if (feedFile.getClass() == FeedImage.class) { + titleText = "[Image] " + ((FeedImage) feedFile).getTitle(); + } + holder.title.setText(titleText); holder.downloaded.setText(Converter.byteToString(status.getSoFar()) + " / " + Converter.byteToString(status.getSize())); holder.percent.setText(status.getProgressPercent() + "%"); diff --git a/src/de/podfetcher/service/DownloadService.java b/src/de/podfetcher/service/DownloadService.java index bbd64d420..cafdeecab 100644 --- a/src/de/podfetcher/service/DownloadService.java +++ b/src/de/podfetcher/service/DownloadService.java @@ -154,18 +154,16 @@ public class DownloadService extends Service { } if (status == DownloadManager.STATUS_SUCCESSFUL) { - Feed feed = requester.getFeed(downloadId); - if (feed != null) { - handleCompletedFeedDownload(context, feed); - } else { - FeedImage image = requester.getFeedImage(downloadId); - if (image != null) { - handleCompletedImageDownload(context, image); - } else { - FeedMedia media = requester.getFeedMedia(downloadId); - if (media != null) { - handleCompletedFeedMediaDownload(context, media); - } + FeedFile download = requester.getFeedFile(downloadId); + if (download != null) { + if (download.getClass() == Feed.class) { + handleCompletedFeedDownload(context, (Feed) download); + } else if (download.getClass() == FeedImage.class) { + handleCompletedImageDownload(context, + (FeedImage) download); + } else if (download.getClass() == FeedMedia.class) { + handleCompletedFeedMediaDownload(context, + (FeedMedia) download); } } queryDownloads(); @@ -189,7 +187,8 @@ public class DownloadService extends Service { initiateShutdown(); } else { // update notification - notificationBuilder.setContentText(numOfDownloads + " Downloads left"); + notificationBuilder.setContentText(numOfDownloads + + " Downloads left"); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(NOTIFICATION_ID, notificationBuilder.getNotification()); } @@ -241,7 +240,7 @@ public class DownloadService extends Service { Log.d(TAG, "Feed has image; Downloading...."); requester.downloadImage(service, feed.getImage()); } - requester.removeFeed(feed); + requester.removeDownload(feed); cleanup(); @@ -274,7 +273,7 @@ public class DownloadService extends Service { @Override public void run() { image.setDownloaded(true); - requester.removeFeedImage(image); + requester.removeDownload(image); manager.setFeedImage(service, image); queryDownloads(); } @@ -293,7 +292,7 @@ public class DownloadService extends Service { @Override public void run() { - requester.removeFeedMedia(media); + requester.removeDownload(media); media.setDownloaded(true); // Get duration try { diff --git a/src/de/podfetcher/storage/DownloadRequester.java b/src/de/podfetcher/storage/DownloadRequester.java index 4fcc710c0..52bb663e8 100644 --- a/src/de/podfetcher/storage/DownloadRequester.java +++ b/src/de/podfetcher/storage/DownloadRequester.java @@ -33,7 +33,7 @@ public class DownloadRequester { public static String ACTION_FEED_DOWNLOAD_COMPLETED = "action.de.podfetcher.storage.feed_download_completed"; public static String ACTION_MEDIA_DOWNLOAD_COMPLETED = "action.de.podfetcher.storage.media_download_completed"; public static String ACTION_IMAGE_DOWNLOAD_COMPLETED = "action.de.podfetcher.storage.image_download_completed"; - + private static boolean STORE_ON_SD = true; public static String IMAGE_DOWNLOADPATH = "images/"; public static String FEED_DOWNLOADPATH = "cache/"; @@ -42,15 +42,10 @@ public class DownloadRequester { private static DownloadRequester downloader; private DownloadManager manager; - public ArrayList<FeedFile> feeds; - public ArrayList<FeedFile> images; - public ArrayList<FeedFile> media; + public ArrayList<FeedFile> downloads; private DownloadRequester() { - feeds = new ArrayList<FeedFile>(); - images = new ArrayList<FeedFile>(); - media = new ArrayList<FeedFile>(); - + downloads = new ArrayList<FeedFile>(); } public static DownloadRequester getInstance() { @@ -60,17 +55,16 @@ public class DownloadRequester { return downloader; } - private long download(Context context, ArrayList<FeedFile> type, - FeedFile item, File dest) { + private long download(Context context, FeedFile item, File dest) { if (dest.exists()) { Log.d(TAG, "File already exists. Deleting !"); dest.delete(); } Log.d(TAG, "Requesting download of url " + item.getDownload_url()); - type.add(item); + downloads.add(item); DownloadManager.Request request = new DownloadManager.Request( - Uri.parse(item.getDownload_url())) - .setDestinationUri(Uri.fromFile(dest)); + Uri.parse(item.getDownload_url())).setDestinationUri(Uri + .fromFile(dest)); Log.d(TAG, "Version is " + currentApi); if (currentApi >= 11) { request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN); @@ -78,7 +72,7 @@ public class DownloadRequester { request.setVisibleInDownloadsUi(false); request.setShowRunningNotification(false); } - + // TODO Set Allowed Network Types DownloadManager manager = (DownloadManager) context .getSystemService(Context.DOWNLOAD_SERVICE); @@ -86,23 +80,23 @@ public class DownloadRequester { long downloadId = manager.enqueue(request); item.setDownloadId(downloadId); item.setFile_url(dest.toString()); - + notifyDownloadService(context); return downloadId; } public long downloadFeed(Context context, Feed feed) { - return download(context, feeds, feed, new File( - getFeedfilePath(context), getFeedfileName(feed))); + return download(context, feed, new File(getFeedfilePath(context), + getFeedfileName(feed))); } public long downloadImage(Context context, FeedImage image) { - return download(context, images, image, new File( - getImagefilePath(context), getImagefileName(image))); + return download(context, image, new File(getImagefilePath(context), + getImagefileName(image))); } public long downloadMedia(Context context, FeedMedia feedmedia) { - return download(context, media, feedmedia, + return download(context, feedmedia, new File(getMediafilePath(context, feedmedia), getMediafilename(feedmedia))); } @@ -117,81 +111,39 @@ public class DownloadRequester { * */ public void cancelDownload(final Context context, final long id) { Log.d(TAG, "Cancelling download with id " + id); - DownloadManager manager = (DownloadManager) context + DownloadManager dm = (DownloadManager) context .getSystemService(Context.DOWNLOAD_SERVICE); - int removed = manager.remove(id); + int removed = dm.remove(id); if (removed > 0) { - // Delete downloads in lists - Feed feed = getFeed(id); - if (feed != null) { - feeds.remove(feed); - } else { - FeedImage image = getFeedImage(id); - if (image != null) { - images.remove(image); - } else { - FeedMedia m = getFeedMedia(id); - if (media != null) { - media.remove(m); - } - } - } - } - } - - /** Get a Feed by its download id */ - public Feed getFeed(long id) { - for (FeedFile f : feeds) { - if (f.getDownloadId() == id) { - return (Feed) f; + FeedFile f = getFeedFile(id); + if (f != null) { + downloads.remove(f); } } - return null; } - /** Get a FeedImage by its download id */ - public FeedImage getFeedImage(long id) { - for (FeedFile f : images) { + /** Get a feedfile by its download id */ + public FeedFile getFeedFile(long id) { + for (FeedFile f : downloads) { if (f.getDownloadId() == id) { - return (FeedImage) f; + return f; } } return null; } - /** Get media by its download id */ - public FeedMedia getFeedMedia(long id) { - for (FeedFile f : media) { - if (f.getDownloadId() == id) { - return (FeedMedia) f; - } - } - return null; - } - - public void removeFeed(Feed f) { - feeds.remove(f); - } - - public void removeFeedMedia(FeedMedia m) { - media.remove(m); - } - - public void removeFeedImage(FeedImage fi) { - images.remove(fi); + /** Remove an object from the downloads-list of the requester. */ + public void removeDownload(FeedFile f) { + downloads.remove(f); } - public ArrayList<FeedFile> getMediaDownloads() { - return media; + public ArrayList<FeedFile> getDownloads() { + return downloads; } /** Get the number of uncompleted Downloads */ public int getNumberOfDownloads() { - return feeds.size() + images.size() + media.size(); - } - - public int getNumberOfFeedDownloads() { - return feeds.size(); + return downloads.size(); } public String getFeedfilePath(Context context) { @@ -223,30 +175,6 @@ public class DownloadRequester { media.getMime_type()); } - public boolean isDownloaded(Feed feed) { - return feed.getFile_url() != null && !feeds.contains(feed); - } - - public boolean isDownloaded(FeedImage image) { - return image.getFile_url() != null && !images.contains(image); - } - - public boolean isDownloaded(FeedMedia m) { - return m.getFile_url() != null && media.contains(m); - } - - public boolean isDownloading(Feed feed) { - return feed.getFile_url() != null && feeds.contains(feed); - } - - public boolean isDownloading(FeedImage image) { - return image.getFile_url() != null && images.contains(image); - } - - public boolean isDownloading(FeedMedia m) { - return m.getFile_url() != null && media.contains(m); - } - /* * ------------ Methods for communicating with the DownloadService * ------------- @@ -256,7 +184,7 @@ public class DownloadRequester { private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { - mService = ((DownloadService.LocalBinder)service).getService(); + mService = ((DownloadService.LocalBinder) service).getService(); Log.d(TAG, "Connection to service established"); mService.queryDownloads(); } @@ -272,7 +200,7 @@ public class DownloadRequester { context.bindService(new Intent(context, DownloadService.class), mConnection, Context.BIND_AUTO_CREATE); mIsBound = true; - + context.unbindService(mConnection); mIsBound = false; } |