From 4caa46e03f16a3dcbc8e8fe383c4d64ed533980a Mon Sep 17 00:00:00 2001 From: Daniel Oeh Date: Mon, 28 May 2012 12:19:13 +0200 Subject: [PATCH] DownloadObserver can now show progress in percent --- .../podfetcher/activity/AddFeedActivity.java | 3 +-- .../podfetcher/service/DownloadObserver.java | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/de/podfetcher/activity/AddFeedActivity.java b/src/de/podfetcher/activity/AddFeedActivity.java index e0f83383d..9d6432496 100644 --- a/src/de/podfetcher/activity/AddFeedActivity.java +++ b/src/de/podfetcher/activity/AddFeedActivity.java @@ -88,8 +88,7 @@ public class AddFeedActivity extends SherlockActivity { dialog.dismiss(); finish(); }else { - Log.d(TAG, "Changing message of dialog."); - dialog.setMessage(AddFeedActivity.this.getString(observer.getResult())); + dialog.setMessage(AddFeedActivity.this.getString(observer.getResult()) + ": " + observer.getProgressPercent() + "%"); } }}); diff --git a/src/de/podfetcher/service/DownloadObserver.java b/src/de/podfetcher/service/DownloadObserver.java index 4472f6557..7fc006bf1 100644 --- a/src/de/podfetcher/service/DownloadObserver.java +++ b/src/de/podfetcher/service/DownloadObserver.java @@ -18,6 +18,7 @@ public class DownloadObserver extends Thread { long waiting_intervall; private volatile int result; private volatile boolean done; + private int progressPercent; private Cursor cursor; private final long DEFAULT_WAITING_INTERVALL = 500L; private DownloadRequester requester; @@ -44,6 +45,7 @@ public class DownloadObserver extends Thread { while(!isInterrupted() && !timedOut) { 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."); @@ -118,6 +120,18 @@ public class DownloadObserver extends Thread { } } + private int getDownloadProgress(Cursor c) { + if (c.moveToFirst()) { + long size = c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); + long soFar = c.getLong(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); + int progress = (int) ((soFar / size) * 100); + Log.d(TAG, "Setting progress to " + progress); + return progress; + } else { + return -1; + } + } + private DownloadManager.Query buildQuery(long id) { DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(id); @@ -132,6 +146,10 @@ public class DownloadObserver extends Thread { return done; } + public int getProgressPercent() { + return progressPercent; + } + public boolean isTimedOut() { return timedOut; }