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

215 lines
7.7 KiB
Java
Raw Normal View History

package com.topjohnwu.magisk;
import android.os.Bundle;
import android.support.annotation.Nullable;
2016-10-17 02:11:26 +00:00
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
2016-10-17 02:11:26 +00:00
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
2016-10-17 02:11:26 +00:00
import android.widget.SearchView;
import android.widget.TextView;
2016-11-07 16:09:08 +00:00
import com.topjohnwu.magisk.adapters.ReposAdapter;
import com.topjohnwu.magisk.adapters.SimpleSectionedRecyclerViewAdapter;
2017-02-06 18:01:32 +00:00
import com.topjohnwu.magisk.components.Fragment;
import com.topjohnwu.magisk.module.Module;
import com.topjohnwu.magisk.module.Repo;
2016-09-20 17:08:05 +00:00
import com.topjohnwu.magisk.utils.Async;
2016-12-25 07:11:59 +00:00
import com.topjohnwu.magisk.utils.CallbackHandler;
2016-09-25 15:11:57 +00:00
import com.topjohnwu.magisk.utils.Logger;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
2016-12-25 07:11:59 +00:00
public class ReposFragment extends Fragment implements CallbackHandler.EventListener {
private Unbinder unbinder;
@BindView(R.id.recyclerView) RecyclerView recyclerView;
2017-01-27 17:10:50 +00:00
@BindView(R.id.empty_rv) TextView emptyRv;
@BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
2016-10-17 02:11:26 +00:00
2016-09-28 17:42:25 +00:00
private List<Repo> mUpdateRepos = new ArrayList<>();
private List<Repo> mInstalledRepos = new ArrayList<>();
private List<Repo> mOthersRepos = new ArrayList<>();
2016-10-17 02:11:26 +00:00
private List<Repo> fUpdateRepos = new ArrayList<>();
private List<Repo> fInstalledRepos = new ArrayList<>();
private List<Repo> fOthersRepos = new ArrayList<>();
private SimpleSectionedRecyclerViewAdapter mSectionedAdapter;
private SearchView.OnQueryTextListener searchListener;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
2017-01-25 09:07:23 +00:00
View view = inflater.inflate(R.layout.fragment_repos, container, false);
unbinder = ButterKnife.bind(this, view);
2016-09-27 14:57:20 +00:00
mSectionedAdapter = new SimpleSectionedRecyclerViewAdapter(R.layout.section,
2016-12-25 07:11:59 +00:00
R.id.section_text, new ReposAdapter(fUpdateRepos, fInstalledRepos, fOthersRepos));
2016-10-17 02:11:26 +00:00
recyclerView.setAdapter(mSectionedAdapter);
2016-09-27 14:57:20 +00:00
mSwipeRefreshLayout.setRefreshing(true);
mSwipeRefreshLayout.setOnRefreshListener(() -> {
recyclerView.setVisibility(View.GONE);
2017-02-06 18:01:32 +00:00
new Async.LoadRepos(getApplication()).exec();
2016-09-25 13:59:54 +00:00
});
2016-09-27 14:57:20 +00:00
2017-02-06 18:01:32 +00:00
if (getApplication().repoLoadDone.isTriggered) {
2016-10-17 02:11:26 +00:00
reloadRepos();
2016-09-27 14:57:20 +00:00
updateUI();
}
2016-10-17 02:11:26 +00:00
searchListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
2016-11-20 10:49:09 +00:00
new FilterApps().exec(newText);
2016-10-17 02:11:26 +00:00
return false;
}
};
2016-09-28 17:42:25 +00:00
return view;
}
2016-12-25 07:11:59 +00:00
@Override
public void onTrigger(CallbackHandler.Event event) {
Logger.dev("ReposFragment: UI refresh triggered");
reloadRepos();
updateUI();
}
2016-09-13 20:44:07 +00:00
@Override
2016-10-17 02:11:26 +00: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 20:44:07 +00:00
}
@Override
public void onStart() {
super.onStart();
2017-02-06 18:01:32 +00:00
CallbackHandler.register(getApplication().repoLoadDone, this);
2016-12-24 19:05:22 +00:00
getActivity().setTitle(R.string.downloads);
2016-09-23 21:22:11 +00:00
}
2016-09-27 14:57:20 +00:00
@Override
public void onStop() {
2017-02-06 18:01:32 +00:00
CallbackHandler.unRegister(getApplication().repoLoadDone, this);
super.onStop();
2016-09-27 14:57:20 +00:00
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
2016-10-17 02:11:26 +00:00
private void reloadRepos() {
2017-02-06 18:01:32 +00:00
mUpdateRepos.clear();
mInstalledRepos.clear();
mOthersRepos.clear();
for (Repo repo : getApplication().repoMap.values()) {
Module module = getApplication().moduleMap.get(repo.getId());
if (module != null) {
if (repo.getVersionCode() > module.getVersionCode())
mUpdateRepos.add(repo);
else
mInstalledRepos.add(repo);
} else {
mOthersRepos.add(repo);
}
}
2016-10-17 02:11:26 +00: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) {
2017-01-27 17:10:50 +00:00
emptyRv.setVisibility(View.VISIBLE);
2016-09-27 14:57:20 +00:00
recyclerView.setVisibility(View.GONE);
} else {
2016-09-28 17:42:25 +00:00
List<SimpleSectionedRecyclerViewAdapter.Section> sections = new ArrayList<>();
2016-10-17 02:11:26 +00:00
if (!fUpdateRepos.isEmpty()) {
2016-09-28 17:42:25 +00:00
sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0, getString(R.string.update_available)));
}
2016-10-17 02:11:26 +00:00
if (!fInstalledRepos.isEmpty()) {
sections.add(new SimpleSectionedRecyclerViewAdapter.Section(fUpdateRepos.size(), getString(R.string.installed)));
2016-09-28 17:42:25 +00:00
}
2016-10-17 02:11:26 +00:00
if (!fOthersRepos.isEmpty()) {
sections.add(new SimpleSectionedRecyclerViewAdapter.Section(fUpdateRepos.size() + fInstalledRepos.size(), getString(R.string.not_installed)));
2016-09-28 17:42:25 +00:00
}
SimpleSectionedRecyclerViewAdapter.Section[] array = sections.toArray(new SimpleSectionedRecyclerViewAdapter.Section[sections.size()]);
mSectionedAdapter.setSections(array);
2017-01-27 17:10:50 +00:00
emptyRv.setVisibility(View.GONE);
2016-09-27 14:57:20 +00:00
recyclerView.setVisibility(View.VISIBLE);
}
2016-09-27 14:57:20 +00:00
mSwipeRefreshLayout.setRefreshing(false);
}
2016-11-20 10:49:09 +00:00
private class FilterApps extends Async.NormalTask<String, Void, Void> {
@Override
protected Void doInBackground(String... strings) {
String newText = strings[0];
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);
}
}
return null;
}
@Override
protected void onPostExecute(Void v) {
updateUI();
}
}
}