2016-09-11 18:36:58 +00:00
package com.topjohnwu.magisk ;
2016-09-23 21:22:11 +00:00
import android.app.Fragment ;
2016-09-27 14:57:20 +00:00
import android.content.SharedPreferences ;
2016-09-25 13:31:38 +00:00
import android.os.AsyncTask ;
2016-09-11 18:36:58 +00:00
import android.os.Bundle ;
2016-09-25 15:11:57 +00:00
import android.preference.PreferenceManager ;
2016-09-11 18:36:58 +00:00
import android.support.annotation.Nullable ;
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.view.LayoutInflater ;
import android.view.View ;
import android.view.ViewGroup ;
import android.widget.TextView ;
import com.topjohnwu.magisk.module.Repo ;
2016-09-20 17:08:05 +00:00
import com.topjohnwu.magisk.utils.Async ;
2016-09-25 15:11:57 +00:00
import com.topjohnwu.magisk.utils.Logger ;
2016-09-11 18:36:58 +00:00
import java.util.ArrayList ;
import java.util.List ;
import butterknife.BindView ;
import butterknife.ButterKnife ;
public class ReposFragment extends Fragment {
public static List < Repo > mListRepos = new ArrayList < > ( ) ;
2016-09-27 14:57:20 +00:00
@BindView ( R . id . recyclerView ) RecyclerView recyclerView ;
@BindView ( R . id . empty_rv ) TextView emptyTv ;
@BindView ( R . id . swipeRefreshLayout ) SwipeRefreshLayout mSwipeRefreshLayout ;
2016-09-13 20:44:07 +00:00
private View mView ;
private boolean mCanUpdate ;
private boolean alertUpdate ;
private boolean ignoreAlertUpdate ;
private String alertPackage ;
2016-09-25 15:11:57 +00:00
private AlertDialog . Builder builder ;
2016-09-27 14:57:20 +00:00
private SharedPreferences . OnSharedPreferenceChangeListener listener ;
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-27 14:57:20 +00:00
mView = inflater . inflate ( R . layout . repos_fragment , container , false ) ;
ButterKnife . bind ( this , mView ) ;
mSwipeRefreshLayout . setRefreshing ( true ) ;
prefs = PreferenceManager . getDefaultSharedPreferences ( getActivity ( ) ) ;
mSwipeRefreshLayout . setOnRefreshListener ( ( ) - > {
recyclerView . setVisibility ( View . GONE ) ;
prefs . edit ( ) . putBoolean ( " repo_done " , false ) . apply ( ) ;
new Async . LoadRepos ( getActivity ( ) ) . executeOnExecutor ( AsyncTask . THREAD_POOL_EXECUTOR ) ;
2016-09-25 13:59:54 +00:00
} ) ;
2016-09-27 14:57:20 +00:00
if ( prefs . getBoolean ( " repo_done " , false ) ) {
updateUI ( ) ;
}
listener = ( pref , s ) - > {
if ( s . equals ( " repo_done " ) ) {
if ( pref . getBoolean ( s , false ) ) {
2016-09-27 16:33:01 +00:00
Logger . dev ( " ReposFragment: UI refresh triggered " ) ;
2016-09-27 14:57:20 +00:00
updateUI ( ) ;
}
}
} ;
2016-09-26 02:45:34 +00:00
//LoadRepo(false);
// if (mListRepos.size() == 0) {
// emptyTv.setVisibility(View.VISIBLE);
// recyclerView.setVisibility(View.GONE);
// return view;
// }
//CheckForUpdates();
//recyclerView.setAdapter(new ReposAdapter(this, mListRepos));
2016-09-24 18:46:42 +00:00
2016-09-27 14:57:20 +00:00
return mView ;
2016-09-11 18:36:58 +00:00
}
2016-09-27 14:57:20 +00:00
// private void CheckForUpdates() {
// for (int i = 0; i < mListRepos.size(); i++) {
// if (mListRepos.get(i).canUpdate()) {
// alertUpdate = true;
// mListReposToUpdate.add(mListRepos.get(i));
//
// }
// }
// }
2016-09-13 20:44:07 +00:00
@Override
public void onAttachFragment ( Fragment childFragment ) {
super . onAttachFragment ( childFragment ) ;
}
2016-09-26 02:45:34 +00:00
// private void LoadRepo(boolean doReload) {
// RepoHelper.TaskDelegate taskDelegate = result -> {
// if (result.equals("Complete")) {
// Log.d("Magisk", "ReposFragment, got delegate");
// UpdateUI();
// if (mView != null) {
// mView.invalidate();
// mView.requestLayout();
// }
//
// }
//
// };
// Log.d("Magisk", "ReposFragment, LoadRepo called");
// new Async.LoadRepos(getActivity());
// }
// 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");
// new Async.FlashZIP(getActivity(), repo.getId(), file.toString()).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
// }
// };
// String filename = repo.getId().replace(" ", "") + ".zip";
// Utils.downloadAndReceive(getActivity(), receiver, repo.getZipUrl(), filename);
//
// break;
//
// case DialogInterface.BUTTON_NEGATIVE:
//// ignoreAlertUpdate = true;
//// SharedPreferences.Editor editor = prefs.edit();
//// editor.putBoolean("ignoreUpdateAlerts", ignoreAlertUpdate);
//// editor.apply();
// break;
// }
// };
//
// String theme = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString("theme", "");
2016-09-27 16:33:01 +00:00
// Logger.dev("ReposFragment: Theme is " + theme);
2016-09-26 02:45:34 +00:00
// if (theme.equals("Dark")) {
// builder = new AlertDialog.Builder(getActivity(),R.style.AlertDialog_dh);
// } else {
// 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 ( ) ;
2016-09-27 14:57:20 +00:00
getActivity ( ) . setTitle ( R . string . downloads ) ;
prefs . registerOnSharedPreferenceChangeListener ( listener ) ;
2016-09-23 21:22:11 +00:00
}
2016-09-11 18:36:58 +00:00
2016-09-27 14:57:20 +00:00
@Override
public void onDestroy ( ) {
super . onDestroy ( ) ;
prefs . unregisterOnSharedPreferenceChangeListener ( listener ) ;
}
2016-09-11 18:36:58 +00:00
2016-09-27 14:57:20 +00:00
private void updateUI ( ) {
if ( mListRepos . size ( ) = = 0 ) {
emptyTv . setVisibility ( View . VISIBLE ) ;
recyclerView . setVisibility ( View . GONE ) ;
} else {
recyclerView . setVisibility ( View . VISIBLE ) ;
2016-09-26 02:45:34 +00:00
recyclerView . setAdapter ( new ReposAdapter ( mListRepos ) ) ;
2016-09-11 18:36:58 +00:00
}
2016-09-27 14:57:20 +00:00
mSwipeRefreshLayout . setRefreshing ( false ) ;
2016-09-11 18:36:58 +00:00
}
}