Created DownloadService
This commit is contained in:
parent
b0b069a20c
commit
f751b3803b
|
@ -22,6 +22,7 @@
|
|||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<service android:enabled="true" android:name=".DownloadService" />
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
|
|
@ -25,10 +25,10 @@ public class FeedHandler {
|
|||
public final static String ENC_TYPE = "type";
|
||||
|
||||
public Feed parseFeed(Feed feed) {
|
||||
SAXParserFactory factory = SAXParserFactory.newInstance();
|
||||
SAXParser saxParser = factory.newSAXParser();
|
||||
RSSHandler handler = new RSSHandler(feed);
|
||||
try {
|
||||
SAXParserFactory factory = SAXParserFactory.newInstance();
|
||||
SAXParser saxParser = factory.newSAXParser();
|
||||
saxParser.parse(new File(feed.file_url), handler);
|
||||
} catch (SAXException e) {
|
||||
e.printStackTrace();
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package de.podfetcher.storage;
|
||||
|
||||
import de.podfetcher.PodcastApp;
|
||||
import de.podfetcher.feed.*;
|
||||
import android.app.DownloadManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
|
@ -29,19 +30,19 @@ public class DownloadReceiver extends BroadcastReceiver {
|
|||
|
||||
/** Is called whenever a Feed is Downloaded */
|
||||
private void handleCompletedFeedDownload(Context context, Intent intent) {
|
||||
RSSHandler handler = new RSSHandler();
|
||||
FeedHandler handler = new FeedHandler();
|
||||
|
||||
requester.removeFeedByID(item_intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1));
|
||||
requester.removeFeedByID(intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1));
|
||||
// Get Feed Information
|
||||
Feed feed = manager.getFeed(intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1));
|
||||
feed.file_url = DownloadRequester.getFeedfilePath() + DownloadRequester.getFeedfileName(feed.id);
|
||||
feed.file_url = requester.getFeedfilePath(context) + requester.getFeedfileName(feed.id);
|
||||
feed = handler.parseFeed(feed);
|
||||
// Download Feed Image if provided
|
||||
if(feed.image != null) {
|
||||
requester.downloadImage(context, feed.image);
|
||||
}
|
||||
// Update Information in Database
|
||||
manager.setFeed(feed);
|
||||
manager.setFeed(context, feed);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ public class DownloadRequester {
|
|||
}
|
||||
public void downloadFeed(Context context, Feed feed) {
|
||||
download(context, feeds, feed.download_url,
|
||||
new File(getFeedfilePath(id), getFeedfileName(id)),
|
||||
new File(getFeedfilePath(context), getFeedfileName(feed.id)),
|
||||
true, ACTION_FEED_DOWNLOAD_COMPLETED, feed.id);
|
||||
}
|
||||
|
||||
|
@ -129,8 +129,17 @@ public class DownloadRequester {
|
|||
return null;
|
||||
}
|
||||
|
||||
public String getFeedfilePath() {
|
||||
return context.getExternalFilesDir(FEED_DOWNLOADPATH);
|
||||
/** Get the number of uncompleted Downloads */
|
||||
public int getNumberOfDownloads() {
|
||||
return feeds.size() + images.size() + media.size();
|
||||
}
|
||||
|
||||
public int getNumberOfFeedDownloads() {
|
||||
return feeds.size();
|
||||
}
|
||||
|
||||
public String getFeedfilePath(Context context) {
|
||||
return context.getExternalFilesDir(FEED_DOWNLOADPATH).toString();
|
||||
}
|
||||
|
||||
public String getFeedfileName(long id) {
|
||||
|
|
|
@ -0,0 +1,89 @@
|
|||
/**
|
||||
* Registers a DownloadReceiver and waits for all Downloads
|
||||
* to complete, then stops
|
||||
* */
|
||||
|
||||
|
||||
package de.podfetcher.storage;
|
||||
|
||||
import de.podfetcher.feed.*;
|
||||
import android.app.Service;
|
||||
import android.app.DownloadManager;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.IBinder;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
|
||||
public class DownloadService extends Service {
|
||||
|
||||
private DownloadRequester requester;
|
||||
private FeedManager manager;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
receiver = new DownloadReceiver();
|
||||
|
||||
registerReceiver(receiver, createIntentFilter());
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
unregisterReceiver(receiver);
|
||||
}
|
||||
|
||||
private IntentFilter createIntentFilter() {
|
||||
IntentFilter filter = new IntentFilter();
|
||||
|
||||
filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
|
||||
|
||||
return filter;
|
||||
}
|
||||
|
||||
private BroadcastReceiver receiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
|
||||
requester = DownloadRequester.getInstance();
|
||||
manager = FeedManager.getInstance();
|
||||
Intent item_intent = requester.getItemIntent(id);
|
||||
String action = item_intent.getAction();
|
||||
if(action.equals(DownloadRequester.ACTION_FEED_DOWNLOAD_COMPLETED)) {
|
||||
handleCompletedFeedDownload(context, intent);
|
||||
} else if(action.equals(DownloadRequester.ACTION_MEDIA_DOWNLOAD_COMPLETED)) {
|
||||
requester.removeMediaByID(item_intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1));
|
||||
} else if(action.equals(DownloadRequester.ACTION_IMAGE_DOWNLOAD_COMPLETED)) {
|
||||
requester.removeImageByID(item_intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1));
|
||||
}
|
||||
|
||||
// Check if there's something else to download, otherwise stop
|
||||
if(requester.getNumberOfDownloads() == 0) {
|
||||
stopSelf();
|
||||
}
|
||||
//PodcastApp.getInstance().getApplicationContext().sendBroadcast(item_intent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** Is called whenever a Feed is Downloaded */
|
||||
private void handleCompletedFeedDownload(Context context, Intent intent) {
|
||||
FeedHandler handler = new FeedHandler();
|
||||
|
||||
requester.removeFeedByID(intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1));
|
||||
// Get Feed Information
|
||||
Feed feed = manager.getFeed(intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1));
|
||||
feed.file_url = requester.getFeedfilePath(context) + requester.getFeedfileName(feed.id);
|
||||
feed = handler.parseFeed(feed);
|
||||
// Download Feed Image if provided
|
||||
if(feed.image != null) {
|
||||
requester.downloadImage(context, feed.image);
|
||||
}
|
||||
// Update Information in Database
|
||||
manager.setFeed(context, feed);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue