From 8ac3aaf36c58be549275cfbc424ec2b8070ead27 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 23 Mar 2019 21:58:42 -0400 Subject: [PATCH] Rename Topic -> Event --- .../com/topjohnwu/magisk/MainActivity.java | 8 +- .../magisk/adapters/ApplicationAdapter.java | 4 +- .../magisk/components/BaseActivity.java | 12 +- .../magisk/components/BaseFragment.java | 12 +- .../components/BasePreferenceFragment.java | 10 +- .../magisk/fragments/MagiskFragment.java | 13 +- .../magisk/fragments/MagiskHideFragment.java | 10 +- .../magisk/fragments/ModulesFragment.java | 12 +- .../magisk/fragments/ReposFragment.java | 23 ++-- .../magisk/fragments/SettingsFragment.java | 10 +- .../topjohnwu/magisk/tasks/CheckUpdates.java | 4 +- .../topjohnwu/magisk/tasks/UpdateRepos.java | 6 +- .../com/topjohnwu/magisk/utils/Event.java | 123 ++++++++++++++++++ .../topjohnwu/magisk/utils/LocaleManager.java | 2 +- .../com/topjohnwu/magisk/utils/Topic.java | 108 --------------- .../com/topjohnwu/magisk/utils/Utils.java | 4 +- 16 files changed, 183 insertions(+), 178 deletions(-) create mode 100644 app/src/main/java/com/topjohnwu/magisk/utils/Event.java delete mode 100644 app/src/main/java/com/topjohnwu/magisk/utils/Topic.java diff --git a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java index 9b4a8082f..a9ca5ddca 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java @@ -24,7 +24,6 @@ import com.topjohnwu.magisk.fragments.ModulesFragment; import com.topjohnwu.magisk.fragments.ReposFragment; import com.topjohnwu.magisk.fragments.SettingsFragment; import com.topjohnwu.magisk.fragments.SuperuserFragment; -import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.net.Networking; import com.topjohnwu.superuser.Shell; @@ -32,7 +31,7 @@ import com.topjohnwu.superuser.Shell; import butterknife.BindView; public class MainActivity extends BaseActivity - implements NavigationView.OnNavigationItemSelectedListener, Topic.Subscriber { + implements NavigationView.OnNavigationItemSelectedListener { private final Handler mDrawerHandler = new Handler(); private int mDrawerItem; @@ -116,11 +115,6 @@ public class MainActivity extends BaseActivity return true; } - @Override - public void onPublish(int topic, Object[] result) { - recreate(); - } - public void checkHideSection() { Menu menu = navigationView.getMenu(); menu.findItem(R.id.magiskhide).setVisible(Shell.rootAccess() && 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 48fd82378..ec6159dd7 100644 --- a/app/src/main/java/com/topjohnwu/magisk/adapters/ApplicationAdapter.java +++ b/app/src/main/java/com/topjohnwu/magisk/adapters/ApplicationAdapter.java @@ -25,7 +25,7 @@ import com.topjohnwu.magisk.Config; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.uicomponents.ArrowExpandable; import com.topjohnwu.magisk.uicomponents.Expandable; -import com.topjohnwu.magisk.utils.Topic; +import com.topjohnwu.magisk.utils.Event; import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.internal.UiThreadHandler; @@ -257,7 +257,7 @@ public class ApplicationAdapter extends SectionedAdapter }).filter(Objects::nonNull).sorted() .collect(Collectors.toList()); - Topic.publish(false, Topic.MAGISK_HIDE_DONE); + Event.trigger(false, Event.MAGISK_HIDE_DONE); } // True if not system app or user already hidden it diff --git a/app/src/main/java/com/topjohnwu/magisk/components/BaseActivity.java b/app/src/main/java/com/topjohnwu/magisk/components/BaseActivity.java index 672bd0933..5bd8a6628 100644 --- a/app/src/main/java/com/topjohnwu/magisk/components/BaseActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/components/BaseActivity.java @@ -22,10 +22,10 @@ import com.topjohnwu.magisk.App; import com.topjohnwu.magisk.Config; import com.topjohnwu.magisk.Const; import com.topjohnwu.magisk.R; +import com.topjohnwu.magisk.utils.Event; import com.topjohnwu.magisk.utils.LocaleManager; -import com.topjohnwu.magisk.utils.Topic; -public abstract class BaseActivity extends AppCompatActivity implements Topic.AutoSubscriber { +public abstract class BaseActivity extends AppCompatActivity implements Event.AutoListener { public static final String INTENT_PERM = "perm_dialog"; private static Runnable grantCallback; @@ -40,12 +40,12 @@ public abstract class BaseActivity extends AppCompatActivity implements Topic.Au } @Override - public int[] getSubscribedTopics() { + public int[] getListeningEvents() { return EMPTY_INT_ARRAY; } @Override - public void onPublish(int topic, Object[] result) {} + public void onEvent(int event) {} @StyleRes public int getDarkTheme() { @@ -59,7 +59,7 @@ public abstract class BaseActivity extends AppCompatActivity implements Topic.Au @Override protected void onCreate(@Nullable Bundle savedInstanceState) { - Topic.subscribe(this); + Event.register(this); if (getDarkTheme() != -1 && (boolean) Config.get(Config.Key.DARK_THEME)) { setTheme(getDarkTheme()); } @@ -71,7 +71,7 @@ public abstract class BaseActivity extends AppCompatActivity implements Topic.Au @Override protected void onDestroy() { - Topic.unsubscribe(this); + Event.unregister(this); super.onDestroy(); } diff --git a/app/src/main/java/com/topjohnwu/magisk/components/BaseFragment.java b/app/src/main/java/com/topjohnwu/magisk/components/BaseFragment.java index d3fa9a887..fa564295d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/components/BaseFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/components/BaseFragment.java @@ -5,11 +5,11 @@ import android.content.Intent; import androidx.fragment.app.Fragment; import com.topjohnwu.magisk.App; -import com.topjohnwu.magisk.utils.Topic; +import com.topjohnwu.magisk.utils.Event; import butterknife.Unbinder; -public abstract class BaseFragment extends Fragment implements Topic.AutoSubscriber { +public abstract class BaseFragment extends Fragment implements Event.AutoListener { public App app = App.self; protected Unbinder unbinder = null; @@ -17,12 +17,12 @@ public abstract class BaseFragment extends Fragment implements Topic.AutoSubscri @Override public void onResume() { super.onResume(); - Topic.subscribe(this); + Event.register(this); } @Override public void onPause() { - Topic.unsubscribe(this); + Event.unregister(this); super.onPause(); } @@ -47,10 +47,10 @@ public abstract class BaseFragment extends Fragment implements Topic.AutoSubscri } @Override - public int[] getSubscribedTopics() { + public int[] getListeningEvents() { return BaseActivity.EMPTY_INT_ARRAY; } @Override - public void onPublish(int topic, Object[] result) {} + public void onEvent(int event) {} } diff --git a/app/src/main/java/com/topjohnwu/magisk/components/BasePreferenceFragment.java b/app/src/main/java/com/topjohnwu/magisk/components/BasePreferenceFragment.java index 7e1cc85ff..f6512518f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/components/BasePreferenceFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/components/BasePreferenceFragment.java @@ -18,10 +18,10 @@ import androidx.recyclerview.widget.RecyclerView; import com.topjohnwu.magisk.App; import com.topjohnwu.magisk.R; -import com.topjohnwu.magisk.utils.Topic; +import com.topjohnwu.magisk.utils.Event; public abstract class BasePreferenceFragment extends PreferenceFragmentCompat - implements SharedPreferences.OnSharedPreferenceChangeListener, Topic.AutoSubscriber { + implements SharedPreferences.OnSharedPreferenceChangeListener, Event.AutoListener { public App app = App.self; @@ -29,19 +29,19 @@ public abstract class BasePreferenceFragment extends PreferenceFragmentCompat public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = super.onCreateView(inflater, container, savedInstanceState); app.prefs.registerOnSharedPreferenceChangeListener(this); - Topic.subscribe(this); + Event.register(this); return v; } @Override public void onDestroyView() { app.prefs.unregisterOnSharedPreferenceChangeListener(this); - Topic.unsubscribe(this); + Event.unregister(this); super.onDestroyView(); } @Override - public int[] getSubscribedTopics() { + public int[] getListeningEvents() { return BaseActivity.EMPTY_INT_ARRAY; } diff --git a/app/src/main/java/com/topjohnwu/magisk/fragments/MagiskFragment.java b/app/src/main/java/com/topjohnwu/magisk/fragments/MagiskFragment.java index 0f70a6857..bb8d11b2f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/fragments/MagiskFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/fragments/MagiskFragment.java @@ -38,7 +38,7 @@ import com.topjohnwu.magisk.uicomponents.MarkDownWindow; import com.topjohnwu.magisk.uicomponents.SafetyNet; import com.topjohnwu.magisk.uicomponents.UpdateCardHolder; import com.topjohnwu.magisk.utils.AppUtils; -import com.topjohnwu.magisk.utils.Topic; +import com.topjohnwu.magisk.utils.Event; import com.topjohnwu.net.Networking; import com.topjohnwu.superuser.Shell; @@ -48,8 +48,7 @@ import butterknife.BindColor; import butterknife.BindView; import butterknife.OnClick; -public class MagiskFragment extends BaseFragment - implements SwipeRefreshLayout.OnRefreshListener, Topic.Subscriber { +public class MagiskFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener { private static boolean shownDialog = false; @@ -195,7 +194,7 @@ public class MagiskFragment extends BaseFragment Config.loadMagiskInfo(); updateUI(); - Topic.reset(getSubscribedTopics()); + Event.reset(this); Config.remoteMagiskVersionString = null; Config.remoteMagiskVersionCode = -1; @@ -208,12 +207,12 @@ public class MagiskFragment extends BaseFragment } @Override - public int[] getSubscribedTopics() { - return new int[] {Topic.UPDATE_CHECK_DONE}; + public int[] getListeningEvents() { + return new int[] {Event.UPDATE_CHECK_DONE}; } @Override - public void onPublish(int topic, Object[] result) { + public void onEvent(int event) { updateCheckUI(); } diff --git a/app/src/main/java/com/topjohnwu/magisk/fragments/MagiskHideFragment.java b/app/src/main/java/com/topjohnwu/magisk/fragments/MagiskHideFragment.java index c7bae793a..1f31e27d2 100644 --- a/app/src/main/java/com/topjohnwu/magisk/fragments/MagiskHideFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/fragments/MagiskHideFragment.java @@ -18,11 +18,11 @@ import com.topjohnwu.magisk.Config; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.adapters.ApplicationAdapter; import com.topjohnwu.magisk.components.BaseFragment; -import com.topjohnwu.magisk.utils.Topic; +import com.topjohnwu.magisk.utils.Event; import butterknife.BindView; -public class MagiskHideFragment extends BaseFragment implements Topic.Subscriber { +public class MagiskHideFragment extends BaseFragment { @BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout; @BindView(R.id.recyclerView) RecyclerView recyclerView; @@ -89,12 +89,12 @@ public class MagiskHideFragment extends BaseFragment implements Topic.Subscriber } @Override - public int[] getSubscribedTopics() { - return new int[] {Topic.MAGISK_HIDE_DONE}; + public int[] getListeningEvents() { + return new int[] {Event.MAGISK_HIDE_DONE}; } @Override - public void onPublish(int topic, Object[] result) { + public void onEvent(int event) { mSwipeRefreshLayout.setRefreshing(false); adapter.filter(search.getQuery().toString()); } diff --git a/app/src/main/java/com/topjohnwu/magisk/fragments/ModulesFragment.java b/app/src/main/java/com/topjohnwu/magisk/fragments/ModulesFragment.java index 32188b283..768672fd0 100644 --- a/app/src/main/java/com/topjohnwu/magisk/fragments/ModulesFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/fragments/ModulesFragment.java @@ -24,7 +24,7 @@ import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.adapters.ModulesAdapter; import com.topjohnwu.magisk.components.BaseFragment; import com.topjohnwu.magisk.container.Module; -import com.topjohnwu.magisk.utils.Topic; +import com.topjohnwu.magisk.utils.Event; import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.superuser.Shell; @@ -35,7 +35,7 @@ import java.util.Map; import butterknife.BindView; import butterknife.OnClick; -public class ModulesFragment extends BaseFragment implements Topic.Subscriber { +public class ModulesFragment extends BaseFragment { @BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout; @BindView(R.id.recyclerView) RecyclerView recyclerView; @@ -82,13 +82,13 @@ public class ModulesFragment extends BaseFragment implements Topic.Subscriber { } @Override - public int[] getSubscribedTopics() { - return new int[] {Topic.MODULE_LOAD_DONE}; + public int[] getListeningEvents() { + return new int[] {Event.MODULE_LOAD_DONE}; } @Override - public void onPublish(int topic, Object[] result) { - updateUI((Map) result[0]); + public void onEvent(int event) { + updateUI(Event.getResult(event)); } @Override diff --git a/app/src/main/java/com/topjohnwu/magisk/fragments/ReposFragment.java b/app/src/main/java/com/topjohnwu/magisk/fragments/ReposFragment.java index 2ef28eabd..d415d860b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/fragments/ReposFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/fragments/ReposFragment.java @@ -20,15 +20,12 @@ import com.topjohnwu.magisk.Config; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.adapters.ReposAdapter; import com.topjohnwu.magisk.components.BaseFragment; -import com.topjohnwu.magisk.container.Module; import com.topjohnwu.magisk.tasks.UpdateRepos; -import com.topjohnwu.magisk.utils.Topic; - -import java.util.Map; +import com.topjohnwu.magisk.utils.Event; import butterknife.BindView; -public class ReposFragment extends BaseFragment implements Topic.Subscriber { +public class ReposFragment extends BaseFragment { @BindView(R.id.recyclerView) RecyclerView recyclerView; @BindView(R.id.empty_rv) TextView emptyRv; @@ -59,23 +56,23 @@ public class ReposFragment extends BaseFragment implements Topic.Subscriber { } @Override - public int[] getSubscribedTopics() { - return new int[] {Topic.MODULE_LOAD_DONE, Topic.REPO_LOAD_DONE}; + public int[] getListeningEvents() { + return new int[] {Event.MODULE_LOAD_DONE, Event.REPO_LOAD_DONE}; } @Override - public void onPublish(int topic, Object[] result) { - switch (topic) { - case Topic.MODULE_LOAD_DONE: - adapter = new ReposAdapter(app.repoDB, (Map) result[0]); + public void onEvent(int event) { + switch (event) { + case Event.MODULE_LOAD_DONE: + adapter = new ReposAdapter(app.repoDB, Event.getResult(event)); recyclerView.setAdapter(adapter); break; - case Topic.REPO_LOAD_DONE: + case Event.REPO_LOAD_DONE: if (adapter != null) adapter.notifyDBChanged(); break; } - if (Topic.isPublished(this)) { + if (Event.isTriggered(this)) { mSwipeRefreshLayout.setRefreshing(false); boolean empty = adapter.getItemCount() == 0; recyclerView.setVisibility(empty ? View.GONE : View.VISIBLE); diff --git a/app/src/main/java/com/topjohnwu/magisk/fragments/SettingsFragment.java b/app/src/main/java/com/topjohnwu/magisk/fragments/SettingsFragment.java index 58a46658b..94a2000bb 100644 --- a/app/src/main/java/com/topjohnwu/magisk/fragments/SettingsFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/fragments/SettingsFragment.java @@ -24,10 +24,10 @@ import com.topjohnwu.magisk.dialogs.FingerprintAuthDialog; import com.topjohnwu.magisk.tasks.CheckUpdates; import com.topjohnwu.magisk.utils.AppUtils; import com.topjohnwu.magisk.utils.DownloadApp; +import com.topjohnwu.magisk.utils.Event; import com.topjohnwu.magisk.utils.FingerprintHelper; import com.topjohnwu.magisk.utils.LocaleManager; import com.topjohnwu.magisk.utils.PatchAPK; -import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.net.Networking; import com.topjohnwu.superuser.Shell; @@ -36,7 +36,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.Locale; -public class SettingsFragment extends BasePreferenceFragment implements Topic.Subscriber { +public class SettingsFragment extends BasePreferenceFragment { private ListPreference updateChannel, autoRes, suNotification, requestTimeout, rootConfig, multiuserConfig, nsConfig; @@ -288,12 +288,12 @@ public class SettingsFragment extends BasePreferenceFragment implements Topic.Su } @Override - public void onPublish(int topic, Object[] result) { + public void onEvent(int event) { setLocalePreference((ListPreference) findPreference(Config.Key.LOCALE)); } @Override - public int[] getSubscribedTopics() { - return new int[] {Topic.LOCALE_FETCH_DONE}; + public int[] getListeningEvents() { + return new int[] {Event.LOCALE_FETCH_DONE}; } } diff --git a/app/src/main/java/com/topjohnwu/magisk/tasks/CheckUpdates.java b/app/src/main/java/com/topjohnwu/magisk/tasks/CheckUpdates.java index e26401e15..03e8dd92c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/tasks/CheckUpdates.java +++ b/app/src/main/java/com/topjohnwu/magisk/tasks/CheckUpdates.java @@ -4,7 +4,7 @@ import android.os.SystemClock; import com.topjohnwu.magisk.Config; import com.topjohnwu.magisk.Const; -import com.topjohnwu.magisk.utils.Topic; +import com.topjohnwu.magisk.utils.Event; import com.topjohnwu.net.Networking; import com.topjohnwu.net.Request; import com.topjohnwu.net.ResponseListener; @@ -104,7 +104,7 @@ public class CheckUpdates { JSONObject uninstaller = getJson(json, "uninstaller"); Config.uninstallerLink = getString(uninstaller, "link", null); - UiThreadHandler.handler.postAtTime(() -> Topic.publish(Topic.UPDATE_CHECK_DONE), + UiThreadHandler.handler.postAtTime(() -> Event.trigger(Event.UPDATE_CHECK_DONE), start + 1000 /* Add artificial delay to let UI behave correctly */); if (cb != null) diff --git a/app/src/main/java/com/topjohnwu/magisk/tasks/UpdateRepos.java b/app/src/main/java/com/topjohnwu/magisk/tasks/UpdateRepos.java index c348e21c6..d13dd6546 100644 --- a/app/src/main/java/com/topjohnwu/magisk/tasks/UpdateRepos.java +++ b/app/src/main/java/com/topjohnwu/magisk/tasks/UpdateRepos.java @@ -7,8 +7,8 @@ import com.topjohnwu.magisk.App; import com.topjohnwu.magisk.Config; import com.topjohnwu.magisk.Const; import com.topjohnwu.magisk.container.Repo; +import com.topjohnwu.magisk.utils.Event; import com.topjohnwu.magisk.utils.Logger; -import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.net.Networking; import com.topjohnwu.net.Request; @@ -155,7 +155,7 @@ public class UpdateRepos { } public void exec(boolean force) { - Topic.reset(Topic.REPO_LOAD_DONE); + Event.reset(Event.REPO_LOAD_DONE); App.THREAD_POOL.execute(() -> { cached = Collections.synchronizedSet(app.repoDB.getRepoIDSet()); moduleQueue = new ConcurrentLinkedQueue<>(); @@ -166,7 +166,7 @@ public class UpdateRepos { } else if (force) { fullReload(); } - Topic.publish(Topic.REPO_LOAD_DONE); + Event.trigger(Event.REPO_LOAD_DONE); }); } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Event.java b/app/src/main/java/com/topjohnwu/magisk/utils/Event.java new file mode 100644 index 000000000..ef786974a --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Event.java @@ -0,0 +1,123 @@ +package com.topjohnwu.magisk.utils; + +import androidx.annotation.IntDef; +import androidx.collection.ArraySet; + +import com.topjohnwu.superuser.internal.UiThreadHandler; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.util.Set; + +public class Event { + + public static final int MAGISK_HIDE_DONE = 0; + public static final int MODULE_LOAD_DONE = 1; + public static final int REPO_LOAD_DONE = 2; + public static final int UPDATE_CHECK_DONE = 3; + public static final int LOCALE_FETCH_DONE = 4; + + @IntDef({MAGISK_HIDE_DONE, MODULE_LOAD_DONE, REPO_LOAD_DONE, + UPDATE_CHECK_DONE, LOCALE_FETCH_DONE}) + @Retention(RetentionPolicy.SOURCE) + public @interface EventID {} + + // We will not dynamically add topics, so use arrays instead of hash tables + private static Store[] eventList = new Store[5]; + + public static void register(Listener listener, @EventID int... events) { + for (int event : events) { + if (eventList[event] == null) + eventList[event] = new Store(); + eventList[event].listeners.add(listener); + if (eventList[event].triggered) { + listener.onEvent(event); + } + } + } + + public static void register(AutoListener listener) { + register(listener, listener.getListeningEvents()); + } + + public static void unregister(Listener listener, @EventID int... events) { + for (int event : events) { + if (eventList[event] == null) + continue; + eventList[event].listeners.remove(listener); + } + } + + public static void unregister(AutoListener listener) { + unregister(listener, listener.getListeningEvents()); + } + + public static void trigger(@EventID int event) { + trigger(true, event, null); + } + + public static void trigger(@EventID int event, Object result) { + trigger(true, event, result); + } + + public static void trigger(boolean perm, @EventID int event) { + trigger(perm, event, null); + } + + public static void trigger(boolean perm, @EventID int event, Object result) { + if (eventList[event] == null) + eventList[event] = new Store(); + if (perm) { + eventList[event].result = result; + eventList[event].triggered = true; + } + for (Listener sub : eventList[event].listeners) { + UiThreadHandler.run(() -> sub.onEvent(event)); + } + } + + public static void reset(@EventID int event) { + if (eventList[event] == null) + return; + eventList[event].triggered = false; + eventList[event].result = null; + } + + public static void reset(AutoListener listener) { + for (int event : listener.getListeningEvents()) + reset(event); + } + + public static boolean isTriggered(@EventID int event) { + if (eventList[event] == null) + return false; + return eventList[event].triggered; + } + + public static boolean isTriggered(AutoListener listener) { + for (int event : listener.getListeningEvents()) { + if (!isTriggered(event)) + return false; + } + return true; + } + + public static T getResult(@EventID int event) { + return (T) eventList[event].result; + } + + private static class Store { + boolean triggered = false; + Set listeners = new ArraySet<>(); + Object result; + } + + public interface Listener { + void onEvent(int event); + } + + public interface AutoListener extends Listener { + @EventID + int[] getListeningEvents(); + } +} diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/LocaleManager.java b/app/src/main/java/com/topjohnwu/magisk/utils/LocaleManager.java index b337593b3..ea14f1c01 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/LocaleManager.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/LocaleManager.java @@ -141,7 +141,7 @@ public class LocaleManager { } Collections.sort(locales, (a, b) -> a.getDisplayName(a).compareTo(b.getDisplayName(b))); - Topic.publish(Topic.LOCALE_FETCH_DONE); + Event.trigger(Event.LOCALE_FETCH_DONE); }); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Topic.java b/app/src/main/java/com/topjohnwu/magisk/utils/Topic.java deleted file mode 100644 index b6d23926a..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Topic.java +++ /dev/null @@ -1,108 +0,0 @@ -package com.topjohnwu.magisk.utils; - -import androidx.annotation.IntDef; - -import com.topjohnwu.superuser.internal.UiThreadHandler; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.util.HashSet; -import java.util.Set; - -public class Topic { - - public static final int MAGISK_HIDE_DONE = 0; - public static final int MODULE_LOAD_DONE = 1; - public static final int REPO_LOAD_DONE = 2; - public static final int UPDATE_CHECK_DONE = 3; - public static final int LOCALE_FETCH_DONE = 4; - - @IntDef({MAGISK_HIDE_DONE, MODULE_LOAD_DONE, REPO_LOAD_DONE, - UPDATE_CHECK_DONE, LOCALE_FETCH_DONE}) - @Retention(RetentionPolicy.SOURCE) - public @interface TopicID {} - - // We will not dynamically add topics, so use arrays instead of hash tables - private static Store[] topicList = new Store[5]; - - public static void subscribe(Subscriber sub, @TopicID int... topics) { - for (int topic : topics) { - if (topicList[topic] == null) - topicList[topic] = new Store(); - topicList[topic].subscribers.add(sub); - if (topicList[topic].published) { - sub.onPublish(topic, topicList[topic].results); - } - } - } - - public static void subscribe(AutoSubscriber sub) { - subscribe(sub, sub.getSubscribedTopics()); - } - - public static void unsubscribe(Subscriber sub, @TopicID int... topics) { - for (int topic : topics) { - if (topicList[topic] == null) - continue; - topicList[topic].subscribers.remove(sub); - } - } - - public static void unsubscribe(AutoSubscriber sub) { - unsubscribe(sub, sub.getSubscribedTopics()); - } - - public static void publish(@TopicID int topic, Object... results) { - publish(true, topic, results); - } - - public static void publish(boolean persist, @TopicID int topic, Object... results) { - if (topicList[topic] == null) - topicList[topic] = new Store(); - if (persist) { - topicList[topic].results = results; - topicList[topic].published = true; - } - for (Subscriber sub : topicList[topic].subscribers) { - UiThreadHandler.run(() -> sub.onPublish(topic, results)); - } - } - - public static void reset(@TopicID int... topics) { - for (int topic : topics) { - if (topicList[topic] == null) - continue; - topicList[topic].published = false; - topicList[topic].results = null; - } - } - - public static boolean isPublished(@TopicID int... topics) { - for (int topic : topics) { - if (topicList[topic] == null) - return false; - if (!topicList[topic].published) - return false; - } - return true; - } - - public static boolean isPublished(AutoSubscriber sub) { - return isPublished(sub.getSubscribedTopics()); - } - - private static class Store { - boolean published = false; - Set subscribers = new HashSet<>(); - Object[] results; - } - - public interface Subscriber { - void onPublish(int topic, Object[] result); - } - - public interface AutoSubscriber extends Subscriber { - @TopicID - int[] getSubscribedTopics(); - } -} 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 098342478..f398a3065 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -95,7 +95,7 @@ public class Utils { } public static void loadModules() { - Topic.reset(Topic.MODULE_LOAD_DONE); + Event.reset(Event.MODULE_LOAD_DONE); App.THREAD_POOL.execute(() -> { Map moduleMap = new ValueSortedMap<>(); SuFile path = new SuFile(Const.MAGISK_PATH); @@ -106,7 +106,7 @@ public class Utils { Module module = new Module(Const.MAGISK_PATH + "/" + file.getName()); moduleMap.put(module.getId(), module); } - Topic.publish(Topic.MODULE_LOAD_DONE, moduleMap); + Event.trigger(Event.MODULE_LOAD_DONE, moduleMap); }); }