From 1cb46ad0371573053ead9231347fbc703137dfbb Mon Sep 17 00:00:00 2001 From: Daniel Oeh Date: Wed, 30 May 2012 17:19:20 +0200 Subject: [PATCH] Rewrote DownloadObserver --- .../podfetcher/activity/AddFeedActivity.java | 46 ++---- .../podfetcher/activity/ItemviewActivity.java | 4 +- src/de/podfetcher/feed/Feed.java | 1 + src/de/podfetcher/feed/FeedFile.java | 12 ++ src/de/podfetcher/feed/FeedImage.java | 1 + src/de/podfetcher/feed/FeedMedia.java | 1 + .../podfetcher/service/DownloadObserver.java | 137 ++++++++---------- .../podfetcher/storage/DownloadRequester.java | 23 +++ 8 files changed, 115 insertions(+), 110 deletions(-) diff --git a/src/de/podfetcher/activity/AddFeedActivity.java b/src/de/podfetcher/activity/AddFeedActivity.java index 9d6432496..c699f66c0 100644 --- a/src/de/podfetcher/activity/AddFeedActivity.java +++ b/src/de/podfetcher/activity/AddFeedActivity.java @@ -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()) { - dialog.dismiss(); - finish(); - } - - if(observer.getDone()) { - dialog.dismiss(); - finish(); - }else { - dialog.setMessage(AddFeedActivity.this.getString(observer.getResult()) + ": " + observer.getProgressPercent() + "%"); - } - }}); - - return null; - }}; - observer.setClient(client); + final ProgressDialog dialog = new ProgressDialog(this); + final DownloadObserver observer = new DownloadObserver(this) { + @Override + protected void onPostExecute(Boolean result) { + dialog.dismiss(); + finish(); + } + @Override + protected void onProgressUpdate(Integer... values) { + Integer progr = values[0]; + dialog.setMessage(getContext().getString(getStatusMsg()) + + " (" + progr.toString() + "%)"); + } + }; dialog.show(); - observer.start(); + observer.execute(feed); } - - } diff --git a/src/de/podfetcher/activity/ItemviewActivity.java b/src/de/podfetcher/activity/ItemviewActivity.java index 06c12b86a..91439879d 100644 --- a/src/de/podfetcher/activity/ItemviewActivity.java +++ b/src/de/podfetcher/activity/ItemviewActivity.java @@ -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(); } + */ } } diff --git a/src/de/podfetcher/feed/Feed.java b/src/de/podfetcher/feed/Feed.java index 66be64778..e200fbaad 100644 --- a/src/de/podfetcher/feed/Feed.java +++ b/src/de/podfetcher/feed/Feed.java @@ -24,6 +24,7 @@ public class Feed extends FeedFile{ } public Feed(String url) { + super(); this.download_url = url; } diff --git a/src/de/podfetcher/feed/FeedFile.java b/src/de/podfetcher/feed/FeedFile.java index fce50d08f..722a968b9 100644 --- a/src/de/podfetcher/feed/FeedFile.java +++ b/src/de/podfetcher/feed/FeedFile.java @@ -5,6 +5,10 @@ public abstract class FeedFile extends FeedComponent { protected String file_url; 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; + } } diff --git a/src/de/podfetcher/feed/FeedImage.java b/src/de/podfetcher/feed/FeedImage.java index 02d83b5ae..098aa15b6 100644 --- a/src/de/podfetcher/feed/FeedImage.java +++ b/src/de/podfetcher/feed/FeedImage.java @@ -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; diff --git a/src/de/podfetcher/feed/FeedMedia.java b/src/de/podfetcher/feed/FeedMedia.java index 1234cc0bd..ca2a534f2 100644 --- a/src/de/podfetcher/feed/FeedMedia.java +++ b/src/de/podfetcher/feed/FeedMedia.java @@ -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; diff --git a/src/de/podfetcher/service/DownloadObserver.java b/src/de/podfetcher/service/DownloadObserver.java index 7fc006bf1..34e253dfb 100644 --- a/src/de/podfetcher/service/DownloadObserver.java +++ b/src/de/podfetcher/service/DownloadObserver.java @@ -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 { 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; - } - public void run() { - Log.d(TAG, "Thread started."); - while(!isInterrupted() && !timedOut) { - cursor = getDownloadCursor(); + 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); + } + + 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 { - 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); - } - } - }catch (InterruptedException e) { - Log.w(TAG, "Thread was interrupted while waiting."); - } + publishProgress(progressPercent); + + try { + 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; } } diff --git a/src/de/podfetcher/storage/DownloadRequester.java b/src/de/podfetcher/storage/DownloadRequester.java index f7ed29df2..d6dbdc4ca 100644 --- a/src/de/podfetcher/storage/DownloadRequester.java +++ b/src/de/podfetcher/storage/DownloadRequester.java @@ -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;