Fixed DownloadObserver
This commit is contained in:
parent
4b5160c30c
commit
593af6970a
|
@ -10,6 +10,7 @@ import de.podfetcher.R;
|
||||||
import de.podfetcher.feed.Feed;
|
import de.podfetcher.feed.Feed;
|
||||||
import de.podfetcher.storage.DownloadRequester;
|
import de.podfetcher.storage.DownloadRequester;
|
||||||
import de.podfetcher.util.URLChecker;
|
import de.podfetcher.util.URLChecker;
|
||||||
|
import de.podfetcher.service.DownloadObserver;
|
||||||
import com.actionbarsherlock.app.SherlockActivity;
|
import com.actionbarsherlock.app.SherlockActivity;
|
||||||
import com.actionbarsherlock.view.Menu;
|
import com.actionbarsherlock.view.Menu;
|
||||||
import com.actionbarsherlock.view.MenuInflater;
|
import com.actionbarsherlock.view.MenuInflater;
|
||||||
|
@ -71,20 +72,29 @@ public class AddFeedActivity extends SherlockActivity {
|
||||||
|
|
||||||
dialog = new ProgressDialog(this);
|
dialog = new ProgressDialog(this);
|
||||||
|
|
||||||
final DownloadRequester.DownloadObserver observer = new DownloadRequester.DownloadObserver(
|
final DownloadObserver observer = new DownloadObserver(
|
||||||
feed.getDownloadId(), this);
|
feed.getDownloadId(), this, 10000);
|
||||||
|
|
||||||
client = new Callable() {
|
client = new Callable() {
|
||||||
public Object call() {
|
public Object call() {
|
||||||
if(observer.getDone()) {
|
runOnUiThread(new Runnable() {
|
||||||
dialog.dismiss();
|
public void run() {
|
||||||
finish();
|
if(observer.isTimedOut()) {
|
||||||
}else {
|
dialog.dismiss();
|
||||||
Log.d(TAG, "Changing message of dialog.");
|
finish();
|
||||||
dialog.setMessage(AddFeedActivity.this.getString(observer.getResult()));
|
}
|
||||||
}
|
|
||||||
|
if(observer.getDone()) {
|
||||||
|
dialog.dismiss();
|
||||||
|
finish();
|
||||||
|
}else {
|
||||||
|
Log.d(TAG, "Changing message of dialog.");
|
||||||
|
dialog.setMessage(AddFeedActivity.this.getString(observer.getResult()));
|
||||||
|
}
|
||||||
|
}});
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}};
|
||||||
};
|
|
||||||
observer.setClient(client);
|
observer.setClient(client);
|
||||||
dialog.show();
|
dialog.show();
|
||||||
observer.start();
|
observer.start();
|
||||||
|
|
|
@ -0,0 +1,140 @@
|
||||||
|
package de.podfetcher.service;
|
||||||
|
|
||||||
|
import de.podfetcher.storage.DownloadRequester;
|
||||||
|
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;
|
||||||
|
|
||||||
|
/** Observes the status of a specific Download */
|
||||||
|
public class DownloadObserver extends Thread {
|
||||||
|
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;
|
||||||
|
private Cursor cursor;
|
||||||
|
private final long DEFAULT_WAITING_INTERVALL = 500L;
|
||||||
|
private DownloadRequester requester;
|
||||||
|
private long time_passed;
|
||||||
|
private long timeout;
|
||||||
|
private boolean timedOut = false;
|
||||||
|
|
||||||
|
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(long id, Context c, long timeout) {
|
||||||
|
this(id, c);
|
||||||
|
this.timeout = timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
Log.d(TAG, "Thread started.");
|
||||||
|
while(!isInterrupted() && !timedOut) {
|
||||||
|
cursor = getDownloadCursor();
|
||||||
|
int status = getDownloadStatus(cursor, DownloadManager.COLUMN_STATUS);
|
||||||
|
switch(status) {
|
||||||
|
case DownloadManager.STATUS_SUCCESSFUL:
|
||||||
|
Log.d(TAG, "Download was successful.");
|
||||||
|
done = true;
|
||||||
|
result = R.string.download_successful;
|
||||||
|
break;
|
||||||
|
case DownloadManager.STATUS_RUNNING:
|
||||||
|
Log.d(TAG, "Download is running.");
|
||||||
|
result = R.string.download_running;
|
||||||
|
break;
|
||||||
|
case DownloadManager.STATUS_FAILED:
|
||||||
|
Log.d(TAG, "Download failed.");
|
||||||
|
result = R.string.download_failed;
|
||||||
|
done = true;
|
||||||
|
requester.notifyDownloadService(context);
|
||||||
|
break;
|
||||||
|
case DownloadManager.STATUS_PENDING:
|
||||||
|
Log.d(TAG, "Download pending.");
|
||||||
|
result = 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 {
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
cancelDownload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}catch (InterruptedException e) {
|
||||||
|
Log.w(TAG, "Thread was interrupted while waiting.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Log.d(TAG, "Thread stopped.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setClient(Callable callable) {
|
||||||
|
this.client = callable;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void cancelDownload() {}
|
||||||
|
|
||||||
|
public Cursor getDownloadCursor() {
|
||||||
|
DownloadManager.Query query = buildQuery(id);
|
||||||
|
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||||
|
|
||||||
|
Cursor result = manager.query(query);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
public int getDownloadStatus(Cursor c, String column) {
|
||||||
|
if(c.moveToFirst()) {
|
||||||
|
int status = c.getInt(c.getColumnIndex(column));
|
||||||
|
return status;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private DownloadManager.Query buildQuery(long id) {
|
||||||
|
DownloadManager.Query query = new DownloadManager.Query();
|
||||||
|
query.setFilterById(id);
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getResult() {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getDone() {
|
||||||
|
return done;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isTimedOut() {
|
||||||
|
return timedOut;
|
||||||
|
}
|
||||||
|
}
|
|
@ -93,6 +93,7 @@ public class DownloadService extends Service {
|
||||||
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
|
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
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) {
|
||||||
|
|
|
@ -160,111 +160,6 @@ public class DownloadRequester {
|
||||||
return "image-" + NumberGenerator.generateLong(image.getDownload_url());
|
return "image-" + NumberGenerator.generateLong(image.getDownload_url());
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------ Methods for communicating with the DownloadManager ------------- */
|
|
||||||
|
|
||||||
|
|
||||||
/** observes the status of a specific Download */
|
|
||||||
public static class DownloadObserver extends Thread {
|
|
||||||
private static final String TAG = "DownloadObserver";
|
|
||||||
long id;
|
|
||||||
Context context;
|
|
||||||
Callable client;
|
|
||||||
long waiting_intervall;
|
|
||||||
private volatile int result;
|
|
||||||
private volatile boolean done;
|
|
||||||
private Cursor cursor;
|
|
||||||
private final long DEFAULT_WAITING_INTERVALL = 500L;
|
|
||||||
private DownloadRequester requester;
|
|
||||||
|
|
||||||
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 void run() {
|
|
||||||
Log.d(TAG, "Thread started.");
|
|
||||||
while(!isInterrupted()) {
|
|
||||||
cursor = getDownloadCursor();
|
|
||||||
int status = getDownloadStatus(cursor, DownloadManager.COLUMN_STATUS);
|
|
||||||
switch(status) {
|
|
||||||
case DownloadManager.STATUS_SUCCESSFUL:
|
|
||||||
Log.d(TAG, "Download was successful.");
|
|
||||||
done = true;
|
|
||||||
result = R.string.download_successful;
|
|
||||||
break;
|
|
||||||
case DownloadManager.STATUS_RUNNING:
|
|
||||||
Log.d(TAG, "Download is running.");
|
|
||||||
result = R.string.download_running;
|
|
||||||
break;
|
|
||||||
case DownloadManager.STATUS_FAILED:
|
|
||||||
Log.d(TAG, "Download failed.");
|
|
||||||
result = R.string.download_failed;
|
|
||||||
done = true;
|
|
||||||
requester.notifyDownloadService(context);
|
|
||||||
break;
|
|
||||||
case DownloadManager.STATUS_PENDING:
|
|
||||||
Log.d(TAG, "Download pending.");
|
|
||||||
result = 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 {
|
|
||||||
try {
|
|
||||||
sleep(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 manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
|
||||||
|
|
||||||
Cursor result = manager.query(query);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
public int getDownloadStatus(Cursor c, String column) {
|
|
||||||
if(c.moveToFirst()) {
|
|
||||||
int status = c.getInt(c.getColumnIndex(column));
|
|
||||||
return status;
|
|
||||||
} else {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private DownloadManager.Query buildQuery(long id) {
|
|
||||||
DownloadManager.Query query = new DownloadManager.Query();
|
|
||||||
query.setFilterById(id);
|
|
||||||
return query;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getResult() {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean getDone() {
|
|
||||||
return done;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ------------ 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