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