From 72820b162c7ce9796600d2503f8716630a15aa28 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 21 Jul 2017 05:08:39 +0800 Subject: [PATCH] Code cleanups --- .../topjohnwu/magisk/MagiskHideFragment.java | 19 ++---- .../com/topjohnwu/magisk/MagiskManager.java | 3 - .../topjohnwu/magisk/SettingsActivity.java | 15 +---- .../com/topjohnwu/magisk/SplashActivity.java | 2 - .../magisk/adapters/ApplicationAdapter.java | 61 ++++++++++++++----- .../com/topjohnwu/magisk/asyncs/LoadApps.java | 44 ------------- .../topjohnwu/magisk/asyncs/MagiskHide.java | 61 ------------------- .../com/topjohnwu/magisk/utils/Utils.java | 20 ++++++ 8 files changed, 71 insertions(+), 154 deletions(-) delete mode 100644 app/src/main/java/com/topjohnwu/magisk/asyncs/LoadApps.java delete mode 100644 app/src/main/java/com/topjohnwu/magisk/asyncs/MagiskHide.java diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java b/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java index 05e3191c3..25da18629 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java @@ -1,12 +1,10 @@ package com.topjohnwu.magisk; -import android.content.pm.PackageManager; 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.text.TextUtils; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -15,7 +13,6 @@ import android.view.ViewGroup; import android.widget.SearchView; import com.topjohnwu.magisk.adapters.ApplicationAdapter; -import com.topjohnwu.magisk.asyncs.MagiskHide; import com.topjohnwu.magisk.components.Fragment; import com.topjohnwu.magisk.utils.CallbackEvent; import com.topjohnwu.magisk.utils.Logger; @@ -46,13 +43,12 @@ public class MagiskHideFragment extends Fragment implements CallbackEvent.Listen public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_magisk_hide, container, false); unbinder = ButterKnife.bind(this, view); - - PackageManager packageManager = getActivity().getPackageManager(); + lastFilter = ""; mSwipeRefreshLayout.setRefreshing(true); - mSwipeRefreshLayout.setOnRefreshListener(() -> new MagiskHide(getActivity()).list()); + mSwipeRefreshLayout.setOnRefreshListener(() -> appAdapter.refresh()); - appAdapter = new ApplicationAdapter(packageManager); + appAdapter = new ApplicationAdapter(getActivity()); recyclerView.setAdapter(appAdapter); searchListener = new SearchView.OnQueryTextListener() { @@ -71,10 +67,6 @@ public class MagiskHideFragment extends Fragment implements CallbackEvent.Listen } }; - if (getApplication().magiskHideDone.isTriggered) { - onTrigger(getApplication().magiskHideDone); - } - return view; } @@ -107,10 +99,7 @@ public class MagiskHideFragment extends Fragment implements CallbackEvent.Listen @Override public void onTrigger(CallbackEvent event) { Logger.dev("MagiskHideFragment: UI refresh"); - appAdapter.setLists(getApplication().appList, getApplication().magiskHideList); mSwipeRefreshLayout.setRefreshing(false); - if (!TextUtils.isEmpty(lastFilter)) { - appAdapter.filter(lastFilter); - } + appAdapter.filter(lastFilter); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java index 53b956c26..f984cbd0b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java @@ -5,7 +5,6 @@ import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Context; import android.content.SharedPreferences; -import android.content.pm.ApplicationInfo; import android.os.Build; import android.os.Handler; import android.preference.PreferenceManager; @@ -66,8 +65,6 @@ public class MagiskManager extends Application { // Data public Map moduleMap; public List blockList; - public List appList; - public List magiskHideList; // Configurations public static boolean shellLogging; diff --git a/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java b/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java index 82559d742..a7c5fb875 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java @@ -13,9 +13,7 @@ import android.support.v7.app.ActionBar; import android.support.v7.widget.Toolbar; import android.widget.Toast; -import com.topjohnwu.magisk.asyncs.MagiskHide; import com.topjohnwu.magisk.components.Activity; -import com.topjohnwu.magisk.components.AlertDialogBuilder; import com.topjohnwu.magisk.database.SuDatabaseHelper; import com.topjohnwu.magisk.utils.Logger; import com.topjohnwu.magisk.utils.Shell; @@ -158,18 +156,9 @@ public class SettingsActivity extends Activity { case "magiskhide": enabled = prefs.getBoolean("magiskhide", false); if (enabled) { - if (!magiskManager.isSuClient) { - new AlertDialogBuilder(getActivity()) - .setTitle(R.string.no_magisksu_title) - .setMessage(R.string.no_magisksu_msg) - .setPositiveButton(R.string.understand, (dialog, which) -> new MagiskHide(getActivity()).enable()) - .setCancelable(false) - .show(); - } else { - new MagiskHide(getActivity()).enable(); - } + Utils.enableMagiskHide(magiskManager.shell); } else { - new MagiskHide(getActivity()).disable(); + Utils.disableMagiskHide(magiskManager.shell); } break; case "hosts": diff --git a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java index 0512e8702..b44749237 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java @@ -8,7 +8,6 @@ import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; -import com.topjohnwu.magisk.asyncs.LoadApps; import com.topjohnwu.magisk.asyncs.LoadModules; import com.topjohnwu.magisk.asyncs.UpdateRepos; import com.topjohnwu.magisk.components.Activity; @@ -53,7 +52,6 @@ public class SplashActivity extends Activity{ // Now fire all async tasks loadModuleTask.exec(); - new LoadApps(this).exec(); Intent intent = new Intent(this, MainActivity.class); String section = getIntent().getStringExtra(MagiskManager.INTENT_SECTION); 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 228e7f6e7..8e4d6f854 100644 --- a/app/src/main/java/com/topjohnwu/magisk/adapters/ApplicationAdapter.java +++ b/app/src/main/java/com/topjohnwu/magisk/adapters/ApplicationAdapter.java @@ -1,6 +1,6 @@ package com.topjohnwu.magisk.adapters; -import android.app.Activity; +import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.support.design.widget.Snackbar; @@ -14,13 +14,16 @@ import android.widget.ImageView; import android.widget.TextView; import com.topjohnwu.magisk.R; -import com.topjohnwu.magisk.asyncs.MagiskHide; +import com.topjohnwu.magisk.asyncs.ParallelTask; import com.topjohnwu.magisk.components.SnackbarMaker; +import com.topjohnwu.magisk.utils.CallbackEvent; +import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Utils; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.Iterator; import java.util.List; import butterknife.BindView; @@ -41,20 +44,19 @@ public class ApplicationAdapter extends RecyclerView.Adapter mOriginalList, mList; private List mHideList; - private PackageManager packageManager; + private PackageManager pm; private ApplicationFilter filter; + private CallbackEvent magiskHideDone; + private Shell shell; - public ApplicationAdapter(PackageManager packageManager) { + public ApplicationAdapter(Context context) { mOriginalList = mList = Collections.emptyList(); mHideList = Collections.emptyList(); - this.packageManager = packageManager; filter = new ApplicationFilter(); - } - - public void setLists(List listApps, List hideList) { - mOriginalList = mList = listApps; - mHideList = hideList; - notifyDataSetChanged(); + pm = context.getPackageManager(); + magiskHideDone = Utils.getMagiskManager(context).magiskHideDone; + shell = Utils.getMagiskManager(context).shell; + new LoadApps().exec(); } @Override @@ -67,8 +69,8 @@ public class ApplicationAdapter extends RecyclerView.Adapter { if (isChecked) { - new MagiskHide((Activity) holder.itemView.getContext()).add(info.packageName); + Utils.addMagiskHide(shell, info.packageName); mHideList.add(info.packageName); } else { - new MagiskHide((Activity) holder.itemView.getContext()).rm(info.packageName); + Utils.rmMagiskHide(shell, info.packageName); mHideList.remove(info.packageName); } }); @@ -106,6 +108,10 @@ public class ApplicationAdapter extends RecyclerView.Adapter(); String filter = constraint.toString().toLowerCase(); for (ApplicationInfo info : mOriginalList) { - if (Utils.lowercaseContains(info.loadLabel(packageManager), filter) + if (Utils.lowercaseContains(info.loadLabel(pm), filter) || Utils.lowercaseContains(info.packageName, filter)) { filteredApps.add(info); } @@ -150,4 +156,27 @@ public class ApplicationAdapter extends RecyclerView.Adapter { + + @Override + protected Void doInBackground(Void... voids) { + mOriginalList = pm.getInstalledApplications(0); + for (Iterator i = mOriginalList.iterator(); i.hasNext(); ) { + ApplicationInfo info = i.next(); + if (ApplicationAdapter.BLACKLIST.contains(info.packageName) || !info.enabled) { + i.remove(); + } + } + Collections.sort(mOriginalList, (a, b) -> a.loadLabel(pm).toString().toLowerCase() + .compareTo(b.loadLabel(pm).toString().toLowerCase())); + mHideList = Utils.listMagiskHide(shell); + return null; + } + + @Override + protected void onPostExecute(Void v) { + magiskHideDone.trigger(); + } + } } diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/LoadApps.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/LoadApps.java deleted file mode 100644 index ec38297b7..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/LoadApps.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.topjohnwu.magisk.asyncs; - -import android.content.Context; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageManager; - -import com.topjohnwu.magisk.MagiskManager; -import com.topjohnwu.magisk.adapters.ApplicationAdapter; - -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - -public class LoadApps extends ParallelTask { - - public LoadApps(Context context) { - super(context); - } - - @Override - protected Void doInBackground(Void... voids) { - MagiskManager magiskManager = getMagiskManager(); - if (magiskManager == null) return null; - PackageManager pm = magiskManager.getPackageManager(); - List list = pm.getInstalledApplications(0); - for (Iterator i = list.iterator(); i.hasNext(); ) { - ApplicationInfo info = i.next(); - if (ApplicationAdapter.BLACKLIST.contains(info.packageName) || !info.enabled) { - i.remove(); - } - } - Collections.sort(list, (a, b) -> a.loadLabel(pm).toString().toLowerCase() - .compareTo(b.loadLabel(pm).toString().toLowerCase())); - magiskManager.appList = Collections.unmodifiableList(list); - return null; - } - - @Override - protected void onPostExecute(Void v) { - MagiskManager magiskManager = getMagiskManager(); - if (magiskManager == null) return; - new MagiskHide(magiskManager).list(); - } -} diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/MagiskHide.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/MagiskHide.java deleted file mode 100644 index 531829c74..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/MagiskHide.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.topjohnwu.magisk.asyncs; - -import android.content.Context; - -import com.topjohnwu.magisk.MagiskManager; - -import java.util.List; - -public class MagiskHide extends ParallelTask { - - private boolean isList = false; - - public MagiskHide(Context context) { - super(context); - } - - @Override - protected Void doInBackground(Object... params) { - MagiskManager magiskManager = getMagiskManager(); - if (magiskManager == null) return null; - String command = (String) params[0]; - List ret = magiskManager.shell.su("magiskhide --" + command); - if (isList) { - magiskManager.magiskHideList = ret; - } - return null; - } - - @Override - protected void onPostExecute(Void v) { - MagiskManager magiskManager = getMagiskManager(); - if (magiskManager == null) return; - if (isList) { - magiskManager.magiskHideDone.trigger(); - } - super.onPostExecute(v); - } - - public void add(CharSequence packageName) { - exec("add " + packageName); - } - - public void rm(CharSequence packageName) { - exec("rm " + packageName); - } - - public void enable() { - exec("enable"); - } - - public void disable() { - exec("disable"); - } - - public void list() { - isList = true; - if (getMagiskManager() == null) return; - exec("ls"); - } - -} 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 2b7a6d840..10a5c4995 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -226,4 +226,24 @@ public class Utils { (NotificationManager) magiskManager.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(APK_UPDATE_NOTIFICATION_ID, builder.build()); } + + public static void enableMagiskHide(Shell shell) { + shell.su_raw("magiskhide --enable"); + } + + public static void disableMagiskHide(Shell shell) { + shell.su_raw("magiskhide --disable"); + } + + public static List listMagiskHide(Shell shell) { + return shell.su("magiskhide --ls"); + } + + public static void addMagiskHide(Shell shell, String pkg) { + shell.su_raw("magiskhide --add " + pkg); + } + + public static void rmMagiskHide(Shell shell, String pkg) { + shell.su_raw("magiskhide --rm " + pkg); + } } \ No newline at end of file