mirror of
https://github.com/topjohnwu/Magisk.git
synced 2026-01-13 21:36:58 -08:00
MagiskHide Fragment complete refactor
This commit is contained in:
@@ -1,208 +1,80 @@
|
||||
package com.topjohnwu.magisk;
|
||||
|
||||
import android.app.ListFragment;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.SharedPreferences;
|
||||
import android.app.Fragment;
|
||||
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;
|
||||
import android.util.TypedValue;
|
||||
import android.support.v4.widget.SwipeRefreshLayout;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ListView;
|
||||
|
||||
import com.topjohnwu.magisk.utils.ApplicationAdapter;
|
||||
import com.topjohnwu.magisk.utils.Logger;
|
||||
import com.topjohnwu.magisk.utils.Async;
|
||||
import com.topjohnwu.magisk.utils.Shell;
|
||||
import com.topjohnwu.magisk.utils.Utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static com.topjohnwu.magisk.utils.Utils.WhichHide;
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
public class MagiskHideFragment extends ListFragment {
|
||||
private PackageManager packageManager = null;
|
||||
private List<ApplicationInfo> applist = null;
|
||||
private ApplicationAdapter listadaptor = null;
|
||||
public ListView listView;
|
||||
public SharedPreferences prefs;
|
||||
private int hideVersion;
|
||||
List<String> arrayBlackList;
|
||||
public class MagiskHideFragment extends Fragment {
|
||||
|
||||
@BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
|
||||
@BindView(R.id.recyclerView) RecyclerView recyclerView;
|
||||
|
||||
private PackageManager packageManager;
|
||||
private View mView;
|
||||
private ApplicationAdapter appAdapter;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
||||
View view = inflater.inflate(R.layout.auto_root_fragment, container, false);
|
||||
int horizontalMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
|
||||
int verticalMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
|
||||
TypedValue tv = new TypedValue();
|
||||
int actionBarHeight = 130;
|
||||
if (getActivity().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
|
||||
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
|
||||
}
|
||||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
mView = inflater.inflate(R.layout.magisk_hide_fragment, container, false);
|
||||
ButterKnife.bind(this, mView);
|
||||
|
||||
view.setPadding(horizontalMargin, actionBarHeight, horizontalMargin, verticalMargin);
|
||||
hideVersion = WhichHide(getActivity());
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
packageManager = getActivity().getPackageManager();
|
||||
|
||||
mSwipeRefreshLayout.setRefreshing(true);
|
||||
mSwipeRefreshLayout.setOnRefreshListener(() -> {
|
||||
recyclerView.setVisibility(View.GONE);
|
||||
new LoadApps().exec();
|
||||
});
|
||||
|
||||
new LoadApps().exec();
|
||||
return mView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
initializeElements();
|
||||
super.onResume();
|
||||
getActivity().setTitle(R.string.magiskhide);
|
||||
hideVersion = WhichHide(getActivity());
|
||||
|
||||
mView = this.getView();
|
||||
}
|
||||
|
||||
private void initializeElements() {
|
||||
listView = getListView();
|
||||
packageManager = getActivity().getPackageManager();
|
||||
new LoadApplications().execute();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onListItemClick(ListView l, View v, int position, long id) {
|
||||
Logger.dev("Click");
|
||||
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) {
|
||||
Logger.dev("Magisk", "MagiskHideFragment: ToggleApp called for " + appToCheck);
|
||||
Set<String> blackListSet = prefs.getStringSet("auto_blacklist", null);
|
||||
assert blackListSet != null;
|
||||
arrayBlackList = new ArrayList<>(blackListSet);
|
||||
String UID = Utils.getAppUID(appToCheck);
|
||||
if (!arrayBlackList.contains(appToCheck)) {
|
||||
arrayBlackList.add(appToCheck);
|
||||
switch (hideVersion) {
|
||||
case 1 :
|
||||
Shell.su("/magisk/.core/magiskhide/add " + appToCheck);
|
||||
break;
|
||||
case 2 :
|
||||
Shell.su("/su/suhide/add " + UID);
|
||||
break;
|
||||
case 3 :
|
||||
Shell.su("/su/suhide/add " + UID + "&& /magisk/.core/magiskhide/add " + appToCheck);
|
||||
break;
|
||||
default :
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
for (int i = 0; i < arrayBlackList.size(); i++) {
|
||||
if (appToCheck.equals(arrayBlackList.get(i))) {
|
||||
arrayBlackList.remove(i);
|
||||
}
|
||||
}
|
||||
switch (hideVersion) {
|
||||
case 1 :
|
||||
Shell.su("/magisk/.core/magiskhide/rm " + appToCheck);
|
||||
break;
|
||||
case 2 :
|
||||
Shell.su("/su/suhide/rm " + UID);
|
||||
break;
|
||||
case 3 :
|
||||
Shell.su("/su/suhide/rm " + UID + "&& /magisk/.core/magiskhide/rm " + appToCheck);
|
||||
break;
|
||||
default :
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Logger.dev("Committing set, value is: " + arrayBlackList.toString());
|
||||
SharedPreferences.Editor editor = prefs.edit();
|
||||
editor.putStringSet("auto_blacklist", new HashSet<>(arrayBlackList));
|
||||
editor.apply();
|
||||
listadaptor.UpdateRootStatusView(position, v);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
|
||||
ArrayList<ApplicationInfo> applist = new ArrayList<>();
|
||||
for (ApplicationInfo info : list) {
|
||||
try {
|
||||
if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
|
||||
if (!info.packageName.contains("magisk")) {
|
||||
applist.add(info);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
Collections.sort(applist, new CustomComparator());
|
||||
|
||||
return applist;
|
||||
}
|
||||
|
||||
public class CustomComparator implements Comparator<ApplicationInfo> {
|
||||
private class LoadApps extends Async.RootTask<Void, Void, Void> {
|
||||
@Override
|
||||
public int compare(ApplicationInfo o1, ApplicationInfo o2) {
|
||||
return o1.loadLabel(packageManager).toString().compareToIgnoreCase(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.list_item_app, applist);
|
||||
|
||||
protected Void doInBackground(Void... voids) {
|
||||
List<ApplicationInfo> listApps = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
|
||||
Collections.sort(listApps, (a, b) -> a.loadLabel(packageManager).toString().toLowerCase()
|
||||
.compareTo(b.loadLabel(packageManager).toString().toLowerCase()));
|
||||
List<String> hideList = Shell.su(Async.MAGISK_HIDE_PATH + "list");
|
||||
appAdapter = new ApplicationAdapter(listApps, hideList);
|
||||
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);
|
||||
protected void onPostExecute(Void aVoid) {
|
||||
super.onPostExecute(aVoid);
|
||||
updateUI();
|
||||
}
|
||||
}
|
||||
|
||||
private void updateUI() {
|
||||
recyclerView.setAdapter(appAdapter);
|
||||
recyclerView.setVisibility(View.VISIBLE);
|
||||
mSwipeRefreshLayout.setRefreshing(false);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user