DownloadObserver can now show progress in percent

This commit is contained in:
Daniel Oeh 2012-05-28 12:19:13 +02:00
parent 049df1ac74
commit 4caa46e03f
2 changed files with 19 additions and 2 deletions

View File

@ -88,8 +88,7 @@ public class AddFeedActivity extends SherlockActivity {
dialog.dismiss(); dialog.dismiss();
finish(); finish();
}else { }else {
Log.d(TAG, "Changing message of dialog."); dialog.setMessage(AddFeedActivity.this.getString(observer.getResult()) + ": " + observer.getProgressPercent() + "%");
dialog.setMessage(AddFeedActivity.this.getString(observer.getResult()));
} }
}}); }});

View File

@ -18,6 +18,7 @@ public class DownloadObserver extends Thread {
long waiting_intervall; long waiting_intervall;
private volatile int result; private volatile int result;
private volatile boolean done; private volatile boolean done;
private int progressPercent;
private Cursor cursor; private Cursor cursor;
private final long DEFAULT_WAITING_INTERVALL = 500L; private final long DEFAULT_WAITING_INTERVALL = 500L;
private DownloadRequester requester; private DownloadRequester requester;
@ -44,6 +45,7 @@ public class DownloadObserver extends Thread {
while(!isInterrupted() && !timedOut) { while(!isInterrupted() && !timedOut) {
cursor = getDownloadCursor(); cursor = getDownloadCursor();
int status = getDownloadStatus(cursor, DownloadManager.COLUMN_STATUS); int status = getDownloadStatus(cursor, DownloadManager.COLUMN_STATUS);
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.");
@ -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) { private DownloadManager.Query buildQuery(long id) {
DownloadManager.Query query = new DownloadManager.Query(); DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(id); query.setFilterById(id);
@ -132,6 +146,10 @@ public class DownloadObserver extends Thread {
return done; return done;
} }
public int getProgressPercent() {
return progressPercent;
}
public boolean isTimedOut() { public boolean isTimedOut() {
return timedOut; return timedOut;
} }