From 51b22d1ad4c9411a2c8580d8735392ac903266d0 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 7 Feb 2017 04:09:49 +0800 Subject: [PATCH] Make callback events non-static --- .../com/topjohnwu/magisk/InstallFragment.java | 10 +-- .../topjohnwu/magisk/MagiskHideFragment.java | 16 ++--- .../com/topjohnwu/magisk/MagiskManager.java | 19 +++--- .../com/topjohnwu/magisk/MainActivity.java | 22 +++--- .../com/topjohnwu/magisk/ModulesFragment.java | 10 +-- .../com/topjohnwu/magisk/ReposFragment.java | 10 +-- .../com/topjohnwu/magisk/StatusFragment.java | 25 ++++--- .../magisk/superuser/SuRequestActivity.java | 28 ++++---- .../topjohnwu/magisk/utils/CallbackEvent.java | 47 +++++++++++++ .../magisk/utils/CallbackHandler.java | 67 ------------------- 10 files changed, 116 insertions(+), 138 deletions(-) create mode 100644 app/src/main/java/com/topjohnwu/magisk/utils/CallbackEvent.java delete mode 100644 app/src/main/java/com/topjohnwu/magisk/utils/CallbackHandler.java diff --git a/app/src/main/java/com/topjohnwu/magisk/InstallFragment.java b/app/src/main/java/com/topjohnwu/magisk/InstallFragment.java index dc4b8a412..0912188df 100644 --- a/app/src/main/java/com/topjohnwu/magisk/InstallFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/InstallFragment.java @@ -18,7 +18,7 @@ import android.widget.TextView; import com.topjohnwu.magisk.components.Fragment; import com.topjohnwu.magisk.receivers.MagiskDlReceiver; -import com.topjohnwu.magisk.utils.CallbackHandler; +import com.topjohnwu.magisk.utils.CallbackEvent; import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Utils; @@ -33,7 +33,7 @@ import butterknife.BindView; import butterknife.ButterKnife; import butterknife.Unbinder; -public class InstallFragment extends Fragment implements CallbackHandler.EventListener { +public class InstallFragment extends Fragment implements CallbackEvent.Listener { private static final String UNINSTALLER = "magisk_uninstaller.sh"; @@ -132,7 +132,7 @@ public class InstallFragment extends Fragment implements CallbackHandler.EventLi } @Override - public void onTrigger(CallbackHandler.Event event) { + public void onTrigger(CallbackEvent event) { updateUI(); } @@ -157,12 +157,12 @@ public class InstallFragment extends Fragment implements CallbackHandler.EventLi public void onStart() { super.onStart(); getActivity().setTitle(R.string.install); - CallbackHandler.register(getApplication().blockDetectionDone, this); + getApplication().blockDetectionDone.register(this); } @Override public void onStop() { - CallbackHandler.unRegister(getApplication().blockDetectionDone, this); + getApplication().blockDetectionDone.unRegister(this); super.onStop(); } diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java b/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java index 6c83f152f..430937003 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java @@ -17,14 +17,14 @@ import android.widget.SearchView; import com.topjohnwu.magisk.adapters.ApplicationAdapter; import com.topjohnwu.magisk.components.Fragment; import com.topjohnwu.magisk.utils.Async; -import com.topjohnwu.magisk.utils.CallbackHandler; +import com.topjohnwu.magisk.utils.CallbackEvent; import com.topjohnwu.magisk.utils.Logger; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.Unbinder; -public class MagiskHideFragment extends Fragment implements CallbackHandler.EventListener { +public class MagiskHideFragment extends Fragment implements CallbackEvent.Listener { private Unbinder unbinder; @BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout; @@ -71,6 +71,9 @@ public class MagiskHideFragment extends Fragment implements CallbackHandler.Even } }; + if (getApplication().packageLoadDone.isTriggered) + onTrigger(getApplication().packageLoadDone); + return view; } @@ -85,15 +88,12 @@ public class MagiskHideFragment extends Fragment implements CallbackHandler.Even public void onStart() { super.onStart(); getActivity().setTitle(R.string.magiskhide); - CallbackHandler.register(getApplication().packageLoadDone, this); - if (getApplication().packageLoadDone.isTriggered) { - onTrigger(getApplication().packageLoadDone); - } + getApplication().packageLoadDone.register(this); } @Override public void onStop() { - CallbackHandler.unRegister(getApplication().packageLoadDone, this); + getApplication().packageLoadDone.unRegister(this); super.onStop(); } @@ -104,7 +104,7 @@ public class MagiskHideFragment extends Fragment implements CallbackHandler.Even } @Override - public void onTrigger(CallbackHandler.Event event) { + public void onTrigger(CallbackEvent event) { Logger.dev("MagiskHideFragment: UI refresh"); appAdapter.setLists(getApplication().appList, getApplication().magiskHideList); mSwipeRefreshLayout.setRefreshing(false); diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java index a5f5fbe00..011bf18b7 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java @@ -8,7 +8,8 @@ import android.util.SparseArray; import com.topjohnwu.magisk.module.Module; import com.topjohnwu.magisk.module.Repo; -import com.topjohnwu.magisk.utils.CallbackHandler; +import com.topjohnwu.magisk.superuser.Policy; +import com.topjohnwu.magisk.utils.CallbackEvent; import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.ValueSortedMap; @@ -21,13 +22,14 @@ public class MagiskManager extends Application { public static final String MAGISK_DISABLE_FILE = "/cache/.disable_magisk"; // Events - public final CallbackHandler.Event blockDetectionDone = new CallbackHandler.Event(); - public final CallbackHandler.Event packageLoadDone = new CallbackHandler.Event(); - public final CallbackHandler.Event reloadMainActivity = new CallbackHandler.Event(); - public final CallbackHandler.Event moduleLoadDone = new CallbackHandler.Event(); - public final CallbackHandler.Event repoLoadDone = new CallbackHandler.Event(); - public final CallbackHandler.Event updateCheckDone = new CallbackHandler.Event(); - public final CallbackHandler.Event safetyNetDone = new CallbackHandler.Event(); + public final CallbackEvent blockDetectionDone = new CallbackEvent<>(); + public final CallbackEvent packageLoadDone = new CallbackEvent<>(); + public final CallbackEvent reloadMainActivity = 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 SparseArray> uidMap = new SparseArray<>(); // Info public double magiskVersion; @@ -47,7 +49,6 @@ public class MagiskManager extends Application { public List blockList; public List appList; public List magiskHideList; - public SparseArray uidMap = new SparseArray<>(); // Configurations public static boolean shellLogging; diff --git a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java index f41ca37cb..47617ab99 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java @@ -21,14 +21,14 @@ import android.view.MenuItem; import android.view.View; import com.topjohnwu.magisk.components.Activity; -import com.topjohnwu.magisk.utils.CallbackHandler; +import com.topjohnwu.magisk.utils.CallbackEvent; import com.topjohnwu.magisk.utils.Shell; import butterknife.BindView; import butterknife.ButterKnife; public class MainActivity extends Activity - implements NavigationView.OnNavigationItemSelectedListener, CallbackHandler.EventListener { + implements NavigationView.OnNavigationItemSelectedListener, CallbackEvent.Listener { private final Handler mDrawerHandler = new Handler(); private SharedPreferences prefs; @@ -79,28 +79,28 @@ public class MainActivity extends Activity navigate(R.id.status); navigationView.setNavigationItemSelectedListener(this); - CallbackHandler.register(getTopApplication().reloadMainActivity, this); + getTopApplication().reloadMainActivity.register(this); } @Override protected void onResume() { super.onResume(); - CallbackHandler.register(getTopApplication().updateCheckDone, this); - if (getTopApplication().updateCheckDone.isTriggered) - onTrigger(getTopApplication().updateCheckDone); + getTopApplication().updateCheckDone.register(this); +// if (getTopApplication().updateCheckDone.isTriggered) +// onTrigger(getTopApplication().updateCheckDone); checkHideSection(); } @Override protected void onPause() { - CallbackHandler.unRegister(getTopApplication().updateCheckDone, this); + getTopApplication().updateCheckDone.unRegister(this); super.onPause(); } @Override protected void onDestroy() { - CallbackHandler.unRegister(getTopApplication().reloadMainActivity, this); + getTopApplication().reloadMainActivity.unRegister(this); super.onDestroy(); } @@ -121,11 +121,11 @@ public class MainActivity extends Activity } @Override - public void onTrigger(CallbackHandler.Event event) { + public void onTrigger(CallbackEvent event) { if (event == getTopApplication().updateCheckDone) { Menu menu = navigationView.getMenu(); - menu.findItem(R.id.install).setVisible(getTopApplication().remoteMagiskVersion > 0 && - Shell.rootAccess()); + menu.findItem(R.id.install).setVisible( + getTopApplication().remoteMagiskVersion > 0 && Shell.rootAccess()); } else if (event == getTopApplication().reloadMainActivity) { recreate(); } diff --git a/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java b/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java index f296fbecc..09fa73404 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java @@ -17,7 +17,7 @@ import com.topjohnwu.magisk.adapters.ModulesAdapter; import com.topjohnwu.magisk.components.Fragment; import com.topjohnwu.magisk.module.Module; import com.topjohnwu.magisk.utils.Async; -import com.topjohnwu.magisk.utils.CallbackHandler; +import com.topjohnwu.magisk.utils.CallbackEvent; import com.topjohnwu.magisk.utils.Logger; import java.util.ArrayList; @@ -27,7 +27,7 @@ import butterknife.BindView; import butterknife.ButterKnife; import butterknife.Unbinder; -public class ModulesFragment extends Fragment implements CallbackHandler.EventListener { +public class ModulesFragment extends Fragment implements CallbackEvent.Listener { private static final int FETCH_ZIP_CODE = 2; @@ -76,7 +76,7 @@ public class ModulesFragment extends Fragment implements CallbackHandler.EventLi } @Override - public void onTrigger(CallbackHandler.Event event) { + public void onTrigger(CallbackEvent event) { Logger.dev("ModulesFragment: UI refresh triggered"); updateUI(); } @@ -94,13 +94,13 @@ public class ModulesFragment extends Fragment implements CallbackHandler.EventLi @Override public void onStart() { super.onStart(); - CallbackHandler.register(getApplication().moduleLoadDone, this); + getApplication().moduleLoadDone.register(this); getActivity().setTitle(R.string.modules); } @Override public void onStop() { - CallbackHandler.unRegister(getApplication().moduleLoadDone, this); + getApplication().moduleLoadDone.unRegister(this); super.onStop(); } diff --git a/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java b/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java index 0863f04be..ea6c2aba8 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java @@ -19,7 +19,7 @@ import com.topjohnwu.magisk.components.Fragment; import com.topjohnwu.magisk.module.Module; import com.topjohnwu.magisk.module.Repo; import com.topjohnwu.magisk.utils.Async; -import com.topjohnwu.magisk.utils.CallbackHandler; +import com.topjohnwu.magisk.utils.CallbackEvent; import com.topjohnwu.magisk.utils.Logger; import java.util.ArrayList; @@ -29,7 +29,7 @@ import butterknife.BindView; import butterknife.ButterKnife; import butterknife.Unbinder; -public class ReposFragment extends Fragment implements CallbackHandler.EventListener { +public class ReposFragment extends Fragment implements CallbackEvent.Listener { private Unbinder unbinder; @BindView(R.id.recyclerView) RecyclerView recyclerView; @@ -93,7 +93,7 @@ public class ReposFragment extends Fragment implements CallbackHandler.EventList } @Override - public void onTrigger(CallbackHandler.Event event) { + public void onTrigger(CallbackEvent event) { Logger.dev("ReposFragment: UI refresh triggered"); reloadRepos(); updateUI(); @@ -109,13 +109,13 @@ public class ReposFragment extends Fragment implements CallbackHandler.EventList @Override public void onStart() { super.onStart(); - CallbackHandler.register(getApplication().repoLoadDone, this); + getApplication().repoLoadDone.register(this); getActivity().setTitle(R.string.downloads); } @Override public void onStop() { - CallbackHandler.unRegister(getApplication().repoLoadDone, this); + getApplication().repoLoadDone.unRegister(this); super.onStop(); } diff --git a/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java b/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java index ab83d00b7..936a54dec 100644 --- a/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java @@ -16,7 +16,7 @@ import android.widget.TextView; import com.topjohnwu.magisk.components.Fragment; import com.topjohnwu.magisk.utils.Async; -import com.topjohnwu.magisk.utils.CallbackHandler; +import com.topjohnwu.magisk.utils.CallbackEvent; import com.topjohnwu.magisk.utils.Logger; import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Utils; @@ -26,7 +26,7 @@ import butterknife.BindView; import butterknife.ButterKnife; import butterknife.Unbinder; -public class StatusFragment extends Fragment implements CallbackHandler.EventListener { +public class StatusFragment extends Fragment implements CallbackEvent.Listener { private static boolean noDialog = false; @@ -115,11 +115,16 @@ public class StatusFragment extends Fragment implements CallbackHandler.EventLis updateUI(); + if (getApplication().updateCheckDone.isTriggered) + updateCheckUI(); + if (getApplication().safetyNetDone.isTriggered) + updateSafetyNetUI(); + return v; } @Override - public void onTrigger(CallbackHandler.Event event) { + public void onTrigger(CallbackEvent event) { if (event == getApplication().updateCheckDone) { Logger.dev("StatusFragment: Update Check UI refresh triggered"); updateCheckUI(); @@ -132,21 +137,15 @@ public class StatusFragment extends Fragment implements CallbackHandler.EventLis @Override public void onStart() { super.onStart(); - CallbackHandler.register(getApplication().updateCheckDone, this); - CallbackHandler.register(getApplication().safetyNetDone, this); - if (getApplication().updateCheckDone.isTriggered) { - updateCheckUI(); - } - if (getApplication().safetyNetDone.isTriggered) { - updateSafetyNetUI(); - } + getApplication().updateCheckDone.register(this); + getApplication().safetyNetDone.register(this); getActivity().setTitle(R.string.status); } @Override public void onStop() { - CallbackHandler.unRegister(getApplication().updateCheckDone, this); - CallbackHandler.unRegister(getApplication().safetyNetDone, this); + getApplication().updateCheckDone.unRegister(this); + getApplication().safetyNetDone.unRegister(this); super.onStop(); } diff --git a/app/src/main/java/com/topjohnwu/magisk/superuser/SuRequestActivity.java b/app/src/main/java/com/topjohnwu/magisk/superuser/SuRequestActivity.java index 4e2091dc2..0e2ea8446 100644 --- a/app/src/main/java/com/topjohnwu/magisk/superuser/SuRequestActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/superuser/SuRequestActivity.java @@ -21,7 +21,7 @@ import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.components.Activity; import com.topjohnwu.magisk.utils.Async; -import com.topjohnwu.magisk.utils.CallbackHandler; +import com.topjohnwu.magisk.utils.CallbackEvent; import java.io.DataInputStream; import java.io.IOException; @@ -29,7 +29,7 @@ import java.io.IOException; import butterknife.BindView; import butterknife.ButterKnife; -public class SuRequestActivity extends Activity implements CallbackHandler.EventListener { +public class SuRequestActivity extends Activity implements CallbackEvent.Listener { private static final int[] timeoutList = {0, -1, 10, 20, 30, 60}; private static final int SU_PROTOCOL_PARAM_MAX = 20; @@ -56,8 +56,8 @@ public class SuRequestActivity extends Activity implements CallbackHandler.Event private int uid; private Policy policy; private CountDownTimer timer; - private CallbackHandler.EventListener self; - private CallbackHandler.Event event = null; + private CallbackEvent.Listener self; + private CallbackEvent event = null; @Override protected void onCreate(Bundle savedInstanceState) { @@ -143,14 +143,11 @@ public class SuRequestActivity extends Activity implements CallbackHandler.Event } @Override - public void onTrigger(CallbackHandler.Event event) { - Policy policy = (Policy) event.getResult(); + public void onTrigger(CallbackEvent event) { + Policy policy = event.getResult(); String response = "socket:DENY"; - if (policy != null) { - magiskManager.uidMap.remove(policy.uid); - if (policy.policy == Policy.ALLOW) - response = "socket:ALLOW"; - } + if (policy != null &&policy.policy == Policy.ALLOW ) + response = "socket:ALLOW"; try { socket.getOutputStream().write((response).getBytes()); } catch (Exception ignored) {} @@ -229,16 +226,17 @@ public class SuRequestActivity extends Activity implements CallbackHandler.Event event = magiskManager.uidMap.get(uid); if (event == null) { showRequest = true; - event = new CallbackHandler.Event() { + event = new CallbackEvent() { @Override - public void trigger(Object result) { + public void trigger(Policy result) { super.trigger(result); - CallbackHandler.unRegister(this); + unRegister(); + magiskManager.uidMap.remove(uid); } }; magiskManager.uidMap.put(uid, event); } - CallbackHandler.register(event, self); + event.register(self); try { if (showRequest) { policy = new Policy(uid, pm); diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/CallbackEvent.java b/app/src/main/java/com/topjohnwu/magisk/utils/CallbackEvent.java new file mode 100644 index 000000000..c5768ff31 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/utils/CallbackEvent.java @@ -0,0 +1,47 @@ +package com.topjohnwu.magisk.utils; + +import java.util.HashSet; +import java.util.Set; + +public class CallbackEvent { + + public boolean isTriggered = false; + private Result result; + private Set> listeners; + + public void register(Listener l) { + if (listeners == null) + listeners = new HashSet<>(); + listeners.add(l); + } + + public void unRegister() { + listeners = null; + } + + public void unRegister(Listener l) { + if (listeners != null) + listeners.remove(l); + } + + public void trigger() { + trigger(null); + } + + public void trigger(Result r) { + result = r; + isTriggered = true; + if (listeners != null) { + for (Listener listener : listeners) + listener.onTrigger(this); + } + } + + public Result getResult() { + return result; + } + + public interface Listener { + void onTrigger(CallbackEvent event); + } +} diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/CallbackHandler.java b/app/src/main/java/com/topjohnwu/magisk/utils/CallbackHandler.java deleted file mode 100644 index 3218a3b55..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/utils/CallbackHandler.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.topjohnwu.magisk.utils; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -public class CallbackHandler { - - private static Map> listeners = new HashMap<>(); - - public static void register(Event event, EventListener listener) { - Set list = listeners.get(event); - if (list == null) { - list = new HashSet<>(); - listeners.put(event, list); - } - list.add(listener); - } - - public static void unRegister(Event event) { - Set list = listeners.remove(event); - if (list != null) { - list.clear(); - } - } - - public static void unRegister(Event event, EventListener listener) { - Set list = listeners.get(event); - if (list != null) { - list.remove(listener); - } - } - - private static void triggerCallback(Event event) { - Set list = listeners.get(event); - if (list != null) { - for (EventListener listener : list) { - listener.onTrigger(event); - } - } - } - - public static class Event { - - public boolean isTriggered = false; - private Object result; - - public void trigger() { - trigger(null); - } - - public void trigger(Object result) { - this.result = result; - isTriggered = true; - triggerCallback(this); - } - - public Object getResult() { - return result; - } - } - - public interface EventListener { - void onTrigger(Event event); - } -}