Added communication interface to DownloadService

This commit is contained in:
Daniel Oeh 2012-04-15 15:30:00 +02:00
parent 91166cd442
commit 53e5e7c588
1 changed files with 48 additions and 24 deletions

View File

@ -20,6 +20,10 @@ import android.os.IBinder;
import android.content.BroadcastReceiver;
import android.content.Context;
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 {
private static final String TAG = "DownloadService";
@ -31,6 +35,13 @@ public class DownloadService extends Service {
private DownloadRequester requester;
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
public void onCreate() {
Log.d(TAG, "Service started");
@ -42,7 +53,7 @@ public class DownloadService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
return mMessenger.getBinder();
}
@Override
@ -92,22 +103,22 @@ public class DownloadService extends Service {
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) {
unregisterReceiver(downloadReceiver);
initiateShutdown();
}
}
};
/** Is called whenever a Feed is downloaded */
private void handleCompletedFeedDownload(Context context, Feed feed) {
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));
}
@ -116,10 +127,23 @@ public class DownloadService extends Service {
private void handleCompletedImageDownload(Context context, FeedImage image) {
Log.d(TAG, "Handling completed Image Download");
requester.removeFeedImage(image);
//image.setFile_url(requester.getImagefilePath(context) + requester.getImagefileName(image.getId()));
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 */
class FeedSyncThread implements Runnable {
private static final String TAG = "FeedSyncThread";