'Fixed' bug 653, undetermined progress bar.

This commit is contained in:
Pavol Zibrita 2011-09-26 00:44:58 +02:00
parent be734aef85
commit e8733b886c
2 changed files with 44 additions and 4 deletions

View file

@ -1,8 +1,10 @@
package net.osmand.plus;
import net.osmand.IProgress;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Handler;
@ -20,11 +22,17 @@ public class ProgressDialogImplementation implements IProgress {
private Context context;
private ProgressDialog dialog = null;
private final boolean cancelable;
private Activity activity;
public ProgressDialogImplementation(Context ctx, ProgressDialog dlg, boolean cancelable){
this.cancelable = cancelable;
context = ctx;
if (context instanceof Activity) {
activity = (Activity)context;
} else if (ctx instanceof ContextWrapper && ((ContextWrapper)ctx).getBaseContext() instanceof Activity) {
activity = (Activity)((ContextWrapper)ctx).getBaseContext();
}
setDialog(dlg);
mViewUpdateHandler = new Handler(){
@ -34,7 +42,7 @@ public class ProgressDialogImplementation implements IProgress {
if(dialog != null){
dialog.setMessage(message);
if (isIndeterminate()) {
dialog.setMax(0);
dialog.setMax(1);
dialog.setIndeterminate(true);
} else {
dialog.setIndeterminate(false);
@ -109,8 +117,18 @@ public class ProgressDialogImplementation implements IProgress {
@Override
public void progress(int deltaWork) {
this.progress += deltaWork;
final int prg = progress;
if (!isIndeterminate() && dialog != null) {
dialog.setProgress(this.progress);
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.setProgress(prg);
}
});
} else {
dialog.setProgress(prg);
}
}
}

View file

@ -89,14 +89,24 @@ public class DownloadFileHelper {
}
first = false;
int prg = 0;
while ((read = is.read(buffer)) != -1) {
if(interruptDownloading){
throw new InterruptedException();
}
out.write(buffer, 0, read);
progress.progress(read);
if (length > -1) {
prg += read;
if (prg > length / 100) {
progress.progress(prg);
prg = 0;
}
}
fileread += read;
}
if (prg > 0) {
progress.progress(prg);
}
if(length <= fileread){
triesDownload = 0;
}
@ -142,7 +152,6 @@ public class DownloadFileHelper {
File toIndex = fileToDownload;
if (fileToDownload.getName().endsWith(".zip")) { //$NON-NLS-1$
progress.startTask(ctx.getString(R.string.unzipping_file), -1);
if (!unzipToDir) {
toIndex = fileToUnZip;
} else {
@ -152,6 +161,8 @@ public class DownloadFileHelper {
ZipEntry entry = null;
boolean first = true;
while ((entry = zipIn.getNextEntry()) != null) {
int size = (int)entry.getSize();
progress.startTask(ctx.getString(R.string.unzipping_file), size);
File fs;
if (!unzipToDir) {
if (first) {
@ -177,8 +188,19 @@ public class DownloadFileHelper {
out = new FileOutputStream(fs);
int read;
byte[] buffer = new byte[BUFFER_SIZE];
int prg = 0;
while ((read = zipIn.read(buffer)) != -1) {
out.write(buffer, 0, read);
if (size > -1) {
prg += read;
if (prg > size/100) {
progress.progress(prg);
prg = 0;
}
}
}
if (prg > 0) {
progress.progress(prg);
}
out.close();
}