Make callback events non-static

This commit is contained in:
topjohnwu 2017-02-07 04:09:49 +08:00
parent bef5969580
commit 51b22d1ad4
10 changed files with 116 additions and 138 deletions

View File

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

View File

@ -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<Void> {
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<Void> event) {
Logger.dev("MagiskHideFragment: UI refresh");
appAdapter.setLists(getApplication().appList, getApplication().magiskHideList);
mSwipeRefreshLayout.setRefreshing(false);

View File

@ -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<Void> blockDetectionDone = new CallbackEvent<>();
public final CallbackEvent<Void> packageLoadDone = new CallbackEvent<>();
public final CallbackEvent<Void> reloadMainActivity = new CallbackEvent<>();
public final CallbackEvent<Void> moduleLoadDone = new CallbackEvent<>();
public final CallbackEvent<Void> repoLoadDone = new CallbackEvent<>();
public final CallbackEvent<Void> updateCheckDone = new CallbackEvent<>();
public final CallbackEvent<Void> safetyNetDone = new CallbackEvent<>();
public SparseArray<CallbackEvent<Policy>> uidMap = new SparseArray<>();
// Info
public double magiskVersion;
@ -47,7 +49,6 @@ public class MagiskManager extends Application {
public List<String> blockList;
public List<ApplicationInfo> appList;
public List<String> magiskHideList;
public SparseArray<CallbackHandler.Event> uidMap = new SparseArray<>();
// Configurations
public static boolean shellLogging;

View File

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

View File

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

View File

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

View File

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

View File

@ -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<Policy> {
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<Policy> self;
private CallbackEvent<Policy> 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<Policy> 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<Policy>() {
@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);

View File

@ -0,0 +1,47 @@
package com.topjohnwu.magisk.utils;
import java.util.HashSet;
import java.util.Set;
public class CallbackEvent<Result> {
public boolean isTriggered = false;
private Result result;
private Set<Listener<Result>> listeners;
public void register(Listener<Result> l) {
if (listeners == null)
listeners = new HashSet<>();
listeners.add(l);
}
public void unRegister() {
listeners = null;
}
public void unRegister(Listener<Result> 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<Result> listener : listeners)
listener.onTrigger(this);
}
}
public Result getResult() {
return result;
}
public interface Listener<R> {
void onTrigger(CallbackEvent<R> event);
}
}

View File

@ -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<Event, Set<EventListener>> listeners = new HashMap<>();
public static void register(Event event, EventListener listener) {
Set<EventListener> list = listeners.get(event);
if (list == null) {
list = new HashSet<>();
listeners.put(event, list);
}
list.add(listener);
}
public static void unRegister(Event event) {
Set<EventListener> list = listeners.remove(event);
if (list != null) {
list.clear();
}
}
public static void unRegister(Event event, EventListener listener) {
Set<EventListener> list = listeners.get(event);
if (list != null) {
list.remove(listener);
}
}
private static void triggerCallback(Event event) {
Set<EventListener> 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);
}
}