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; 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.BuildConfig;
import com.topjohnwu.magisk.Const; import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.Data; import com.topjohnwu.magisk.Data;
@ -42,27 +43,38 @@ public class CheckUpdates {
} }
} }
public static void fetchUpdates() { public static void check(Runnable cb) {
String jsonStr = ""; String url;
switch (Data.updateChannel) { switch (Data.updateChannel) {
case Const.Value.STABLE_CHANNEL: case Const.Value.STABLE_CHANNEL:
jsonStr = Utils.dlString(Const.Url.STABLE_URL); url = Const.Url.STABLE_URL;
break; break;
case Const.Value.BETA_CHANNEL: case Const.Value.BETA_CHANNEL:
jsonStr = Utils.dlString(Const.Url.BETA_URL); url = Const.Url.BETA_URL;
break; break;
case Const.Value.CUSTOM_CHANNEL: 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; break;
} default:
JSONObject json;
try {
json = new JSONObject(jsonStr);
} catch (JSONException e) {
return; 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"); JSONObject magisk = getJson(json, "magisk");
Data.remoteMagiskVersionString = getString(magisk, "version", null); Data.remoteMagiskVersionString = getString(magisk, "version", null);
Data.remoteMagiskVersionCode = getInt(magisk, "versionCode", -1); Data.remoteMagiskVersionCode = getInt(magisk, "versionCode", -1);
@ -82,11 +94,7 @@ public class CheckUpdates {
JSONObject snet = getJson(json, "snet"); JSONObject snet = getJson(json, "snet");
Data.snetVersionCode = getInt(snet, "versionCode", -1); Data.snetVersionCode = getInt(snet, "versionCode", -1);
Data.snetLink = getString(snet, "link", null); Data.snetLink = getString(snet, "link", null);
}
public static void check(Runnable cb) {
AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> {
fetchUpdates();
if (cb != null) { if (cb != null) {
if (BuildConfig.VERSION_CODE < Data.remoteManagerVersionCode) { if (BuildConfig.VERSION_CODE < Data.remoteManagerVersionCode) {
Notifications.managerUpdate(); Notifications.managerUpdate();
@ -96,10 +104,9 @@ public class CheckUpdates {
cb.run(); cb.run();
} }
Topic.publish(Topic.UPDATE_CHECK_DONE); Topic.publish(Topic.UPDATE_CHECK_DONE);
});
} }
public static void check() { @Override
check(null); public void onError(ANError anError) {}
} }
} }