Rename callbackevents to topic/subscribers

This commit is contained in:
topjohnwu 2017-08-04 00:12:11 +08:00
parent 2ecbca303b
commit 8e7b8825f5
16 changed files with 137 additions and 137 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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<ApplicationAdapter.
private List<String> 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<ApplicationAdapter.
@Override
protected void onPostExecute(Void v) {
magiskHideDone.trigger(false);
magiskHideDone.publish(false);
}
}
}

View File

@ -56,7 +56,7 @@ public class CheckUpdates extends ParallelTask<Void, Void, Void> {
Utils.showMagiskUpdate(magiskManager);
}
}
magiskManager.updateCheckDone.trigger();
magiskManager.updateCheckDone.publish();
super.onPostExecute(v);
}
}

View File

@ -39,7 +39,7 @@ public class LoadModules extends ParallelTask<Void, Void, Void> {
protected void onPostExecute(Void v) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return;
magiskManager.moduleLoadDone.trigger();
magiskManager.moduleLoadDone.publish();
super.onPostExecute(v);
}
}

View File

@ -186,7 +186,7 @@ public class UpdateRepos extends ParallelTask<Void, Void, Void> {
protected void onPostExecute(Void v) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return;
magiskManager.repoLoadDone.trigger();
magiskManager.repoLoadDone.publish();
super.onPostExecute(v);
}
}

View File

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

View File

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

View File

@ -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<WeakReference<Listener>> 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<WeakReference<Listener>> i = listeners.iterator(); i.hasNext();) {
WeakReference<Listener> 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> 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();
}
}

View File

@ -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<WeakReference<Subscriber>> 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<WeakReference<Subscriber>> i = subscribers.iterator(); i.hasNext();) {
WeakReference<Subscriber> 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> 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();
}
}

View File

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