Improvements to download requester
This commit is contained in:
parent
95a7a11fa3
commit
d7e3bfc930
|
@ -14,9 +14,11 @@ import java.util.concurrent.TimeUnit;
|
|||
import de.podfetcher.activity.DownloadActivity;
|
||||
import de.podfetcher.activity.MediaplayerActivity;
|
||||
import de.podfetcher.feed.*;
|
||||
import de.podfetcher.service.PlaybackService.LocalBinder;
|
||||
import de.podfetcher.storage.DownloadRequester;
|
||||
import de.podfetcher.syndication.handler.FeedHandler;
|
||||
import android.R;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.app.DownloadManager;
|
||||
|
@ -31,6 +33,7 @@ import android.graphics.Bitmap;
|
|||
import android.graphics.BitmapFactory;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.util.Log;
|
||||
import android.os.Binder;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.os.Messenger;
|
||||
|
@ -49,14 +52,16 @@ public class DownloadService extends Service {
|
|||
/** Needed to determine the duration of a media file */
|
||||
private MediaPlayer mediaplayer;
|
||||
private DownloadManager downloadManager;
|
||||
|
||||
|
||||
private volatile boolean shutdownInitiated = false;
|
||||
|
||||
// Objects for communication
|
||||
private final Messenger mMessenger = new Messenger(new IncomingHandler());
|
||||
private final IBinder mBinder = new LocalBinder();
|
||||
|
||||
// Message codes
|
||||
public static final int MSG_QUERY_DOWNLOADS_LEFT = 1;
|
||||
public class LocalBinder extends Binder {
|
||||
public DownloadService getService() {
|
||||
return DownloadService.this;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
|
@ -72,7 +77,7 @@ public class DownloadService extends Service {
|
|||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return mMessenger.getBinder();
|
||||
return mBinder;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -165,22 +170,28 @@ public class DownloadService extends Service {
|
|||
}
|
||||
queryDownloads();
|
||||
} else if (status == DownloadManager.STATUS_FAILED) {
|
||||
int reason = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON));
|
||||
int reason = c.getInt(c
|
||||
.getColumnIndex(DownloadManager.COLUMN_REASON));
|
||||
Log.d(TAG, "reason code is " + reason);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
c.close();
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
/** Check if there's something else to download, otherwise stop */
|
||||
private synchronized void queryDownloads() {
|
||||
if (!shutdownInitiated && requester.getNumberOfDownloads() == 0) {
|
||||
public synchronized void queryDownloads() {
|
||||
int numOfDownloads = requester.getNumberOfDownloads();
|
||||
if (!shutdownInitiated && numOfDownloads == 0) {
|
||||
shutdownInitiated = true;
|
||||
initiateShutdown();
|
||||
} else {
|
||||
// update notification
|
||||
notificationBuilder.setContentText(numOfDownloads + " Downloads left");
|
||||
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
nm.notify(NOTIFICATION_ID, notificationBuilder.getNotification());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -204,20 +215,6 @@ public class DownloadService extends Service {
|
|||
syncExecutor.execute(new MediaHandlerThread(media, this));
|
||||
}
|
||||
|
||||
class IncomingHandler extends Handler {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
Log.d(TAG, "Received new Message.");
|
||||
switch (msg.what) {
|
||||
case MSG_QUERY_DOWNLOADS_LEFT:
|
||||
queryDownloads();
|
||||
break;
|
||||
default:
|
||||
super.handleMessage(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a single Feed, parses the corresponding file and refreshes
|
||||
* information in the manager
|
||||
|
|
|
@ -62,6 +62,10 @@ public class DownloadRequester {
|
|||
|
||||
private long download(Context context, ArrayList<FeedFile> type,
|
||||
FeedFile item, File dest) {
|
||||
if (dest.exists()) {
|
||||
Log.d(TAG, "File already exists. Deleting !");
|
||||
dest.delete();
|
||||
}
|
||||
Log.d(TAG, "Requesting download of url " + item.getDownload_url());
|
||||
type.add(item);
|
||||
DownloadManager.Request request = new DownloadManager.Request(
|
||||
|
@ -82,6 +86,8 @@ public class DownloadRequester {
|
|||
long downloadId = manager.enqueue(request);
|
||||
item.setDownloadId(downloadId);
|
||||
item.setFile_url(dest.toString());
|
||||
|
||||
notifyDownloadService(context);
|
||||
return downloadId;
|
||||
}
|
||||
|
||||
|
@ -245,22 +251,14 @@ public class DownloadRequester {
|
|||
* ------------ Methods for communicating with the DownloadService
|
||||
* -------------
|
||||
*/
|
||||
private Messenger mService = null;
|
||||
private DownloadService mService = null;
|
||||
boolean mIsBound;
|
||||
|
||||
private ServiceConnection mConnection = new ServiceConnection() {
|
||||
public void onServiceConnected(ComponentName className, IBinder service) {
|
||||
mService = new Messenger(service);
|
||||
|
||||
try {
|
||||
Message msg = Message.obtain(null,
|
||||
DownloadService.MSG_QUERY_DOWNLOADS_LEFT);
|
||||
Log.d(TAG, "Sending message to DownloadService.");
|
||||
mService.send(msg);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG,
|
||||
"An Exception happened while communication with the DownloadService");
|
||||
}
|
||||
mService = ((DownloadService.LocalBinder)service).getService();
|
||||
Log.d(TAG, "Connection to service established");
|
||||
mService.queryDownloads();
|
||||
}
|
||||
|
||||
public void onServiceDisconnected(ComponentName className) {
|
||||
|
@ -274,6 +272,7 @@ public class DownloadRequester {
|
|||
context.bindService(new Intent(context, DownloadService.class),
|
||||
mConnection, Context.BIND_AUTO_CREATE);
|
||||
mIsBound = true;
|
||||
|
||||
context.unbindService(mConnection);
|
||||
mIsBound = false;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue