Sort hidden apps to the top

This commit is contained in:
topjohnwu 2018-07-04 21:15:26 +08:00
parent 60296493fe
commit 2e091b04e5
2 changed files with 39 additions and 37 deletions

View File

@ -28,7 +28,6 @@ public class MagiskHideFragment extends Fragment implements Topic.Subscriber {
private ApplicationAdapter appAdapter; private ApplicationAdapter appAdapter;
private SearchView.OnQueryTextListener searchListener; private SearchView.OnQueryTextListener searchListener;
private String lastFilter;
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
@ -41,25 +40,22 @@ public class MagiskHideFragment extends Fragment implements Topic.Subscriber {
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_magisk_hide, container, false); View view = inflater.inflate(R.layout.fragment_magisk_hide, container, false);
unbinder = ButterKnife.bind(this, view); unbinder = ButterKnife.bind(this, view);
lastFilter = "";
mSwipeRefreshLayout.setRefreshing(true); mSwipeRefreshLayout.setRefreshing(true);
mSwipeRefreshLayout.setOnRefreshListener(() -> appAdapter.refresh()); mSwipeRefreshLayout.setOnRefreshListener(() -> appAdapter.refresh());
appAdapter = new ApplicationAdapter(getActivity()); appAdapter = new ApplicationAdapter();
recyclerView.setAdapter(appAdapter); recyclerView.setAdapter(appAdapter);
searchListener = new SearchView.OnQueryTextListener() { searchListener = new SearchView.OnQueryTextListener() {
@Override @Override
public boolean onQueryTextSubmit(String query) { public boolean onQueryTextSubmit(String query) {
lastFilter = query;
appAdapter.filter(query); appAdapter.filter(query);
return false; return false;
} }
@Override @Override
public boolean onQueryTextChange(String newText) { public boolean onQueryTextChange(String newText) {
lastFilter = newText;
appAdapter.filter(newText); appAdapter.filter(newText);
return false; return false;
} }
@ -86,7 +82,7 @@ public class MagiskHideFragment extends Fragment implements Topic.Subscriber {
@Override @Override
public void onTopicPublished(Topic topic) { public void onTopicPublished(Topic topic) {
mSwipeRefreshLayout.setRefreshing(false); mSwipeRefreshLayout.setRefreshing(false);
appAdapter.filter(lastFilter); appAdapter.filter(null);
} }
@Override @Override

View File

@ -1,6 +1,5 @@
package com.topjohnwu.magisk.adapters; package com.topjohnwu.magisk.adapters;
import android.content.Context;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
@ -13,11 +12,10 @@ import android.widget.Filter;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.asyncs.ParallelTask; import com.topjohnwu.magisk.asyncs.ParallelTask;
import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.Topic;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.Shell;
import java.util.ArrayList; import java.util.ArrayList;
@ -30,25 +28,19 @@ import butterknife.ButterKnife;
public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.ViewHolder> { public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.ViewHolder> {
private List<ApplicationInfo> mOriginalList, mList; private List<ApplicationInfo> fullList, showList;
private List<String> mHideList; private List<String> hideList;
private PackageManager pm; private PackageManager pm;
private ApplicationFilter filter; private ApplicationFilter filter;
private Topic magiskHideDone;
public ApplicationAdapter(Context context) { public ApplicationAdapter() {
mOriginalList = mList = Collections.emptyList(); fullList = showList = Collections.emptyList();
mHideList = Collections.emptyList(); hideList = Collections.emptyList();
filter = new ApplicationFilter(); filter = new ApplicationFilter();
pm = context.getPackageManager(); pm = MagiskManager.get().getPackageManager();
magiskHideDone = Utils.getMagiskManager(context).magiskHideDone;
new LoadApps().exec(); new LoadApps().exec();
} }
private boolean lowercaseContains(CharSequence string, CharSequence nonNullLowercaseSearch) {
return !TextUtils.isEmpty(string) && string.toString().toLowerCase().contains(nonNullLowercaseSearch);
}
@Override @Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_app, parent, false); View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_app, parent, false);
@ -57,28 +49,28 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.
@Override @Override
public void onBindViewHolder(final ViewHolder holder, int position) { public void onBindViewHolder(final ViewHolder holder, int position) {
ApplicationInfo info = mList.get(position); ApplicationInfo info = showList.get(position);
holder.appIcon.setImageDrawable(info.loadIcon(pm)); holder.appIcon.setImageDrawable(info.loadIcon(pm));
holder.appName.setText(info.loadLabel(pm)); holder.appName.setText(info.loadLabel(pm));
holder.appPackage.setText(info.packageName); holder.appPackage.setText(info.packageName);
holder.checkBox.setOnCheckedChangeListener(null); holder.checkBox.setOnCheckedChangeListener(null);
holder.checkBox.setChecked(mHideList.contains(info.packageName)); holder.checkBox.setChecked(hideList.contains(info.packageName));
holder.checkBox.setOnCheckedChangeListener((v, isChecked) -> { holder.checkBox.setOnCheckedChangeListener((v, isChecked) -> {
if (isChecked) { if (isChecked) {
Shell.Async.su("magiskhide --add " + info.packageName); Shell.Async.su("magiskhide --add " + info.packageName);
mHideList.add(info.packageName); hideList.add(info.packageName);
} else { } else {
Shell.Async.su("magiskhide --rm " + info.packageName); Shell.Async.su("magiskhide --rm " + info.packageName);
mHideList.remove(info.packageName); hideList.remove(info.packageName);
} }
}); });
} }
@Override @Override
public int getItemCount() { public int getItemCount() {
return mList.size(); return showList.size();
} }
public void filter(String constraint) { public void filter(String constraint) {
@ -104,17 +96,21 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.
private class ApplicationFilter extends Filter { private class ApplicationFilter extends Filter {
private boolean lowercaseContains(String s, CharSequence filter) {
return !TextUtils.isEmpty(s) && s.toLowerCase().contains(filter);
}
@Override @Override
protected FilterResults performFiltering(CharSequence constraint) { protected FilterResults performFiltering(CharSequence constraint) {
if (constraint == null || constraint.length() == 0) { if (constraint == null || constraint.length() == 0) {
mList = mOriginalList; showList = fullList;
} else { } else {
mList = new ArrayList<>(); showList = new ArrayList<>();
String filter = constraint.toString().toLowerCase(); String filter = constraint.toString().toLowerCase();
for (ApplicationInfo info : mOriginalList) { for (ApplicationInfo info : fullList) {
if (lowercaseContains(info.loadLabel(pm), filter) if (lowercaseContains(info.loadLabel(pm).toString(), filter)
|| lowercaseContains(info.packageName, filter)) { || lowercaseContains(info.packageName, filter)) {
mList.add(info); showList.add(info);
} }
} }
} }
@ -131,22 +127,32 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.
@Override @Override
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
mOriginalList = pm.getInstalledApplications(0); fullList = pm.getInstalledApplications(0);
for (Iterator<ApplicationInfo> i = mOriginalList.iterator(); i.hasNext(); ) { hideList = Shell.Sync.su("magiskhide --ls");
for (Iterator<ApplicationInfo> i = fullList.iterator(); i.hasNext(); ) {
ApplicationInfo info = i.next(); ApplicationInfo info = i.next();
if (Const.HIDE_BLACKLIST.contains(info.packageName) || !info.enabled) { if (Const.HIDE_BLACKLIST.contains(info.packageName) || !info.enabled) {
i.remove(); i.remove();
} }
} }
Collections.sort(mOriginalList, (a, b) -> a.loadLabel(pm).toString().toLowerCase() Collections.sort(fullList, (a, b) -> {
.compareTo(b.loadLabel(pm).toString().toLowerCase())); boolean ah = hideList.contains(a.packageName);
mHideList = Shell.Sync.su("magiskhide --ls"); boolean bh = hideList.contains(b.packageName);
if (ah == bh) {
return a.loadLabel(pm).toString().toLowerCase().compareTo(
b.loadLabel(pm).toString().toLowerCase());
} else if (ah) {
return -1;
} else {
return 1;
}
});
return null; return null;
} }
@Override @Override
protected void onPostExecute(Void v) { protected void onPostExecute(Void v) {
magiskHideDone.publish(false); MagiskManager.get().magiskHideDone.publish(false);
} }
} }
} }