download progress bar

This commit is contained in:
Christopher Creutzig 2011-09-11 15:23:23 +02:00
parent b0acc299c5
commit 902e756dfb
2 changed files with 36 additions and 33 deletions

View file

@ -10,11 +10,9 @@ import android.os.Message;
public class ProgressDialogImplementation implements IProgress {
private static final float deltaToChange = 0.023f;
private String taskName;
private int progress;
private int work;
private int deltaWork;
private String message = ""; //$NON-NLS-1$
private Handler mViewUpdateHandler;
@ -35,6 +33,13 @@ public class ProgressDialogImplementation implements IProgress {
super.handleMessage(msg);
if(dialog != null){
dialog.setMessage(message);
if (isIndeterminate()) {
dialog.setIndeterminate(true);
} else {
dialog.setIndeterminate(false);
dialog.setMax(work);
}
dialog.show();
}
}
};
@ -80,35 +85,18 @@ public class ProgressDialogImplementation implements IProgress {
@Override
public void progress(int deltaWork) {
this.deltaWork += deltaWork;
if(change(progress + this.deltaWork)){
this.progress += this.deltaWork;
this.deltaWork = 0;
updateMessage();
this.progress += deltaWork;
if (!isIndeterminate() && dialog != null) {
dialog.setProgress(this.progress);
}
}
private void updateMessage() {
message = taskName + String.format(" %.1f %%", this.progress * 100f / ((float) this.work)); //$NON-NLS-1$
mViewUpdateHandler.sendEmptyMessage(0);
}
public boolean change(int newProgress) {
if (newProgress < progress) {
return false;
}
if ((newProgress - progress) / ((float) work) < deltaToChange) {
return false;
}
return true;
}
@Override
public void remaining(int remainingWork) {
if(change(work - remainingWork)){
this.progress = work - remainingWork;
updateMessage();
if (!isIndeterminate() && dialog != null) {
dialog.setProgress(this.progress);
}
deltaWork = work - remainingWork - this.progress;
}
public boolean isIndeterminate(){
@ -121,19 +109,19 @@ public class ProgressDialogImplementation implements IProgress {
taskName = ""; //$NON-NLS-1$
}
message = taskName;
mViewUpdateHandler.sendEmptyMessage(0);
this.taskName = taskName;
startWork(work);
mViewUpdateHandler.sendEmptyMessage(0);
}
@Override
public void finishTask() {
work = -1;
progress = 0;
if (taskName != null) {
message = context.getResources().getString(R.string.finished_task) +" : "+ taskName; //$NON-NLS-1$
mViewUpdateHandler.sendEmptyMessage(0);
}
work = -1;
progress = 0;
}
@Override
public boolean isInterrupted() {
@ -153,7 +141,6 @@ public class ProgressDialogImplementation implements IProgress {
this.work = 1;
}
progress = 0;
deltaWork = 0;
}
@Override

View file

@ -479,10 +479,26 @@ private class DownloadIndexesAsyncTask extends AsyncTask<String, Object, String
@Override
protected void onPreExecute() {
progressFileDlg = ProgressDialog.show(DownloadIndexActivity.this, getString(R.string.downloading),
getString(R.string.downloading_file), true, true);
progressFileDlg = new ProgressDialog(DownloadIndexActivity.this);
progressFileDlg.setTitle(getString(R.string.downloading));
progressFileDlg.setMessage(getString(R.string.downloading_file));
progressFileDlg.setIndeterminate(false);
progressFileDlg.setCancelable(true);
// we'd prefer a plain progress bar without numbers,
// but that is only available starting from API level 11
try {
ProgressDialog.class
.getMethod("setProgressNumberFormat", new Class[] { String.class })
.invoke(progressFileDlg, (String)null);
} catch (NoSuchMethodException nsme) {
// failure, must be older device
} catch (IllegalAccessException nsme) {
// failure, must be older device
} catch (java.lang.reflect.InvocationTargetException nsme) {
// failure, must be older device
}
downloadFileHelper.setInterruptDownloading(false);
progressFileDlg.show();
progressFileDlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress = new ProgressDialogImplementation(progressFileDlg, true);
progressFileDlg.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override