From e17638bc068679edd10e8fa9ae38f0f408fe9811 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 11 Nov 2016 21:45:03 +0800 Subject: [PATCH] Add search to MagiskHide --- .../topjohnwu/magisk/MagiskHideFragment.java | 53 ++++++++++++++++--- app/src/main/res/menu/menu_magiskhide.xml | 9 ++++ 2 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 app/src/main/res/menu/menu_magiskhide.xml diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java b/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java index 94fac596f..eea6259fc 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java @@ -5,16 +5,21 @@ import android.content.pm.ApplicationInfo; 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.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.SearchView; import com.topjohnwu.magisk.adapters.ApplicationAdapter; import com.topjohnwu.magisk.utils.Async; import com.topjohnwu.magisk.utils.Shell; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -28,7 +33,11 @@ public class MagiskHideFragment extends Fragment { private PackageManager packageManager; private View mView; - private ApplicationAdapter appAdapter; + private List listApps = new ArrayList<>(), fListApps = new ArrayList<>(); + private List hideList = new ArrayList<>(); + private ApplicationAdapter appAdapter = new ApplicationAdapter(fListApps, hideList); + + private SearchView.OnQueryTextListener searchListener; @Nullable @Override @@ -44,36 +53,68 @@ public class MagiskHideFragment extends Fragment { new LoadApps().exec(); }); + recyclerView.setAdapter(appAdapter); + + searchListener = new SearchView.OnQueryTextListener() { + @Override + public boolean onQueryTextSubmit(String query) { + return false; + } + + @Override + public boolean onQueryTextChange(String newText) { + fListApps.clear(); + for (ApplicationInfo info : listApps) { + if (info.loadLabel(packageManager).toString().contains(newText) + || info.packageName.contains(newText)) { + fListApps.add(info); + } + } + appAdapter.notifyDataSetChanged(); + return false; + } + }; + new LoadApps().exec(); return mView; } + @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)); + search.setOnQueryTextListener(searchListener); + } + @Override public void onResume() { super.onResume(); + setHasOptionsMenu(true); mView = this.getView(); } private class LoadApps extends Async.RootTask { @Override protected Void doInBackground(Void... voids) { - List listApps = packageManager.getInstalledApplications(PackageManager.GET_META_DATA); + 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())); - List hideList = Shell.su(Async.MAGISK_HIDE_PATH + "list"); - appAdapter = new ApplicationAdapter(listApps, hideList); + hideList.addAll(Shell.su(Async.MAGISK_HIDE_PATH + "list")); + fListApps.addAll(listApps); return null; } @Override protected void onPostExecute(Void aVoid) { - super.onPostExecute(aVoid); updateUI(); } } private void updateUI() { - recyclerView.setAdapter(appAdapter); + appAdapter.notifyDataSetChanged(); recyclerView.setVisibility(View.VISIBLE); mSwipeRefreshLayout.setRefreshing(false); } diff --git a/app/src/main/res/menu/menu_magiskhide.xml b/app/src/main/res/menu/menu_magiskhide.xml new file mode 100644 index 000000000..58f15c364 --- /dev/null +++ b/app/src/main/res/menu/menu_magiskhide.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file