mirror of
https://github.com/AntennaPod/AntennaPod.git
synced 2025-12-01 12:31:45 +00:00
Added communication interface to DownloadService
This commit is contained in:
@ -20,6 +20,10 @@ import android.os.IBinder;
|
|||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.app.NotificationManager;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Message;
|
||||||
|
import android.os.Messenger;
|
||||||
|
|
||||||
public class DownloadService extends Service {
|
public class DownloadService extends Service {
|
||||||
private static final String TAG = "DownloadService";
|
private static final String TAG = "DownloadService";
|
||||||
@ -31,6 +35,13 @@ public class DownloadService extends Service {
|
|||||||
private DownloadRequester requester;
|
private DownloadRequester requester;
|
||||||
private FeedManager manager;
|
private FeedManager manager;
|
||||||
|
|
||||||
|
|
||||||
|
// Objects for communication
|
||||||
|
private final Messenger mMessenger = new Messenger(new IncomingHandler());
|
||||||
|
|
||||||
|
// Message codes
|
||||||
|
public static final int MSG_QUERY_DOWNLOADS = 1;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
Log.d(TAG, "Service started");
|
Log.d(TAG, "Service started");
|
||||||
@ -42,7 +53,7 @@ public class DownloadService extends Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public IBinder onBind(Intent intent) {
|
||||||
return null;
|
return mMessenger.getBinder();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -92,22 +103,22 @@ public class DownloadService extends Service {
|
|||||||
handleCompletedImageDownload(context, image);
|
handleCompletedImageDownload(context, image);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
queryDownloads();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Check if there's something else to download, otherwise stop
|
/** Check if there's something else to download, otherwise stop */
|
||||||
|
private void queryDownloads() {
|
||||||
if(requester.getNumberOfDownloads() == 0) {
|
if(requester.getNumberOfDownloads() == 0) {
|
||||||
unregisterReceiver(downloadReceiver);
|
unregisterReceiver(downloadReceiver);
|
||||||
initiateShutdown();
|
initiateShutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/** Is called whenever a Feed is downloaded */
|
/** Is called whenever a Feed is downloaded */
|
||||||
private void handleCompletedFeedDownload(Context context, Feed feed) {
|
private void handleCompletedFeedDownload(Context context, Feed feed) {
|
||||||
Log.d(TAG, "Handling completed Feed Download");
|
Log.d(TAG, "Handling completed Feed Download");
|
||||||
// Get Feed Information
|
|
||||||
//feed.setFile_url((new File(requester.getFeedfilePath(context), requester.getFeedfileName(feed.getId()))).toString());
|
|
||||||
|
|
||||||
syncExecutor.execute(new FeedSyncThread(feed, this, requester));
|
syncExecutor.execute(new FeedSyncThread(feed, this, requester));
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -116,10 +127,23 @@ public class DownloadService extends Service {
|
|||||||
private void handleCompletedImageDownload(Context context, FeedImage image) {
|
private void handleCompletedImageDownload(Context context, FeedImage image) {
|
||||||
Log.d(TAG, "Handling completed Image Download");
|
Log.d(TAG, "Handling completed Image Download");
|
||||||
requester.removeFeedImage(image);
|
requester.removeFeedImage(image);
|
||||||
//image.setFile_url(requester.getImagefilePath(context) + requester.getImagefileName(image.getId()));
|
|
||||||
manager.setFeedImage(this, image);
|
manager.setFeedImage(this, image);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class IncomingHandler extends Handler {
|
||||||
|
@Override
|
||||||
|
public void handleMessage(Message msg) {
|
||||||
|
Log.d(TAG, "Received new Message.");
|
||||||
|
switch(msg.what) {
|
||||||
|
case MSG_QUERY_DOWNLOADS:
|
||||||
|
queryDownloads();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
super.handleMessage(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Takes a single Feed, parses the corresponding file and refreshes information in the manager */
|
/** Takes a single Feed, parses the corresponding file and refreshes information in the manager */
|
||||||
class FeedSyncThread implements Runnable {
|
class FeedSyncThread implements Runnable {
|
||||||
private static final String TAG = "FeedSyncThread";
|
private static final String TAG = "FeedSyncThread";
|
||||||
|
|||||||
Reference in New Issue
Block a user