Magisk/app/src/main/java/com/topjohnwu/magisk/adapters/ApplicationAdapter.java

176 lines
5.8 KiB
Java
Raw Normal View History

2016-11-07 16:09:08 +00:00
package com.topjohnwu.magisk.adapters;
2016-11-07 13:06:18 +00:00
2017-07-20 21:08:39 +00:00
import android.content.Context;
2016-11-07 13:06:18 +00:00
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.design.widget.Snackbar;
2016-11-07 13:06:18 +00:00
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.Filter;
2016-11-07 13:06:18 +00:00
import android.widget.ImageView;
import android.widget.TextView;
2016-11-07 16:09:08 +00:00
import com.topjohnwu.magisk.R;
2017-07-20 21:08:39 +00:00
import com.topjohnwu.magisk.asyncs.ParallelTask;
2017-02-14 21:24:02 +00:00
import com.topjohnwu.magisk.components.SnackbarMaker;
2017-07-20 21:08:39 +00:00
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Topic;
import com.topjohnwu.magisk.utils.Utils;
2016-11-07 13:06:18 +00:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
2017-07-20 21:08:39 +00:00
import java.util.Iterator;
2016-11-07 13:06:18 +00:00
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.ViewHolder> {
public static final List<String> BLACKLIST = Arrays.asList(
"android",
"com.topjohnwu.magisk",
"com.google.android.gms"
);
private static final List<String> SNLIST = Arrays.asList(
"com.google.android.apps.walletnfcrel",
"com.nianticlabs.pokemongo"
);
private List<ApplicationInfo> mOriginalList, mList;
2016-11-07 13:06:18 +00:00
private List<String> mHideList;
2017-07-20 21:08:39 +00:00
private PackageManager pm;
private ApplicationFilter filter;
private Topic magiskHideDone;
2017-07-20 21:08:39 +00:00
private Shell shell;
2016-11-07 13:06:18 +00:00
2017-07-20 21:08:39 +00:00
public ApplicationAdapter(Context context) {
mOriginalList = mList = Collections.emptyList();
mHideList = Collections.emptyList();
filter = new ApplicationFilter();
2017-07-20 21:08:39 +00:00
pm = context.getPackageManager();
magiskHideDone = Utils.getMagiskManager(context).magiskHideDone;
2017-08-03 15:33:08 +00:00
shell = Shell.getShell(context);
2017-07-20 21:08:39 +00:00
new LoadApps().exec();
2016-11-07 13:06:18 +00:00
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_app, parent, false);
return new ViewHolder(mView);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
ApplicationInfo info = mList.get(position);
2016-11-07 15:59:10 +00:00
2017-07-20 21:08:39 +00:00
holder.appIcon.setImageDrawable(info.loadIcon(pm));
holder.appName.setText(info.loadLabel(pm));
2016-11-07 13:06:18 +00:00
holder.appPackage.setText(info.packageName);
2016-11-07 15:59:10 +00:00
// Remove all listeners
holder.itemView.setOnClickListener(null);
holder.checkBox.setOnCheckedChangeListener(null);
if (SNLIST.contains(info.packageName)) {
holder.checkBox.setChecked(true);
holder.checkBox.setEnabled(false);
2017-02-14 21:24:02 +00:00
holder.itemView.setOnClickListener(v ->
SnackbarMaker.make(holder.itemView,
R.string.safetyNet_hide_notice, Snackbar.LENGTH_LONG).show()
);
} else {
holder.checkBox.setEnabled(true);
holder.checkBox.setChecked(mHideList.contains(info.packageName));
holder.checkBox.setOnCheckedChangeListener((v, isChecked) -> {
if (isChecked) {
2017-07-20 21:08:39 +00:00
Utils.addMagiskHide(shell, info.packageName);
mHideList.add(info.packageName);
} else {
2017-07-20 21:08:39 +00:00
Utils.rmMagiskHide(shell, info.packageName);
mHideList.remove(info.packageName);
}
});
}
2016-11-07 13:06:18 +00:00
}
@Override
public int getItemCount() {
return mList.size();
}
public void filter(String constraint) {
filter.filter(constraint);
}
2017-07-20 21:08:39 +00:00
public void refresh() {
new LoadApps().exec();
}
static class ViewHolder extends RecyclerView.ViewHolder {
2016-11-07 13:06:18 +00:00
@BindView(R.id.app_icon) ImageView appIcon;
@BindView(R.id.app_name) TextView appName;
@BindView(R.id.app_package) TextView appPackage;
@BindView(R.id.checkbox) CheckBox checkBox;
ViewHolder(View itemView) {
2016-11-07 13:06:18 +00:00
super(itemView);
ButterKnife.bind(this, itemView);
}
}
private class ApplicationFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint == null || constraint.length() == 0) {
2017-07-23 10:33:16 +00:00
mList = mOriginalList;
} else {
2017-07-23 10:33:16 +00:00
mList = new ArrayList<>();
String filter = constraint.toString().toLowerCase();
for (ApplicationInfo info : mOriginalList) {
2017-07-20 21:08:39 +00:00
if (Utils.lowercaseContains(info.loadLabel(pm), filter)
|| Utils.lowercaseContains(info.packageName, filter)) {
2017-07-23 10:33:16 +00:00
mList.add(info);
}
}
}
2017-07-23 10:33:16 +00:00
return null;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
notifyDataSetChanged();
}
}
2017-07-20 21:08:39 +00:00
private class LoadApps extends ParallelTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
mOriginalList = pm.getInstalledApplications(0);
for (Iterator<ApplicationInfo> 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.publish(false);
2017-07-20 21:08:39 +00:00
}
}
}