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); super.onCreate(savedInstanceState);
requester = DownloadRequester.getInstance(); requester = DownloadRequester.getInstance();
observer.execute(requester.getMediaDownloads().toArray( observer.execute(requester.getDownloads().toArray(
new FeedFile[requester.getMediaDownloads().size()])); new FeedFile[requester.getDownloads().size()]));
} }

View File

@ -9,6 +9,9 @@ import android.content.Context;
import de.podfetcher.R; import de.podfetcher.R;
import de.podfetcher.util.Converter; 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.feed.FeedMedia;
import de.podfetcher.service.DownloadObserver; 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) { public View getView(int position, View convertView, ViewGroup parent) {
Holder holder; Holder holder;
DownloadObserver.DownloadStatus status = getItem(position); DownloadObserver.DownloadStatus status = getItem(position);
FeedFile feedFile = status.getFeedFile();
// Inflate layout // Inflate layout
if (convertView == null) { if (convertView == null) {
holder = new Holder(); holder = new Holder();
@ -38,8 +41,16 @@ public class DownloadlistAdapter extends ArrayAdapter<DownloadObserver.DownloadS
} else { } else {
holder = (Holder) convertView.getTag(); 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()) + " / " holder.downloaded.setText(Converter.byteToString(status.getSoFar()) + " / "
+ Converter.byteToString(status.getSize())); + Converter.byteToString(status.getSize()));
holder.percent.setText(status.getProgressPercent() + "%"); holder.percent.setText(status.getProgressPercent() + "%");

View File

@ -154,18 +154,16 @@ public class DownloadService extends Service {
} }
if (status == DownloadManager.STATUS_SUCCESSFUL) { if (status == DownloadManager.STATUS_SUCCESSFUL) {
Feed feed = requester.getFeed(downloadId); FeedFile download = requester.getFeedFile(downloadId);
if (feed != null) { if (download != null) {
handleCompletedFeedDownload(context, feed); if (download.getClass() == Feed.class) {
} else { handleCompletedFeedDownload(context, (Feed) download);
FeedImage image = requester.getFeedImage(downloadId); } else if (download.getClass() == FeedImage.class) {
if (image != null) { handleCompletedImageDownload(context,
handleCompletedImageDownload(context, image); (FeedImage) download);
} else { } else if (download.getClass() == FeedMedia.class) {
FeedMedia media = requester.getFeedMedia(downloadId); handleCompletedFeedMediaDownload(context,
if (media != null) { (FeedMedia) download);
handleCompletedFeedMediaDownload(context, media);
}
} }
} }
queryDownloads(); queryDownloads();
@ -189,7 +187,8 @@ public class DownloadService extends Service {
initiateShutdown(); initiateShutdown();
} else { } else {
// update notification // update notification
notificationBuilder.setContentText(numOfDownloads + " Downloads left"); notificationBuilder.setContentText(numOfDownloads
+ " Downloads left");
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NOTIFICATION_ID, notificationBuilder.getNotification()); nm.notify(NOTIFICATION_ID, notificationBuilder.getNotification());
} }
@ -241,7 +240,7 @@ public class DownloadService extends Service {
Log.d(TAG, "Feed has image; Downloading...."); Log.d(TAG, "Feed has image; Downloading....");
requester.downloadImage(service, feed.getImage()); requester.downloadImage(service, feed.getImage());
} }
requester.removeFeed(feed); requester.removeDownload(feed);
cleanup(); cleanup();
@ -274,7 +273,7 @@ public class DownloadService extends Service {
@Override @Override
public void run() { public void run() {
image.setDownloaded(true); image.setDownloaded(true);
requester.removeFeedImage(image); requester.removeDownload(image);
manager.setFeedImage(service, image); manager.setFeedImage(service, image);
queryDownloads(); queryDownloads();
} }
@ -293,7 +292,7 @@ public class DownloadService extends Service {
@Override @Override
public void run() { public void run() {
requester.removeFeedMedia(media); requester.removeDownload(media);
media.setDownloaded(true); media.setDownloaded(true);
// Get duration // Get duration
try { try {

View File

@ -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_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_MEDIA_DOWNLOAD_COMPLETED = "action.de.podfetcher.storage.media_download_completed";
public static String ACTION_IMAGE_DOWNLOAD_COMPLETED = "action.de.podfetcher.storage.image_download_completed"; public static String ACTION_IMAGE_DOWNLOAD_COMPLETED = "action.de.podfetcher.storage.image_download_completed";
private static boolean STORE_ON_SD = true; private static boolean STORE_ON_SD = true;
public static String IMAGE_DOWNLOADPATH = "images/"; public static String IMAGE_DOWNLOADPATH = "images/";
public static String FEED_DOWNLOADPATH = "cache/"; public static String FEED_DOWNLOADPATH = "cache/";
@ -42,15 +42,10 @@ public class DownloadRequester {
private static DownloadRequester downloader; private static DownloadRequester downloader;
private DownloadManager manager; private DownloadManager manager;
public ArrayList<FeedFile> feeds; public ArrayList<FeedFile> downloads;
public ArrayList<FeedFile> images;
public ArrayList<FeedFile> media;
private DownloadRequester() { private DownloadRequester() {
feeds = new ArrayList<FeedFile>(); downloads = new ArrayList<FeedFile>();
images = new ArrayList<FeedFile>();
media = new ArrayList<FeedFile>();
} }
public static DownloadRequester getInstance() { public static DownloadRequester getInstance() {
@ -60,17 +55,16 @@ public class DownloadRequester {
return downloader; return downloader;
} }
private long download(Context context, ArrayList<FeedFile> type, private long download(Context context, FeedFile item, File dest) {
FeedFile item, File dest) {
if (dest.exists()) { if (dest.exists()) {
Log.d(TAG, "File already exists. Deleting !"); Log.d(TAG, "File already exists. Deleting !");
dest.delete(); dest.delete();
} }
Log.d(TAG, "Requesting download of url " + item.getDownload_url()); Log.d(TAG, "Requesting download of url " + item.getDownload_url());
type.add(item); downloads.add(item);
DownloadManager.Request request = new DownloadManager.Request( DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(item.getDownload_url())) Uri.parse(item.getDownload_url())).setDestinationUri(Uri
.setDestinationUri(Uri.fromFile(dest)); .fromFile(dest));
Log.d(TAG, "Version is " + currentApi); Log.d(TAG, "Version is " + currentApi);
if (currentApi >= 11) { if (currentApi >= 11) {
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
@ -78,7 +72,7 @@ public class DownloadRequester {
request.setVisibleInDownloadsUi(false); request.setVisibleInDownloadsUi(false);
request.setShowRunningNotification(false); request.setShowRunningNotification(false);
} }
// TODO Set Allowed Network Types // TODO Set Allowed Network Types
DownloadManager manager = (DownloadManager) context DownloadManager manager = (DownloadManager) context
.getSystemService(Context.DOWNLOAD_SERVICE); .getSystemService(Context.DOWNLOAD_SERVICE);
@ -86,23 +80,23 @@ public class DownloadRequester {
long downloadId = manager.enqueue(request); long downloadId = manager.enqueue(request);
item.setDownloadId(downloadId); item.setDownloadId(downloadId);
item.setFile_url(dest.toString()); item.setFile_url(dest.toString());
notifyDownloadService(context); notifyDownloadService(context);
return downloadId; return downloadId;
} }
public long downloadFeed(Context context, Feed feed) { public long downloadFeed(Context context, Feed feed) {
return download(context, feeds, feed, new File( return download(context, feed, new File(getFeedfilePath(context),
getFeedfilePath(context), getFeedfileName(feed))); getFeedfileName(feed)));
} }
public long downloadImage(Context context, FeedImage image) { public long downloadImage(Context context, FeedImage image) {
return download(context, images, image, new File( return download(context, image, new File(getImagefilePath(context),
getImagefilePath(context), getImagefileName(image))); getImagefileName(image)));
} }
public long downloadMedia(Context context, FeedMedia feedmedia) { public long downloadMedia(Context context, FeedMedia feedmedia) {
return download(context, media, feedmedia, return download(context, feedmedia,
new File(getMediafilePath(context, feedmedia), new File(getMediafilePath(context, feedmedia),
getMediafilename(feedmedia))); getMediafilename(feedmedia)));
} }
@ -117,81 +111,39 @@ public class DownloadRequester {
* */ * */
public void cancelDownload(final Context context, final long id) { public void cancelDownload(final Context context, final long id) {
Log.d(TAG, "Cancelling download with id " + id); Log.d(TAG, "Cancelling download with id " + id);
DownloadManager manager = (DownloadManager) context DownloadManager dm = (DownloadManager) context
.getSystemService(Context.DOWNLOAD_SERVICE); .getSystemService(Context.DOWNLOAD_SERVICE);
int removed = manager.remove(id); int removed = dm.remove(id);
if (removed > 0) { if (removed > 0) {
// Delete downloads in lists FeedFile f = getFeedFile(id);
Feed feed = getFeed(id); if (f != null) {
if (feed != null) { downloads.remove(f);
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 */ /** Get a feedfile by its download id */
public Feed getFeed(long id) { public FeedFile getFeedFile(long id) {
for (FeedFile f : feeds) { for (FeedFile f : downloads) {
if (f.getDownloadId() == id) { if (f.getDownloadId() == id) {
return (Feed) f; return f;
} }
} }
return null; return null;
} }
/** Get a FeedImage by its download id */ /** Remove an object from the downloads-list of the requester. */
public FeedImage getFeedImage(long id) { public void removeDownload(FeedFile f) {
for (FeedFile f : images) { downloads.remove(f);
if (f.getDownloadId() == id) {
return (FeedImage) f;
}
}
return null;
} }
/** Get media by its download id */ public ArrayList<FeedFile> getDownloads() {
public FeedMedia getFeedMedia(long id) { return downloads;
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;
} }
/** Get the number of uncompleted Downloads */ /** Get the number of uncompleted Downloads */
public int getNumberOfDownloads() { public int getNumberOfDownloads() {
return feeds.size() + images.size() + media.size(); return downloads.size();
}
public int getNumberOfFeedDownloads() {
return feeds.size();
} }
public String getFeedfilePath(Context context) { public String getFeedfilePath(Context context) {
@ -223,30 +175,6 @@ public class DownloadRequester {
media.getMime_type()); 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 * ------------ Methods for communicating with the DownloadService
* ------------- * -------------
@ -256,7 +184,7 @@ public class DownloadRequester {
private ServiceConnection mConnection = new ServiceConnection() { private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) { public void onServiceConnected(ComponentName className, IBinder service) {
mService = ((DownloadService.LocalBinder)service).getService(); mService = ((DownloadService.LocalBinder) service).getService();
Log.d(TAG, "Connection to service established"); Log.d(TAG, "Connection to service established");
mService.queryDownloads(); mService.queryDownloads();
} }
@ -272,7 +200,7 @@ public class DownloadRequester {
context.bindService(new Intent(context, DownloadService.class), context.bindService(new Intent(context, DownloadService.class),
mConnection, Context.BIND_AUTO_CREATE); mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true; mIsBound = true;
context.unbindService(mConnection); context.unbindService(mConnection);
mIsBound = false; mIsBound = false;
} }