From 64c363ce533c00f713c2ddf6ea24704251c184d6 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 9 Nov 2017 02:12:55 +0800 Subject: [PATCH] Update repo download progress report --- .../magisk/asyncs/ProcessRepoZip.java | 49 +++++++++---------- .../magisk/container/InputStreamWrapper.java | 17 ++++++- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java index 45b72306b..80c95dd0b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java @@ -6,6 +6,7 @@ import android.app.ProgressDialog; import android.content.Intent; import android.net.Uri; import android.os.Environment; +import android.os.Handler; import android.support.annotation.NonNull; import android.widget.Toast; @@ -39,15 +40,14 @@ public class ProcessRepoZip extends ParallelTask { private String mLink; private File mFile; private int progress = 0, total = -1; - - private static final int UPDATE_DL_PROG = 0; - private static final int SHOW_PROCESSING = 1; + private Handler mHandler; public ProcessRepoZip(Activity context, String link, String filename, boolean install) { super(context); mLink = link; mFile = new File(Environment.getExternalStorageDirectory() + "/MagiskManager", filename); mInstall = install; + mHandler = new Handler(); } private void removeTopFolder(InputStream in, File output) throws IOException { @@ -81,28 +81,11 @@ public class ProcessRepoZip extends ParallelTask { 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 protected Boolean doInBackground(Void... params) { Activity activity = getActivity(); if (activity == null) return null; try { - // Request zip from Internet HttpURLConnection conn; do { @@ -126,7 +109,10 @@ public class ProcessRepoZip extends ParallelTask { removeTopFolder(in, temp1); 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 ZipUtils.signZip(temp1, temp2, false); @@ -138,8 +124,9 @@ public class ProcessRepoZip extends ParallelTask { ZipUtils.signZip(temp1, temp2, true); // Write it to the target zip, temp2 -> file - try (OutputStream out = new BufferedOutputStream(new FileOutputStream(mFile)); - InputStream source = new BufferedInputStream(new FileInputStream(temp2)) + try ( + OutputStream out = new BufferedOutputStream(new FileOutputStream(mFile)); + InputStream source = new BufferedInputStream(new FileInputStream(temp2)) ) { byte[] buffer = new byte[4096]; int length; @@ -191,10 +178,18 @@ public class ProcessRepoZip extends ParallelTask { super(in); } + private void updateDlProgress(int step) { + progress += step; + progressDialog.setMessage(getActivity().getString(R.string.zip_download_msg, 100 * progress / total)); + } + @Override public synchronized int read() throws IOException { - publishProgress(UPDATE_DL_PROG, 1); - return super.read(); + int b = super.read(); + if (b > 0) { + mHandler.post(() -> updateDlProgress(1)); + } + return b; } @Override @@ -205,7 +200,9 @@ public class ProcessRepoZip extends ParallelTask { @Override public synchronized int read(@NonNull byte[] b, int off, int len) throws IOException { int read = super.read(b, off, len); - publishProgress(UPDATE_DL_PROG, read); + if (read > 0) { + mHandler.post(() -> updateDlProgress(read)); + } return read; } } diff --git a/app/src/main/java/com/topjohnwu/magisk/container/InputStreamWrapper.java b/app/src/main/java/com/topjohnwu/magisk/container/InputStreamWrapper.java index 06626d8df..020d823b7 100644 --- a/app/src/main/java/com/topjohnwu/magisk/container/InputStreamWrapper.java +++ b/app/src/main/java/com/topjohnwu/magisk/container/InputStreamWrapper.java @@ -39,7 +39,7 @@ public class InputStreamWrapper extends InputStream { @Override public int read(@NonNull byte[] b) throws IOException { - return read(b, 0, b.length); + return in.read(b); } @Override @@ -56,4 +56,19 @@ public class InputStreamWrapper extends InputStream { public long skip(long n) throws IOException { 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(); + } }