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;
|
2017-01-29 17:38:30 +00:00
|
|
|
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;
|
2017-01-08 13:41:19 +00:00
|
|
|
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;
|
2017-08-03 16:12:11 +00:00
|
|
|
import com.topjohnwu.magisk.utils.Topic;
|
2017-01-08 13:41:19 +00:00
|
|
|
import com.topjohnwu.magisk.utils.Utils;
|
2016-11-07 13:06:18 +00:00
|
|
|
|
2017-01-08 13:41:19 +00:00
|
|
|
import java.util.ArrayList;
|
2017-01-29 17:38:30 +00:00
|
|
|
import java.util.Arrays;
|
2017-01-08 13:41:19 +00:00
|
|
|
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> {
|
|
|
|
|
2017-01-29 17:38:30 +00:00
|
|
|
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"
|
|
|
|
);
|
|
|
|
|
2017-01-08 13:41:19 +00:00
|
|
|
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;
|
2017-01-08 13:41:19 +00:00
|
|
|
private ApplicationFilter filter;
|
2017-08-03 16:12:11 +00:00
|
|
|
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) {
|
2017-01-08 13:41:19 +00:00
|
|
|
mOriginalList = mList = Collections.emptyList();
|
|
|
|
mHideList = Collections.emptyList();
|
2017-01-29 17:38:30 +00:00
|
|
|
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
|
|
|
|
2017-01-29 17:38:30 +00:00
|
|
|
// Remove all listeners
|
|
|
|
holder.itemView.setOnClickListener(null);
|
2017-01-08 14:52:53 +00:00
|
|
|
holder.checkBox.setOnCheckedChangeListener(null);
|
2017-01-29 17:38:30 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
);
|
2017-01-29 17:38:30 +00:00
|
|
|
} 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);
|
2017-01-29 17:38:30 +00:00
|
|
|
mHideList.add(info.packageName);
|
|
|
|
} else {
|
2017-07-20 21:08:39 +00:00
|
|
|
Utils.rmMagiskHide(shell, info.packageName);
|
2017-01-29 17:38:30 +00:00
|
|
|
mHideList.remove(info.packageName);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-11-07 13:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return mList.size();
|
|
|
|
}
|
|
|
|
|
2017-01-08 13:41:19 +00:00
|
|
|
public void filter(String constraint) {
|
|
|
|
filter.filter(constraint);
|
|
|
|
}
|
|
|
|
|
2017-07-20 21:08:39 +00:00
|
|
|
public void refresh() {
|
|
|
|
new LoadApps().exec();
|
|
|
|
}
|
|
|
|
|
2017-01-08 13:41:19 +00:00
|
|
|
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;
|
|
|
|
|
2017-01-08 13:47:56 +00:00
|
|
|
ViewHolder(View itemView) {
|
2016-11-07 13:06:18 +00:00
|
|
|
super(itemView);
|
|
|
|
ButterKnife.bind(this, itemView);
|
|
|
|
}
|
|
|
|
}
|
2017-01-08 13:41:19 +00:00
|
|
|
|
|
|
|
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;
|
2017-01-08 13:41:19 +00:00
|
|
|
} else {
|
2017-07-23 10:33:16 +00:00
|
|
|
mList = new ArrayList<>();
|
2017-01-08 13:41:19 +00:00
|
|
|
String filter = constraint.toString().toLowerCase();
|
|
|
|
for (ApplicationInfo info : mOriginalList) {
|
2017-07-20 21:08:39 +00:00
|
|
|
if (Utils.lowercaseContains(info.loadLabel(pm), filter)
|
2017-01-08 13:41:19 +00:00
|
|
|
|| Utils.lowercaseContains(info.packageName, filter)) {
|
2017-07-23 10:33:16 +00:00
|
|
|
mList.add(info);
|
2017-01-08 13:41:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-23 10:33:16 +00:00
|
|
|
return null;
|
2017-01-08 13:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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) {
|
2017-08-03 16:12:11 +00:00
|
|
|
magiskHideDone.publish(false);
|
2017-07-20 21:08:39 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-08 13:41:19 +00:00
|
|
|
}
|