diff --git a/src/de/podfetcher/activity/AddFeedActivity.java b/src/de/podfetcher/activity/AddFeedActivity.java index 273cabf2e..e0f83383d 100644 --- a/src/de/podfetcher/activity/AddFeedActivity.java +++ b/src/de/podfetcher/activity/AddFeedActivity.java @@ -10,6 +10,7 @@ import de.podfetcher.R; import de.podfetcher.feed.Feed; import de.podfetcher.storage.DownloadRequester; import de.podfetcher.util.URLChecker; +import de.podfetcher.service.DownloadObserver; import com.actionbarsherlock.app.SherlockActivity; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; @@ -71,20 +72,29 @@ public class AddFeedActivity extends SherlockActivity { dialog = new ProgressDialog(this); - final DownloadRequester.DownloadObserver observer = new DownloadRequester.DownloadObserver( - feed.getDownloadId(), this); + final DownloadObserver observer = new DownloadObserver( + feed.getDownloadId(), this, 10000); + client = new Callable() { public Object call() { - if(observer.getDone()) { - dialog.dismiss(); - finish(); - }else { - Log.d(TAG, "Changing message of dialog."); - dialog.setMessage(AddFeedActivity.this.getString(observer.getResult())); - } + runOnUiThread(new Runnable() { + public void run() { + if(observer.isTimedOut()) { + dialog.dismiss(); + finish(); + } + + if(observer.getDone()) { + dialog.dismiss(); + finish(); + }else { + Log.d(TAG, "Changing message of dialog."); + dialog.setMessage(AddFeedActivity.this.getString(observer.getResult())); + } + }}); + return null; - } - }; + }}; observer.setClient(client); dialog.show(); observer.start(); diff --git a/src/de/podfetcher/service/DownloadObserver.java b/src/de/podfetcher/service/DownloadObserver.java new file mode 100644 index 000000000..54ba5e5ac --- /dev/null +++ b/src/de/podfetcher/service/DownloadObserver.java @@ -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; + } +} diff --git a/src/de/podfetcher/service/DownloadService.java b/src/de/podfetcher/service/DownloadService.java index 1ae9c3cf2..21dfe9c67 100644 --- a/src/de/podfetcher/service/DownloadService.java +++ b/src/de/podfetcher/service/DownloadService.java @@ -93,6 +93,7 @@ public class DownloadService extends Service { private BroadcastReceiver downloadReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { + Log.d(TAG, "Received 'Download Complete' - message."); long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0); Feed feed = requester.getFeed(downloadId); if(feed != null) { diff --git a/src/de/podfetcher/storage/DownloadRequester.java b/src/de/podfetcher/storage/DownloadRequester.java index d18d442fe..fabe1bfb5 100644 --- a/src/de/podfetcher/storage/DownloadRequester.java +++ b/src/de/podfetcher/storage/DownloadRequester.java @@ -160,111 +160,6 @@ public class DownloadRequester { 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 ------------- */ private Messenger mService = null;