Update CallbackEvents

This commit is contained in:
topjohnwu 2017-07-23 00:12:15 +08:00
parent bd6585765e
commit 36124ddca4
13 changed files with 125 additions and 131 deletions

View File

@ -49,7 +49,7 @@ import butterknife.OnClick;
import butterknife.Unbinder;
public class MagiskFragment extends Fragment
implements CallbackEvent.Listener<Void>, SwipeRefreshLayout.OnRefreshListener {
implements CallbackEvent.Listener, SwipeRefreshLayout.OnRefreshListener {
public static final String SHOW_DIALOG = "dialog";
@ -253,6 +253,8 @@ public class MagiskFragment extends Fragment
if (getArguments() != null && getArguments().getBoolean(SHOW_DIALOG))
install();
getActivity().setTitle(R.string.magisk);
return v;
}
@ -281,7 +283,7 @@ public class MagiskFragment extends Fragment
}
@Override
public void onTrigger(CallbackEvent<Void> event) {
public void onTrigger(CallbackEvent event) {
if (event == magiskManager.updateCheckDone) {
updateCheckUI();
} else if (event == magiskManager.safetyNetDone) {
@ -290,23 +292,8 @@ public class MagiskFragment extends Fragment
}
@Override
public void onStart() {
super.onStart();
// Manual trigger if already done
if (magiskManager.updateCheckDone.isTriggered)
updateCheckUI();
if (magiskManager.safetyNetDone.isTriggered)
updateSafetyNetUI();
magiskManager.updateCheckDone.register(this);
magiskManager.safetyNetDone.register(this);
getActivity().setTitle(R.string.magisk);
}
@Override
public void onStop() {
magiskManager.updateCheckDone.unRegister(this);
magiskManager.safetyNetDone.unRegister(this);
super.onStop();
public CallbackEvent[] getRegisterEvents() {
return new CallbackEvent[] { magiskManager.updateCheckDone, magiskManager.safetyNetDone };
}
@Override

View File

@ -2,7 +2,6 @@ package com.topjohnwu.magisk;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
@ -15,13 +14,12 @@ 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.Logger;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
public class MagiskHideFragment extends Fragment implements CallbackEvent.Listener<Void> {
public class MagiskHideFragment extends Fragment implements CallbackEvent.Listener {
private Unbinder unbinder;
@BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
@ -67,29 +65,18 @@ public class MagiskHideFragment extends Fragment implements CallbackEvent.Listen
}
};
getActivity().setTitle(R.string.magiskhide);
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_magiskhide, menu);
SearchView search = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.app_search));
SearchView search = (SearchView) menu.findItem(R.id.app_search).getActionView();
search.setOnQueryTextListener(searchListener);
}
@Override
public void onStart() {
super.onStart();
getActivity().setTitle(R.string.magiskhide);
getApplication().magiskHideDone.register(this);
}
@Override
public void onStop() {
getApplication().magiskHideDone.unRegister(this);
super.onStop();
}
@Override
public void onDestroyView() {
super.onDestroyView();
@ -97,9 +84,13 @@ public class MagiskHideFragment extends Fragment implements CallbackEvent.Listen
}
@Override
public void onTrigger(CallbackEvent<Void> event) {
Logger.dev("MagiskHideFragment: UI refresh");
public void onTrigger(CallbackEvent event) {
mSwipeRefreshLayout.setRefreshing(false);
appAdapter.filter(lastFilter);
}
@Override
public CallbackEvent[] getRegisterEvents() {
return new CallbackEvent[] { getApplication().magiskHideDone };
}
}

View File

