2016-08-22 21:18:28 +00:00
|
|
|
package com.topjohnwu.magisk;
|
2016-08-21 14:42:50 +00:00
|
|
|
|
2016-09-02 18:18:37 +00:00
|
|
|
import android.content.SharedPreferences;
|
2016-08-21 14:42:50 +00:00
|
|
|
import android.os.Bundle;
|
2016-09-02 18:18:37 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2016-08-21 14:42:50 +00:00
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.design.widget.Snackbar;
|
|
|
|
import android.support.v4.app.Fragment;
|
2016-09-02 18:18:37 +00:00
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
2016-08-21 14:42:50 +00:00
|
|
|
import android.support.v7.widget.RecyclerView;
|
2016-09-02 18:18:37 +00:00
|
|
|
import android.util.Log;
|
2016-08-21 14:42:50 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2016-08-22 10:30:13 +00:00
|
|
|
import android.widget.CheckBox;
|
2016-08-23 16:28:27 +00:00
|
|
|
import android.widget.TextView;
|
2016-08-21 14:42:50 +00:00
|
|
|
|
2016-08-22 21:18:28 +00:00
|
|
|
import com.topjohnwu.magisk.module.Module;
|
2016-08-28 22:35:07 +00:00
|
|
|
import com.topjohnwu.magisk.utils.Utils;
|
2016-08-21 14:42:50 +00:00
|
|
|
|
|
|
|
import java.util.List;
|
2016-09-02 18:18:37 +00:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2016-08-21 14:42:50 +00:00
|
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
|
|
|
public abstract class BaseModuleFragment extends Fragment {
|
|
|
|
|
|
|
|
@BindView(R.id.recyclerView) RecyclerView recyclerView;
|
2016-08-23 16:28:27 +00:00
|
|
|
@BindView(R.id.empty_rv) TextView emptyTv;
|
2016-09-02 18:18:37 +00:00
|
|
|
private SwipeRefreshLayout mSwipeRefreshLayout;
|
|
|
|
private View view;
|
|
|
|
private SharedPreferences prefs;
|
2016-08-21 14:42:50 +00:00
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
2016-09-02 18:18:37 +00:00
|
|
|
view = inflater.inflate(R.layout.single_module_fragment, container, false);
|
|
|
|
mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
|
|
|
|
mSwipeRefreshLayout.setOnRefreshListener(() -> {
|
|
|
|
refreshItems();
|
|
|
|
});
|
|
|
|
|
2016-08-21 14:42:50 +00:00
|
|
|
ButterKnife.bind(this, view);
|
2016-09-02 18:18:37 +00:00
|
|
|
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
|
|
|
prefs.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
|
|
|
|
if (s.contains("updated")) {
|
|
|
|
view.invalidate();
|
|
|
|
view.requestLayout();
|
2016-08-21 14:42:50 +00:00
|
|
|
|
2016-09-02 18:18:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-08-23 16:28:27 +00:00
|
|
|
if (listModules().size() == 0) {
|
|
|
|
emptyTv.setVisibility(View.VISIBLE);
|
|
|
|
recyclerView.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2016-08-28 22:35:07 +00:00
|
|
|
recyclerView.setAdapter(new ModulesAdapter(listModules(), (chk, position) -> {
|
|
|
|
// On Checkbox change listener
|
|
|
|
CheckBox chbox = (CheckBox) chk;
|
2016-08-22 10:30:13 +00:00
|
|
|
|
2016-08-28 22:35:07 +00:00
|
|
|
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();
|
2016-08-22 10:30:13 +00:00
|
|
|
}
|
2016-08-28 22:35:07 +00:00
|
|
|
}, (deleteBtn, position) -> {
|
|
|
|
// On delete button click listener
|
2016-08-23 10:39:36 +00:00
|
|
|
|
2016-08-28 22:35:07 +00:00
|
|
|
listModules().get(position).createRemoveFile();
|
|
|
|
Snackbar.make(deleteBtn, R.string.remove_file_created, Snackbar.LENGTH_SHORT).show();
|
|
|
|
}, (undeleteBtn, position) -> {
|
|
|
|
// On undelete button click listener
|
2016-08-23 10:39:36 +00:00
|
|
|
|
2016-08-28 22:35:07 +00:00
|
|
|
listModules().get(position).deleteRemoveFile();
|
|
|
|
Snackbar.make(undeleteBtn, R.string.remove_file_deleted, Snackbar.LENGTH_SHORT).show();
|
2016-08-22 10:30:13 +00:00
|
|
|
}));
|
2016-08-21 14:42:50 +00:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2016-09-02 18:18:37 +00:00
|
|
|
void refreshItems() {
|
|
|
|
Log.d("Magisk", "Calling refreshitems for online");
|
|
|
|
Utils.LoadModules utils = new Utils.LoadModules(getActivity(),true);
|
|
|
|
utils.execute();
|
|
|
|
onItemsLoadComplete();
|
|
|
|
view.requestLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void onItemsLoadComplete() {
|
|
|
|
// Update the adapter and notify data set changed
|
|
|
|
// ...
|
|
|
|
|
|
|
|
// Stop refresh animation
|
|
|
|
mSwipeRefreshLayout.setRefreshing(false);
|
|
|
|
}
|
|
|
|
|
2016-08-21 14:42:50 +00:00
|
|
|
protected abstract List<Module> listModules();
|
|
|
|
}
|