Cleaned up the Download requester

This commit is contained in:
daniel oeh 2012-06-16 13:21:41 +02:00
parent c41605ef3b
commit 26c8772e0a
4 changed files with 62 additions and 124 deletions

View File

@ -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()]));
}

View File

@ -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();
@ -39,7 +42,15 @@ public class DownloadlistAdapter extends ArrayAdapter<DownloadObserver.DownloadS
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() + "%");

View File

@ -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 {

View File

@ -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);
@ -92,17 +86,17 @@ public class DownloadRequester {
}
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);
}
}
FeedFile f = getFeedFile(id);
if (f != null) {
downloads.remove(f);
}
}
}
/** Get a Feed by its download id */
public Feed getFeed(long id) {
for (FeedFile f : feeds) {
/** Get a feedfile by its download id */
public FeedFile getFeedFile(long id) {
for (FeedFile f : downloads) {
if (f.getDownloadId() == id) {
return (Feed) f;
return f;
}
}
return null;
}
/** Get a FeedImage by its download id */
public FeedImage getFeedImage(long id) {
for (FeedFile f : images) {
if (f.getDownloadId() == id) {
return (FeedImage) f;
}
}
return null;
/** Remove an object from the downloads-list of the requester. */
public void removeDownload(FeedFile f) {
downloads.remove(f);
}
/** 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);
}
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
* -------------