Update CheckUpdate

This commit is contained in:
topjohnwu 2018-12-03 10:05:33 -05:00
parent 7b04386162
commit 86d8b50547

View File

@ -1,7 +1,8 @@
package com.topjohnwu.magisk.asyncs;
import android.os.AsyncTask;
import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.JSONObjectRequestListener;
import com.topjohnwu.magisk.BuildConfig;
import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.Data;
@ -42,27 +43,38 @@ public class CheckUpdates {
}
}
public static void fetchUpdates() {
String jsonStr = "";
public static void check(Runnable cb) {
String url;
switch (Data.updateChannel) {
case Const.Value.STABLE_CHANNEL:
jsonStr = Utils.dlString(Const.Url.STABLE_URL);
url = Const.Url.STABLE_URL;
break;
case Const.Value.BETA_CHANNEL:
jsonStr = Utils.dlString(Const.Url.BETA_URL);
url = Const.Url.BETA_URL;
break;
case Const.Value.CUSTOM_CHANNEL:
jsonStr = Utils.dlString(Data.MM().prefs.getString(Const.Key.CUSTOM_CHANNEL, ""));
url = Data.MM().prefs.getString(Const.Key.CUSTOM_CHANNEL, "");
break;
}
JSONObject json;
try {
json = new JSONObject(jsonStr);
} catch (JSONException e) {
default:
return;
}
AndroidNetworking.get(url).build().getAsJSONObject(new UpdateListener(cb));
}
public static void check() {
check(null);
}
private static class UpdateListener implements JSONObjectRequestListener {
private Runnable cb;
UpdateListener(Runnable callback) {
cb = callback;
}
@Override
public void onResponse(JSONObject json) {
JSONObject magisk = getJson(json, "magisk");
Data.remoteMagiskVersionString = getString(magisk, "version", null);
Data.remoteMagiskVersionCode = getInt(magisk, "versionCode", -1);
@ -82,11 +94,7 @@ public class CheckUpdates {
JSONObject snet = getJson(json, "snet");
Data.snetVersionCode = getInt(snet, "versionCode", -1);
Data.snetLink = getString(snet, "link", null);
}
public static void check(Runnable cb) {
AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> {
fetchUpdates();
if (cb != null) {
if (BuildConfig.VERSION_CODE < Data.remoteManagerVersionCode) {
Notifications.managerUpdate();
@ -96,10 +104,9 @@ public class CheckUpdates {
cb.run();
}
Topic.publish(Topic.UPDATE_CHECK_DONE);
});
}
public static void check() {
check(null);
@Override
public void onError(ANError anError) {}
}
}