Merge pull request #68 from ccreutzig/master

download progress bar
This commit is contained in:
pavolzibrita 2011-09-20 12:53:05 -07:00
commit 127c6a258c
2 changed files with 48 additions and 37 deletions

View file

@ -10,11 +10,9 @@ import android.os.Message;
public class ProgressDialogImplementation implements IProgress { public class ProgressDialogImplementation implements IProgress {
private static final float deltaToChange = 0.023f;
private String taskName; private String taskName;
private int progress; private int progress;
private int work; private int work;
private int deltaWork;
private String message = ""; //$NON-NLS-1$ private String message = ""; //$NON-NLS-1$
private Handler mViewUpdateHandler; private Handler mViewUpdateHandler;
@ -35,6 +33,13 @@ public class ProgressDialogImplementation implements IProgress {
super.handleMessage(msg); super.handleMessage(msg);
if(dialog != null){ if(dialog != null){
dialog.setMessage(message); dialog.setMessage(message);
if (isIndeterminate()) {
dialog.setIndeterminate(true);
} else {
dialog.setIndeterminate(false);
dialog.setMax(work);
}
dialog.show();
} }
} }
}; };
@ -44,11 +49,33 @@ public class ProgressDialogImplementation implements IProgress {
this(dlg.getContext(), dlg, cancelable); this(dlg.getContext(), dlg, cancelable);
} }
public ProgressDialogImplementation(final ProgressDialog dlg){ public ProgressDialogImplementation(final ProgressDialog dlg){
this(dlg, false); this(dlg, false);
} }
public static ProgressDialogImplementation createProgressDialog(Context ctx, String title, String message, int style) {
ProgressDialog dlg = new ProgressDialog(ctx);
dlg.setTitle(title);
dlg.setMessage(message);
dlg.setIndeterminate(style == ProgressDialog.STYLE_HORIZONTAL); // re-set in mViewUpdateHandler.handleMessage above
dlg.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(dlg, (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
}
dlg.setProgressStyle(style);
return new ProgressDialogImplementation(dlg, true);
}
public void setDialog(ProgressDialog dlg){ public void setDialog(ProgressDialog dlg){
if(dlg != null){ if(dlg != null){
if(cancelable){ if(cancelable){
@ -80,35 +107,18 @@ public class ProgressDialogImplementation implements IProgress {
@Override @Override
public void progress(int deltaWork) { public void progress(int deltaWork) {
this.deltaWork += deltaWork; this.progress += deltaWork;
if(change(progress + this.deltaWork)){ if (!isIndeterminate() && dialog != null) {
this.progress += this.deltaWork; dialog.setProgress(this.progress);
this.deltaWork = 0;
updateMessage();
} }
} }
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 @Override
public void remaining(int remainingWork) { public void remaining(int remainingWork) {
if(change(work - remainingWork)){ this.progress = work - remainingWork;
this.progress = work - remainingWork; if (!isIndeterminate() && dialog != null) {
updateMessage(); dialog.setProgress(this.progress);
} }
deltaWork = work - remainingWork - this.progress;
} }
public boolean isIndeterminate(){ public boolean isIndeterminate(){
@ -121,19 +131,19 @@ public class ProgressDialogImplementation implements IProgress {
taskName = ""; //$NON-NLS-1$ taskName = ""; //$NON-NLS-1$
} }
message = taskName; message = taskName;
mViewUpdateHandler.sendEmptyMessage(0);
this.taskName = taskName; this.taskName = taskName;
startWork(work); startWork(work);
mViewUpdateHandler.sendEmptyMessage(0);
} }
@Override @Override
public void finishTask() { public void finishTask() {
work = -1;
progress = 0;
if (taskName != null) { if (taskName != null) {
message = context.getResources().getString(R.string.finished_task) +" : "+ taskName; //$NON-NLS-1$ message = context.getResources().getString(R.string.finished_task) +" : "+ taskName; //$NON-NLS-1$
mViewUpdateHandler.sendEmptyMessage(0); mViewUpdateHandler.sendEmptyMessage(0);
} }
work = -1;
progress = 0;
} }
@Override @Override
public boolean isInterrupted() { public boolean isInterrupted() {
@ -149,11 +159,10 @@ public class ProgressDialogImplementation implements IProgress {
@Override @Override
public void startWork(int work) { public void startWork(int work) {
this.work = work; this.work = work;
if(this.work == 0){ if (this.work == 0) {
this.work = 1; this.work = 1;
} }
progress = 0; progress = 0;
deltaWork = 0;
} }
@Override @Override

View file

@ -479,11 +479,13 @@ private class DownloadIndexesAsyncTask extends AsyncTask<String, Object, String
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
progressFileDlg = ProgressDialog.show(DownloadIndexActivity.this, getString(R.string.downloading),
getString(R.string.downloading_file), true, true);
downloadFileHelper.setInterruptDownloading(false); downloadFileHelper.setInterruptDownloading(false);
progressFileDlg.show(); progress = ProgressDialogImplementation.createProgressDialog(
progress = new ProgressDialogImplementation(progressFileDlg, true); DownloadIndexActivity.this,
getString(R.string.downloading),
getString(R.string.downloading_file),
ProgressDialog.STYLE_HORIZONTAL);
progressFileDlg = ((ProgressDialogImplementation)progress).getDialog();
progressFileDlg.setOnCancelListener(new DialogInterface.OnCancelListener() { progressFileDlg.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override @Override
public void onCancel(DialogInterface dialog) { public void onCancel(DialogInterface dialog) {