2016-08-20 15:26:49 +00:00
|
|
|
package com.topjohnwu.magisk.ui;
|
2016-08-17 11:00:55 +00:00
|
|
|
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Bundle;
|
2016-08-21 13:29:42 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2016-08-21 14:08:45 +00:00
|
|
|
import android.support.design.widget.Snackbar;
|
|
|
|
import android.support.v7.widget.PopupMenu;
|
2016-08-21 13:29:42 +00:00
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.view.LayoutInflater;
|
2016-08-21 14:08:45 +00:00
|
|
|
import android.view.MenuItem;
|
2016-08-21 13:29:42 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2016-08-21 14:08:45 +00:00
|
|
|
import android.widget.ProgressBar;
|
2016-08-20 15:26:49 +00:00
|
|
|
|
|
|
|
import com.topjohnwu.magisk.R;
|
|
|
|
import com.topjohnwu.magisk.model.Module;
|
2016-08-21 14:08:45 +00:00
|
|
|
import com.topjohnwu.magisk.rv.ItemClickListener;
|
2016-08-20 15:26:49 +00:00
|
|
|
import com.topjohnwu.magisk.rv.ModulesAdapter;
|
2016-08-21 13:29:42 +00:00
|
|
|
import com.topjohnwu.magisk.ui.utils.Utils;
|
2016-08-17 11:00:55 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
2016-08-21 13:29:42 +00:00
|
|
|
import java.io.FileFilter;
|
2016-08-21 14:08:45 +00:00
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.lang.reflect.Method;
|
2016-08-17 11:00:55 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2016-08-21 13:29:42 +00:00
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
|
|
|
public class ModulesFragment extends android.support.v4.app.Fragment {
|
2016-08-17 11:00:55 +00:00
|
|
|
|
|
|
|
private static final String MAGISK_PATH = "/magisk";
|
|
|
|
private static final String MAGISK_CACHE_PATH = "/cache/magisk";
|
|
|
|
|
2016-08-21 13:29:42 +00:00
|
|
|
@BindView(R.id.recyclerView) RecyclerView recyclerView;
|
2016-08-21 14:08:45 +00:00
|
|
|
@BindView(R.id.progressBar) ProgressBar progressBar;
|
2016-08-21 13:29:42 +00:00
|
|
|
|
2016-08-17 11:00:55 +00:00
|
|
|
private List<Module> listModules = new ArrayList<>();
|
|
|
|
|
2016-08-21 14:08:45 +00:00
|
|
|
private ItemClickListener moduleActions = new ItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onItemClick(final View view, final int position) {
|
|
|
|
PopupMenu popup = new PopupMenu(getContext(), view);
|
|
|
|
try {
|
|
|
|
Field[] fields = popup.getClass().getDeclaredFields();
|
|
|
|
for (Field field : fields) {
|
|
|
|
if ("mPopup".equals(field.getName())) {
|
|
|
|
field.setAccessible(true);
|
|
|
|
Object menuPopupHelper = field.get(popup);
|
|
|
|
Class<?> classPopupHelper = Class.forName(menuPopupHelper.getClass().getName());
|
|
|
|
Method setForceIcons = classPopupHelper.getMethod("setForceShowIcon", boolean.class);
|
|
|
|
setForceIcons.invoke(menuPopupHelper, true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
|
|
|
|
|
|
|
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onMenuItemClick(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.remove:
|
|
|
|
listModules.get(position).createRemoveFile();
|
|
|
|
Snackbar.make(view, R.string.remove_file_created, Snackbar.LENGTH_SHORT).show();
|
|
|
|
break;
|
|
|
|
case R.id.disable:
|
|
|
|
listModules.get(position).createDisableFile();
|
|
|
|
Snackbar.make(view, R.string.disable_file_created, Snackbar.LENGTH_SHORT).show();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
popup.inflate(R.menu.module_popup);
|
|
|
|
popup.show();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-21 13:29:42 +00:00
|
|
|
@Nullable
|
2016-08-17 11:00:55 +00:00
|
|
|
@Override
|
2016-08-21 13:29:42 +00:00
|
|
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
2016-08-21 14:08:45 +00:00
|
|
|
View view = inflater.inflate(R.layout.modules_fragment, container, false);
|
|
|
|
ButterKnife.bind(this, view);
|
2016-08-17 11:00:55 +00:00
|
|
|
|
|
|
|
new CheckFolders().execute();
|
2016-08-21 13:29:42 +00:00
|
|
|
|
2016-08-21 14:08:45 +00:00
|
|
|
return view;
|
2016-08-17 11:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private class CheckFolders extends AsyncTask<Void, Integer, Boolean> {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Boolean doInBackground(Void... voids) {
|
2016-08-21 13:29:42 +00:00
|
|
|
File[] magisk = new File(MAGISK_PATH).listFiles(new FileFilter() {
|
|
|
|
@Override
|
|
|
|
public boolean accept(File file) {
|
|
|
|
return file.isDirectory();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Utils.executeCommand("chmod 777 /cache");
|
|
|
|
|
|
|
|
File[] magiskCache = new File(MAGISK_CACHE_PATH).listFiles(new FileFilter() {
|
|
|
|
@Override
|
|
|
|
public boolean accept(File file) {
|
|
|
|
return file.isDirectory();
|
|
|
|
}
|
|
|
|
});
|
2016-08-17 11:00:55 +00:00
|
|
|
|
|
|
|
if (magisk != null) {
|
|
|
|
for (File mod : magisk) {
|
2016-08-18 09:27:26 +00:00
|
|
|
Module m = new Module(mod);
|
|
|
|
if (m.isValid()) {
|
|
|
|
listModules.add(m);
|
|
|
|
}
|
2016-08-17 11:00:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (magiskCache != null) {
|
|
|
|
for (File mod : magiskCache) {
|
2016-08-18 09:27:26 +00:00
|
|
|
Module m = new Module(mod);
|
|
|
|
if (m.isValid()) {
|
|
|
|
listModules.add(m);
|
|
|
|
}
|
2016-08-17 11:00:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//noinspection Convert2streamapi
|
|
|
|
for (Module module : listModules) {
|
2016-08-18 09:27:26 +00:00
|
|
|
try {
|
2016-08-17 11:00:55 +00:00
|
|
|
module.parse();
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-08-18 09:27:26 +00:00
|
|
|
protected void onPostExecute(Boolean result) {
|
|
|
|
super.onPostExecute(result);
|
2016-08-17 11:00:55 +00:00
|
|
|
|
2016-08-21 14:08:45 +00:00
|
|
|
progressBar.setVisibility(View.GONE);
|
2016-08-17 11:00:55 +00:00
|
|
|
|
2016-08-21 14:08:45 +00:00
|
|
|
recyclerView.setAdapter(new ModulesAdapter(listModules, moduleActions));
|
2016-08-17 11:00:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|