Rewrote DownloadObserver
This commit is contained in:
parent
d98fa57468
commit
1cb46ad037
|
@ -67,38 +67,22 @@ public class AddFeedActivity extends SherlockActivity {
|
|||
}
|
||||
|
||||
private void observeDownload(Feed feed) {
|
||||
final ProgressDialog dialog;
|
||||
final Callable client;
|
||||
|
||||
dialog = new ProgressDialog(this);
|
||||
|
||||
final DownloadObserver observer = new DownloadObserver(
|
||||
feed.getDownloadId(), this, 10000);
|
||||
|
||||
client = new Callable() {
|
||||
public Object call() {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
if(observer.isTimedOut()) {
|
||||
final ProgressDialog dialog = new ProgressDialog(this);
|
||||
final DownloadObserver observer = new DownloadObserver(this) {
|
||||
@Override
|
||||
protected void onPostExecute(Boolean result) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
|
||||
if(observer.getDone()) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}else {
|
||||
dialog.setMessage(AddFeedActivity.this.getString(observer.getResult()) + ": " + observer.getProgressPercent() + "%");
|
||||
@Override
|
||||
protected void onProgressUpdate(Integer... values) {
|
||||
Integer progr = values[0];
|
||||
dialog.setMessage(getContext().getString(getStatusMsg())
|
||||
+ " (" + progr.toString() + "%)");
|
||||
}
|
||||
}});
|
||||
|
||||
return null;
|
||||
}};
|
||||
observer.setClient(client);
|
||||
};
|
||||
dialog.show();
|
||||
observer.start();
|
||||
observer.execute(feed);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -97,13 +97,14 @@ public class ItemviewActivity extends SherlockActivity {
|
|||
}
|
||||
|
||||
private void getDownloadStatus() {
|
||||
/*
|
||||
if(item.getMedia().getFile_url() == null) {
|
||||
butPlay.setEnabled(false);
|
||||
butDownload.setEnabled(true);
|
||||
butRemove.setEnabled(false);
|
||||
} else {
|
||||
final DownloadObserver observer = new DownloadObserver(
|
||||
item.getMedia().getDownloadId(), this);
|
||||
item.getMedia().getDownloadId(), DownloadObserver.TYPE_MEDIA, this);
|
||||
|
||||
final Callable client = new Callable() {
|
||||
public Object call() {
|
||||
|
@ -129,6 +130,7 @@ public class ItemviewActivity extends SherlockActivity {
|
|||
observer.setClient(client);
|
||||
observer.start();
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -24,6 +24,7 @@ public class Feed extends FeedFile{
|
|||
}
|
||||
|
||||
public Feed(String url) {
|
||||
super();
|
||||
this.download_url = url;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,10 @@ public abstract class FeedFile extends FeedComponent {
|
|||
protected String download_url;
|
||||
protected long downloadId; // temporary id given by the Android DownloadManager
|
||||
|
||||
public FeedFile() {
|
||||
downloadId = -1;
|
||||
}
|
||||
|
||||
public String getFile_url() {
|
||||
return file_url;
|
||||
}
|
||||
|
@ -26,4 +30,12 @@ public abstract class FeedFile extends FeedComponent {
|
|||
public void setDownloadId(long downloadId) {
|
||||
this.downloadId = downloadId;
|
||||
}
|
||||
|
||||
public boolean isDownloaded() {
|
||||
return downloadId == -1 && file_url != null;
|
||||
}
|
||||
|
||||
public boolean isDownloading() {
|
||||
return downloadId != -1 && file_url != null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ public class FeedImage extends FeedFile {
|
|||
}
|
||||
|
||||
public FeedImage(long id, String title, String file_url, String download_url) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.file_url = file_url;
|
||||
|
|
|
@ -8,6 +8,7 @@ public class FeedMedia extends FeedFile{
|
|||
private FeedItem item;
|
||||
|
||||
public FeedMedia(FeedItem i, String download_url, long size, String mime_type) {
|
||||
super();
|
||||
this.item = i;
|
||||
this.download_url = download_url;
|
||||
this.size = size;
|
||||
|
|
|
@ -1,111 +1,96 @@
|
|||
package de.podfetcher.service;
|
||||
|
||||
import de.podfetcher.storage.DownloadRequester;
|
||||
import de.podfetcher.feed.*;
|
||||
import de.podfetcher.R;
|
||||
import android.content.Context;
|
||||
import android.app.DownloadManager;
|
||||
import android.util.Log;
|
||||
import android.database.Cursor;
|
||||
import java.util.concurrent.Callable;
|
||||
import android.os.AsyncTask;
|
||||
|
||||
/** Observes the status of a specific Download */
|
||||
public class DownloadObserver extends Thread {
|
||||
public class DownloadObserver extends AsyncTask<FeedFile, Integer, Boolean> {
|
||||
private static final String TAG = "DownloadObserver";
|
||||
/* Download ID*/
|
||||
long id;
|
||||
Context context;
|
||||
Callable client;
|
||||
long waiting_intervall;
|
||||
private volatile int result;
|
||||
private volatile boolean done;
|
||||
|
||||
/** Types of downloads to observe. */
|
||||
public static final int TYPE_FEED = 0;
|
||||
public static final int TYPE_IMAGE = 1;
|
||||
public static final int TYPE_MEDIA = 2;
|
||||
|
||||
/** Error codes */
|
||||
public static final int ALREADY_DOWNLOADED = 1;
|
||||
public static final int NO_DOWNLOAD_FOUND = 2;
|
||||
|
||||
private final long DEFAULT_WAITING_INTERVALL = 1000L;
|
||||
|
||||
private int progressPercent;
|
||||
private Cursor cursor;
|
||||
private final long DEFAULT_WAITING_INTERVALL = 500L;
|
||||
private int statusMsg;
|
||||
|
||||
private int reason;
|
||||
|
||||
private DownloadRequester requester;
|
||||
private long time_passed;
|
||||
private long timeout;
|
||||
private boolean timedOut = false;
|
||||
private FeedFile feedfile;
|
||||
private Context context;
|
||||
|
||||
public DownloadObserver(long id, Context c) {
|
||||
this.id = id;
|
||||
this.context = c;
|
||||
this.client = client;
|
||||
this.waiting_intervall = DEFAULT_WAITING_INTERVALL;
|
||||
done = false;
|
||||
requester = DownloadRequester.getInstance();
|
||||
public DownloadObserver(Context context) {
|
||||
super();
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public DownloadObserver(long id, Context c, long timeout) {
|
||||
this(id, c);
|
||||
this.timeout = timeout;
|
||||
|
||||
protected Boolean doInBackground(FeedFile... files) {
|
||||
Log.d(TAG, "Background Task started.");
|
||||
|
||||
feedfile = files[0];
|
||||
if (feedfile.getFile_url() == null) {
|
||||
reason = NO_DOWNLOAD_FOUND;
|
||||
return Boolean.valueOf(false);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
Log.d(TAG, "Thread started.");
|
||||
while(!isInterrupted() && !timedOut) {
|
||||
cursor = getDownloadCursor();
|
||||
if (feedfile.isDownloaded()) {
|
||||
reason = ALREADY_DOWNLOADED;
|
||||
return Boolean.valueOf(false);
|
||||
}
|
||||
|
||||
while(true) {
|
||||
Cursor cursor = getDownloadCursor();
|
||||
int status = getDownloadStatus(cursor, DownloadManager.COLUMN_STATUS);
|
||||
int progressPercent = getDownloadProgress(cursor);
|
||||
switch(status) {
|
||||
case DownloadManager.STATUS_SUCCESSFUL:
|
||||
Log.d(TAG, "Download was successful.");
|
||||
done = true;
|
||||
result = R.string.download_successful;
|
||||
break;
|
||||
statusMsg = R.string.download_successful;
|
||||
return Boolean.valueOf(true);
|
||||
case DownloadManager.STATUS_RUNNING:
|
||||
Log.d(TAG, "Download is running.");
|
||||
result = R.string.download_running;
|
||||
statusMsg = R.string.download_running;
|
||||
break;
|
||||
case DownloadManager.STATUS_FAILED:
|
||||
Log.d(TAG, "Download failed.");
|
||||
result = R.string.download_failed;
|
||||
done = true;
|
||||
statusMsg = R.string.download_failed;
|
||||
requester.notifyDownloadService(context);
|
||||
break;
|
||||
return Boolean.valueOf(false);
|
||||
case DownloadManager.STATUS_PENDING:
|
||||
Log.d(TAG, "Download pending.");
|
||||
result = R.string.download_pending;
|
||||
statusMsg = R.string.download_pending;
|
||||
break;
|
||||
|
||||
}
|
||||
try {
|
||||
client.call();
|
||||
}catch (Exception e) {
|
||||
Log.e(TAG, "Error happened when calling client: " + e.getMessage());
|
||||
}
|
||||
|
||||
if(done) {
|
||||
break;
|
||||
} else {
|
||||
publishProgress(progressPercent);
|
||||
|
||||
try {
|
||||
sleep(waiting_intervall);
|
||||
if (timeout > 0) {
|
||||
time_passed += waiting_intervall;
|
||||
if(time_passed >= timeout) {
|
||||
Log.e(TAG, "Download timed out.");
|
||||
timedOut = true;
|
||||
try {
|
||||
client.call();
|
||||
}catch (Exception e) {
|
||||
Log.e(TAG, "Error happened when calling client: " + e.getMessage());
|
||||
}
|
||||
requester.cancelDownload(context, id);
|
||||
}
|
||||
}
|
||||
Thread.sleep(DEFAULT_WAITING_INTERVALL);
|
||||
} catch (InterruptedException e) {
|
||||
Log.w(TAG, "Thread was interrupted while waiting.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Log.d(TAG, "Thread stopped.");
|
||||
}
|
||||
|
||||
public void setClient(Callable callable) {
|
||||
this.client = callable;
|
||||
}
|
||||
|
||||
public Cursor getDownloadCursor() {
|
||||
DownloadManager.Query query = buildQuery(id);
|
||||
DownloadManager.Query query = buildQuery(feedfile.getDownloadId());
|
||||
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
|
||||
Cursor result = manager.query(query);
|
||||
|
@ -138,19 +123,15 @@ public class DownloadObserver extends Thread {
|
|||
return query;
|
||||
}
|
||||
|
||||
public int getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean getDone() {
|
||||
return done;
|
||||
}
|
||||
|
||||
public int getProgressPercent() {
|
||||
return progressPercent;
|
||||
}
|
||||
|
||||
public boolean isTimedOut() {
|
||||
return timedOut;
|
||||
public int getStatusMsg() {
|
||||
return statusMsg;
|
||||
}
|
||||
|
||||
public Context getContext() {
|
||||
return context;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -193,6 +193,29 @@ public class DownloadRequester {
|
|||
return URLUtil.guessFileName(media.getDownload_url(), null, media.getMime_type());
|
||||
}
|
||||
|
||||
public boolean isDownloaded(Feed feed) {
|
||||
return feed.getFile_url() != null && !feeds.contains(feed);
|
||||
}
|
||||
|
||||
public boolean isDownloaded(FeedImage image) {
|
||||
return image.getFile_url() != null && !images.contains(image);
|
||||
}
|
||||
|
||||
public boolean isDownloaded(FeedMedia m) {
|
||||
return m.getFile_url() != null && media.contains(m);
|
||||
}
|
||||
|
||||
public boolean isDownloading(Feed feed) {
|
||||
return feed.getFile_url() != null && feeds.contains(feed);
|
||||
}
|
||||
|
||||
public boolean isDownloading(FeedImage image) {
|
||||
return image.getFile_url() != null && images.contains(image);
|
||||
}
|
||||
|
||||
public boolean isDownloading(FeedMedia m) {
|
||||
return m.getFile_url() != null && media.contains(m);
|
||||
}
|
||||
|
||||
/* ------------ Methods for communicating with the DownloadService ------------- */
|
||||
private Messenger mService = null;
|
||||
|
|
Loading…
Reference in New Issue