Support new canary links

This commit is contained in:
topjohnwu 2020-10-11 03:37:03 -07:00
parent 0a14f43f9c
commit 66cad101c0

View File

@ -2,16 +2,14 @@ package com.topjohnwu.magisk;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Application;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.ContextThemeWrapper; import android.view.ContextThemeWrapper;
import com.topjohnwu.magisk.net.ErrorHandler;
import com.topjohnwu.magisk.net.Networking; import com.topjohnwu.magisk.net.Networking;
import com.topjohnwu.magisk.net.ResponseListener; import com.topjohnwu.magisk.net.Request;
import com.topjohnwu.magisk.utils.APKInstall; import com.topjohnwu.magisk.utils.APKInstall;
import org.json.JSONException; import org.json.JSONException;
@ -22,45 +20,19 @@ import java.io.File;
import static android.R.string.no; import static android.R.string.no;
import static android.R.string.ok; import static android.R.string.ok;
import static android.R.string.yes; import static android.R.string.yes;
import static com.topjohnwu.magisk.R.string.dling;
import static com.topjohnwu.magisk.R.string.no_internet_msg;
import static com.topjohnwu.magisk.R.string.upgrade_msg;
public class MainActivity extends Activity { public class MainActivity extends Activity {
private static final String APP_NAME = "Magisk Manager"; private static final String APP_NAME = "Magisk Manager";
private static final String CDN_URL = "https://cdn.jsdelivr.net/gh/topjohnwu/magisk_files@%s/%s";
private String apkLink; private String apkLink;
private ErrorHandler err = (conn, e) -> { private String sha;
Log.e(getClass().getSimpleName(), "", e);
finish();
};
private Context themed; private Context themed;
private ProgressDialog dialog;
private String URL() {
if (BuildConfig.DEV_CHANNEL != null)
return BuildConfig.DEV_CHANNEL;
else if (BuildConfig.CANARY)
return "https://raw.githubusercontent.com/topjohnwu/magisk_files/canary/release.json";
else
return "https://topjohnwu.github.io/magisk_files/stable.json";
}
private void showDialog() {
ProgressDialog.show(themed,
getString(R.string.dling),
getString(R.string.dling) + " " + APP_NAME,
true);
}
private void dlAPK() {
showDialog();
// Download and upgrade the app
Application app = getApplication();
Networking.get(apkLink)
.setErrorHandler(err)
.getAsFile(
new File(getCacheDir(), "manager.apk"),
apk -> APKInstall.install(app, apk)
);
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -69,36 +41,77 @@ public class MainActivity extends Activity {
themed = new ContextThemeWrapper(this, android.R.style.Theme_DeviceDefault); themed = new ContextThemeWrapper(this, android.R.style.Theme_DeviceDefault);
if (Networking.checkNetworkStatus(this)) { if (Networking.checkNetworkStatus(this)) {
Networking.get(URL()) fetchAPKURL();
.setErrorHandler(err)
.getAsJSONObject(new JSONLoader());
} else { } else {
new AlertDialog.Builder(themed) new AlertDialog.Builder(themed)
.setCancelable(false) .setCancelable(false)
.setTitle(APP_NAME) .setTitle(APP_NAME)
.setMessage(getString(R.string.no_internet_msg)) .setMessage(getString(no_internet_msg))
.setNegativeButton(ok, (d, w) -> finish()) .setNegativeButton(ok, (d, w) -> finish())
.show(); .show();
} }
} }
class JSONLoader implements ResponseListener<JSONObject> { private void fetchAPKURL() {
dialog = ProgressDialog.show(themed, "", "", true);
String url;
if (BuildConfig.DEV_CHANNEL != null) {
url = BuildConfig.DEV_CHANNEL;
} else if (!BuildConfig.CANARY) {
url = "https://topjohnwu.github.io/magisk_files/stable.json";
} else {
url = "https://api.github.com/repos/topjohnwu/magisk_files/branches/canary";
request(url).getAsJSONObject(this::handleCanary);
return;
}
request(url).getAsJSONObject(this::handleJSON);
}
@Override private void error(Throwable e) {
public void onResponse(JSONObject json) { Log.e(getClass().getSimpleName(), "", e);
try { finish();
JSONObject manager = json.getJSONObject("app"); }
apkLink = manager.getString("link");
new AlertDialog.Builder(themed) private Request request(String url) {
.setCancelable(false) return Networking.get(url).setErrorHandler((conn, e) -> error(e));
.setTitle(APP_NAME) }
.setMessage(getString(R.string.upgrade_msg))
.setPositiveButton(yes, (d, w) -> dlAPK()) private void handleCanary(JSONObject json) {
.setNegativeButton(no, (d, w) -> finish()) try {
.show(); sha = json.getJSONObject("commit").getString("sha");
} catch (JSONException e) { String url = String.format(CDN_URL, sha, "canary.json");
finish(); request(url).getAsJSONObject(this::handleJSON);
} } catch (JSONException e) {
error(e);
} }
} }
private void handleJSON(JSONObject json) {
dialog.dismiss();
try {
apkLink = json.getJSONObject("app").getString("link");
if (!apkLink.startsWith("http"))
apkLink = String.format(CDN_URL, sha, apkLink);
new AlertDialog.Builder(themed)
.setCancelable(false)
.setTitle(APP_NAME)
.setMessage(getString(upgrade_msg))
.setPositiveButton(yes, (d, w) -> dlAPK())
.setNegativeButton(no, (d, w) -> finish())
.show();
} catch (JSONException e) {
error(e);
}
}
private void dlAPK() {
dialog = ProgressDialog.show(themed, getString(dling), getString(dling) + " " + APP_NAME, true);
// Download and upgrade the app
File apk = new File(getCacheDir(), "manager.apk");
request(apkLink).getAsFile(apk, file -> {
dialog.dismiss();
APKInstall.install(this, file);
finish();
});
}
} }