mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-10-25 22:38:46 +00:00
Refactor modules fragment
This commit is contained in:
@@ -1,147 +1,66 @@
|
||||
package com.topjohnwu.magisk;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.net.Uri;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.provider.MediaStore;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.design.widget.TabLayout;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentPagerAdapter;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.util.Log;
|
||||
import android.support.v4.widget.SwipeRefreshLayout;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.Toast;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.ipaulpro.afilechooser.FileInfo;
|
||||
import com.ipaulpro.afilechooser.utils.FileUtils;
|
||||
import com.topjohnwu.magisk.module.Module;
|
||||
import com.topjohnwu.magisk.module.RepoHelper;
|
||||
import com.topjohnwu.magisk.utils.Utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
public class ModulesFragment extends Fragment {
|
||||
@BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
|
||||
@BindView(R.id.recyclerView) RecyclerView recyclerView;
|
||||
@BindView(R.id.empty_rv) TextView emptyTv;
|
||||
|
||||
private static final int FETCH_ZIP_CODE = 2;
|
||||
private SharedPreferences prefs;
|
||||
public static List<Module> listModules = new ArrayList<>();
|
||||
public static List<Module> listModulesCache = new ArrayList<>();
|
||||
@BindView(R.id.progressBar) ProgressBar progressBar;
|
||||
@BindView(R.id.fab) FloatingActionButton fabio;
|
||||
@BindView(R.id.pager) ViewPager viewPager;
|
||||
@BindView(R.id.tab_layout) TabLayout tabLayout;
|
||||
private int viewPagePosition;
|
||||
private RepoHelper.TaskDelegate mTaskDelegate;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.modules_fragment, container, false);
|
||||
View viewMain = inflater.inflate(R.layout.modules_fragment, container, false);
|
||||
|
||||
ButterKnife.bind(this, view);
|
||||
fabio.setOnClickListener(v -> {
|
||||
Intent getContentIntent = FileUtils.createGetContentIntent(null);
|
||||
getContentIntent.setType("application/zip");
|
||||
Intent fileIntent = Intent.createChooser(getContentIntent, "Select a file");
|
||||
|
||||
startActivityForResult(fileIntent, FETCH_ZIP_CODE);
|
||||
ButterKnife.bind(this, viewMain);
|
||||
|
||||
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
||||
mSwipeRefreshLayout.setOnRefreshListener(() -> {
|
||||
|
||||
recyclerView.setVisibility(View.GONE);
|
||||
new Utils.LoadModules(getActivity()).execute();
|
||||
new updateUI().execute();
|
||||
prefs.edit().putBoolean("ignoreUpdateAlerts", false).apply();
|
||||
|
||||
});
|
||||
|
||||
mTaskDelegate = result -> {
|
||||
if (result.equals("OK")) {
|
||||
RefreshUI();
|
||||
prefs.registerOnSharedPreferenceChangeListener((sharedPreferences, s) -> {
|
||||
if (s.contains("updated")) {
|
||||
viewMain.invalidate();
|
||||
viewMain.requestLayout();
|
||||
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
new updateUI().execute();
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (data != null) {
|
||||
// Get the URI of the selected file
|
||||
final Uri uri = data.getData();
|
||||
Log.i("Magisk", "ModulesFragment: Uri = " + uri.toString() + " or ");
|
||||
new Utils.FlashZIP(getActivity(),uri).execute();
|
||||
try {
|
||||
// Get the file path from the URI
|
||||
FileInfo fileInfo = FileUtils.getFileInfo(getActivity(), uri);
|
||||
Toast.makeText(getActivity(),
|
||||
"File Selected: " + fileInfo.getDisplayName() + " size: " + fileInfo.getSize(), Toast.LENGTH_LONG).show();
|
||||
|
||||
if (!fileInfo.isExternal()) {
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e("FileSelectorTestAc...", "File select error", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
super.onCreateOptionsMenu(menu, inflater);
|
||||
inflater.inflate(R.menu.menu_module, menu);
|
||||
|
||||
}
|
||||
|
||||
private void RefreshUI() {
|
||||
viewPagePosition = tabLayout.getSelectedTabPosition();
|
||||
listModules.clear();
|
||||
listModulesCache.clear();
|
||||
progressBar.setVisibility(View.VISIBLE);
|
||||
viewPager.setAdapter(new TabsAdapter(getChildFragmentManager()));
|
||||
tabLayout.setupWithViewPager(viewPager);
|
||||
viewPager.setCurrentItem(viewPagePosition);
|
||||
new Utils.LoadModules(getActivity()).execute();
|
||||
Collections.sort(listModules, new CustomComparator());
|
||||
Collections.sort(listModulesCache, new CustomComparator());
|
||||
new updateUI().execute();
|
||||
}
|
||||
|
||||
void selectPage(int pageIndex) {
|
||||
tabLayout.setScrollPosition(pageIndex, 0f, true);
|
||||
viewPager.setCurrentItem(pageIndex);
|
||||
}
|
||||
|
||||
public static class NormalModuleFragment extends BaseModuleFragment {
|
||||
|
||||
@Override
|
||||
protected List<Module> listModules() {
|
||||
return listModules;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class CacheModuleFragment extends BaseModuleFragment {
|
||||
|
||||
@Override
|
||||
protected List<Module> listModules() {
|
||||
return listModulesCache;
|
||||
}
|
||||
|
||||
return viewMain;
|
||||
}
|
||||
|
||||
private class updateUI extends AsyncTask<Void, Void, Void> {
|
||||
@@ -154,53 +73,46 @@ public class ModulesFragment extends Fragment {
|
||||
@Override
|
||||
protected void onPostExecute(Void v) {
|
||||
super.onPostExecute(v);
|
||||
progressBar.setVisibility(View.GONE);
|
||||
viewPager.setAdapter(new TabsAdapter(getChildFragmentManager()));
|
||||
tabLayout.setupWithViewPager(viewPager);
|
||||
selectPage(viewPagePosition);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private class TabsAdapter extends FragmentPagerAdapter {
|
||||
|
||||
String[] tabTitles = new String[]{
|
||||
getString(R.string.modules), getString(R.string.cache_modules)
|
||||
};
|
||||
|
||||
public TabsAdapter(FragmentManager fm) {
|
||||
super(fm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return tabTitles.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPageTitle(int position) {
|
||||
return tabTitles[position];
|
||||
}
|
||||
|
||||
@Override
|
||||
public Fragment getItem(int position) {
|
||||
if (position == 0) {
|
||||
NormalModuleFragment nmf = new NormalModuleFragment();
|
||||
nmf.SetDelegate(mTaskDelegate);
|
||||
return nmf;
|
||||
if (listModules().size() == 0) {
|
||||
emptyTv.setVisibility(View.VISIBLE);
|
||||
recyclerView.setVisibility(View.GONE);
|
||||
} else {
|
||||
CacheModuleFragment cmf = new CacheModuleFragment();
|
||||
cmf.SetDelegate(mTaskDelegate);
|
||||
return cmf;
|
||||
recyclerView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
recyclerView.setAdapter(new ModulesAdapter(listModules(), (chk, position) -> {
|
||||
// On Checkbox change listener
|
||||
CheckBox chbox = (CheckBox) chk;
|
||||
|
||||
if (!chbox.isChecked()) {
|
||||
listModules().get(position).createDisableFile();
|
||||
Snackbar.make(chk, R.string.disable_file_created, Snackbar.LENGTH_SHORT).show();
|
||||
} else {
|
||||
listModules().get(position).removeDisableFile();
|
||||
Snackbar.make(chk, R.string.disable_file_removed, Snackbar.LENGTH_SHORT).show();
|
||||
}
|
||||
}, (deleteBtn, position) -> {
|
||||
// On delete button click listener
|
||||
|
||||
listModules().get(position).createRemoveFile();
|
||||
Snackbar.make(deleteBtn, R.string.remove_file_created, Snackbar.LENGTH_SHORT).show();
|
||||
}, (undeleteBtn, position) -> {
|
||||
// On undelete button click listener
|
||||
|
||||
listModules().get(position).deleteRemoveFile();
|
||||
Snackbar.make(undeleteBtn, R.string.remove_file_deleted, Snackbar.LENGTH_SHORT).show();
|
||||
}));
|
||||
|
||||
if (mSwipeRefreshLayout.isRefreshing())
|
||||
mSwipeRefreshLayout.setRefreshing(false);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class CustomComparator implements Comparator<Module> {
|
||||
@Override
|
||||
public int compare(Module o1, Module o2) {
|
||||
return o1.getName().compareTo(o2.getName());
|
||||
}
|
||||
|
||||
// protected abstract List<Module> listModules();
|
||||
protected List<Module> listModules() {
|
||||
return listModules;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user