summaryrefslogtreecommitdiff
path: root/src/de/podfetcher/service/FeedSyncService.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/de/podfetcher/service/FeedSyncService.java')
-rw-r--r--src/de/podfetcher/service/FeedSyncService.java35
1 files changed, 35 insertions, 0 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 {