2016-09-11 18:36:58 +00:00
|
|
|
package com.topjohnwu.magisk;
|
|
|
|
|
2016-09-13 20:44:07 +00:00
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.SharedPreferences;
|
2016-09-11 18:36:58 +00:00
|
|
|
import android.os.Bundle;
|
2016-09-13 20:44:07 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2016-09-11 18:36:58 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2016-09-21 21:55:20 +00:00
|
|
|
import android.app.Fragment;
|
2016-09-11 18:36:58 +00:00
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
2016-09-13 20:44:07 +00:00
|
|
|
import android.support.v7.app.AlertDialog;
|
2016-09-11 18:36:58 +00:00
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.topjohnwu.magisk.module.Repo;
|
|
|
|
import com.topjohnwu.magisk.module.RepoHelper;
|
2016-09-20 17:08:05 +00:00
|
|
|
import com.topjohnwu.magisk.utils.Async;
|
2016-09-13 20:44:07 +00:00
|
|
|
import com.topjohnwu.magisk.utils.Utils;
|
2016-09-11 18:36:58 +00:00
|
|
|
|
2016-09-13 20:44:07 +00:00
|
|
|
import java.io.File;
|
2016-09-11 18:36:58 +00:00
|
|
|
import java.util.ArrayList;
|
2016-09-13 20:44:07 +00:00
|
|
|
import java.util.Iterator;
|
2016-09-11 18:36:58 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
|
|
|
public class ReposFragment extends Fragment {
|
|
|
|
|
|
|
|
public static List<Repo> mListRepos = new ArrayList<>();
|
2016-09-13 20:44:07 +00:00
|
|
|
public static List<Repo> mListReposToUpdate = new ArrayList<>();
|
2016-09-11 18:36:58 +00:00
|
|
|
@BindView(R.id.recyclerView)
|
|
|
|
RecyclerView recyclerView;
|
|
|
|
@BindView(R.id.empty_rv)
|
|
|
|
TextView emptyTv;
|
|
|
|
@BindView(R.id.swipeRefreshLayout)
|
|
|
|
SwipeRefreshLayout swipeRefreshLayout;
|
2016-09-13 20:44:07 +00:00
|
|
|
private View mView;
|
|
|
|
private boolean mCanUpdate;
|
|
|
|
private boolean alertUpdate;
|
|
|
|
private boolean ignoreAlertUpdate;
|
|
|
|
private String alertPackage;
|
|
|
|
private SharedPreferences prefs;
|
2016-09-11 18:36:58 +00:00
|
|
|
|
|
|
|
@Nullable
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
2016-09-18 14:56:12 +00:00
|
|
|
View view = inflater.inflate(R.layout.repos_fragment, container, false);
|
2016-09-13 20:44:07 +00:00
|
|
|
mView = view;
|
2016-09-11 18:36:58 +00:00
|
|
|
ButterKnife.bind(this, view);
|
2016-09-13 20:44:07 +00:00
|
|
|
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
|
|
|
if (prefs.contains("ignoreUpdateAlerts")) {
|
|
|
|
ignoreAlertUpdate = prefs.getBoolean("ignoreUpdateAlerts", false);
|
|
|
|
}
|
2016-09-11 18:36:58 +00:00
|
|
|
swipeRefreshLayout.setOnRefreshListener(() -> {
|
|
|
|
this.LoadRepo(true);
|
2016-09-13 20:44:07 +00:00
|
|
|
ignoreAlertUpdate = false;
|
|
|
|
prefs.edit().putBoolean("ignoreUpdateAlerts",false).apply();
|
|
|
|
|
2016-09-11 18:36:58 +00:00
|
|
|
});
|
|
|
|
LoadRepo(false);
|
|
|
|
setHasOptionsMenu(false);
|
2016-09-13 20:44:07 +00:00
|
|
|
alertUpdate = false;
|
2016-09-11 18:36:58 +00:00
|
|
|
if (mListRepos.size() == 0) {
|
|
|
|
emptyTv.setVisibility(View.VISIBLE);
|
|
|
|
recyclerView.setVisibility(View.GONE);
|
|
|
|
return view;
|
|
|
|
}
|
2016-09-13 20:44:07 +00:00
|
|
|
CheckForUpdates();
|
2016-09-11 18:36:58 +00:00
|
|
|
Log.d("Magisk", "ReposFragment: ListRepos size is " + listRepos().size());
|
|
|
|
recyclerView.setAdapter(new ReposAdapter(this, mListRepos));
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2016-09-13 20:44:07 +00:00
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
NotifyOfAlerts();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CheckForUpdates() {
|
|
|
|
for (int i = 0; i < mListRepos.size(); i++) {
|
|
|
|
if (mListRepos.get(i).canUpdate()) {
|
|
|
|
alertUpdate = true;
|
|
|
|
mListReposToUpdate.add(mListRepos.get(i));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAttachFragment(Fragment childFragment) {
|
|
|
|
super.onAttachFragment(childFragment);
|
|
|
|
}
|
|
|
|
|
2016-09-11 18:36:58 +00:00
|
|
|
private void LoadRepo (boolean doReload) {
|
2016-09-13 04:05:04 +00:00
|
|
|
RepoHelper.TaskDelegate taskDelegate = result -> {
|
2016-09-11 18:36:58 +00:00
|
|
|
if (result.equals("Complete")) {
|
|
|
|
Log.d("Magisk", "ReposFragment, got delegate");
|
|
|
|
UpdateUI();
|
2016-09-13 20:44:07 +00:00
|
|
|
if (mView != null) {
|
|
|
|
mView.invalidate();
|
|
|
|
mView.requestLayout();
|
|
|
|
}
|
|
|
|
|
2016-09-11 18:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
Log.d("Magisk","ReposFragment, LoadRepo called");
|
2016-09-21 03:29:43 +00:00
|
|
|
new Async.LoadRepos(getActivity());
|
2016-09-11 18:36:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 20:44:07 +00:00
|
|
|
private void NotifyOfAlerts() {
|
|
|
|
if (alertUpdate && !ignoreAlertUpdate) {
|
|
|
|
Iterator<Repo> iterRepo = mListReposToUpdate.iterator();
|
|
|
|
while (iterRepo.hasNext()) {
|
|
|
|
Repo repo = iterRepo.next();
|
|
|
|
DialogInterface.OnClickListener dialogClickListener = (dialog, which) -> {
|
|
|
|
switch (which) {
|
|
|
|
case DialogInterface.BUTTON_POSITIVE:
|
|
|
|
Utils.DownloadReceiver receiver = new Utils.DownloadReceiver() {
|
|
|
|
@Override
|
|
|
|
public void task(File file) {
|
|
|
|
Log.d("Magisk", "Task firing");
|
2016-09-20 17:08:05 +00:00
|
|
|
new Async.FlashZIP(getActivity(), repo.getId(), file.toString()).execute();
|
2016-09-13 20:44:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
String filename = repo.getId().replace(" ", "") + ".zip";
|
2016-09-21 03:29:43 +00:00
|
|
|
Utils.downloadAndReceive(getActivity(), receiver, repo.getZipUrl(), filename);
|
2016-09-13 20:44:07 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DialogInterface.BUTTON_NEGATIVE:
|
|
|
|
ignoreAlertUpdate = true;
|
|
|
|
SharedPreferences.Editor editor = prefs.edit();
|
|
|
|
editor.putBoolean("ignoreUpdateAlerts", ignoreAlertUpdate);
|
|
|
|
editor.apply();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
|
|
|
builder.setMessage("An update is available for " + repo.getName() + ". Would you like to install it?").setPositiveButton("Yes", dialogClickListener)
|
|
|
|
.setNegativeButton("No", dialogClickListener).show();
|
|
|
|
iterRepo.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2016-09-11 18:36:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
LoadRepo(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected List<Repo> listRepos() {
|
|
|
|
return mListRepos;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UpdateUI() {
|
|
|
|
Log.d("Magisk","ReposFragment: UpdateUI Called, size is " + listRepos().size());
|
|
|
|
|
|
|
|
if (listRepos().size() == 0) {
|
|
|
|
emptyTv.setVisibility(View.VISIBLE);
|
|
|
|
recyclerView.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
}
|
|
|
|
Log.d("Magisk", "ReposFragment: ListRepos size is " + listRepos().size());
|
|
|
|
recyclerView.setAdapter(new ReposAdapter(this, listRepos()));
|
|
|
|
if (swipeRefreshLayout.isRefreshing()) {
|
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
2016-09-13 20:44:07 +00:00
|
|
|
CheckForUpdates();
|
|
|
|
NotifyOfAlerts();
|
2016-09-11 18:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|