@ -18,7 +18,6 @@ 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.Logger;
import com.topjohnwu.magisk.utils.SafetyNetHelper;
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Utils;
@ -44,13 +43,13 @@ public class MagiskManager extends Application {
public static final String NOTIFICATION_CHANNEL = "magisk_update_notice";
// Events
public final CallbackEvent<Void> magiskHideDone = 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 final CallbackEvent<Void> localeDone = new CallbackEvent<>();
public final CallbackEvent magiskHideDone = 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 final CallbackEvent localeDone = new CallbackEvent();
// Info
public String magiskVersionString;

View File

@ -28,7 +28,7 @@ import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends Activity
implements NavigationView.OnNavigationItemSelectedListener, CallbackEvent.Listener<Void> {
implements NavigationView.OnNavigationItemSelectedListener, CallbackEvent.Listener {
private final Handler mDrawerHandler = new Handler();
private SharedPreferences prefs;
@ -81,7 +81,6 @@ public class MainActivity extends Activity
navigate(getIntent().getStringExtra(MagiskManager.INTENT_SECTION));
navigationView.setNavigationItemSelectedListener(this);
getApplicationContext().reloadMainActivity.register(this);
}
@ -91,12 +90,6 @@ public class MainActivity extends Activity
checkHideSection();
}
@Override
protected void onDestroy() {
getApplicationContext().reloadMainActivity.unRegister(this);
super.onDestroy();
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(navigationView)) {
@ -117,10 +110,15 @@ public class MainActivity extends Activity
}
@Override
public void onTrigger(CallbackEvent<Void> event) {
public void onTrigger(CallbackEvent event) {
recreate();
}
@Override
public CallbackEvent[] getRegisterEvents() {
return new CallbackEvent[] { getApplicationContext().reloadMainActivity };
}
public void checkHideSection() {
Menu menu = navigationView.getMenu();
menu.findItem(R.id.magiskhide).setVisible(

View File

@ -26,7 +26,7 @@ import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
public class ModulesFragment extends Fragment implements CallbackEvent.Listener<Void> {
public class ModulesFragment extends Fragment implements CallbackEvent.Listener {
private static final int FETCH_ZIP_CODE = 2;
@ -67,19 +67,22 @@ public class ModulesFragment extends Fragment implements CallbackEvent.Listener<
}
});
if (getApplication().moduleLoadDone.isTriggered) {
updateUI();
}
getActivity().setTitle(R.string.modules);
return view;
}
@Override
public void onTrigger(CallbackEvent<Void> event) {
public void onTrigger(CallbackEvent event) {
Logger.dev("ModulesFragment: UI refresh triggered");
updateUI();
}
@Override
public CallbackEvent[] getRegisterEvents() {
return new CallbackEvent[] { getApplication().moduleLoadDone };
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FETCH_ZIP_CODE && resultCode == Activity.RESULT_OK && data != null) {
@ -88,20 +91,6 @@ public class ModulesFragment extends Fragment implements CallbackEvent.Listener<
intent.setData(data.getData()).putExtra("ACTION", "flash");
startActivity(intent);
}
}
@Override
public void onStart() {
super.onStart();
getApplication().moduleLoadDone.register(this);
getActivity().setTitle(R.string.modules);
}
@Override
public void onStop() {
getApplication().moduleLoadDone.unRegister(this);
super.onStop();
}
@Override

View File

@ -22,7 +22,7 @@ import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
public class ReposFragment extends Fragment implements CallbackEvent.Listener<Void> {
public class ReposFragment extends Fragment implements CallbackEvent.Listener {
private Unbinder unbinder;
@BindView(R.id.recyclerView) RecyclerView recyclerView;
@ -53,15 +53,13 @@ public class ReposFragment extends Fragment implements CallbackEvent.Listener<Vo
new UpdateRepos(getActivity()).exec();
});
if (getApplication().repoLoadDone.isTriggered) {
onTrigger(null);
}
getActivity().setTitle(R.string.downloads);
return view;
}
@Override
public void onTrigger(CallbackEvent<Void> event) {
public void onTrigger(CallbackEvent event) {
Logger.dev("ReposFragment: UI refresh triggered");
mSwipeRefreshLayout.setRefreshing(false);
adapter.notifyDBChanged();
@ -69,6 +67,11 @@ public class ReposFragment extends Fragment implements CallbackEvent.Listener<Vo
emptyRv.setVisibility(adapter.getItemCount() == 0 ? View.VISIBLE : View.GONE);
}
@Override
public CallbackEvent[] getRegisterEvents() {
return new CallbackEvent[] { getApplication().repoLoadDone };
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_repo, menu);
@ -87,19 +90,6 @@ public class ReposFragment extends Fragment implements CallbackEvent.Listener<Vo
});
}
@Override
public void onStart() {
super.onStart();
getApplication().repoLoadDone.register(this);
getActivity().setTitle(R.string.downloads);
}
@Override
public void onStop() {
getApplication().repoLoadDone.unRegister(this);
super.onStop();
}
@Override
public void onDestroyView() {
super.onDestroyView();

View File

@ -59,7 +59,7 @@ public class SettingsActivity extends Activity {
public static class SettingsFragment extends PreferenceFragment
implements SharedPreferences.OnSharedPreferenceChangeListener,
CallbackEvent.Listener<Void>{
CallbackEvent.Listener {
private SharedPreferences prefs;
private PreferenceScreen prefScreen;
@ -149,16 +149,14 @@ public class SettingsActivity extends Activity {
public void onResume() {
super.onResume();
prefs.registerOnSharedPreferenceChangeListener(this);
magiskManager.localeDone.register(this);
if (magiskManager.localeDone.isTriggered)
onTrigger(null);
registerEvents();
}
@Override
public void onPause() {
super.onPause();
prefs.unregisterOnSharedPreferenceChangeListener(this);
magiskManager.localeDone.unRegister(this);
unregisterEvents();
super.onPause();
}
@Override
@ -171,7 +169,7 @@ public class SettingsActivity extends Activity {
enabled = prefs.getBoolean("dark_theme", false);
if (magiskManager.isDarkTheme != enabled) {
magiskManager.isDarkTheme = enabled;
magiskManager.reloadMainActivity.trigger();
magiskManager.reloadMainActivity.trigger(false);
getActivity().recreate();
}
break;
@ -233,7 +231,7 @@ public class SettingsActivity extends Activity {
break;
case "locale":
magiskManager.setLocale();
magiskManager.reloadMainActivity.trigger();
magiskManager.reloadMainActivity.trigger(false);
getActivity().recreate();
break;
}
@ -256,7 +254,7 @@ public class SettingsActivity extends Activity {
}
@Override
public void onTrigger(CallbackEvent<Void> event) {
public void onTrigger(CallbackEvent event) {
ListPreference language = setLocalePreference(null);
language.setOnPreferenceClickListener((pref) -> {
setLocalePreference((ListPreference) pref);
@ -264,6 +262,11 @@ public class SettingsActivity extends Activity {
});
generalCatagory.addPreference(language);
}
@Override
public CallbackEvent[] getRegisterEvents() {
return new CallbackEvent[] { magiskManager.localeDone };
}
}
}

View File

@ -46,7 +46,7 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.
private List<String> mHideList;
private PackageManager pm;
private ApplicationFilter filter;
private CallbackEvent<Void> magiskHideDone;
private CallbackEvent magiskHideDone;
private Shell shell;
public ApplicationAdapter(Context context) {
@ -176,7 +176,7 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.
@Override
protected void onPostExecute(Void v) {
magiskHideDone.trigger();
magiskHideDone.trigger(false);
}
}
}

View File

@ -1,14 +1,14 @@
package com.topjohnwu.magisk.components;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Utils;
import java.util.Locale;
import com.topjohnwu.magisk.utils.CallbackEvent;
public class Activity extends AppCompatActivity {
@ -19,6 +19,22 @@ public class Activity extends AppCompatActivity {
applyOverrideConfiguration(configuration);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (this instanceof CallbackEvent.Listener) {
((CallbackEvent.Listener) this).registerEvents();
}
}
@Override
protected void onDestroy() {
if (this instanceof CallbackEvent.Listener) {
((CallbackEvent.Listener) this).unregisterEvents();
}
super.onDestroy();
}
@Override
public MagiskManager getApplicationContext() {
return (MagiskManager) super.getApplicationContext();

View File

@ -1,6 +1,7 @@
package com.topjohnwu.magisk.components;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.utils.CallbackEvent;
import com.topjohnwu.magisk.utils.Utils;
public class Fragment extends android.support.v4.app.Fragment {
@ -9,4 +10,19 @@ public class Fragment extends android.support.v4.app.Fragment {
return Utils.getMagiskManager(getActivity());
}
@Override
public void onResume() {
super.onResume();
if (this instanceof CallbackEvent.Listener) {
((CallbackEvent.Listener) this).registerEvents();
}
}
@Override
public void onPause() {
if (this instanceof CallbackEvent.Listener) {
((CallbackEvent.Listener) this).unregisterEvents();
}
super.onPause();
}
}

View File

@ -10,7 +10,6 @@ import com.topjohnwu.magisk.MagiskManager;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class SuLogEntry implements Parcelable {

View File

@ -1,32 +1,29 @@
package com.topjohnwu.magisk.utils;
import java.lang.ref.WeakReference;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
public class CallbackEvent<Result> {
public class CallbackEvent {
public boolean isTriggered = false;
private Result result;
private List<WeakReference<Listener<Result>>> listeners;
private List<WeakReference<Listener>> listeners;
public void register(Listener<Result> l) {
public void register(Listener l) {
if (listeners == null) {
listeners = new LinkedList<>();
}
listeners.add(new WeakReference<>(l));
}
public void unRegister() {
public void unregister() {
listeners = null;
}
public void unRegister(Listener<Result> l) {
for (Iterator<WeakReference<Listener<Result>>> i = listeners.iterator(); i.hasNext();) {
WeakReference<Listener<Result>> listener = i.next();
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();
}
@ -34,25 +31,38 @@ public class CallbackEvent<Result> {
}
public void trigger() {
trigger(null);
trigger(true);
}
public void trigger(Result r) {
result = r;
isTriggered = true;
public void trigger(boolean b) {
isTriggered = b;
if (listeners != null) {
for (WeakReference<Listener<Result>> listener : listeners) {
for (WeakReference<Listener> listener : listeners) {
if (listener.get() != null)
listener.get().onTrigger(this);
}
}
}
public Result getResult() {
return result;
}
public interface Listener<R> {
void onTrigger(CallbackEvent<R> event);
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

@ -11,14 +11,12 @@ import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Environment;
import android.provider.OpenableColumns;
import android.support.annotation.IdRes;
import android.support.annotation.StringRes;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
@ -26,7 +24,6 @@ import android.support.v4.app.FragmentActivity;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.NotificationCompat;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.widget.Toast;
import com.topjohnwu.magisk.MagiskManager;
@ -40,7 +37,6 @@ import com.topjohnwu.magisk.receivers.ManagerUpdate;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;