Update net module

This commit is contained in:
topjohnwu 2019-01-01 18:45:48 +08:00
parent 1df65940b9
commit 7bd52d0245
9 changed files with 81 additions and 100 deletions

View File

@ -71,12 +71,12 @@ class InstallMethodDialog extends AlertDialog.Builder {
Networking.get(Data.magiskLink) Networking.get(Data.magiskLink)
.setDownloadProgressListener(progress) .setDownloadProgressListener(progress)
.setErrorHandler(((conn, e) -> progress.dlFail())) .setErrorHandler(((conn, e) -> progress.dlFail()))
.getAsFile(f -> { .getAsFile(zip, f -> {
progress.dlDone(); progress.dlDone();
SnackbarMaker.make(a, SnackbarMaker.make(a,
a.getString(R.string.internal_storage, "/Download/" + filename), a.getString(R.string.internal_storage, "/Download/" + filename),
Snackbar.LENGTH_LONG).show(); Snackbar.LENGTH_LONG).show();
}, zip); });
}); });
} }

View File

@ -46,14 +46,14 @@ public class UninstallDialog extends CustomAlertDialog {
Networking.get(Data.uninstallerLink) Networking.get(Data.uninstallerLink)
.setDownloadProgressListener(progress) .setDownloadProgressListener(progress)
.setErrorHandler(((conn, e) -> progress.dlFail())) .setErrorHandler(((conn, e) -> progress.dlFail()))
.getAsFile(f -> { .getAsFile(zip, f -> {
progress.dismiss(); progress.dismiss();
Intent intent = new Intent(activity, ClassMap.get(FlashActivity.class)) Intent intent = new Intent(activity, ClassMap.get(FlashActivity.class))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setData(Uri.fromFile(f)) .setData(Uri.fromFile(f))
.putExtra(Const.Key.FLASH_ACTION, Const.Value.UNINSTALL); .putExtra(Const.Key.FLASH_ACTION, Const.Value.UNINSTALL);
activity.startActivity(intent); activity.startActivity(intent);
}, zip); });
}); });
} }
} }

View File

