Implemented FeedSyncService Termination Process
This commit is contained in:
parent
53daf91337
commit
48144440cf
|
@ -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
|
||||
|
@ -36,6 +42,11 @@ public class FeedSyncService extends Service {
|
|||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
unregisterReceiver(allFeedsDownloaded);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
executor.submit(new FeedSyncThread(handleIntent(intent), this));
|
||||
|
@ -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 {
|
||||
|
||||
|
|
|
@ -17,6 +17,8 @@ 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);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue