Rename Topic -> Event

This commit is contained in:
topjohnwu 2019-03-23 21:58:42 -04:00
parent a199b0ace1
commit 8ac3aaf36c
16 changed files with 183 additions and 178 deletions

View File

@ -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() &&

View File

@ -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

View File

@ -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();
}

View File

@ -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) {}
}

View File

@ -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;
}

View File

@ -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();
}

View File

@ -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());
}

View File

@ -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<String, Module>) result[0]);
public void onEvent(int event) {
updateUI(Event.getResult(event));
}
@Override

View File

@ -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<String, Module>) 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);

View File

@ -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};
}
}

View File

@ -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)

View File

@ -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);
});
}

View File

@ -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> T getResult(@EventID int event) {
return (T) eventList[event].result;
}
private static class Store {
boolean triggered = false;
Set<Listener> listeners = new ArraySet<>();
Object result;
}
public interface Listener {
void onEvent(int event);
}
public interface AutoListener extends Listener {
@EventID
int[] getListeningEvents();
}
}

View File

@ -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);
});
}
}

View File

@ -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<Subscriber> subscribers = new HashSet<>();
Object[] results;
}
public interface Subscriber {
void onPublish(int topic, Object[] result);
}
public interface AutoSubscriber extends Subscriber {
@TopicID
int[] getSubscribedTopics();
}
}

View File

@ -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<String, Module> 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);
});
}