Moved Download Handlers into separate Threads

This commit is contained in:
daniel oeh 2012-06-13 19:29:15 +02:00
parent 12e957b6df
commit cacfb4c33b
2 changed files with 107 additions and 51 deletions

View File

@ -3,7 +3,6 @@
* to complete, then stops * to complete, then stops
* */ * */
package de.podfetcher.service; package de.podfetcher.service;
import java.io.File; import java.io.File;
@ -23,7 +22,6 @@ 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.Handler;
import android.os.Message; import android.os.Message;
import android.os.Messenger; import android.os.Messenger;
@ -37,11 +35,10 @@ public class DownloadService extends Service {
private ExecutorService syncExecutor; private ExecutorService syncExecutor;
private DownloadRequester requester; private DownloadRequester requester;
private FeedManager manager; private FeedManager manager;
/** Needed to determine the duration of a media file */ /** Needed to determine the duration of a media file */
private MediaPlayer mediaplayer; private MediaPlayer mediaplayer;
// Objects for communication // Objects for communication
private final Messenger mMessenger = new Messenger(new IncomingHandler()); private final Messenger mMessenger = new Messenger(new IncomingHandler());
@ -86,11 +83,13 @@ public class DownloadService extends Service {
syncExecutor.shutdown(); syncExecutor.shutdown();
try { try {
Log.d(TAG, "Starting to wait for termination"); Log.d(TAG, "Starting to wait for termination");
boolean b = syncExecutor.awaitTermination(20L, TimeUnit.SECONDS); boolean b = syncExecutor.awaitTermination(20L,
Log.d(TAG, "Stopping waiting for termination; Result : "+ b); TimeUnit.SECONDS);
Log.d(TAG, "Stopping waiting for termination; Result : "
+ b);
stopSelf(); stopSelf();
}catch(InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -102,13 +101,14 @@ public class DownloadService extends Service {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Received 'Download Complete' - message."); Log.d(TAG, "Received 'Download Complete' - message.");
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0); long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Feed feed = requester.getFeed(downloadId); Feed feed = requester.getFeed(downloadId);
if(feed != null) { if (feed != null) {
handleCompletedFeedDownload(context, feed); handleCompletedFeedDownload(context, feed);
}else { } else {
FeedImage image = requester.getFeedImage(downloadId); FeedImage image = requester.getFeedImage(downloadId);
if(image != null) { if (image != null) {
handleCompletedImageDownload(context, image); handleCompletedImageDownload(context, image);
} else { } else {
FeedMedia media = requester.getFeedMedia(downloadId); FeedMedia media = requester.getFeedMedia(downloadId);
@ -123,70 +123,59 @@ public class DownloadService extends Service {
/** Check if there's something else to download, otherwise stop */ /** Check if there's something else to download, otherwise stop */
private void queryDownloads() { 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");
syncExecutor.execute(new FeedSyncThread(feed, this, requester)); syncExecutor.execute(new FeedSyncThread(feed, this));
} }
/** Is called whenever a Feed-Image is downloaded */ /** Is called whenever a Feed-Image is downloaded */
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); syncExecutor.execute(new ImageHandlerThread(image, this));
manager.setFeedImage(this, image);
} }
/** Is called whenever a FeedMedia is downloaded. */ /** Is called whenever a FeedMedia is downloaded. */
private void handleCompletedFeedMediaDownload(Context context, FeedMedia media) { private void handleCompletedFeedMediaDownload(Context context,
FeedMedia media) {
Log.d(TAG, "Handling completed FeedMedia Download"); Log.d(TAG, "Handling completed FeedMedia Download");
requester.removeFeedMedia(media); syncExecutor.execute(new MediaHandlerThread(media, this));
// Get duration
try {
mediaplayer.setDataSource(media.getFile_url());
mediaplayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
media.setDuration(mediaplayer.getDuration());
Log.d(TAG, "Duration of file is " + media.getDuration());
mediaplayer.reset();
manager.setFeedMedia(this, media);
} }
class IncomingHandler extends Handler { class IncomingHandler extends Handler {
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
Log.d(TAG, "Received new Message."); Log.d(TAG, "Received new Message.");
switch(msg.what) { switch (msg.what) {
case MSG_QUERY_DOWNLOADS_LEFT: case MSG_QUERY_DOWNLOADS_LEFT:
queryDownloads(); queryDownloads();
break; break;
default: default:
super.handleMessage(msg); 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";
private Feed feed; private Feed feed;
private DownloadService service; private DownloadService service;
private DownloadRequester requester;
public FeedSyncThread(Feed feed, DownloadService service, DownloadRequester requester) { public FeedSyncThread(Feed feed, DownloadService service) {
this.feed = feed; this.feed = feed;
this.service = service; this.service = service;
this.requester = requester;
} }
public void run() { public void run() {
@ -196,7 +185,7 @@ public class DownloadService extends Service {
feed = handler.parseFeed(feed); feed = handler.parseFeed(feed);
Log.d(TAG, feed.getTitle() + " parsed"); Log.d(TAG, feed.getTitle() + " parsed");
// Download Feed Image if provided // Download Feed Image if provided
if(feed.getImage() != null) { if (feed.getImage() != null) {
Log.d(TAG, "Feed has image; Downloading...."); Log.d(TAG, "Feed has image; Downloading....");
requester.downloadImage(service, feed.getImage()); requester.downloadImage(service, feed.getImage());
} }
@ -210,10 +199,57 @@ public class DownloadService extends Service {
/** Delete files that aren't needed anymore */ /** Delete files that aren't needed anymore */
private void cleanup() { private void cleanup() {
if(new File(feed.getFile_url()).delete()) if (new File(feed.getFile_url()).delete())
Log.d(TAG, "Successfully deleted cache file."); else Log.e(TAG, "Failed to delete cache file."); Log.d(TAG, "Successfully deleted cache file.");
else
Log.e(TAG, "Failed to delete cache file.");
feed.setFile_url(null); feed.setFile_url(null);
} }
} }
/** Handles a completed image download. */
class ImageHandlerThread implements Runnable {
private FeedImage image;
private DownloadService service;
public ImageHandlerThread(FeedImage image, DownloadService service) {
this.image = image;
this.service = service;
}
@Override
public void run() {
requester.removeFeedImage(image);
manager.setFeedImage(service, image);
}
}
/** Handles a completed media download. */
class MediaHandlerThread implements Runnable {
private FeedMedia media;
private DownloadService service;
public MediaHandlerThread(FeedMedia media, DownloadService service) {
super();
this.media = media;
this.service = service;
}
@Override
public void run() {
requester.removeFeedMedia(media);
// Get duration
try {
mediaplayer.setDataSource(media.getFile_url());
mediaplayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
media.setDuration(mediaplayer.getDuration());
Log.d(TAG, "Duration of file is " + media.getDuration());
mediaplayer.reset();
manager.setFeedMedia(service, media);
}
}
} }

View File

@ -1,6 +1,7 @@
package de.podfetcher.service; package de.podfetcher.service;
import java.io.File; import java.io.File;
import java.io.IOException;
import android.R; import android.R;
import android.app.Notification; import android.app.Notification;
@ -68,21 +69,40 @@ public class PlaybackService extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
if (player != null) {
player.stop();
}
long mediaId = intent.getLongExtra(EXTRA_MEDIA_ID, -1); long mediaId = intent.getLongExtra(EXTRA_MEDIA_ID, -1);
long feedId = intent.getLongExtra(EXTRA_FEED_ID, -1); long feedId = intent.getLongExtra(EXTRA_FEED_ID, -1);
if (mediaId == -1 || feedId == -1) { if (mediaId == -1 || feedId == -1) {
Log.e(TAG, "Media ID or Feed ID wasn't provided to the Service."); Log.e(TAG, "Media ID or Feed ID wasn't provided to the Service.");
} else { } else {
feed = manager.getFeed(feedId); Feed newFeed = manager.getFeed(feedId);
media = manager.getFeedMedia(mediaId, feed); FeedMedia newMedia = manager.getFeedMedia(mediaId, newFeed);
if (media != null && media != newMedia) {
pause();
player.reset();
try {
player.setDataSource(newMedia.getFile_url());
player.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
media = newMedia;
feed = newFeed;
} else if (media == null) {
media = newMedia;
feed = newFeed;
player = MediaPlayer.create(this, Uri.fromFile(new File(media.getFile_url())));
setStatus(PlayerStatus.PREPARING);
player.setOnPreparedListener(preparedListener);
Log.d(TAG, "Preparing to play file");
}
player = MediaPlayer.create(this, Uri.fromFile(new File(media.getFile_url())));
setStatus(PlayerStatus.PREPARING);
player.setOnPreparedListener(preparedListener);
Log.d(TAG, "Preparing to play file");
} }
setupNotification(); setupNotification();
return Service.START_STICKY; return Service.START_STICKY;