summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/de/podfetcher/service/FeedSyncService.java35
-rw-r--r--src/de/podfetcher/storage/DownloadService.java8
2 files changed, 42 insertions, 1 deletions
diff --git a/src/de/podfetcher/service/FeedSyncService.java b/src/de/podfetcher/service/FeedSyncService.java
index 5fd748351..1c827f1f3 100644
--- a/src/de/podfetcher/service/FeedSyncService.java
+++ b/src/de/podfetcher/service/FeedSyncService.java
@@ -8,16 +8,21 @@ package de.podfetcher.service;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.lang.Runtime;
+import java.util.concurrent.TimeUnit;
import de.podfetcher.feed.*;
import de.podfetcher.storage.DownloadRequester;
+import de.podfetcher.storage.DownloadService;
+
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
+import android.content.BroadcastReceiver;
import android.os.IBinder;
import android.content.Context;
+
public class FeedSyncService extends Service {
private volatile ScheduledThreadPoolExecutor executor;
@@ -29,6 +34,7 @@ public class FeedSyncService extends Service {
executor = new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors() + 2);
manager = FeedManager.getInstance();
requester = DownloadRequester.getInstance();
+ registerReceiver(allFeedsDownloaded, new IntentFilter(DownloadService.ACTION_ALL_FEED_DOWNLOADS_COMPLETED));
}
@Override
@@ -37,6 +43,11 @@ public class FeedSyncService extends Service {
}
@Override
+ public void onDestroy() {
+ unregisterReceiver(allFeedsDownloaded);
+ }
+
+ @Override
public int onStartCommand(Intent intent, int flags, int startId) {
executor.submit(new FeedSyncThread(handleIntent(intent), this));
return START_STICKY;
@@ -49,6 +60,30 @@ public class FeedSyncService extends Service {
return feed;
}
+ /** Prepares itself for stopping */
+ private void initiateShutdown() {
+ // Wait until PoolExecutor is done
+ Thread waiter = new Thread() {
+ @Override
+ public void run() {
+ executor.shutdown();
+ try {
+ executor.awaitTermination(20, TimeUnit.SECONDS);
+ stopSelf();
+ }catch(InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ };
+ }
+
+ BroadcastReceiver allFeedsDownloaded = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ initiateShutdown();
+ }
+ };
+
/** Takes a single Feed, parses the corresponding file and refreshes information in the manager */
class FeedSyncThread implements Runnable {
diff --git a/src/de/podfetcher/storage/DownloadService.java b/src/de/podfetcher/storage/DownloadService.java
index 74a346623..d6bb19967 100644
--- a/src/de/podfetcher/storage/DownloadService.java
+++ b/src/de/podfetcher/storage/DownloadService.java
@@ -16,6 +16,8 @@ import android.content.BroadcastReceiver;
import android.content.Context;
public class DownloadService extends Service {
+
+ public static String ACTION_ALL_FEED_DOWNLOADS_COMPLETED = "action.de.podfetcher.storage.all_feed_downloads_completed";
private DownloadRequester requester;
private FeedManager manager;
@@ -53,6 +55,11 @@ public class DownloadService extends Service {
String action = item_intent.getAction();
if(action.equals(DownloadRequester.ACTION_FEED_DOWNLOAD_COMPLETED)) {
handleCompletedFeedDownload(context, intent);
+ // Notify FeedSyncService about the new Feed
+ sendBroadcast(item_intent);
+ if(requester.getNumberOfFeedDownloads() == 0) {
+ sendBroadcast(new Intent(ACTION_ALL_FEED_DOWNLOADS_COMPLETED));
+ }
} 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)) {
@@ -63,7 +70,6 @@ public class DownloadService extends Service {
if(requester.getNumberOfDownloads() == 0) {
stopSelf();
}
- //PodcastApp.getInstance().getApplicationContext().sendBroadcast(item_intent);
}
};