2016-08-23 05:18:28 +08:00
|
|
|
package com.topjohnwu.magisk;
|
2016-08-17 13:00:55 +02:00
|
|
|
|
2016-12-11 20:38:15 +08:00
|
|
|
import android.app.Activity;
|
2016-09-21 16:55:20 -05:00
|
|
|
import android.app.Fragment;
|
2016-09-23 16:22:11 -05:00
|
|
|
import android.content.Intent;
|
2016-09-18 22:56:12 +08:00
|
|
|
import android.content.SharedPreferences;
|
2016-09-25 00:16:28 -05:00
|
|
|
import android.net.Uri;
|
2016-08-17 13:00:55 +02:00
|
|
|
import android.os.Bundle;
|
2016-09-18 22:56:12 +08:00
|
|
|
import android.preference.PreferenceManager;
|
2016-08-21 15:29:42 +02:00
|
|
|
import android.support.annotation.Nullable;
|
2016-09-18 22:56:12 +08:00
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
2016-08-21 15:29:42 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2016-09-18 22:56:12 +08:00
|
|
|
import android.widget.TextView;
|
2016-08-20 17:26:49 +02:00
|
|
|
|
2016-11-10 00:22:01 +08:00
|
|
|
import com.github.clans.fab.FloatingActionButton;
|
2016-11-08 00:09:08 +08:00
|
|
|
import com.topjohnwu.magisk.adapters.ModulesAdapter;
|
2016-08-23 05:18:28 +08:00
|
|
|
import com.topjohnwu.magisk.module.Module;
|
2016-09-21 01:08:05 +08:00
|
|
|
import com.topjohnwu.magisk.utils.Async;
|
2016-09-27 22:57:20 +08:00
|
|
|
import com.topjohnwu.magisk.utils.Logger;
|
2016-09-29 01:42:25 +08:00
|
|
|
import com.topjohnwu.magisk.utils.ModuleHelper;
|
2016-08-17 13:00:55 +02:00
|
|
|
|
2016-08-25 18:40:00 +08:00
|
|
|
import java.util.ArrayList;
|
2016-08-17 13:00:55 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2016-08-21 15:29:42 +02:00
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
2016-08-21 17:21:37 +02:00
|
|
|
public class ModulesFragment extends Fragment {
|
2016-09-25 00:16:28 -05:00
|
|
|
private static final int FETCH_ZIP_CODE = 2;
|
2016-09-26 10:45:34 +08:00
|
|
|
|
|
|
|
@BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
|
|
|
|
@BindView(R.id.recyclerView) RecyclerView recyclerView;
|
|
|
|
@BindView(R.id.empty_rv) TextView emptyTv;
|
|
|
|
@BindView(R.id.fab) FloatingActionButton fabio;
|
|
|
|
|
2016-09-18 22:56:12 +08:00
|
|
|
private SharedPreferences prefs;
|
2016-09-29 01:42:25 +08:00
|
|
|
private List<Module> listModules = new ArrayList<>();
|
2016-09-27 22:57:20 +08:00
|
|
|
private View mView;
|
|
|
|
private SharedPreferences.OnSharedPreferenceChangeListener listener;
|
2016-08-21 16:08:45 +02:00
|
|
|
|
2016-08-21 15:29:42 +02:00
|
|
|
@Nullable
|
2016-08-17 13:00:55 +02:00
|
|
|
@Override
|
2016-08-21 15:29:42 +02:00
|
|
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
2016-09-27 22:57:20 +08:00
|
|
|
mView = inflater.inflate(R.layout.modules_fragment, container, false);
|
|
|
|
ButterKnife.bind(this, mView);
|
2016-09-26 10:45:34 +08:00
|
|
|
|
2016-09-25 00:16:28 -05:00
|
|
|
fabio.setOnClickListener(v -> {
|
2016-12-11 20:38:15 +08:00
|
|
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
|
intent.setType("application/zip");
|
|
|
|
startActivityForResult(intent, FETCH_ZIP_CODE);
|
2016-09-23 16:22:11 -05:00
|
|
|
});
|
2016-09-26 10:45:34 +08:00
|
|
|
|
2016-09-18 22:56:12 +08:00
|
|
|
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
2016-09-14 17:12:47 -05:00
|
|
|
|
2016-09-26 10:45:34 +08:00
|
|
|
mSwipeRefreshLayout.setOnRefreshListener(() -> {
|
2016-09-18 22:56:12 +08:00
|
|
|
recyclerView.setVisibility(View.GONE);
|
2016-09-27 22:57:20 +08:00
|
|
|
prefs.edit().putBoolean("module_done", false).apply();
|
2016-11-07 21:06:18 +08:00
|
|
|
new Async.LoadModules(prefs).exec();
|
2016-09-18 22:56:12 +08:00
|
|
|
});
|
2016-09-15 12:52:58 -05:00
|
|
|
|
2016-09-27 22:57:20 +08:00
|
|
|
if (prefs.getBoolean("module_done", false)) {
|
|
|
|
updateUI();
|
|
|
|
}
|
|
|
|
|
|
|
|
listener = (pref, s) -> {
|
|
|
|
if (s.equals("module_done")) {
|
|
|
|
if (pref.getBoolean(s, false)) {
|
2016-09-28 00:33:01 +08:00
|
|
|
Logger.dev("ModulesFragment: UI refresh triggered");
|
2016-09-27 22:57:20 +08:00
|
|
|
updateUI();
|
|
|
|
}
|
2016-09-15 12:52:58 -05:00
|
|
|
}
|
2016-09-27 22:57:20 +08:00
|
|
|
};
|
2016-09-14 17:12:47 -05:00
|
|
|
|
2016-09-27 22:57:20 +08:00
|
|
|
return mView;
|
2016-08-21 16:42:50 +02:00
|
|
|
}
|
2016-09-09 23:40:57 -05:00
|
|
|
|
2016-09-25 00:16:28 -05:00
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
2016-12-11 20:38:15 +08:00
|
|
|
if (requestCode == FETCH_ZIP_CODE && resultCode == Activity.RESULT_OK && data != null) {
|
2016-09-25 00:16:28 -05:00
|
|
|
// Get the URI of the selected file
|
|
|
|
final Uri uri = data.getData();
|
2016-11-07 21:06:18 +08:00
|
|
|
new Async.FlashZIP(getActivity(), uri).exec();
|
2016-09-25 00:16:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-23 16:22:11 -05:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
2016-09-27 22:57:20 +08:00
|
|
|
mView = this.getView();
|
|
|
|
prefs.registerOnSharedPreferenceChangeListener(listener);
|
2016-09-23 16:22:11 -05:00
|
|
|
}
|
|
|
|
|
2016-09-27 22:57:20 +08:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
prefs.unregisterOnSharedPreferenceChangeListener(listener);
|
|
|
|
}
|
2016-08-21 16:42:50 +02:00
|
|
|
|
2016-09-27 22:57:20 +08:00
|
|
|
private void updateUI() {
|
2016-09-29 01:42:25 +08:00
|
|
|
ModuleHelper.getModuleList(listModules);
|
2016-09-27 22:57:20 +08:00
|
|
|
if (listModules.size() == 0) {
|
|
|
|
emptyTv.setVisibility(View.VISIBLE);
|
|
|
|
recyclerView.setVisibility(View.GONE);
|
|
|
|
} else {
|
2016-11-11 10:40:54 +08:00
|
|
|
emptyTv.setVisibility(View.GONE);
|
2016-09-27 22:57:20 +08:00
|
|
|
recyclerView.setVisibility(View.VISIBLE);
|
|
|
|
recyclerView.setAdapter(new ModulesAdapter(listModules));
|
2016-08-21 16:42:50 +02:00
|
|
|
}
|
2016-09-27 22:57:20 +08:00
|
|
|
mSwipeRefreshLayout.setRefreshing(false);
|
2016-08-21 16:42:50 +02:00
|
|
|
}
|
2016-09-14 17:12:47 -05:00
|
|
|
|
2016-08-17 13:00:55 +02:00
|
|
|
}
|