From fb0923f3ab4a8eb329cb794d888906dc62bf7b07 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 7 Jan 2017 00:29:53 +0800 Subject: [PATCH] Magisk Hide fragment improvements --- .../topjohnwu/magisk/MagiskHideFragment.java | 56 +++++++++++-------- .../com/topjohnwu/magisk/SplashActivity.java | 1 + .../magisk/adapters/ApplicationAdapter.java | 10 +--- .../com/topjohnwu/magisk/utils/Async.java | 37 ++++++++++++ 4 files changed, 72 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java b/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java index 8f7164944..88403d0a6 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java @@ -17,24 +17,38 @@ import android.widget.SearchView; import com.topjohnwu.magisk.adapters.ApplicationAdapter; import com.topjohnwu.magisk.utils.Async; +import com.topjohnwu.magisk.utils.CallbackHandler; +import com.topjohnwu.magisk.utils.Logger; import com.topjohnwu.magisk.utils.Shell; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; +import java.util.Iterator; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; -public class MagiskHideFragment extends Fragment { +public class MagiskHideFragment extends Fragment implements CallbackHandler.EventListener { @BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout; @BindView(R.id.recyclerView) RecyclerView recyclerView; + public static List listApps, fListApps = new ArrayList<>(); + public static List hideList = new ArrayList<>(); + // Don't show in list... + public static final List blacklist = Arrays.asList( + "android", + "com.topjohnwu.magisk", + "com.google.android.gms", + "com.google.android.apps.walletnfcrel", + "com.nianticlabs.pokemongo" + ); + public static CallbackHandler.Event packageLoadDone = new CallbackHandler.Event(); + private PackageManager packageManager; private View mView; - private List listApps = new ArrayList<>(), fListApps = new ArrayList<>(); - private List hideList = new ArrayList<>(); private ApplicationAdapter appAdapter = new ApplicationAdapter(fListApps, hideList); private SearchView.OnQueryTextListener searchListener; @@ -50,14 +64,15 @@ public class MagiskHideFragment extends Fragment { mSwipeRefreshLayout.setRefreshing(true); mSwipeRefreshLayout.setOnRefreshListener(() -> { recyclerView.setVisibility(View.GONE); - new LoadApps().exec(); + new Async.LoadApps(packageManager).exec(); }); - recyclerView.swapAdapter(appAdapter, true); + recyclerView.setAdapter(appAdapter); searchListener = new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { + new FilterApps().exec(query); return false; } @@ -68,7 +83,6 @@ public class MagiskHideFragment extends Fragment { } }; - new LoadApps().exec(); return mView; } @@ -85,26 +99,22 @@ public class MagiskHideFragment extends Fragment { setHasOptionsMenu(true); mView = this.getView(); getActivity().setTitle(R.string.magiskhide); + CallbackHandler.register(packageLoadDone, this); + if (packageLoadDone.isTriggered) { + onTrigger(packageLoadDone); + } } - private class LoadApps extends Async.RootTask { - @Override - protected Void doInBackground(Void... voids) { - listApps.clear(); - hideList.clear(); - fListApps.clear(); - listApps.addAll(packageManager.getInstalledApplications(PackageManager.GET_META_DATA)); - Collections.sort(listApps, (a, b) -> a.loadLabel(packageManager).toString().toLowerCase() - .compareTo(b.loadLabel(packageManager).toString().toLowerCase())); - hideList.addAll(Shell.su(Async.MAGISK_HIDE_PATH + "list")); - fListApps.addAll(listApps); - return null; - } + @Override + public void onPause() { + super.onPause(); + CallbackHandler.unRegister(packageLoadDone, this); + } - @Override - protected void onPostExecute(Void aVoid) { - updateUI(); - } + @Override + public void onTrigger(CallbackHandler.Event event) { + Logger.dev("MagiskHideFragment: UI refresh"); + updateUI(); } private class FilterApps extends Async.NormalTask { diff --git a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java index a6113fb04..d897f3f69 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java @@ -42,6 +42,7 @@ public class SplashActivity extends AppCompatActivity { new Async.LoadRepos(getApplicationContext()).exec(); } }.exec(); + new Async.LoadApps(getPackageManager()).exec(); // Start main activity Intent intent = new Intent(getApplicationContext(), MainActivity.class); 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 8d7f45903..fd1bf54fd 100644 --- a/app/src/main/java/com/topjohnwu/magisk/adapters/ApplicationAdapter.java +++ b/app/src/main/java/com/topjohnwu/magisk/adapters/ApplicationAdapter.java @@ -29,19 +29,11 @@ public class ApplicationAdapter extends RecyclerView.Adapter list, List hideList) { mList = list; mHideList = hideList; - List bl = Arrays.asList(blackList); - for (int i = 0; i < mList.size(); ++i) - if (bl.contains(mList.get(i).packageName)) - mList.remove(i); } @Override diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Async.java b/app/src/main/java/com/topjohnwu/magisk/utils/Async.java index 9ed74fbe4..21438d81a 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Async.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Async.java @@ -2,6 +2,8 @@ package com.topjohnwu.magisk.utils; import android.app.ProgressDialog; import android.content.Context; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; import android.os.AsyncTask; @@ -10,6 +12,7 @@ import android.util.Log; import android.widget.Toast; import com.topjohnwu.magisk.InstallFragment; +import com.topjohnwu.magisk.MagiskHideFragment; import com.topjohnwu.magisk.MainActivity; import com.topjohnwu.magisk.ModulesFragment; import com.topjohnwu.magisk.R; @@ -25,6 +28,8 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.Collections; +import java.util.Iterator; import java.util.List; public class Async { @@ -114,6 +119,38 @@ public class Async { } } + public static class LoadApps extends RootTask { + + private PackageManager pm; + + public LoadApps(PackageManager packageManager) { + pm = packageManager; + } + + @Override + protected Void doInBackground(Void... voids) { + if (! Shell.rootAccess()) return null; + MagiskHideFragment.hideList.clear(); + MagiskHideFragment.fListApps.clear(); + MagiskHideFragment.listApps = pm.getInstalledApplications(PackageManager.GET_META_DATA); + for (Iterator i = MagiskHideFragment.listApps.iterator(); i.hasNext(); ) { + ApplicationInfo info = i.next(); + if (MagiskHideFragment.blacklist.contains(info.packageName) || !info.enabled) + i.remove(); + } + Collections.sort(MagiskHideFragment.listApps, (a, b) -> a.loadLabel(pm).toString().toLowerCase() + .compareTo(b.loadLabel(pm).toString().toLowerCase())); + MagiskHideFragment.hideList.addAll(Shell.su(Async.MAGISK_HIDE_PATH + "list")); + MagiskHideFragment.fListApps.addAll(MagiskHideFragment.listApps); + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + MagiskHideFragment.packageLoadDone.trigger(); + } + } + public static class FlashZIP extends RootTask { protected Uri mUri;