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)
.setDownloadProgressListener(progress)
.setErrorHandler(((conn, e) -> progress.dlFail()))
.getAsFile(f -> {
.getAsFile(zip, f -> {
progress.dlDone();
SnackbarMaker.make(a,
a.getString(R.string.internal_storage, "/Download/" + filename),
Snackbar.LENGTH_LONG).show();
}, zip);
});
});
}

View File

@ -46,14 +46,14 @@ public class UninstallDialog extends CustomAlertDialog {
Networking.get(Data.uninstallerLink)
.setDownloadProgressListener(progress)
.setErrorHandler(((conn, e) -> progress.dlFail()))
.getAsFile(f -> {
.getAsFile(zip, f -> {
progress.dismiss();
Intent intent = new Intent(activity, ClassMap.get(FlashActivity.class))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setData(Uri.fromFile(f))
.putExtra(Const.Key.FLASH_ACTION, Const.Value.UNINSTALL);
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.ResponseListener;
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.FileOutputStream;
import dalvik.system.DexClassLoader;
@ -36,37 +32,25 @@ public class DownloadApp {
private static void dlInstall(String name, ManagerDownloadListener listener) {
File apk = new File(App.self.getCacheDir(), "manager.apk");
ProgressNotification progress = new ProgressNotification(name);
listener.setProgressNotification(progress);
listener.progress = progress;
Networking.get(Data.managerLink)
.setExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
.setDownloadProgressListener(progress)
.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 void setProgressNotification(ProgressNotification progress) {
this.progress = progress;
}
public abstract void onDownloadComplete(File apk, ProgressNotification progress);
private static class PatchPackageName extends ManagerDownloadListener {
@Override
public void onResponse(File apk) {
onDownloadComplete(apk, progress);
}
}
static class PatchPackageName extends ManagerDownloadListener {
@Override
public void onDownloadComplete(File apk, ProgressNotification progress) {
File patched = apk;
App app = App.self;
if (!App.self.getPackageName().equals(BuildConfig.APPLICATION_ID)) {
if (!app.getPackageName().equals(BuildConfig.APPLICATION_ID)) {
progress.getNotificationBuilder()
.setProgress(0, 0, true)
.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
public void onDownloadComplete(File apk, ProgressNotification progress) {
public void onResponse(File apk) {
App app = App.self;
progress.getNotificationBuilder()
.setProgress(0, 0, true)

View File

@ -38,14 +38,14 @@ public class SafetyNet {
} catch (Exception ignored) {
Shell.sh("rm -rf " + EXT_APK.getParent()).exec();
EXT_APK.getParentFile().mkdir();
Networking.get(Const.Url.SNET_URL).getAsFile(f -> {
Networking.get(Const.Url.SNET_URL).getAsFile(EXT_APK, f -> {
try {
dyRun(activity);
} catch (Exception e) {
e.printStackTrace();
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);
return new Request(conn);
} 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 bytesDownloaded;
private DownloadProgressListener progress;
public ProgressInputStream(InputStream in, long total) {
public ProgressInputStream(InputStream in, long total, DownloadProgressListener listener) {
super(in);
totalBytes = total;
}
protected void updateProgress(long bytesDownloaded, long totalBytes) {}
private void update() {
Networking.mainHandler.post(() -> updateProgress(bytesDownloaded, totalBytes));
progress = listener;
}
@Override
@ -25,7 +21,7 @@ public class ProgressInputStream extends FilterInputStream {
int b = super.read();
if (b >= 0) {
bytesDownloaded++;
update();
Networking.mainHandler.post(() -> progress.onProgress(bytesDownloaded, totalBytes));
}
return b;
}
@ -40,7 +36,7 @@ public class ProgressInputStream extends FilterInputStream {
int sz = super.read(b, off, len);
if (sz > 0) {
bytesDownloaded += sz;
update();
Networking.mainHandler.post(() -> progress.onProgress(bytesDownloaded, totalBytes));
}
return sz;
}

View File

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