Update repo download progress report

This commit is contained in:
topjohnwu 2017-11-09 02:12:55 +08:00
parent cca4347bf9
commit 64c363ce53
2 changed files with 39 additions and 27 deletions

View File

@ -6,6 +6,7 @@ import android.app.ProgressDialog;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Environment; import android.os.Environment;
import android.os.Handler;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.widget.Toast; import android.widget.Toast;
@ -39,15 +40,14 @@ public class ProcessRepoZip extends ParallelTask<Void, Object, Boolean> {
private String mLink; private String mLink;
private File mFile; private File mFile;
private int progress = 0, total = -1; private int progress = 0, total = -1;
private Handler mHandler;
private static final int UPDATE_DL_PROG = 0;
private static final int SHOW_PROCESSING = 1;
public ProcessRepoZip(Activity context, String link, String filename, boolean install) { public ProcessRepoZip(Activity context, String link, String filename, boolean install) {
super(context); super(context);
mLink = link; mLink = link;
mFile = new File(Environment.getExternalStorageDirectory() + "/MagiskManager", filename); mFile = new File(Environment.getExternalStorageDirectory() + "/MagiskManager", filename);
mInstall = install; mInstall = install;
mHandler = new Handler();
} }
private void removeTopFolder(InputStream in, File output) throws IOException { private void removeTopFolder(InputStream in, File output) throws IOException {
@ -81,28 +81,11 @@ public class ProcessRepoZip extends ParallelTask<Void, Object, Boolean> {
progressDialog = ProgressDialog.show(activity, activity.getString(R.string.zip_download_title), activity.getString(R.string.zip_download_msg, 0)); progressDialog = ProgressDialog.show(activity, activity.getString(R.string.zip_download_title), activity.getString(R.string.zip_download_msg, 0));
} }
@Override
protected void onProgressUpdate(Object... values) {
int mode = (int) values[0];
switch (mode) {
case UPDATE_DL_PROG:
int add = (int) values[1];
progress += add;
progressDialog.setMessage(getActivity().getString(R.string.zip_download_msg, 100 * progress / total));
break;
case SHOW_PROCESSING:
progressDialog.setTitle(R.string.zip_process_title);
progressDialog.setMessage(getActivity().getString(R.string.zip_process_msg));
break;
}
}
@Override @Override
protected Boolean doInBackground(Void... params) { protected Boolean doInBackground(Void... params) {
Activity activity = getActivity(); Activity activity = getActivity();
if (activity == null) return null; if (activity == null) return null;
try { try {
// Request zip from Internet // Request zip from Internet
HttpURLConnection conn; HttpURLConnection conn;
do { do {
@ -126,7 +109,10 @@ public class ProcessRepoZip extends ParallelTask<Void, Object, Boolean> {
removeTopFolder(in, temp1); removeTopFolder(in, temp1);
conn.disconnect(); conn.disconnect();
publishProgress(SHOW_PROCESSING); mHandler.post(() -> {
progressDialog.setTitle(R.string.zip_process_title);
progressDialog.setMessage(getActivity().getString(R.string.zip_process_msg));
});
// Then sign the zip for the first time, temp1 -> temp2 // Then sign the zip for the first time, temp1 -> temp2
ZipUtils.signZip(temp1, temp2, false); ZipUtils.signZip(temp1, temp2, false);
@ -138,7 +124,8 @@ public class ProcessRepoZip extends ParallelTask<Void, Object, Boolean> {
ZipUtils.signZip(temp1, temp2, true); ZipUtils.signZip(temp1, temp2, true);
// Write it to the target zip, temp2 -> file // Write it to the target zip, temp2 -> file
try (OutputStream out = new BufferedOutputStream(new FileOutputStream(mFile)); try (
OutputStream out = new BufferedOutputStream(new FileOutputStream(mFile));
InputStream source = new BufferedInputStream(new FileInputStream(temp2)) InputStream source = new BufferedInputStream(new FileInputStream(temp2))
) { ) {
byte[] buffer = new byte[4096]; byte[] buffer = new byte[4096];
@ -191,10 +178,18 @@ public class ProcessRepoZip extends ParallelTask<Void, Object, Boolean> {
super(in); super(in);
} }
private void updateDlProgress(int step) {
progress += step;
progressDialog.setMessage(getActivity().getString(R.string.zip_download_msg, 100 * progress / total));
}
@Override @Override
public synchronized int read() throws IOException { public synchronized int read() throws IOException {
publishProgress(UPDATE_DL_PROG, 1); int b = super.read();
return super.read(); if (b > 0) {
mHandler.post(() -> updateDlProgress(1));
}
return b;
} }
@Override @Override
@ -205,7 +200,9 @@ public class ProcessRepoZip extends ParallelTask<Void, Object, Boolean> {
@Override @Override
public synchronized int read(@NonNull byte[] b, int off, int len) throws IOException { public synchronized int read(@NonNull byte[] b, int off, int len) throws IOException {
int read = super.read(b, off, len); int read = super.read(b, off, len);
publishProgress(UPDATE_DL_PROG, read); if (read > 0) {
mHandler.post(() -> updateDlProgress(read));
}
return read; return read;
} }
} }

View File

@ -39,7 +39,7 @@ public class InputStreamWrapper extends InputStream {
@Override @Override
public int read(@NonNull byte[] b) throws IOException { public int read(@NonNull byte[] b) throws IOException {
return read(b, 0, b.length); return in.read(b);
} }
@Override @Override
@ -56,4 +56,19 @@ public class InputStreamWrapper extends InputStream {
public long skip(long n) throws IOException { public long skip(long n) throws IOException {
return in.skip(n); return in.skip(n);
} }
@Override
public int hashCode() {
return in.hashCode();
}
@Override
public boolean equals(Object obj) {
return in.equals(obj);
}
@Override
public String toString() {
return in.toString();
}
} }