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.components.Fragment;
import com.topjohnwu.magisk.receivers.MagiskDlReceiver; 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.Shell;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
@ -33,7 +33,7 @@ import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.Unbinder; 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"; private static final String UNINSTALLER = "magisk_uninstaller.sh";
@ -132,7 +132,7 @@ public class InstallFragment extends Fragment implements CallbackHandler.EventLi
} }
@Override @Override
public void onTrigger(CallbackHandler.Event event) { public void onTrigger(CallbackEvent<Void> event) {
updateUI(); updateUI();
} }
@ -157,12 +157,12 @@ public class InstallFragment extends Fragment implements CallbackHandler.EventLi
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
getActivity().setTitle(R.string.install); getActivity().setTitle(R.string.install);
CallbackHandler.register(getApplication().blockDetectionDone, this); getApplication().blockDetectionDone.register(this);
} }
@Override @Override
public void onStop() { public void onStop() {
CallbackHandler.unRegister(getApplication().blockDetectionDone, this); getApplication().blockDetectionDone.unRegister(this);
super.onStop(); super.onStop();
} }

View File

@ -17,14 +17,14 @@ import android.widget.SearchView;
import com.topjohnwu.magisk.adapters.ApplicationAdapter; import com.topjohnwu.magisk.adapters.ApplicationAdapter;
import com.topjohnwu.magisk.components.Fragment; import com.topjohnwu.magisk.components.Fragment;
import com.topjohnwu.magisk.utils.Async; 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.Logger;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.Unbinder; import butterknife.Unbinder;
public class MagiskHideFragment extends Fragment implements CallbackHandler.EventListener { public class MagiskHideFragment extends Fragment implements CallbackEvent.Listener<Void> {
private Unbinder unbinder; private Unbinder unbinder;
@BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout; @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; return view;
} }
@ -85,15 +88,12 @@ public class MagiskHideFragment extends Fragment implements CallbackHandler.Even
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
getActivity().setTitle(R.string.magiskhide); getActivity().setTitle(R.string.magiskhide);
CallbackHandler.register(getApplication().packageLoadDone, this); getApplication().packageLoadDone.register(this);
if (getApplication().packageLoadDone.isTriggered) {
onTrigger(getApplication().packageLoadDone);
}
} }
@Override @Override
public void onStop() { public void onStop() {
CallbackHandler.unRegister(getApplication().packageLoadDone, this); getApplication().packageLoadDone.unRegister(this);
super.onStop(); super.onStop();
} }
@ -104,7 +104,7 @@ public class MagiskHideFragment extends Fragment implements CallbackHandler.Even
} }
@Override @Override
public void onTrigger(CallbackHandler.Event event) { public void onTrigger(CallbackEvent<Void> event) {
Logger.dev("MagiskHideFragment: UI refresh"); Logger.dev("MagiskHideFragment: UI refresh");
appAdapter.setLists(getApplication().appList, getApplication().magiskHideList); appAdapter.setLists(getApplication().appList, getApplication().magiskHideList);
mSwipeRefreshLayout.setRefreshing(false); mSwipeRefreshLayout.setRefreshing(false);

View File

@ -8,7 +8,8 @@ import android.util.SparseArray;
import com.topjohnwu.magisk.module.Module; import com.topjohnwu.magisk.module.Module;
import com.topjohnwu.magisk.module.Repo; 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.Shell;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.ValueSortedMap; 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"; public static final String MAGISK_DISABLE_FILE = "/cache/.disable_magisk";
// Events // Events
public final CallbackHandler.Event blockDetectionDone = new CallbackHandler.Event(); public final CallbackEvent<Void> blockDetectionDone = new CallbackEvent<>();
public final CallbackHandler.Event packageLoadDone = new CallbackHandler.Event(); public final CallbackEvent<Void> packageLoadDone = new CallbackEvent<>();
public final CallbackHandler.Event reloadMainActivity = new CallbackHandler.Event(); public final CallbackEvent<Void> reloadMainActivity = new CallbackEvent<>();
public final CallbackHandler.Event moduleLoadDone = new CallbackHandler.Event(); public final CallbackEvent<Void> moduleLoadDone = new CallbackEvent<>();
public final CallbackHandler.Event repoLoadDone = new CallbackHandler.Event(); public final CallbackEvent<Void> repoLoadDone = new CallbackEvent<>();
public final CallbackHandler.Event updateCheckDone = new CallbackHandler.Event(); public final CallbackEvent<Void> updateCheckDone = new CallbackEvent<>();
public final CallbackHandler.Event safetyNetDone = new CallbackHandler.Event(); public final CallbackEvent<Void> safetyNetDone = new CallbackEvent<>();
public SparseArray<CallbackEvent<Policy>> uidMap = new SparseArray<>();
// Info // Info
public double magiskVersion; public double magiskVersion;
@ -47,7 +49,6 @@ public class MagiskManager extends Application {
public List<String> blockList; public List<String> blockList;
public List<ApplicationInfo> appList; public List<ApplicationInfo> appList;
public List<String> magiskHideList; public List<String> magiskHideList;
public SparseArray<CallbackHandler.Event> uidMap = new SparseArray<>();
// Configurations // Configurations
public static boolean shellLogging; public static boolean shellLogging;

View File

@ -21,14 +21,14 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import com.topjohnwu.magisk.components.Activity; 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 com.topjohnwu.magisk.utils.Shell;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
public class MainActivity extends Activity public class MainActivity extends Activity
implements NavigationView.OnNavigationItemSelectedListener, CallbackHandler.EventListener { implements NavigationView.OnNavigationItemSelectedListener, CallbackEvent.Listener<Void> {
private final Handler mDrawerHandler = new Handler(); private final Handler mDrawerHandler = new Handler();
private SharedPreferences prefs; private SharedPreferences prefs;
@ -79,28 +79,28 @@ public class MainActivity extends Activity
navigate(R.id.status); navigate(R.id.status);
navigationView.setNavigationItemSelectedListener(this); navigationView.setNavigationItemSelectedListener(this);
CallbackHandler.register(getTopApplication().reloadMainActivity, this); getTopApplication().reloadMainActivity.register(this);
} }
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
CallbackHandler.register(getTopApplication().updateCheckDone, this); getTopApplication().updateCheckDone.register(this);
if (getTopApplication().updateCheckDone.isTriggered) // if (getTopApplication().updateCheckDone.isTriggered)
onTrigger(getTopApplication().updateCheckDone); // onTrigger(getTopApplication().updateCheckDone);
checkHideSection(); checkHideSection();
} }
@Override @Override
protected void onPause() { protected void onPause() {
CallbackHandler.unRegister(getTopApplication().updateCheckDone, this); getTopApplication().updateCheckDone.unRegister(this);
super.onPause(); super.onPause();
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
CallbackHandler.unRegister(getTopApplication().reloadMainActivity, this); getTopApplication().reloadMainActivity.unRegister(this);
super.onDestroy(); super.onDestroy();
} }
@ -121,11 +121,11 @@ public class MainActivity extends Activity
} }
@Override @Override
public void onTrigger(CallbackHandler.Event event) { public void onTrigger(CallbackEvent<Void> event) {
if (event == getTopApplication().updateCheckDone) { if (event == getTopApplication().updateCheckDone) {
Menu menu = navigationView.getMenu(); Menu menu = navigationView.getMenu();
menu.findItem(R.id.install).setVisible(getTopApplication().remoteMagiskVersion > 0 && menu.findItem(R.id.install).setVisible(
Shell.rootAccess()); getTopApplication().remoteMagiskVersion > 0 && Shell.rootAccess());
} else if (event == getTopApplication().reloadMainActivity) { } else if (event == getTopApplication().reloadMainActivity) {
recreate(); recreate();
} }

View File

@ -17,7 +17,7 @@ import com.topjohnwu.magisk.adapters.ModulesAdapter;
import com.topjohnwu.magisk.components.Fragment; import com.topjohnwu.magisk.components.Fragment;
import com.topjohnwu.magisk.module.Module; import com.topjohnwu.magisk.module.Module;
import com.topjohnwu.magisk.utils.Async; 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.Logger;
import java.util.ArrayList; import java.util.ArrayList;
@ -27,7 +27,7 @@ import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.Unbinder; 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; private static final int FETCH_ZIP_CODE = 2;
@ -76,7 +76,7 @@ public class ModulesFragment extends Fragment implements CallbackHandler.EventLi
} }
@Override @Override
public void onTrigger(CallbackHandler.Event event) { public void onTrigger(CallbackEvent<Void> event) {
Logger.dev("ModulesFragment: UI refresh triggered"); Logger.dev("ModulesFragment: UI refresh triggered");
updateUI(); updateUI();
} }
@ -94,13 +94,13 @@ public class ModulesFragment extends Fragment implements CallbackHandler.EventLi
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
CallbackHandler.register(getApplication().moduleLoadDone, this); getApplication().moduleLoadDone.register(this);
getActivity().setTitle(R.string.modules); getActivity().setTitle(R.string.modules);
} }
@Override @Override
public void onStop() { public void onStop() {
CallbackHandler.unRegister(getApplication().moduleLoadDone, this); getApplication().moduleLoadDone.unRegister(this);
super.onStop(); 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.Module;
import com.topjohnwu.magisk.module.Repo; import com.topjohnwu.magisk.module.Repo;
import com.topjohnwu.magisk.utils.Async; 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.Logger;
import java.util.ArrayList; import java.util.ArrayList;
@ -29,7 +29,7 @@ import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.Unbinder; import butterknife.Unbinder;
public class ReposFragment extends Fragment implements CallbackHandler.EventListener { public class ReposFragment extends Fragment implements CallbackEvent.Listener<Void> {
private Unbinder unbinder; private Unbinder unbinder;
@BindView(R.id.recyclerView) RecyclerView recyclerView; @BindView(R.id.recyclerView) RecyclerView recyclerView;
@ -93,7 +93,7 @@ public class ReposFragment extends Fragment implements CallbackHandler.EventList
} }
@Override @Override
public void onTrigger(CallbackHandler.Event event) { public void onTrigger(CallbackEvent<Void> event) {
Logger.dev("ReposFragment: UI refresh triggered"); Logger.dev("ReposFragment: UI refresh triggered");
reloadRepos(); reloadRepos();
updateUI(); updateUI();
@ -109,13 +109,13 @@ public class ReposFragment extends Fragment implements CallbackHandler.EventList
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
CallbackHandler.register(getApplication().repoLoadDone, this); getApplication().repoLoadDone.register(this);
getActivity().setTitle(R.string.downloads); getActivity().setTitle(R.string.downloads);
} }
@Override @Override
public void onStop() { public void onStop() {
CallbackHandler.unRegister(getApplication().repoLoadDone, this); getApplication().repoLoadDone.unRegister(this);
super.onStop(); super.onStop();
} }

View File

@ -16,7 +16,7 @@ import android.widget.TextView;
import com.topjohnwu.magisk.components.Fragment; import com.topjohnwu.magisk.components.Fragment;
import com.topjohnwu.magisk.utils.Async; 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.Logger;
import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
@ -26,7 +26,7 @@ import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.Unbinder; 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; private static boolean noDialog = false;
@ -115,11 +115,16 @@ public class StatusFragment extends Fragment implements CallbackHandler.EventLis
updateUI(); updateUI();
if (getApplication().updateCheckDone.isTriggered)
updateCheckUI();
if (getApplication().safetyNetDone.isTriggered)
updateSafetyNetUI();
return v; return v;
} }
@Override @Override
public void onTrigger(CallbackHandler.Event event) { public void onTrigger(CallbackEvent<Void> event) {
if (event == getApplication().updateCheckDone) { if (event == getApplication().updateCheckDone) {
Logger.dev("StatusFragment: Update Check UI refresh triggered"); Logger.dev("StatusFragment: Update Check UI refresh triggered");
updateCheckUI(); updateCheckUI();
@ -132,21 +137,15 @@ public class StatusFragment extends Fragment implements CallbackHandler.EventLis
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
CallbackHandler.register(getApplication().updateCheckDone, this); getApplication().updateCheckDone.register(this);
CallbackHandler.register(getApplication().safetyNetDone, this); getApplication().safetyNetDone.register(this);
if (getApplication().updateCheckDone.isTriggered) {
updateCheckUI();
}
if (getApplication().safetyNetDone.isTriggered) {
updateSafetyNetUI();
}
getActivity().setTitle(R.string.status); getActivity().setTitle(R.string.status);
} }
@Override @Override
public void onStop() { public void onStop() {
CallbackHandler.unRegister(getApplication().updateCheckDone, this); getApplication().updateCheckDone.unRegister(this);
CallbackHandler.unRegister(getApplication().safetyNetDone, this); getApplication().safetyNetDone.unRegister(this);
super.onStop(); super.onStop();
} }

View File

@ -21,7 +21,7 @@ import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.components.Activity; import com.topjohnwu.magisk.components.Activity;
import com.topjohnwu.magisk.utils.Async; 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.DataInputStream;
import java.io.IOException; import java.io.IOException;
@ -29,7 +29,7 @@ import java.io.IOException;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; 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[] timeoutList = {0, -1, 10, 20, 30, 60};
private static final int SU_PROTOCOL_PARAM_MAX = 20; 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 int uid;
private Policy policy; private Policy policy;
private CountDownTimer timer; private CountDownTimer timer;
private CallbackHandler.EventListener self; private CallbackEvent.Listener<Policy> self;
private CallbackHandler.Event event = null; private CallbackEvent<Policy> event = null;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -143,14 +143,11 @@ public class SuRequestActivity extends Activity implements CallbackHandler.Event
} }
@Override @Override
public void onTrigger(CallbackHandler.Event event) { public void onTrigger(CallbackEvent<Policy> event) {
Policy policy = (Policy) event.getResult(); Policy policy = event.getResult();
String response = "socket:DENY"; String response = "socket:DENY";
if (policy != null) { if (policy != null &&policy.policy == Policy.ALLOW )
magiskManager.uidMap.remove(policy.uid); response = "socket:ALLOW";
if (policy.policy == Policy.ALLOW)
response = "socket:ALLOW";
}
try { try {
socket.getOutputStream().write((response).getBytes()); socket.getOutputStream().write((response).getBytes());
} catch (Exception ignored) {} } catch (Exception ignored) {}
@ -229,16 +226,17 @@ public class SuRequestActivity extends Activity implements CallbackHandler.Event
event = magiskManager.uidMap.get(uid); event = magiskManager.uidMap.get(uid);
if (event == null) { if (event == null) {
showRequest = true; showRequest = true;
event = new CallbackHandler.Event() { event = new CallbackEvent<Policy>() {
@Override @Override
public void trigger(Object result) { public void trigger(Policy result) {
super.trigger(result); super.trigger(result);
CallbackHandler.unRegister(this); unRegister();
magiskManager.uidMap.remove(uid);
} }
}; };
magiskManager.uidMap.put(uid, event); magiskManager.uidMap.put(uid, event);
} }
CallbackHandler.register(event, self); event.register(self);
try { try {
if (showRequest) { if (showRequest) {
policy = new Policy(uid, pm); 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);
}
}