@ -12,12 +12,8 @@ import com.topjohnwu.magisk.components.ProgressNotification;
import com.topjohnwu.net.Networking; import com.topjohnwu.net.Networking;
import com.topjohnwu.net.ResponseListener; import com.topjohnwu.net.ResponseListener;
import com.topjohnwu.superuser.ShellUtils; import com.topjohnwu.superuser.ShellUtils;
import com.topjohnwu.utils.JarMap;
import com.topjohnwu.utils.SignAPK;
import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import dalvik.system.DexClassLoader; import dalvik.system.DexClassLoader;
@ -36,37 +32,25 @@ public class DownloadApp {
private static void dlInstall(String name, ManagerDownloadListener listener) { private static void dlInstall(String name, ManagerDownloadListener listener) {
File apk = new File(App.self.getCacheDir(), "manager.apk"); File apk = new File(App.self.getCacheDir(), "manager.apk");
ProgressNotification progress = new ProgressNotification(name); ProgressNotification progress = new ProgressNotification(name);
listener.setProgressNotification(progress); listener.progress = progress;
Networking.get(Data.managerLink) Networking.get(Data.managerLink)
.setExecutor(AsyncTask.THREAD_POOL_EXECUTOR) .setExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
.setDownloadProgressListener(progress) .setDownloadProgressListener(progress)
.setErrorHandler((conn, e) -> progress.dlFail()) .setErrorHandler((conn, e) -> progress.dlFail())
.getAsFile(listener, apk); .getAsFile(apk, listener);
} }
abstract static class ManagerDownloadListener implements ResponseListener<File> { private abstract static class ManagerDownloadListener implements ResponseListener<File> {
ProgressNotification progress;
}
private ProgressNotification progress; private static class PatchPackageName extends ManagerDownloadListener {
private void setProgressNotification(ProgressNotification progress) {
this.progress = progress;
}
public abstract void onDownloadComplete(File apk, ProgressNotification progress);
@Override @Override
public void onResponse(File apk) { public void onResponse(File apk) {
onDownloadComplete(apk, progress);
}
}
static class PatchPackageName extends ManagerDownloadListener {
@Override
public void onDownloadComplete(File apk, ProgressNotification progress) {
File patched = apk; File patched = apk;
App app = App.self; App app = App.self;
if (!App.self.getPackageName().equals(BuildConfig.APPLICATION_ID)) { if (!app.getPackageName().equals(BuildConfig.APPLICATION_ID)) {
progress.getNotificationBuilder() progress.getNotificationBuilder()
.setProgress(0, 0, true) .setProgress(0, 0, true)
.setContentTitle(app.getString(R.string.hide_manager_title)) .setContentTitle(app.getString(R.string.hide_manager_title))
@ -91,10 +75,10 @@ public class DownloadApp {
} }
} }
static class RestoreManager extends ManagerDownloadListener { private static class RestoreManager extends ManagerDownloadListener {
@Override @Override
public void onDownloadComplete(File apk, ProgressNotification progress) { public void onResponse(File apk) {
App app = App.self; App app = App.self;
progress.getNotificationBuilder() progress.getNotificationBuilder()
.setProgress(0, 0, true) .setProgress(0, 0, true)

View File

@ -38,14 +38,14 @@ public class SafetyNet {
} catch (Exception ignored) { } catch (Exception ignored) {
Shell.sh("rm -rf " + EXT_APK.getParent()).exec(); Shell.sh("rm -rf " + EXT_APK.getParent()).exec();
EXT_APK.getParentFile().mkdir(); EXT_APK.getParentFile().mkdir();
Networking.get(Const.Url.SNET_URL).getAsFile(f -> { Networking.get(Const.Url.SNET_URL).getAsFile(EXT_APK, f -> {
try { try {
dyRun(activity); dyRun(activity);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Topic.publish(false, Topic.SNET_CHECK_DONE, -1); Topic.publish(false, Topic.SNET_CHECK_DONE, -1);
} }
}, EXT_APK); });
} }
} }
} }

View File

@ -0,0 +1,51 @@
package com.topjohnwu.net;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Executor;
class BadRequest extends Request {
private IOException ex;
BadRequest(IOException e) { super(null); ex = e; }
@Override
public Request addHeaders(String key, String value) { return this; }
@Override
public Result<InputStream> execForInputStream() { fail(); return new Result<>(); }
@Override
public void getAsFile(File out, ResponseListener<File> rs) { fail(); }
@Override
public void execForFile(File out) { fail(); }
@Override
public void getAsString(ResponseListener<String> rs) { fail(); }
@Override
public Result<String> execForString() { fail(); return new Result<>(); }
@Override
public void getAsJSONObject(ResponseListener<JSONObject> rs) { fail(); }
@Override
public Result<JSONObject> execForJSONObject() { fail(); return new Result<>(); }
@Override
public void getAsJSONArray(ResponseListener<JSONArray> rs) { fail(); }
@Override
public Result<JSONArray> execForJSONArray() { fail(); return new Result<>(); }
private void fail() {
if (err != null)
err.onError(null, ex);
}
}

View File

@ -27,7 +27,7 @@ public class Networking {
conn.setConnectTimeout(CONNECT_TIMEOUT); conn.setConnectTimeout(CONNECT_TIMEOUT);
return new Request(conn); return new Request(conn);
} catch (IOException e) { } catch (IOException e) {
return new StubRequest(); return new BadRequest(e);
} }
} }

View File

