'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; package net.osmand.plus;
import net.osmand.IProgress; import net.osmand.IProgress;
import android.app.Activity;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.Context; import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener; import android.content.DialogInterface.OnCancelListener;
import android.os.Handler; import android.os.Handler;
@ -20,11 +22,17 @@ public class ProgressDialogImplementation implements IProgress {
private Context context; private Context context;
private ProgressDialog dialog = null; private ProgressDialog dialog = null;
private final boolean cancelable; private final boolean cancelable;
private Activity activity;
public ProgressDialogImplementation(Context ctx, ProgressDialog dlg, boolean cancelable){ public ProgressDialogImplementation(Context ctx, ProgressDialog dlg, boolean cancelable){
this.cancelable = cancelable; this.cancelable = cancelable;
context = ctx; 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); setDialog(dlg);
mViewUpdateHandler = new Handler(){ mViewUpdateHandler = new Handler(){
@ -34,7 +42,7 @@ public class ProgressDialogImplementation implements IProgress {
if(dialog != null){ if(dialog != null){
dialog.setMessage(message); dialog.setMessage(message);
if (isIndeterminate()) { if (isIndeterminate()) {
dialog.setMax(0); dialog.setMax(1);
dialog.setIndeterminate(true); dialog.setIndeterminate(true);
} else { } else {
dialog.setIndeterminate(false); dialog.setIndeterminate(false);
@ -109,8 +117,18 @@ public class ProgressDialogImplementation implements IProgress {
@Override @Override
public void progress(int deltaWork) { public void progress(int deltaWork) {
this.progress += deltaWork; this.progress += deltaWork;
final int prg = progress;
if (!isIndeterminate() && dialog != null) { 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; first = false;
int prg = 0;
while ((read = is.read(buffer)) != -1) { while ((read = is.read(buffer)) != -1) {
if(interruptDownloading){ if(interruptDownloading){
throw new InterruptedException(); throw new InterruptedException();
} }
out.write(buffer, 0, read); out.write(buffer, 0, read);
progress.progress(read); if (length > -1) {
prg += read;
if (prg > length / 100) {
progress.progress(prg);
prg = 0;
}
}
fileread += read; fileread += read;
} }
if (prg > 0) {
progress.progress(prg);
}
if(length <= fileread){ if(length <= fileread){
triesDownload = 0; triesDownload = 0;
} }
@ -142,7 +152,6 @@ public class DownloadFileHelper {
File toIndex = fileToDownload; File toIndex = fileToDownload;
if (fileToDownload.getName().endsWith(".zip")) { //$NON-NLS-1$ if (fileToDownload.getName().endsWith(".zip")) { //$NON-NLS-1$
progress.startTask(ctx.getString(R.string.unzipping_file), -1);
if (!unzipToDir) { if (!unzipToDir) {
toIndex = fileToUnZip; toIndex = fileToUnZip;
} else { } else {
@ -152,6 +161,8 @@ public class DownloadFileHelper {
ZipEntry entry = null; ZipEntry entry = null;
boolean first = true; boolean first = true;
while ((entry = zipIn.getNextEntry()) != null) { while ((entry = zipIn.getNextEntry()) != null) {
int size = (int)entry.getSize();
progress.startTask(ctx.getString(R.string.unzipping_file), size);
File fs; File fs;
if (!unzipToDir) { if (!unzipToDir) {
if (first) { if (first) {
@ -177,8 +188,19 @@ public class DownloadFileHelper {
out = new FileOutputStream(fs); out = new FileOutputStream(fs);
int read; int read;
byte[] buffer = new byte[BUFFER_SIZE]; byte[] buffer = new byte[BUFFER_SIZE];
int prg = 0;
while ((read = zipIn.read(buffer)) != -1) { while ((read = zipIn.read(buffer)) != -1) {
out.write(buffer, 0, read); 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(); out.close();
} }