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

152 lines
5.1 KiB
Java
Raw Normal View History

2016-08-22 21:18:28 +00:00
package com.topjohnwu.magisk;
2016-08-17 11:00:55 +00:00
2016-09-21 21:55:20 +00:00
import android.app.Fragment;
2016-09-23 21:22:11 +00:00
import android.content.Intent;
2016-09-18 14:56:12 +00:00
import android.content.SharedPreferences;
import android.net.Uri;
2016-08-17 11:00:55 +00:00
import android.os.AsyncTask;
import android.os.Bundle;
2016-09-18 14:56:12 +00:00
import android.preference.PreferenceManager;
2016-08-21 13:29:42 +00:00
import android.support.annotation.Nullable;
2016-09-23 21:22:11 +00:00
import android.support.design.widget.FloatingActionButton;
2016-09-18 14:56:12 +00:00
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
2016-08-21 13:29:42 +00:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
2016-09-18 14:56:12 +00:00
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
2016-08-20 15:26:49 +00:00
import com.ipaulpro.afilechooser.FileInfo;
2016-09-23 21:22:11 +00:00
import com.ipaulpro.afilechooser.utils.FileUtils;
2016-08-22 21:18:28 +00:00
import com.topjohnwu.magisk.module.Module;
2016-09-20 17:08:05 +00:00
import com.topjohnwu.magisk.utils.Async;
2016-08-17 11:00:55 +00:00
2016-08-25 10:40:00 +00:00
import java.util.ArrayList;
2016-08-17 11:00:55 +00:00
import java.util.List;
2016-08-21 13:29:42 +00:00
import butterknife.BindView;
import butterknife.ButterKnife;
2016-08-21 15:21:37 +00:00
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;
2016-09-18 14:56:12 +00:00
private SharedPreferences prefs;
2016-08-28 22:35:07 +00:00
public static List<Module> listModules = new ArrayList<>();
2016-09-23 21:22:11 +00:00
@BindView(R.id.fab)
FloatingActionButton fabio;
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-09-18 14:56:12 +00:00
View viewMain = inflater.inflate(R.layout.modules_fragment, container, false);
2016-09-06 21:54:08 +00:00
2016-09-18 14:56:12 +00:00
ButterKnife.bind(this, viewMain);
fabio.setOnClickListener(v -> {
2016-09-23 21:22:11 +00:00
Intent getContentIntent = FileUtils.createGetContentIntent(null);
getContentIntent.setType("application/zip");
Intent fileIntent = Intent.createChooser(getContentIntent, "Select a file");
startActivityForResult(fileIntent, FETCH_ZIP_CODE);
2016-09-14 22:12:47 +00:00
2016-09-23 21:22:11 +00:00
});
2016-09-18 14:56:12 +00:00
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
mSwipeRefreshLayout.setOnRefreshListener(() -> {
2016-09-14 22:12:47 +00:00
2016-09-18 14:56:12 +00:00
recyclerView.setVisibility(View.GONE);
2016-09-20 17:08:05 +00:00
new Async.LoadModules(getActivity()).execute();
2016-09-18 14:56:12 +00:00
new updateUI().execute();
prefs.edit().putBoolean("ignoreUpdateAlerts", false).apply();
2016-08-17 11:00:55 +00:00
2016-09-18 14:56:12 +00:00
});
2016-09-15 17:52:58 +00:00
2016-09-18 14:56:12 +00:00
prefs.registerOnSharedPreferenceChangeListener((sharedPreferences, s) -> {
if (s.contains("updated")) {
viewMain.invalidate();
viewMain.requestLayout();
2016-09-15 17:52:58 +00:00
}
2016-09-18 14:56:12 +00:00
});
2016-09-14 22:12:47 +00:00
new updateUI().execute();
2016-09-18 14:56:12 +00:00
return viewMain;
}
@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();
try {
// Get the file path from the URI
new Async.FlashZIP(getActivity(), uri).execute();
FileInfo fileInfo = FileUtils.getFileInfo(getActivity(), uri);
} catch (Exception e) {
Log.e("FileSelectorTestAc...", "File select error", e);
}
}
}
2016-09-23 21:22:11 +00:00
@Override
public void onResume() {
super.onResume();
getActivity().setTitle("Modules");
}
2016-09-08 19:47:04 +00:00
private class updateUI extends AsyncTask<Void, Void, Void> {
2016-08-17 11:00:55 +00:00
@Override
2016-08-22 21:18:28 +00:00
protected Void doInBackground(Void... voids) {
return null;
2016-08-17 11:00:55 +00:00
}
@Override
2016-08-22 21:18:28 +00:00
protected void onPostExecute(Void v) {
super.onPostExecute(v);
2016-09-09 21:49:25 +00:00
2016-09-19 02:08:46 +00:00
if (listModules.size() == 0) {
2016-09-18 14:56:12 +00:00
emptyTv.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
} else {
recyclerView.setVisibility(View.VISIBLE);
}
2016-09-19 02:08:46 +00:00
recyclerView.setAdapter(new ModulesAdapter(listModules, (chk, position) -> {
2016-09-18 14:56:12 +00:00
// On Checkbox change listener
CheckBox chbox = (CheckBox) chk;
2016-09-18 14:56:12 +00:00
if (!chbox.isChecked()) {
2016-09-19 02:08:46 +00:00
listModules.get(position).createDisableFile();
2016-09-18 14:56:12 +00:00
Snackbar.make(chk, R.string.disable_file_created, Snackbar.LENGTH_SHORT).show();
} else {
2016-09-19 02:08:46 +00:00
listModules.get(position).removeDisableFile();
2016-09-18 14:56:12 +00:00
Snackbar.make(chk, R.string.disable_file_removed, Snackbar.LENGTH_SHORT).show();
}
}, (deleteBtn, position) -> {
// On delete button click listener
2016-09-19 02:08:46 +00:00
listModules.get(position).createRemoveFile();
2016-09-18 14:56:12 +00:00
Snackbar.make(deleteBtn, R.string.remove_file_created, Snackbar.LENGTH_SHORT).show();
}, (undeleteBtn, position) -> {
// On undelete button click listener
2016-09-19 02:08:46 +00:00
listModules.get(position).deleteRemoveFile();
2016-09-18 14:56:12 +00:00
Snackbar.make(undeleteBtn, R.string.remove_file_deleted, Snackbar.LENGTH_SHORT).show();
}));
2016-09-18 14:56:12 +00:00
if (mSwipeRefreshLayout.isRefreshing())
mSwipeRefreshLayout.setRefreshing(false);
}
}
2016-09-14 22:12:47 +00:00
2016-08-17 11:00:55 +00:00
}