From 36124ddca4d4a643bb32486c427e6dd9b9f59261 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 23 Jul 2017 00:12:15 +0800 Subject: [PATCH] Update CallbackEvents --- .../com/topjohnwu/magisk/MagiskFragment.java | 25 +++------- .../topjohnwu/magisk/MagiskHideFragment.java | 29 ++++------- .../com/topjohnwu/magisk/MagiskManager.java | 15 +++--- .../com/topjohnwu/magisk/MainActivity.java | 16 +++--- .../com/topjohnwu/magisk/ModulesFragment.java | 27 +++------- .../com/topjohnwu/magisk/ReposFragment.java | 26 +++------- .../topjohnwu/magisk/SettingsActivity.java | 21 ++++---- .../magisk/adapters/ApplicationAdapter.java | 4 +- .../topjohnwu/magisk/components/Activity.java | 22 ++++++-- .../topjohnwu/magisk/components/Fragment.java | 16 ++++++ .../magisk/superuser/SuLogEntry.java | 1 - .../topjohnwu/magisk/utils/CallbackEvent.java | 50 +++++++++++-------- .../com/topjohnwu/magisk/utils/Utils.java | 4 -- 13 files changed, 125 insertions(+), 131 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java b/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java index d752be43b..8cdf8163e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java @@ -49,7 +49,7 @@ import butterknife.OnClick; import butterknife.Unbinder; public class MagiskFragment extends Fragment - implements CallbackEvent.Listener, 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 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 diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java b/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java index 25da18629..59f616ca6 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java @@ -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 { +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 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 }; + } } diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java index a3d29041f..e96f9e10f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java @@ -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 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<>(); + 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; diff --git a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java index 06b9078d9..9fc19df3f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java @@ -28,7 +28,7 @@ import butterknife.BindView; import butterknife.ButterKnife; public class MainActivity extends Activity - implements NavigationView.OnNavigationItemSelectedListener, CallbackEvent.Listener { + 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 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( diff --git a/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java b/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java index 758a05e5a..266108527 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java @@ -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 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 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 diff --git a/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java b/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java index f82f4d3e9..9fea50d9e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java @@ -22,7 +22,7 @@ import butterknife.BindView; import butterknife.ButterKnife; import butterknife.Unbinder; -public class ReposFragment extends Fragment implements CallbackEvent.Listener { +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 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{ + 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 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 }; + } } } diff --git a/app/src/main/java/com/topjohnwu/magisk/adapters/ApplicationAdapter.java b/app/src/main/java/com/topjohnwu/magisk/adapters/ApplicationAdapter.java index 8e4d6f854..3aaadecf5 100644 --- a/app/src/main/java/com/topjohnwu/magisk/adapters/ApplicationAdapter.java +++ b/app/src/main/java/com/topjohnwu/magisk/adapters/ApplicationAdapter.java @@ -46,7 +46,7 @@ public class ApplicationAdapter extends RecyclerView.Adapter mHideList; private PackageManager pm; private ApplicationFilter filter; - private CallbackEvent magiskHideDone; + private CallbackEvent magiskHideDone; private Shell shell; public ApplicationAdapter(Context context) { @@ -176,7 +176,7 @@ public class ApplicationAdapter extends RecyclerView.Adapter { +public class CallbackEvent { public boolean isTriggered = false; - private Result result; - private List>> listeners; + private List> listeners; - public void register(Listener 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 l) { - for (Iterator>> i = listeners.iterator(); i.hasNext();) { - WeakReference> listener = i.next(); + public void unregister(Listener l) { + for (Iterator> i = listeners.iterator(); i.hasNext();) { + WeakReference listener = i.next(); if (listener.get() == null || listener.get() == l) { i.remove(); } @@ -34,25 +31,38 @@ public class CallbackEvent { } 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 : listeners) { + for (WeakReference listener : listeners) { if (listener.get() != null) listener.get().onTrigger(this); } } } - public Result getResult() { - return result; - } - public interface Listener { - void onTrigger(CallbackEvent 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(); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java index 76b703567..2ce645402 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -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;