Magisk/app/src/main/java/com/topjohnwu/magisk/AutoRootFragment.java

156 lines
4.9 KiB
Java
Raw Normal View History

package com.topjohnwu.magisk;
2016-09-21 16:55:20 -05:00
import android.app.ListFragment;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
2016-09-21 16:55:20 -05:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.topjohnwu.magisk.utils.ApplicationAdapter;
import java.util.ArrayList;
2016-09-15 23:46:10 -05:00
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class AutoRootFragment extends ListFragment {
private PackageManager packageManager = null;
private List<ApplicationInfo> applist = null;
private ApplicationAdapter listadaptor = null;
public ListView listView;
public SharedPreferences prefs;
List<String> arrayBlackList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.auto_root_fragment, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView = getListView();
packageManager = getActivity().getPackageManager();
2016-09-21 16:55:20 -05:00
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
2016-09-15 16:59:34 -05:00
if (!prefs.contains("auto_blacklist")) {
SharedPreferences.Editor editor = prefs.edit();
2016-09-15 16:59:34 -05:00
Set<String> set = new HashSet<>();
set.add("com.google.android.apps.walletnfcrel");
2016-09-15 23:46:10 -05:00
set.add("com.google.android.gms");
2016-09-15 16:59:34 -05:00
editor.putStringSet("auto_blacklist", set);
editor.apply();
}
new LoadApplications().execute();
}
@Override
public void onResume() {
super.onResume();
new LoadApplications().execute();
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
ApplicationInfo app = applist.get(position);
ToggleApp(app.packageName, position, v);
}
private void ToggleApp(String appToCheck, int position, View v) {
2016-09-15 16:59:34 -05:00
Set<String> blackListSet = prefs.getStringSet("auto_blacklist", null);
2016-09-15 16:59:34 -05:00
assert blackListSet != null;
arrayBlackList = new ArrayList<>(blackListSet);
if (!arrayBlackList.contains(appToCheck)) {
2016-09-15 16:59:34 -05:00
arrayBlackList.add(appToCheck);
} else {
2016-09-15 16:59:34 -05:00
for (int i = 0; i < arrayBlackList.size(); i++) {
if (appToCheck.equals(arrayBlackList.get(i))) {
arrayBlackList.remove(i);
}
}
}
prefs.edit().putStringSet("auto_blacklist", new HashSet<>(arrayBlackList)).apply();
2016-09-15 23:46:10 -05:00
listadaptor.UpdateRootStatusView(position, v);
}
private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
2016-09-15 16:59:34 -05:00
ArrayList<ApplicationInfo> applist = new ArrayList<>();
for (ApplicationInfo info : list) {
try {
if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
applist.add(info);
}
} catch (Exception e) {
e.printStackTrace();
}
}
2016-09-15 23:46:10 -05:00
Collections.sort(applist, new CustomComparator());
return applist;
}
2016-09-15 23:46:10 -05:00
public class CustomComparator implements Comparator<ApplicationInfo> {
@Override
public int compare(ApplicationInfo o1, ApplicationInfo o2) {
packageManager = getActivity().getPackageManager();
return o1.loadLabel(packageManager).toString().compareTo(o2.loadLabel(packageManager).toString());
}
}
private class LoadApplications extends AsyncTask<Void, Void, Void> {
private ProgressDialog progress = null;
@Override
protected Void doInBackground(Void... params) {
applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
listadaptor = new ApplicationAdapter(getActivity(),
R.layout.app_list_row, applist);
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPostExecute(Void result) {
setListAdapter(listadaptor);
progress.dismiss();
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(getActivity(), null,
"Loading application info...");
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
}