2016-09-11 20:36:58 +02:00
|
|
|
package com.topjohnwu.magisk;
|
|
|
|
|
2016-09-23 23:22:11 +02:00
|
|
|
import android.app.Fragment;
|
2016-09-27 16:57:20 +02:00
|
|
|
import android.content.SharedPreferences;
|
2016-09-11 20:36:58 +02:00
|
|
|
import android.os.Bundle;
|
2016-09-25 17:11:57 +02:00
|
|
|
import android.preference.PreferenceManager;
|
2016-09-11 20:36:58 +02:00
|
|
|
import android.support.annotation.Nullable;
|
2016-10-17 04:11:26 +02:00
|
|
|
import android.support.v4.view.MenuItemCompat;
|
2016-09-11 20:36:58 +02:00
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.view.LayoutInflater;
|
2016-10-17 04:11:26 +02:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
2016-09-11 20:36:58 +02:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2016-10-17 04:11:26 +02:00
|
|
|
import android.widget.SearchView;
|
2016-09-11 20:36:58 +02:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.topjohnwu.magisk.module.Repo;
|
2016-09-20 19:08:05 +02:00
|
|
|
import com.topjohnwu.magisk.utils.Async;
|
2016-09-25 17:11:57 +02:00
|
|
|
import com.topjohnwu.magisk.utils.Logger;
|
2016-09-28 19:42:25 +02:00
|
|
|
import com.topjohnwu.magisk.utils.ModuleHelper;
|
2016-09-11 20:36:58 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
|
|
|
public class ReposFragment extends Fragment {
|
|
|
|
|
2016-09-29 22:18:41 +02:00
|
|
|
@BindView(R.id.recyclerView) RecyclerView recyclerView;
|
|
|
|
@BindView(R.id.empty_rv) TextView emptyTv;
|
|
|
|
@BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
|
2016-10-17 04:11:26 +02:00
|
|
|
|
2016-09-28 19:42:25 +02:00
|
|
|
private List<Repo> mUpdateRepos = new ArrayList<>();
|
|
|
|
private List<Repo> mInstalledRepos = new ArrayList<>();
|
|
|
|
private List<Repo> mOthersRepos = new ArrayList<>();
|
2016-10-17 04:11:26 +02:00
|
|
|
private List<Repo> fUpdateRepos = new ArrayList<>();
|
|
|
|
private List<Repo> fInstalledRepos = new ArrayList<>();
|
|
|
|
private List<Repo> fOthersRepos = new ArrayList<>();
|
|
|
|
|
|
|
|
private SimpleSectionedRecyclerViewAdapter mSectionedAdapter;
|
|
|
|
|
2016-09-27 16:57:20 +02:00
|
|
|
private SharedPreferences.OnSharedPreferenceChangeListener listener;
|
|
|
|
private SharedPreferences prefs;
|
2016-10-17 04:11:26 +02:00
|
|
|
private SearchView.OnQueryTextListener searchListener;
|
2016-09-11 20:36:58 +02:00
|
|
|
|
|
|
|
@Nullable
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
2016-09-28 19:42:25 +02:00
|
|
|
View view = inflater.inflate(R.layout.repos_fragment, container, false);
|
2016-10-17 04:11:26 +02:00
|
|
|
|
2016-09-28 19:42:25 +02:00
|
|
|
ButterKnife.bind(this, view);
|
2016-09-27 16:57:20 +02:00
|
|
|
|
2016-10-17 04:11:26 +02:00
|
|
|
mSectionedAdapter = new
|
2016-10-17 10:07:47 +02:00
|
|
|
SimpleSectionedRecyclerViewAdapter(getActivity(), R.layout.section, R.id.section_text, new ReposAdapter(fUpdateRepos, fInstalledRepos, fOthersRepos));
|
2016-10-17 04:11:26 +02:00
|
|
|
|
|
|
|
recyclerView.setAdapter(mSectionedAdapter);
|
|
|
|
|
2016-09-27 16:57:20 +02:00
|
|
|
mSwipeRefreshLayout.setRefreshing(true);
|
|
|
|
|
|
|
|
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
|
|
|
|
|
|
|
mSwipeRefreshLayout.setOnRefreshListener(() -> {
|
|
|
|
recyclerView.setVisibility(View.GONE);
|
|
|
|
prefs.edit().putBoolean("repo_done", false).apply();
|
2016-11-07 14:06:18 +01:00
|
|
|
new Async.LoadRepos(getActivity()).exec();
|
2016-09-25 15:59:54 +02:00
|
|
|
});
|
2016-09-27 16:57:20 +02:00
|
|
|
|
|
|
|
if (prefs.getBoolean("repo_done", false)) {
|
2016-10-17 04:11:26 +02:00
|
|
|
reloadRepos();
|
2016-09-27 16:57:20 +02:00
|
|
|
updateUI();
|
|
|
|
}
|
|
|
|
|
|
|
|
listener = (pref, s) -> {
|
|
|
|
if (s.equals("repo_done")) {
|
|
|
|
if (pref.getBoolean(s, false)) {
|
2016-09-27 18:33:01 +02:00
|
|
|
Logger.dev("ReposFragment: UI refresh triggered");
|
2016-10-17 04:11:26 +02:00
|
|
|
reloadRepos();
|
2016-09-27 16:57:20 +02:00
|
|
|
updateUI();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-17 04:11:26 +02:00
|
|
|
searchListener = new SearchView.OnQueryTextListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onQueryTextSubmit(String query) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onQueryTextChange(String newText) {
|
|
|
|
fUpdateRepos.clear();
|
|
|
|
fInstalledRepos.clear();
|
|
|
|
fOthersRepos.clear();
|
|
|
|
for (Repo repo: mUpdateRepos) {
|
|
|
|
if (repo.getName().toLowerCase().contains(newText.toLowerCase())
|
|
|
|
|| repo.getAuthor().toLowerCase().contains(newText.toLowerCase())
|
|
|
|
|| repo.getDescription().toLowerCase().contains(newText.toLowerCase())
|
|
|
|
) {
|
|
|
|
fUpdateRepos.add(repo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (Repo repo: mInstalledRepos) {
|
|
|
|
if (repo.getName().toLowerCase().contains(newText.toLowerCase())
|
|
|
|
|| repo.getAuthor().toLowerCase().contains(newText.toLowerCase())
|
|
|
|
|| repo.getDescription().toLowerCase().contains(newText.toLowerCase())
|
|
|
|
) {
|
|
|
|
fInstalledRepos.add(repo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (Repo repo: mOthersRepos) {
|
|
|
|
if (repo.getName().toLowerCase().contains(newText.toLowerCase())
|
|
|
|
|| repo.getAuthor().toLowerCase().contains(newText.toLowerCase())
|
|
|
|
|| repo.getDescription().toLowerCase().contains(newText.toLowerCase())
|
|
|
|
) {
|
|
|
|
fOthersRepos.add(repo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updateUI();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-28 19:42:25 +02:00
|
|
|
return view;
|
2016-09-11 20:36:58 +02:00
|
|
|
}
|
|
|
|
|
2016-09-13 22:44:07 +02:00
|
|
|
@Override
|
2016-10-17 04:11:26 +02:00
|
|
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
|
|
|
inflater.inflate(R.menu.menu_repo, menu);
|
|
|
|
SearchView search = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.repo_search));
|
|
|
|
search.setOnQueryTextListener(searchListener);
|
2016-09-13 22:44:07 +02:00
|
|
|
}
|
|
|
|
|
2016-09-11 20:36:58 +02:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
2016-10-17 04:11:26 +02:00
|
|
|
setHasOptionsMenu(true);
|
2016-09-27 16:57:20 +02:00
|
|
|
prefs.registerOnSharedPreferenceChangeListener(listener);
|
2016-09-23 23:22:11 +02:00
|
|
|
}
|
2016-09-11 20:36:58 +02:00
|
|
|
|
2016-09-27 16:57:20 +02:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
prefs.unregisterOnSharedPreferenceChangeListener(listener);
|
|
|
|
}
|
2016-09-11 20:36:58 +02:00
|
|
|
|
2016-10-17 04:11:26 +02:00
|
|
|
private void reloadRepos() {
|
2016-09-28 19:42:25 +02:00
|
|
|
ModuleHelper.getRepoLists(mUpdateRepos, mInstalledRepos, mOthersRepos);
|
2016-10-17 04:11:26 +02:00
|
|
|
fUpdateRepos.clear();
|
|
|
|
fInstalledRepos.clear();
|
|
|
|
fOthersRepos.clear();
|
|
|
|
fUpdateRepos.addAll(mUpdateRepos);
|
|
|
|
fInstalledRepos.addAll(mInstalledRepos);
|
|
|
|
fOthersRepos.addAll(mOthersRepos);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateUI() {
|
|
|
|
if (fUpdateRepos.size() + fInstalledRepos.size() + fOthersRepos.size() == 0) {
|
2016-09-27 16:57:20 +02:00
|
|
|
emptyTv.setVisibility(View.VISIBLE);
|
|
|
|
recyclerView.setVisibility(View.GONE);
|
|
|
|
} else {
|
2016-09-28 19:42:25 +02:00
|
|
|
List<SimpleSectionedRecyclerViewAdapter.Section> sections = new ArrayList<>();
|
2016-10-17 04:11:26 +02:00
|
|
|
if (!fUpdateRepos.isEmpty()) {
|
2016-09-28 19:42:25 +02:00
|
|
|
sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0, getString(R.string.update_available)));
|
|
|
|
}
|
2016-10-17 04:11:26 +02:00
|
|
|
if (!fInstalledRepos.isEmpty()) {
|
|
|
|
sections.add(new SimpleSectionedRecyclerViewAdapter.Section(fUpdateRepos.size(), getString(R.string.installed)));
|
2016-09-28 19:42:25 +02:00
|
|
|
}
|
2016-10-17 04:11:26 +02:00
|
|
|
if (!fOthersRepos.isEmpty()) {
|
|
|
|
sections.add(new SimpleSectionedRecyclerViewAdapter.Section(fUpdateRepos.size() + fInstalledRepos.size(), getString(R.string.not_installed)));
|
2016-09-28 19:42:25 +02:00
|
|
|
}
|
|
|
|
SimpleSectionedRecyclerViewAdapter.Section[] array = sections.toArray(new SimpleSectionedRecyclerViewAdapter.Section[sections.size()]);
|
|
|
|
mSectionedAdapter.setSections(array);
|
2016-10-17 10:07:47 +02:00
|
|
|
emptyTv.setVisibility(View.GONE);
|
2016-09-27 16:57:20 +02:00
|
|
|
recyclerView.setVisibility(View.VISIBLE);
|
2016-09-11 20:36:58 +02:00
|
|
|
}
|
2016-09-27 16:57:20 +02:00
|
|
|
mSwipeRefreshLayout.setRefreshing(false);
|
2016-09-11 20:36:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|