Magisk Update checker use prefs listener

This commit is contained in:
topjohnwu
2016-09-28 18:05:55 +08:00
parent cb5187fd8d
commit 0acc5e33b3
11 changed files with 411 additions and 276 deletions

View File

@@ -2,6 +2,7 @@ package com.topjohnwu.magisk;
import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.net.Uri;
@@ -11,6 +12,7 @@ import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
@@ -21,6 +23,7 @@ import android.widget.TextView;
import com.topjohnwu.magisk.receivers.DownloadReceiver;
import com.topjohnwu.magisk.utils.Async;
import com.topjohnwu.magisk.utils.Logger;
import com.topjohnwu.magisk.utils.Utils;
import java.io.File;
@@ -30,6 +33,8 @@ import butterknife.BindView;
import butterknife.ButterKnife;
public class MagiskFragment extends Fragment {
@BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
@BindView(R.id.magiskStatusView) View magiskStatusView;
@BindView(R.id.magisk_status_container) View magiskStatusContainer;
@BindView(R.id.magisk_status_icon) ImageView magiskStatusIcon;
@@ -54,6 +59,9 @@ public class MagiskFragment extends Fragment {
int statusUnknown = R.drawable.ic_help;
private AlertDialog.Builder builder;
private SharedPreferences prefs;
private SharedPreferences.OnSharedPreferenceChangeListener listener;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@@ -86,7 +94,25 @@ public class MagiskFragment extends Fragment {
magiskVersion.setTextColor(colorOK);
}
new updateUI().executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
mSwipeRefreshLayout.setOnRefreshListener(() -> {
prefs.edit().putBoolean("update_check_done", false).apply();
new Async.CheckUpdates(getActivity()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
});
if (prefs.getBoolean("update_check_done", false)) {
updateUI();
}
listener = (pref, s) -> {
if (s.equals("update_check_done")) {
if (pref.getBoolean(s, false)) {
Logger.dev("MagiskFragment: UI refresh triggered");
updateUI();
}
}
};
return v;
}
@@ -94,102 +120,215 @@ public class MagiskFragment extends Fragment {
@Override
public void onResume() {
super.onResume();
getActivity().setTitle("Magisk");
getActivity().setTitle(R.string.magisk);
prefs.registerOnSharedPreferenceChangeListener(listener);
}
private class updateUI extends AsyncTask<Void, Void, Void> {
@Override
public void onDestroy() {
super.onDestroy();
prefs.unregisterOnSharedPreferenceChangeListener(listener);
}
@Override
protected Void doInBackground(Void... voids) {
return null;
private void updateUI() {
String theme = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString("theme", "");
if (theme.equals("Dark")) {
builder = new AlertDialog.Builder(getActivity(),R.style.AlertDialog_dh);
} else {
builder = new AlertDialog.Builder(getActivity());
}
@Override
protected void onPostExecute(Void v) {
super.onPostExecute(v);
String theme = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString("theme", "");
if (theme.equals("Dark")) {
builder = new AlertDialog.Builder(getActivity(),R.style.AlertDialog_dh);
if (Utils.remoteMagiskVersion == -1) {
appCheckUpdatesContainer.setBackgroundColor(colorWarn);
magiskCheckUpdatesContainer.setBackgroundColor(colorWarn);
appCheckUpdatesIcon.setImageResource(R.drawable.ic_warning);
magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_warning);
appCheckUpdatesStatus.setText(R.string.cannot_check_updates);
appCheckUpdatesStatus.setTextColor(colorWarn);
magiskCheckUpdatesStatus.setText(R.string.cannot_check_updates);
magiskCheckUpdatesStatus.setTextColor(colorWarn);
} else {
if (Utils.remoteMagiskVersion > Utils.magiskVersion) {
magiskCheckUpdatesContainer.setBackgroundColor(colorNeutral);
magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_file_download);
magiskCheckUpdatesStatus.setText(getString(R.string.magisk_update_available, String.valueOf(Utils.remoteMagiskVersion)));
magiskCheckUpdatesStatus.setTextColor(colorNeutral);
magiskUpdateView.setOnClickListener(view -> builder
.setTitle(getString(R.string.update_title, getString(R.string.magisk)))
.setMessage(getString(R.string.update_msg, getString(R.string.magisk), String.valueOf(Utils.remoteMagiskVersion), Utils.magiskChangelog))
.setCancelable(true)
.setPositiveButton(R.string.download_install, (dialogInterface, i) -> Utils.downloadAndReceive(
getActivity(),
new DownloadReceiver() {
@Override
public void task(Uri uri) {
new Async.FlashZIP(mContext, uri).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}
},
Utils.magiskLink,
"Magisk-v" + String.valueOf(Utils.remoteMagiskVersion) + ".zip"))
.setNegativeButton(R.string.no_thanks, null)
.show());
} else {
builder = new AlertDialog.Builder(getActivity());
magiskCheckUpdatesContainer.setBackgroundColor(colorOK);
magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_check_circle);
magiskCheckUpdatesStatus.setText(getString(R.string.up_to_date, getString(R.string.magisk)));
magiskCheckUpdatesStatus.setTextColor(colorOK);
}
if (Utils.remoteMagiskVersion == -1) {
appCheckUpdatesContainer.setBackgroundColor(colorWarn);
magiskCheckUpdatesContainer.setBackgroundColor(colorWarn);
appCheckUpdatesIcon.setImageResource(R.drawable.ic_warning);
magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_warning);
appCheckUpdatesStatus.setText(R.string.cannot_check_updates);
appCheckUpdatesStatus.setTextColor(colorWarn);
magiskCheckUpdatesStatus.setText(R.string.cannot_check_updates);
magiskCheckUpdatesStatus.setTextColor(colorWarn);
if (Utils.remoteAppVersionCode > BuildConfig.VERSION_CODE) {
appCheckUpdatesContainer.setBackgroundColor(colorNeutral);
appCheckUpdatesIcon.setImageResource(R.drawable.ic_file_download);
appCheckUpdatesStatus.setText(getString(R.string.app_update_available, Utils.remoteAppVersion));
appCheckUpdatesStatus.setTextColor(colorNeutral);
appUpdateView.setOnClickListener(view -> builder
.setTitle(getString(R.string.update_title, getString(R.string.app_name)))
.setMessage(getString(R.string.update_msg, getString(R.string.app_name), Utils.remoteAppVersion, Utils.appChangelog))
.setCancelable(true)
.setPositiveButton(R.string.download_install, (dialogInterface, i) -> Utils.downloadAndReceive(getActivity(),
new DownloadReceiver() {
@Override
public void task(Uri uri) {
Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri content = FileProvider.getUriForFile(getActivity(), "com.topjohnwu.magisk.provider", new File(uri.getPath()));
install.setData(content);
mContext.startActivity(install);
}
},
Utils.appLink,
"MagiskManager-v" + Utils.remoteAppVersion + ".apk"))
.setNegativeButton(R.string.no_thanks, null)
.show()
);
} else {
if (Utils.remoteMagiskVersion > Utils.magiskVersion) {
magiskCheckUpdatesContainer.setBackgroundColor(colorNeutral);
magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_file_download);
magiskCheckUpdatesStatus.setText(getString(R.string.magisk_update_available, String.valueOf(Utils.remoteMagiskVersion)));
magiskCheckUpdatesStatus.setTextColor(colorNeutral);
magiskUpdateView.setOnClickListener(view -> builder
.setTitle(getString(R.string.update_title, getString(R.string.magisk)))
.setMessage(getString(R.string.update_msg, getString(R.string.magisk), String.valueOf(Utils.remoteMagiskVersion), Utils.magiskChangelog))
.setCancelable(true)
.setPositiveButton(R.string.download_install, (dialogInterface, i) -> Utils.downloadAndReceive(
getActivity(),
new DownloadReceiver() {
@Override
public void task(Uri uri) {
new Async.FlashZIP(mContext, uri).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}
},
Utils.magiskLink,
"Magisk-v" + String.valueOf(Utils.remoteMagiskVersion) + ".zip"))
.setNegativeButton(R.string.no_thanks, null)
.show());
} else {
magiskCheckUpdatesContainer.setBackgroundColor(colorOK);
magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_check_circle);
magiskCheckUpdatesStatus.setText(getString(R.string.up_to_date, getString(R.string.magisk)));
magiskCheckUpdatesStatus.setTextColor(colorOK);
}
appCheckUpdatesContainer.setBackgroundColor(colorOK);
appCheckUpdatesIcon.setImageResource(R.drawable.ic_check_circle);
appCheckUpdatesStatus.setText(getString(R.string.up_to_date, getString(R.string.app_name)));
appCheckUpdatesStatus.setTextColor(colorOK);
if (Utils.remoteAppVersionCode > BuildConfig.VERSION_CODE) {
appCheckUpdatesContainer.setBackgroundColor(colorNeutral);
appCheckUpdatesIcon.setImageResource(R.drawable.ic_file_download);
appCheckUpdatesStatus.setText(getString(R.string.app_update_available, Utils.remoteAppVersion));
appCheckUpdatesStatus.setTextColor(colorNeutral);
appUpdateView.setOnClickListener(view -> builder
.setTitle(getString(R.string.update_title, getString(R.string.app_name)))
.setMessage(getString(R.string.update_msg, getString(R.string.app_name), Utils.remoteAppVersion, Utils.appChangelog))
.setCancelable(true)
.setPositiveButton(R.string.download_install, (dialogInterface, i) -> Utils.downloadAndReceive(getActivity(),
new DownloadReceiver() {
@Override
public void task(Uri uri) {
Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri content = FileProvider.getUriForFile(getActivity(), "com.topjohnwu.magisk.provider", new File(uri.getPath()));
install.setData(content);
mContext.startActivity(install);
}
},
Utils.appLink,
"MagiskManager-v" + Utils.remoteAppVersion + ".apk"))
.setNegativeButton(R.string.no_thanks, null)
.show()
);
} else {
appCheckUpdatesContainer.setBackgroundColor(colorOK);
appCheckUpdatesIcon.setImageResource(R.drawable.ic_check_circle);
appCheckUpdatesStatus.setText(getString(R.string.up_to_date, getString(R.string.app_name)));
appCheckUpdatesStatus.setTextColor(colorOK);
}
}
}
appCheckUpdatesProgress.setVisibility(View.GONE);
magiskCheckUpdatesProgress.setVisibility(View.GONE);
appCheckUpdatesProgress.setVisibility(View.GONE);
magiskCheckUpdatesProgress.setVisibility(View.GONE);
if (Utils.magiskVersion == -1) {
builder
.setTitle(R.string.no_magisk_title)
.setMessage(R.string.no_magisk_msg)
.setCancelable(true)
.setPositiveButton(R.string.download_install, (dialogInterface, i) -> Utils.downloadAndReceive(
getActivity(),
new DownloadReceiver() {
@Override
public void task(Uri uri) {
new Async.FlashZIP(mContext, uri).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}
},
Utils.magiskLink,
"Magisk-v" + String.valueOf(Utils.remoteMagiskVersion) + ".zip"))
.setNegativeButton(R.string.no_thanks, null)
.show();
}
}
// private class updateUI extends AsyncTask<Void, Void, Void> {
//
// @Override
// protected Void doInBackground(Void... voids) {
// return null;
// }
//
// @Override
// protected void onPostExecute(Void v) {
// super.onPostExecute(v);
// String theme = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString("theme", "");
// if (theme.equals("Dark")) {
// builder = new AlertDialog.Builder(getActivity(),R.style.AlertDialog_dh);
// } else {
// builder = new AlertDialog.Builder(getActivity());
// }
//
// if (Utils.remoteMagiskVersion == -1) {
// appCheckUpdatesContainer.setBackgroundColor(colorWarn);
// magiskCheckUpdatesContainer.setBackgroundColor(colorWarn);
//
// appCheckUpdatesIcon.setImageResource(R.drawable.ic_warning);
// magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_warning);
//
// appCheckUpdatesStatus.setText(R.string.cannot_check_updates);
// appCheckUpdatesStatus.setTextColor(colorWarn);
// magiskCheckUpdatesStatus.setText(R.string.cannot_check_updates);
// magiskCheckUpdatesStatus.setTextColor(colorWarn);
// } else {
// if (Utils.remoteMagiskVersion > Utils.magiskVersion) {
// magiskCheckUpdatesContainer.setBackgroundColor(colorNeutral);
// magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_file_download);
// magiskCheckUpdatesStatus.setText(getString(R.string.magisk_update_available, String.valueOf(Utils.remoteMagiskVersion)));
// magiskCheckUpdatesStatus.setTextColor(colorNeutral);
// magiskUpdateView.setOnClickListener(view -> builder
// .setTitle(getString(R.string.update_title, getString(R.string.magisk)))
// .setMessage(getString(R.string.update_msg, getString(R.string.magisk), String.valueOf(Utils.remoteMagiskVersion), Utils.magiskChangelog))
// .setCancelable(true)
// .setPositiveButton(R.string.download_install, (dialogInterface, i) -> Utils.downloadAndReceive(
// getActivity(),
// new DownloadReceiver() {
// @Override
// public void task(Uri uri) {
// new Async.FlashZIP(mContext, uri).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
// }
// },
// Utils.magiskLink,
// "Magisk-v" + String.valueOf(Utils.remoteMagiskVersion) + ".zip"))
// .setNegativeButton(R.string.no_thanks, null)
// .show());
// } else {
// magiskCheckUpdatesContainer.setBackgroundColor(colorOK);
// magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_check_circle);
// magiskCheckUpdatesStatus.setText(getString(R.string.up_to_date, getString(R.string.magisk)));
// magiskCheckUpdatesStatus.setTextColor(colorOK);
// }
//
// if (Utils.remoteAppVersionCode > BuildConfig.VERSION_CODE) {
// appCheckUpdatesContainer.setBackgroundColor(colorNeutral);
// appCheckUpdatesIcon.setImageResource(R.drawable.ic_file_download);
// appCheckUpdatesStatus.setText(getString(R.string.app_update_available, Utils.remoteAppVersion));
// appCheckUpdatesStatus.setTextColor(colorNeutral);
// appUpdateView.setOnClickListener(view -> builder
// .setTitle(getString(R.string.update_title, getString(R.string.app_name)))
// .setMessage(getString(R.string.update_msg, getString(R.string.app_name), Utils.remoteAppVersion, Utils.appChangelog))
// .setCancelable(true)
// .setPositiveButton(R.string.download_install, (dialogInterface, i) -> Utils.downloadAndReceive(getActivity(),
// new DownloadReceiver() {
// @Override
// public void task(Uri uri) {
// Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
// install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// Uri content = FileProvider.getUriForFile(getActivity(), "com.topjohnwu.magisk.provider", new File(uri.getPath()));
// install.setData(content);
// mContext.startActivity(install);
// }
// },
// Utils.appLink,
// "MagiskManager-v" + Utils.remoteAppVersion + ".apk"))
// .setNegativeButton(R.string.no_thanks, null)
// .show()
// );
// } else {
// appCheckUpdatesContainer.setBackgroundColor(colorOK);
// appCheckUpdatesIcon.setImageResource(R.drawable.ic_check_circle);
// appCheckUpdatesStatus.setText(getString(R.string.up_to_date, getString(R.string.app_name)));
// appCheckUpdatesStatus.setTextColor(colorOK);
//
// }
// }
//
// appCheckUpdatesProgress.setVisibility(View.GONE);
// magiskCheckUpdatesProgress.setVisibility(View.GONE);
// }
// }
}