@ -8,16 +8,12 @@ public class ProgressInputStream extends FilterInputStream {
private long totalBytes; private long totalBytes;
private long bytesDownloaded; private long bytesDownloaded;
private DownloadProgressListener progress;
public ProgressInputStream(InputStream in, long total) { public ProgressInputStream(InputStream in, long total, DownloadProgressListener listener) {
super(in); super(in);
totalBytes = total; totalBytes = total;
} progress = listener;
protected void updateProgress(long bytesDownloaded, long totalBytes) {}
private void update() {
Networking.mainHandler.post(() -> updateProgress(bytesDownloaded, totalBytes));
} }
@Override @Override
@ -25,7 +21,7 @@ public class ProgressInputStream extends FilterInputStream {
int b = super.read(); int b = super.read();
if (b >= 0) { if (b >= 0) {
bytesDownloaded++; bytesDownloaded++;
update(); Networking.mainHandler.post(() -> progress.onProgress(bytesDownloaded, totalBytes));
} }
return b; return b;
} }
@ -40,7 +36,7 @@ public class ProgressInputStream extends FilterInputStream {
int sz = super.read(b, off, len); int sz = super.read(b, off, len);
if (sz > 0) { if (sz > 0) {
bytesDownloaded += sz; bytesDownloaded += sz;
update(); Networking.mainHandler.post(() -> progress.onProgress(bytesDownloaded, totalBytes));
} }
return sz; return sz;
} }

View File

@ -23,8 +23,9 @@ public class Request {
private HttpURLConnection conn; private HttpURLConnection conn;
private Executor executor = null; private Executor executor = null;
private DownloadProgressListener progress = null; private DownloadProgressListener progress = null;
private ErrorHandler err = null; private int code = -1;
private int code;
ErrorHandler err = null;
private interface Requestor<T> { private interface Requestor<T> {
T request() throws Exception; T request() throws Exception;
@ -74,7 +75,7 @@ public class Request {
return exec(this::getInputStream); return exec(this::getInputStream);
} }
public void getAsFile(ResponseListener<File> rs, File out) { public void getAsFile(File out, ResponseListener<File> rs) {
submit(() -> dlFile(out), rs); submit(() -> dlFile(out), rs);
} }
@ -110,7 +111,10 @@ public class Request {
Result<T> res = new Result<>(); Result<T> res = new Result<>();
try { try {
res.result = req.request(); res.result = req.request();
} catch (Exception ignored) {} } catch (Exception e) {
if (err != null)
err.onError(conn, e);
}
return res; return res;
} }
@ -135,12 +139,7 @@ public class Request {
code = conn.getResponseCode(); code = conn.getResponseCode();
InputStream in = conn.getInputStream(); InputStream in = conn.getInputStream();
if (progress != null) { if (progress != null) {
in = new ProgressInputStream(in, conn.getContentLength()) { in = new ProgressInputStream(in, conn.getContentLength(), progress) {
@Override
protected void updateProgress(long bytesDownloaded, long totalBytes) {
progress.onProgress(bytesDownloaded, totalBytes);
}
@Override @Override
public void close() throws IOException { public void close() throws IOException {
super.close(); super.close();

View File

@ -1,49 +0,0 @@
package com.topjohnwu.net;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.io.InputStream;
import java.util.concurrent.Executor;
class StubRequest extends Request {
StubRequest() { super(null); }
@Override
public Request addHeaders(String key, String value) { return this; }
@Override
public Request setDownloadProgressListener(DownloadProgressListener listener) { return this; }
@Override
public Request setErrorHandler(ErrorHandler handler) { return this; }
@Override
public Request setExecutor(Executor e) { return this; }
@Override
public Result<InputStream> execForInputStream() { return new Result<>(); }
@Override
public void getAsFile(ResponseListener<File> rs, File out) {}
@Override
public void getAsString(ResponseListener<String> rs) {}
@Override
public Result<String> execForString() { return new Result<>(); }
@Override
public void getAsJSONObject(ResponseListener<JSONObject> rs){}
@Override
public Result<JSONObject> execForJSONObject() { return new Result<>(); }
@Override
public void getAsJSONArray(ResponseListener<JSONArray> rs){}
@Override
public Result<JSONArray> execForJSONArray() { return new Result<>(); }
}