From 8e7b8825f5f75561c4fa8072cc55013f81a13d0c Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 4 Aug 2017 00:12:11 +0800 Subject: [PATCH] Rename callbackevents to topic/subscribers --- .../com/topjohnwu/magisk/MagiskFragment.java | 18 ++--- .../topjohnwu/magisk/MagiskHideFragment.java | 10 +-- .../com/topjohnwu/magisk/MagiskManager.java | 20 +++--- .../com/topjohnwu/magisk/MainActivity.java | 10 +-- .../com/topjohnwu/magisk/ModulesFragment.java | 10 +-- .../com/topjohnwu/magisk/ReposFragment.java | 10 +-- .../topjohnwu/magisk/SettingsActivity.java | 26 +++---- .../magisk/adapters/ApplicationAdapter.java | 6 +- .../topjohnwu/magisk/asyncs/CheckUpdates.java | 2 +- .../topjohnwu/magisk/asyncs/LoadModules.java | 2 +- .../topjohnwu/magisk/asyncs/UpdateRepos.java | 2 +- .../topjohnwu/magisk/components/Activity.java | 10 +-- .../topjohnwu/magisk/components/Fragment.java | 10 +-- .../topjohnwu/magisk/utils/CallbackEvent.java | 68 ------------------- .../com/topjohnwu/magisk/utils/Topic.java | 68 +++++++++++++++++++ .../com/topjohnwu/magisk/utils/Utils.java | 2 +- 16 files changed, 137 insertions(+), 137 deletions(-) delete mode 100644 app/src/main/java/com/topjohnwu/magisk/utils/CallbackEvent.java create mode 100644 app/src/main/java/com/topjohnwu/magisk/utils/Topic.java diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java b/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java index b80ab9ca3..c94e1fd3b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java @@ -31,8 +31,8 @@ import com.topjohnwu.magisk.components.AlertDialogBuilder; import com.topjohnwu.magisk.components.Fragment; import com.topjohnwu.magisk.components.SnackbarMaker; import com.topjohnwu.magisk.receivers.DownloadReceiver; -import com.topjohnwu.magisk.utils.CallbackEvent; import com.topjohnwu.magisk.utils.Shell; +import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Utils; import java.io.File; @@ -49,7 +49,7 @@ import butterknife.OnClick; import butterknife.Unbinder; public class MagiskFragment extends Fragment - implements CallbackEvent.Listener, SwipeRefreshLayout.OnRefreshListener { + implements Topic.Subscriber, SwipeRefreshLayout.OnRefreshListener { public static final String SHOW_DIALOG = "dialog"; @@ -268,8 +268,8 @@ public class MagiskFragment extends Fragment safetyNetStatusText.setText(R.string.safetyNet_check_text); - magiskManager.safetyNetDone.isTriggered = false; - magiskManager.updateCheckDone.isTriggered = false; + magiskManager.safetyNetDone.hasPublished = false; + magiskManager.updateCheckDone.hasPublished = false; magiskManager.remoteMagiskVersionString = null; magiskManager.remoteMagiskVersionCode = -1; collapse(); @@ -283,17 +283,17 @@ public class MagiskFragment extends Fragment } @Override - public void onTrigger(CallbackEvent event) { - if (event == magiskManager.updateCheckDone) { + public void onTopicPublished(Topic topic) { + if (topic == magiskManager.updateCheckDone) { updateCheckUI(); - } else if (event == magiskManager.safetyNetDone) { + } else if (topic == magiskManager.safetyNetDone) { updateSafetyNetUI(); } } @Override - public CallbackEvent[] getRegisterEvents() { - return new CallbackEvent[] { magiskManager.updateCheckDone, magiskManager.safetyNetDone }; + public Topic[] getSubscription() { + return new Topic[] { magiskManager.updateCheckDone, magiskManager.safetyNetDone }; } @Override diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java b/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java index 59f616ca6..336e63862 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java @@ -13,13 +13,13 @@ import android.widget.SearchView; import com.topjohnwu.magisk.adapters.ApplicationAdapter; import com.topjohnwu.magisk.components.Fragment; -import com.topjohnwu.magisk.utils.CallbackEvent; +import com.topjohnwu.magisk.utils.Topic; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.Unbinder; -public class MagiskHideFragment extends Fragment implements CallbackEvent.Listener { +public class MagiskHideFragment extends Fragment implements Topic.Subscriber { private Unbinder unbinder; @BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout; @@ -84,13 +84,13 @@ public class MagiskHideFragment extends Fragment implements CallbackEvent.Listen } @Override - public void onTrigger(CallbackEvent event) { + public void onTopicPublished(Topic topic) { mSwipeRefreshLayout.setRefreshing(false); appAdapter.filter(lastFilter); } @Override - public CallbackEvent[] getRegisterEvents() { - return new CallbackEvent[] { getApplication().magiskHideDone }; + public Topic[] getSubscription() { + return new Topic[] { getApplication().magiskHideDone }; } } diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java index 6effd5c77..c56f2364a 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java @@ -16,9 +16,9 @@ import com.topjohnwu.magisk.asyncs.ParallelTask; import com.topjohnwu.magisk.database.RepoDatabaseHelper; import com.topjohnwu.magisk.database.SuDatabaseHelper; import com.topjohnwu.magisk.module.Module; -import com.topjohnwu.magisk.utils.CallbackEvent; import com.topjohnwu.magisk.utils.SafetyNetHelper; import com.topjohnwu.magisk.utils.Shell; +import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Utils; import java.io.File; @@ -43,14 +43,14 @@ public class MagiskManager extends Application { public static final String BUSYBOX_ARM = "https://github.com/topjohnwu/ndk-busybox/releases/download/1.27.1/busybox-arm"; public static final String BUSYBOX_X86 = "https://github.com/topjohnwu/ndk-busybox/releases/download/1.27.1/busybox-x86"; - // Events - public final CallbackEvent magiskHideDone = new CallbackEvent(); - public final CallbackEvent reloadActivity = new CallbackEvent(); - public final CallbackEvent moduleLoadDone = new CallbackEvent(); - public final CallbackEvent repoLoadDone = new CallbackEvent(); - public final CallbackEvent updateCheckDone = new CallbackEvent(); - public final CallbackEvent safetyNetDone = new CallbackEvent(); - public final CallbackEvent localeDone = new CallbackEvent(); + // Topics + public final Topic magiskHideDone = new Topic(); + public final Topic reloadActivity = new Topic(); + public final Topic moduleLoadDone = new Topic(); + public final Topic repoLoadDone = new Topic(); + public final Topic updateCheckDone = new Topic(); + public final Topic safetyNetDone = new Topic(); + public final Topic localeDone = new Topic(); // Info public String magiskVersionString; @@ -113,7 +113,7 @@ public class MagiskManager extends Application { @Override protected void onPostExecute(Void aVoid) { - getMagiskManager().localeDone.trigger(); + getMagiskManager().localeDone.publish(); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java index 7c610ee89..906999fe7 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java @@ -20,15 +20,15 @@ import android.view.MenuItem; import android.view.View; import com.topjohnwu.magisk.components.Activity; -import com.topjohnwu.magisk.utils.CallbackEvent; import com.topjohnwu.magisk.utils.Shell; +import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Utils; import butterknife.BindView; import butterknife.ButterKnife; public class MainActivity extends Activity - implements NavigationView.OnNavigationItemSelectedListener, CallbackEvent.Listener { + implements NavigationView.OnNavigationItemSelectedListener, Topic.Subscriber { private final Handler mDrawerHandler = new Handler(); private SharedPreferences prefs; @@ -110,13 +110,13 @@ public class MainActivity extends Activity } @Override - public void onTrigger(CallbackEvent event) { + public void onTopicPublished(Topic topic) { recreate(); } @Override - public CallbackEvent[] getRegisterEvents() { - return new CallbackEvent[] { getApplicationContext().reloadActivity }; + public Topic[] getSubscription() { + return new Topic[] { getApplicationContext().reloadActivity }; } public void checkHideSection() { diff --git a/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java b/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java index 266108527..fc0879a14 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java @@ -16,8 +16,8 @@ import com.topjohnwu.magisk.adapters.ModulesAdapter; import com.topjohnwu.magisk.asyncs.LoadModules; import com.topjohnwu.magisk.components.Fragment; import com.topjohnwu.magisk.module.Module; -import com.topjohnwu.magisk.utils.CallbackEvent; import com.topjohnwu.magisk.utils.Logger; +import com.topjohnwu.magisk.utils.Topic; import java.util.ArrayList; import java.util.List; @@ -26,7 +26,7 @@ import butterknife.BindView; import butterknife.ButterKnife; import butterknife.Unbinder; -public class ModulesFragment extends Fragment implements CallbackEvent.Listener { +public class ModulesFragment extends Fragment implements Topic.Subscriber { private static final int FETCH_ZIP_CODE = 2; @@ -73,14 +73,14 @@ public class ModulesFragment extends Fragment implements CallbackEvent.Listener } @Override - public void onTrigger(CallbackEvent event) { + public void onTopicPublished(Topic topic) { Logger.dev("ModulesFragment: UI refresh triggered"); updateUI(); } @Override - public CallbackEvent[] getRegisterEvents() { - return new CallbackEvent[] { getApplication().moduleLoadDone }; + public Topic[] getSubscription() { + return new Topic[] { getApplication().moduleLoadDone }; } @Override diff --git a/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java b/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java index 9fea50d9e..7e5bc51c9 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java @@ -15,14 +15,14 @@ import android.widget.TextView; import com.topjohnwu.magisk.adapters.ReposAdapter; import com.topjohnwu.magisk.asyncs.UpdateRepos; import com.topjohnwu.magisk.components.Fragment; -import com.topjohnwu.magisk.utils.CallbackEvent; import com.topjohnwu.magisk.utils.Logger; +import com.topjohnwu.magisk.utils.Topic; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.Unbinder; -public class ReposFragment extends Fragment implements CallbackEvent.Listener { +public class ReposFragment extends Fragment implements Topic.Subscriber { private Unbinder unbinder; @BindView(R.id.recyclerView) RecyclerView recyclerView; @@ -59,7 +59,7 @@ public class ReposFragment extends Fragment implements CallbackEvent.Listener { } @Override - public void onTrigger(CallbackEvent event) { + public void onTopicPublished(Topic topic) { Logger.dev("ReposFragment: UI refresh triggered"); mSwipeRefreshLayout.setRefreshing(false); adapter.notifyDBChanged(); @@ -68,8 +68,8 @@ public class ReposFragment extends Fragment implements CallbackEvent.Listener { } @Override - public CallbackEvent[] getRegisterEvents() { - return new CallbackEvent[] { getApplication().repoLoadDone }; + public Topic[] getSubscription() { + return new Topic[] { getApplication().repoLoadDone }; } @Override diff --git a/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java b/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java index f2bc1f27c..e435bc81d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java @@ -15,9 +15,9 @@ import android.widget.Toast; import com.topjohnwu.magisk.components.Activity; import com.topjohnwu.magisk.database.SuDatabaseHelper; -import com.topjohnwu.magisk.utils.CallbackEvent; import com.topjohnwu.magisk.utils.Logger; import com.topjohnwu.magisk.utils.Shell; +import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Utils; import java.util.Locale; @@ -25,7 +25,7 @@ import java.util.Locale; import butterknife.BindView; import butterknife.ButterKnife; -public class SettingsActivity extends Activity implements CallbackEvent.Listener { +public class SettingsActivity extends Activity implements Topic.Subscriber { @BindView(R.id.toolbar) Toolbar toolbar; @@ -58,18 +58,18 @@ public class SettingsActivity extends Activity implements CallbackEvent.Listener } @Override - public void onTrigger(CallbackEvent event) { + public void onTopicPublished(Topic topic) { recreate(); } @Override - public CallbackEvent[] getRegisterEvents() { - return new CallbackEvent[] { getApplicationContext().reloadActivity }; + public Topic[] getSubscription() { + return new Topic[] { getApplicationContext().reloadActivity }; } public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener, - CallbackEvent.Listener { + Topic.Subscriber { private SharedPreferences prefs; private PreferenceScreen prefScreen; @@ -162,13 +162,13 @@ public class SettingsActivity extends Activity implements CallbackEvent.Listener public void onResume() { super.onResume(); prefs.registerOnSharedPreferenceChangeListener(this); - registerEvents(); + subscribeTopics(); } @Override public void onPause() { prefs.unregisterOnSharedPreferenceChangeListener(this); - unregisterEvents(); + unsubscribeTopics(); super.onPause(); } @@ -182,7 +182,7 @@ public class SettingsActivity extends Activity implements CallbackEvent.Listener enabled = prefs.getBoolean("dark_theme", false); if (magiskManager.isDarkTheme != enabled) { magiskManager.isDarkTheme = enabled; - magiskManager.reloadActivity.trigger(false); + magiskManager.reloadActivity.publish(false); } break; case "disable": @@ -243,7 +243,7 @@ public class SettingsActivity extends Activity implements CallbackEvent.Listener break; case "locale": magiskManager.setLocale(); - magiskManager.reloadActivity.trigger(false); + magiskManager.reloadActivity.publish(false); break; } setSummary(); @@ -265,13 +265,13 @@ public class SettingsActivity extends Activity implements CallbackEvent.Listener } @Override - public void onTrigger(CallbackEvent event) { + public void onTopicPublished(Topic topic) { setLocalePreference((ListPreference) findPreference("locale")); } @Override - public CallbackEvent[] getRegisterEvents() { - return new CallbackEvent[] { magiskManager.localeDone }; + public Topic[] getSubscription() { + return new Topic[] { magiskManager.localeDone }; } } diff --git a/app/src/main/java/com/topjohnwu/magisk/adapters/ApplicationAdapter.java b/app/src/main/java/com/topjohnwu/magisk/adapters/ApplicationAdapter.java index ac3d4b4a5..6ea4ec9e9 100644 --- a/app/src/main/java/com/topjohnwu/magisk/adapters/ApplicationAdapter.java +++ b/app/src/main/java/com/topjohnwu/magisk/adapters/ApplicationAdapter.java @@ -16,8 +16,8 @@ import android.widget.TextView; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.asyncs.ParallelTask; import com.topjohnwu.magisk.components.SnackbarMaker; -import com.topjohnwu.magisk.utils.CallbackEvent; import com.topjohnwu.magisk.utils.Shell; +import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Utils; import java.util.ArrayList; @@ -46,7 +46,7 @@ public class ApplicationAdapter extends RecyclerView.Adapter mHideList; private PackageManager pm; private ApplicationFilter filter; - private CallbackEvent magiskHideDone; + private Topic magiskHideDone; private Shell shell; public ApplicationAdapter(Context context) { @@ -169,7 +169,7 @@ public class ApplicationAdapter extends RecyclerView.Adapter { Utils.showMagiskUpdate(magiskManager); } } - magiskManager.updateCheckDone.trigger(); + magiskManager.updateCheckDone.publish(); super.onPostExecute(v); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/LoadModules.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/LoadModules.java index 5234e3909..90957bfaf 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/LoadModules.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/LoadModules.java @@ -39,7 +39,7 @@ public class LoadModules extends ParallelTask { protected void onPostExecute(Void v) { MagiskManager magiskManager = getMagiskManager(); if (magiskManager == null) return; - magiskManager.moduleLoadDone.trigger(); + magiskManager.moduleLoadDone.publish(); super.onPostExecute(v); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/UpdateRepos.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/UpdateRepos.java index 58723080d..655cbf420 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/UpdateRepos.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/UpdateRepos.java @@ -186,7 +186,7 @@ public class UpdateRepos extends ParallelTask { protected void onPostExecute(Void v) { MagiskManager magiskManager = getMagiskManager(); if (magiskManager == null) return; - magiskManager.repoLoadDone.trigger(); + magiskManager.repoLoadDone.publish(); super.onPostExecute(v); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/components/Activity.java b/app/src/main/java/com/topjohnwu/magisk/components/Activity.java index 41f4c9409..0b410486b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/components/Activity.java +++ b/app/src/main/java/com/topjohnwu/magisk/components/Activity.java @@ -8,7 +8,7 @@ import android.view.WindowManager; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; -import com.topjohnwu.magisk.utils.CallbackEvent; +import com.topjohnwu.magisk.utils.Topic; public class Activity extends AppCompatActivity { @@ -22,15 +22,15 @@ public class Activity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if (this instanceof CallbackEvent.Listener) { - ((CallbackEvent.Listener) this).registerEvents(); + if (this instanceof Topic.Subscriber) { + ((Topic.Subscriber) this).subscribeTopics(); } } @Override protected void onDestroy() { - if (this instanceof CallbackEvent.Listener) { - ((CallbackEvent.Listener) this).unregisterEvents(); + if (this instanceof Topic.Subscriber) { + ((Topic.Subscriber) this).unsubscribeTopics(); } super.onDestroy(); } diff --git a/app/src/main/java/com/topjohnwu/magisk/components/Fragment.java b/app/src/main/java/com/topjohnwu/magisk/components/Fragment.java index 8e7fba06d..74df99c32 100644 --- a/app/src/main/java/com/topjohnwu/magisk/components/Fragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/components/Fragment.java @@ -1,7 +1,7 @@ package com.topjohnwu.magisk.components; import com.topjohnwu.magisk.MagiskManager; -import com.topjohnwu.magisk.utils.CallbackEvent; +import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Utils; public class Fragment extends android.support.v4.app.Fragment { @@ -13,15 +13,15 @@ public class Fragment extends android.support.v4.app.Fragment { @Override public void onResume() { super.onResume(); - if (this instanceof CallbackEvent.Listener) { - ((CallbackEvent.Listener) this).registerEvents(); + if (this instanceof Topic.Subscriber) { + ((Topic.Subscriber) this).subscribeTopics(); } } @Override public void onPause() { - if (this instanceof CallbackEvent.Listener) { - ((CallbackEvent.Listener) this).unregisterEvents(); + if (this instanceof Topic.Subscriber) { + ((Topic.Subscriber) this).unsubscribeTopics(); } super.onPause(); } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/CallbackEvent.java b/app/src/main/java/com/topjohnwu/magisk/utils/CallbackEvent.java deleted file mode 100644 index f7c5fe9a0..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/utils/CallbackEvent.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.topjohnwu.magisk.utils; - -import java.lang.ref.WeakReference; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; - -public class CallbackEvent { - - public boolean isTriggered = false; - private List> listeners; - - public void register(Listener l) { - if (listeners == null) { - listeners = new LinkedList<>(); - } - listeners.add(new WeakReference<>(l)); - } - - public void unregister() { - listeners = null; - } - - public void unregister(Listener l) { - for (Iterator> i = listeners.iterator(); i.hasNext();) { - WeakReference listener = i.next(); - if (listener.get() == null || listener.get() == l) { - i.remove(); - } - } - } - - public void trigger() { - trigger(true); - } - - public void trigger(boolean b) { - isTriggered = b; - if (listeners != null) { - for (WeakReference listener : listeners) { - if (listener.get() != null) - listener.get().onTrigger(this); - } - } - } - - - public interface Listener { - default void registerEvents() { - for (CallbackEvent event : getRegisterEvents()) { - if (event.isTriggered) { - onTrigger(event); - } - event.register(this); - } - } - default void unregisterEvents() { - for (CallbackEvent event : getRegisterEvents()) { - event.unregister(this); - } - } - default void onTrigger() { - onTrigger(null); - } - void onTrigger(CallbackEvent event); - CallbackEvent[] getRegisterEvents(); - } -} diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Topic.java b/app/src/main/java/com/topjohnwu/magisk/utils/Topic.java new file mode 100644 index 000000000..515dcd9b7 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Topic.java @@ -0,0 +1,68 @@ +package com.topjohnwu.magisk.utils; + +import java.lang.ref.WeakReference; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +public class Topic { + + public boolean hasPublished = false; + private List> subscribers; + + public void subscribe(Subscriber sub) { + if (subscribers == null) { + subscribers = new LinkedList<>(); + } + subscribers.add(new WeakReference<>(sub)); + } + + public void unsubscribe() { + subscribers = null; + } + + public void unsubscribe(Subscriber sub) { + for (Iterator> i = subscribers.iterator(); i.hasNext();) { + WeakReference subscriber = i.next(); + if (subscriber.get() == null || subscriber.get() == sub) { + i.remove(); + } + } + } + + public void publish() { + publish(true); + } + + public void publish(boolean b) { + hasPublished = b; + if (subscribers != null) { + for (WeakReference subscriber : subscribers) { + if (subscriber.get() != null) + subscriber.get().onTopicPublished(this); + } + } + } + + + public interface Subscriber { + default void subscribeTopics() { + for (Topic topic : getSubscription()) { + if (topic.hasPublished) { + onTopicPublished(topic); + } + topic.subscribe(this); + } + } + default void unsubscribeTopics() { + for (Topic event : getSubscription()) { + event.unsubscribe(this); + } + } + default void onTopicPublished() { + onTopicPublished(null); + } + void onTopicPublished(Topic topic); + Topic[] getSubscription(); + } +} diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java index 2ce645402..61e4d854e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -140,7 +140,7 @@ public class Utils { @Override public void handleResults(Result result) { getMagiskManager(mActivity).SNCheckResult = result; - getMagiskManager(mActivity).safetyNetDone.trigger(); + getMagiskManager(mActivity).safetyNetDone.publish(); } }.requestTest(); }