mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-11-15 15:23:28 +00:00
CallbackHandler to manage asyncs
This commit is contained in:
@@ -12,7 +12,9 @@ import android.support.v7.app.AlertDialog;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.topjohnwu.magisk.ModulesFragment;
|
||||
import com.topjohnwu.magisk.R;
|
||||
import com.topjohnwu.magisk.ReposFragment;
|
||||
import com.topjohnwu.magisk.StatusFragment;
|
||||
|
||||
import org.json.JSONException;
|
||||
@@ -48,12 +50,6 @@ public class Async {
|
||||
|
||||
public static class CheckUpdates extends NormalTask<Void, Void, Void> {
|
||||
|
||||
private SharedPreferences mPrefs;
|
||||
|
||||
public CheckUpdates(SharedPreferences prefs) {
|
||||
mPrefs = prefs;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
String jsonStr = WebRequest.makeWebServiceCall(UPDATE_JSON, WebRequest.GET);
|
||||
@@ -61,37 +57,33 @@ public class Async {
|
||||
JSONObject json = new JSONObject(jsonStr);
|
||||
|
||||
JSONObject magisk = json.getJSONObject("magisk");
|
||||
JSONObject app = json.getJSONObject("app");
|
||||
|
||||
StatusFragment.remoteMagiskVersion = magisk.getDouble("versionCode");
|
||||
StatusFragment.magiskLink = magisk.getString("link");
|
||||
StatusFragment.magiskChangelog = magisk.getString("changelog");
|
||||
|
||||
StatusFragment.remoteAppVersion = app.getString("version");
|
||||
StatusFragment.remoteAppVersionCode = app.getInt("versionCode");
|
||||
StatusFragment.appLink = app.getString("link");
|
||||
StatusFragment.appChangelog = app.getString("changelog");
|
||||
|
||||
} catch (JSONException ignored) {
|
||||
Logger.dev("JSON error!");
|
||||
}
|
||||
} catch (JSONException ignored) {}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void v) {
|
||||
mPrefs.edit().putBoolean("update_check_done", true).apply();
|
||||
CallbackHandler.triggerCallback(StatusFragment.updateCheckDone);
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkSafetyNet(Context context) {
|
||||
new SafetyNetHelper(context) {
|
||||
@Override
|
||||
public void handleResults(int i) {
|
||||
StatusFragment.SNCheckResult = i;
|
||||
CallbackHandler.triggerCallback(StatusFragment.safetyNetDone);
|
||||
}
|
||||
}.requestTest();
|
||||
}
|
||||
|
||||
public static class LoadModules extends RootTask<Void, Void, Void> {
|
||||
|
||||
private SharedPreferences mPrefs;
|
||||
|
||||
public LoadModules(SharedPreferences prefs) {
|
||||
mPrefs = prefs;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
ModuleHelper.createModuleMap();
|
||||
@@ -100,7 +92,7 @@ public class Async {
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void v) {
|
||||
mPrefs.edit().putBoolean("module_done", true).apply();
|
||||
CallbackHandler.triggerCallback(ModulesFragment.moduleLoadDone);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,8 +112,7 @@ public class Async {
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void v) {
|
||||
PreferenceManager.getDefaultSharedPreferences(mContext).edit()
|
||||
.putBoolean("repo_done", true).apply();
|
||||
CallbackHandler.triggerCallback(ReposFragment.repoLoadDone);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,7 +257,7 @@ public class Async {
|
||||
protected void done() {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
||||
prefs.edit().putBoolean("module_done", false).putBoolean("update_check_done", true).apply();
|
||||
new LoadModules(prefs).exec();
|
||||
new LoadModules().exec();
|
||||
|
||||
AlertDialog.Builder builder;
|
||||
String theme = prefs.getString("theme", "");
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.topjohnwu.magisk.utils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class CallbackHandler {
|
||||
|
||||
private static HashMap<Event, HashSet<EventListener>> listeners = new HashMap<>();
|
||||
|
||||
public static void register(Event event, EventListener listener) {
|
||||
HashSet<EventListener> list = listeners.get(event);
|
||||
if (list == null) {
|
||||
list = new HashSet<>();
|
||||
listeners.put(event, list);
|
||||
}
|
||||
list.add(listener);
|
||||
}
|
||||
|
||||
public static void unRegister(Event event, EventListener listener) {
|
||||
HashSet<EventListener> list = listeners.get(event);
|
||||
if (list != null) {
|
||||
list.remove(listener);
|
||||
}
|
||||
}
|
||||
|
||||
public static void triggerCallback(Event event) {
|
||||
event.isTriggered = true;
|
||||
HashSet<EventListener> list = listeners.get(event);
|
||||
if (list != null) {
|
||||
for (EventListener listener : list) {
|
||||
listener.onTrigger(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class Event {
|
||||
public boolean isTriggered = false;
|
||||
}
|
||||
|
||||
public interface EventListener {
|
||||
void onTrigger(Event event);
|
||||
}
|
||||
}
|
||||
@@ -20,11 +20,9 @@ public abstract class SafetyNetHelper
|
||||
implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
|
||||
|
||||
private GoogleApiClient mGoogleApiClient;
|
||||
protected Context mContext;
|
||||
|
||||
public SafetyNetHelper(Context context) {
|
||||
mContext = context;
|
||||
mGoogleApiClient = new GoogleApiClient.Builder(mContext)
|
||||
mGoogleApiClient = new GoogleApiClient.Builder(context)
|
||||
.addApi(SafetyNet.API)
|
||||
.addConnectionCallbacks(this)
|
||||
.addOnConnectionFailedListener(this)
|
||||
|
||||
Reference in New Issue
Block a user