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

315 lines
12 KiB
Java
Raw Normal View History

2016-12-25 03:05:22 +08:00
package com.topjohnwu.magisk;
2017-01-07 02:46:50 +08:00
import android.app.AlertDialog;
2016-12-25 03:05:22 +08:00
import android.app.Fragment;
import android.app.FragmentTransaction;
2017-01-07 02:46:50 +08:00
import android.content.Intent;
import android.net.Uri;
2016-12-25 03:05:22 +08:00
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.topjohnwu.magisk.utils.Async;
2016-12-25 15:11:59 +08:00
import com.topjohnwu.magisk.utils.CallbackHandler;
2016-12-25 03:05:22 +08:00
import com.topjohnwu.magisk.utils.Logger;
import com.topjohnwu.magisk.utils.Shell;
2017-01-10 22:30:05 +08:00
import com.topjohnwu.magisk.utils.Utils;
2016-12-25 03:05:22 +08:00
import java.util.List;
import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
2016-12-25 03:05:22 +08:00
2016-12-25 15:11:59 +08:00
public class StatusFragment extends Fragment implements CallbackHandler.EventListener {
2016-12-25 03:05:22 +08:00
public static double magiskVersion, remoteMagiskVersion = -1;
2017-01-07 02:46:50 +08:00
public static String magiskVersionString = "(none)", magiskLink, releaseNoteLink;
2016-12-25 15:11:59 +08:00
public static int SNCheckResult = -1;
2016-12-25 03:05:22 +08:00
2017-01-07 02:46:50 +08:00
private static boolean noDialog = false;
2016-12-25 15:11:59 +08:00
public static final CallbackHandler.Event updateCheckDone = new CallbackHandler.Event();
public static final CallbackHandler.Event safetyNetDone = new CallbackHandler.Event();
2016-12-25 03:05:22 +08:00
private Unbinder unbinder;
2016-12-25 03:05:22 +08:00
@BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
@BindView(R.id.magisk_status_container) View magiskStatusContainer;
@BindView(R.id.magisk_status_icon) ImageView magiskStatusIcon;
@BindView(R.id.magisk_version) TextView magiskVersionText;
@BindView(R.id.magisk_update_status) TextView magiskUpdateText;
@BindView(R.id.magisk_check_updates_progress) ProgressBar magiskCheckUpdatesProgress;
@BindView(R.id.root_status_container) View rootStatusContainer;
@BindView(R.id.root_status_icon) ImageView rootStatusIcon;
@BindView(R.id.root_status) TextView rootStatusText;
@BindView(R.id.root_info) TextView rootInfoText;
@BindView(R.id.safetyNet_container) View safetyNetContainer;
@BindView(R.id.safetyNet_icon) ImageView safetyNetIcon;
@BindView(R.id.safetyNet_status) TextView safetyNetStatusText;
@BindView(R.id.safetyNet_check_progress) ProgressBar safetyNetProgress;
@BindColor(R.color.red500) int colorBad;
@BindColor(R.color.green500) int colorOK;
@BindColor(R.color.yellow500) int colorWarn;
@BindColor(R.color.grey500) int colorNeutral;
@BindColor(R.color.blue500) int colorInfo;
@BindColor(android.R.color.transparent) int trans;
2017-01-07 01:19:18 +08:00
int defaultColor;
2016-12-25 03:05:22 +08:00
2016-12-25 22:36:51 +08:00
static {
checkMagiskInfo();
}
2017-01-07 02:46:50 +08:00
private AlertDialog updateMagisk;
2016-12-25 03:05:22 +08:00
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.status_fragment, container, false);
unbinder = ButterKnife.bind(this, v);
2016-12-25 03:05:22 +08:00
2017-01-07 01:19:18 +08:00
defaultColor = magiskUpdateText.getCurrentTextColor();
2016-12-25 03:05:22 +08:00
mSwipeRefreshLayout.setOnRefreshListener(() -> {
magiskStatusContainer.setBackgroundColor(trans);
magiskStatusIcon.setImageResource(0);
magiskUpdateText.setText(R.string.checking_for_updates);
magiskCheckUpdatesProgress.setVisibility(View.VISIBLE);
2017-01-07 01:19:18 +08:00
magiskUpdateText.setTextColor(defaultColor);
2016-12-25 03:05:22 +08:00
2017-01-07 01:19:18 +08:00
safetyNetProgress.setVisibility(View.GONE);
safetyNetContainer.setBackgroundColor(colorNeutral);
safetyNetIcon.setImageResource(R.drawable.ic_safetynet);
safetyNetStatusText.setText(R.string.safetyNet_check_text);
safetyNetStatusText.setTextColor(defaultColor);
safetyNetDone.isTriggered = false;
2017-01-07 02:46:50 +08:00
noDialog = false;
2016-12-25 03:05:22 +08:00
2016-12-25 15:11:59 +08:00
updateUI();
new Async.CheckUpdates().exec();
});
2016-12-25 03:05:22 +08:00
2017-01-07 01:19:18 +08:00
safetyNetContainer.setOnClickListener(view -> {
safetyNetProgress.setVisibility(View.VISIBLE);
safetyNetContainer.setBackgroundColor(trans);
safetyNetIcon.setImageResource(0);
safetyNetStatusText.setText(R.string.checking_safetyNet_status);
Async.checkSafetyNet(getActivity());
});
2016-12-25 03:05:22 +08:00
2017-01-07 02:46:50 +08:00
if (magiskVersion < 0 && Shell.rootAccess() && !noDialog) {
2017-01-10 22:30:05 +08:00
noDialog = true;
Utils.getAlertDialogBuilder(getActivity())
2016-12-25 03:05:22 +08:00
.setTitle(R.string.no_magisk_title)
.setMessage(R.string.no_magisk_msg)
.setCancelable(true)
2016-12-28 04:48:40 +08:00
.setPositiveButton(R.string.goto_install, (dialogInterface, i) -> {
2016-12-25 03:05:22 +08:00
((MainActivity) getActivity()).navigationView.setCheckedItem(R.id.install);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
try {
transaction.replace(R.id.content_frame, new InstallFragment(), "install").commit();
} catch (IllegalStateException ignored) {}
})
2017-01-10 22:30:05 +08:00
.setNegativeButton(R.string.no_thanks, null)
2016-12-25 03:05:22 +08:00
.show();
}
2017-01-07 02:46:50 +08:00
updateUI();
2016-12-25 03:05:22 +08:00
return v;
}
2016-12-25 15:11:59 +08:00
@Override
public void onTrigger(CallbackHandler.Event event) {
if (event == updateCheckDone) {
Logger.dev("StatusFragment: Update Check UI refresh triggered");
updateCheckUI();
} else if (event == safetyNetDone) {
Logger.dev("StatusFragment: SafetyNet UI refresh triggered");
updateSafetyNetUI();
}
}
2016-12-25 03:05:22 +08:00
@Override
public void onStart() {
super.onStart();
2016-12-25 15:11:59 +08:00
CallbackHandler.register(updateCheckDone, this);
CallbackHandler.register(safetyNetDone, this);
2017-01-07 01:19:18 +08:00
if (updateCheckDone.isTriggered) {
updateCheckUI();
}
if (safetyNetDone.isTriggered) {
updateSafetyNetUI();
}
2016-12-25 03:05:22 +08:00
getActivity().setTitle(R.string.status);
}
@Override
public void onStop() {
2016-12-25 15:11:59 +08:00
CallbackHandler.unRegister(updateCheckDone, this);
CallbackHandler.unRegister(safetyNetDone, this);
super.onStop();
2016-12-25 03:05:22 +08:00
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
2016-12-25 22:36:51 +08:00
private static void checkMagiskInfo() {
2016-12-25 03:05:22 +08:00
List<String> ret = Shell.sh("getprop magisk.version");
if (ret.get(0).length() == 0) {
magiskVersion = -1;
} else {
try {
magiskVersionString = ret.get(0);
magiskVersion = Double.parseDouble(ret.get(0));
} catch (NumberFormatException e) {
// Custom version don't need to receive updates
magiskVersion = Double.POSITIVE_INFINITY;
}
2016-12-25 22:36:51 +08:00
}
}
private void updateUI() {
int image, color;
checkMagiskInfo();
if (magiskVersion < 0) {
magiskVersionText.setText(R.string.magisk_version_error);
} else {
2016-12-25 03:05:22 +08:00
magiskVersionText.setText(getString(R.string.magisk_version, magiskVersionString));
}
if (Shell.rootStatus == 1) {
color = colorOK;
image = R.drawable.ic_check_circle;
rootStatusText.setText(R.string.proper_root);
rootInfoText.setText(Shell.sh("su -v").get(0));
} else {
rootInfoText.setText(R.string.root_info_warning);
if (Shell.rootStatus == 0) {
color = colorBad;
image = R.drawable.ic_cancel;
rootStatusText.setText(R.string.not_rooted);
} else {
color = colorNeutral;
image = R.drawable.ic_help;
rootStatusText.setText(R.string.root_error);
}
}
rootStatusContainer.setBackgroundColor(color);
rootStatusText.setTextColor(color);
rootInfoText.setTextColor(color);
rootStatusIcon.setImageResource(image);
}
private void updateCheckUI() {
int image, color;
if (remoteMagiskVersion < 0) {
color = colorNeutral;
image = R.drawable.ic_help;
magiskUpdateText.setText(R.string.cannot_check_updates);
} else if (remoteMagiskVersion > magiskVersion) {
color = colorInfo;
image = R.drawable.ic_update;
2016-12-25 22:36:51 +08:00
magiskUpdateText.setText(getString(R.string.magisk_update_available, remoteMagiskVersion));
2016-12-25 03:05:22 +08:00
} else {
color = colorOK;
image = R.drawable.ic_check_circle;
magiskUpdateText.setText(getString(R.string.up_to_date, getString(R.string.magisk)));
}
if (magiskVersion < 0) {
color = colorBad;
image = R.drawable.ic_cancel;
}
magiskStatusContainer.setBackgroundColor(color);
magiskVersionText.setTextColor(color);
magiskUpdateText.setTextColor(color);
magiskStatusIcon.setImageResource(image);
magiskCheckUpdatesProgress.setVisibility(View.GONE);
mSwipeRefreshLayout.setRefreshing(false);
2017-01-07 02:46:50 +08:00
2017-01-10 22:30:05 +08:00
updateMagisk = Utils.getAlertDialogBuilder(getActivity())
2017-01-07 02:46:50 +08:00
.setTitle(R.string.magisk_update_title)
.setMessage(getString(R.string.magisk_update_message, remoteMagiskVersion))
.setCancelable(true)
.setPositiveButton(R.string.goto_install, (dialogInterface, i) -> {
((MainActivity) getActivity()).navigationView.setCheckedItem(R.id.install);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
try {
transaction.replace(R.id.content_frame, new InstallFragment(), "install").commit();
} catch (IllegalStateException ignored) {}
})
.setNeutralButton(R.string.check_release_notes, (dialog, which) -> {
getActivity().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(releaseNoteLink)));
})
2017-01-10 22:30:05 +08:00
.setNegativeButton(R.string.no_thanks, null)
2017-01-07 02:46:50 +08:00
.create();
if (magiskVersion < remoteMagiskVersion && Shell.rootAccess()) {
magiskStatusContainer.setOnClickListener(view -> updateMagisk.show());
if (!noDialog) {
2017-01-10 22:30:05 +08:00
noDialog = true;
2017-01-07 02:46:50 +08:00
updateMagisk.show();
}
}
2016-12-25 03:05:22 +08:00
}
private void updateSafetyNetUI() {
int image, color;
safetyNetProgress.setVisibility(View.GONE);
2016-12-25 15:11:59 +08:00
switch (SNCheckResult) {
2017-01-03 18:01:57 +08:00
case -3:
color = colorNeutral;
image = R.drawable.ic_help;
safetyNetStatusText.setText(R.string.safetyNet_connection_suspended);
break;
case -2:
color = colorNeutral;
image = R.drawable.ic_help;
safetyNetStatusText.setText(R.string.safetyNet_connection_failed);
break;
2016-12-25 03:05:22 +08:00
case -1:
color = colorNeutral;
image = R.drawable.ic_help;
safetyNetStatusText.setText(R.string.safetyNet_error);
break;
case 0:
color = colorBad;
image = R.drawable.ic_cancel;
safetyNetStatusText.setText(R.string.safetyNet_fail);
break;
case 1:
default:
color = colorOK;
image = R.drawable.ic_check_circle;
safetyNetStatusText.setText(R.string.safetyNet_pass);
break;
}
safetyNetContainer.setBackgroundColor(color);
safetyNetStatusText.setTextColor(color);
safetyNetIcon.setImageResource(image);
}
}