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

324 lines
12 KiB
Java
Raw Normal View History

2016-12-25 03:05:22 +08:00
package com.topjohnwu.magisk;
2017-05-20 03:04:14 +08:00
import android.animation.Animator;
import android.animation.ValueAnimator;
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;
2017-05-20 03:04:14 +08:00
import android.view.ViewTreeObserver;
2016-12-25 03:05:22 +08:00
import android.widget.ImageView;
2017-05-20 03:04:14 +08:00
import android.widget.LinearLayout;
2016-12-25 03:05:22 +08:00
import android.widget.ProgressBar;
import android.widget.TextView;
2017-02-12 19:49:46 +08:00
import com.topjohnwu.magisk.asyncs.CheckUpdates;
2017-02-15 05:24:02 +08:00
import com.topjohnwu.magisk.components.AlertDialogBuilder;
2017-02-07 02:01:32 +08:00
import com.topjohnwu.magisk.components.Fragment;
2017-02-07 04:09:49 +08:00
import com.topjohnwu.magisk.utils.CallbackEvent;
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 butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
2016-12-25 03:05:22 +08:00
2017-02-07 04:09:49 +08:00
public class StatusFragment extends Fragment implements CallbackEvent.Listener<Void> {
2016-12-25 03:05:22 +08:00
2017-01-07 02:46:50 +08:00
private static boolean noDialog = false;
private Unbinder unbinder;
2016-12-25 03:05:22 +08:00
@BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
2017-05-19 08:37:57 -07:00
@BindView(R.id.magisk_update_icon) ImageView magiskUpdateIcon;
2016-12-25 03:05:22 +08:00
@BindView(R.id.magisk_update_status) TextView magiskUpdateText;
@BindView(R.id.magisk_check_updates_progress) ProgressBar magiskCheckUpdatesProgress;
2017-05-19 08:37:57 -07:00
@BindView(R.id.magisk_status_icon) ImageView magiskStatusIcon;
@BindView(R.id.magisk_version) TextView magiskVersionText;
2016-12-25 03:05:22 +08:00
@BindView(R.id.root_status_icon) ImageView rootStatusIcon;
@BindView(R.id.root_status) TextView rootStatusText;
2017-05-20 03:04:14 +08:00
@BindView(R.id.safetyNet_refresh) ImageView safetyNetRefreshIcon;
2016-12-25 03:05:22 +08:00
@BindView(R.id.safetyNet_status) TextView safetyNetStatusText;
@BindView(R.id.safetyNet_check_progress) ProgressBar safetyNetProgress;
2017-05-20 03:04:14 +08:00
@BindView(R.id.expand_layout) LinearLayout expandLayout;
@BindView(R.id.cts_status_icon) ImageView ctsStatusIcon;
@BindView(R.id.cts_status) TextView ctsStatusText;
@BindView(R.id.basic_status_icon) ImageView basicStatusIcon;
@BindView(R.id.basic_status) TextView basicStatusText;
2016-12-25 03:05:22 +08:00
@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;
2017-05-20 03:04:14 +08:00
@OnClick(R.id.safetyNet_title)
public void safetyNet() {
safetyNetProgress.setVisibility(View.VISIBLE);
2017-05-20 03:04:14 +08:00
safetyNetRefreshIcon.setVisibility(View.GONE);
safetyNetStatusText.setText(R.string.checking_safetyNet_status);
2017-05-20 03:04:14 +08:00
Utils.checkSafetyNet(getActivity());
collapse();
}
2017-02-22 04:58:03 +08:00
public void gotoInstall() {
2017-05-19 08:37:57 -07:00
if (magiskManager.remoteMagiskVersionCode > 0) {
2017-03-30 05:16:50 +08:00
((MainActivity) getActivity()).navigate(R.id.install);
}
}
2017-05-19 08:37:57 -07:00
private MagiskManager magiskManager;
2017-05-20 03:04:14 +08:00
private static int expandHeight = 0;
private static boolean mExpanded = false;
2016-12-25 03:05:22 +08:00
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
2017-01-25 17:07:23 +08:00
View v = inflater.inflate(R.layout.fragment_status, container, false);
unbinder = ButterKnife.bind(this, v);
2017-05-19 08:37:57 -07:00
magiskManager = getApplication();
2016-12-25 03:05:22 +08:00
2017-05-20 03:04:14 +08:00
expandLayout.getViewTreeObserver().addOnPreDrawListener(
new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if (expandHeight == 0) {
final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
expandLayout.measure(widthSpec, heightSpec);
expandHeight = expandLayout.getMeasuredHeight();
}
expandLayout.getViewTreeObserver().removeOnPreDrawListener(this);
setExpanded();
return true;
}
});
2017-01-07 01:19:18 +08:00
2016-12-25 03:05:22 +08:00
mSwipeRefreshLayout.setOnRefreshListener(() -> {
magiskUpdateText.setText(R.string.checking_for_updates);
magiskCheckUpdatesProgress.setVisibility(View.VISIBLE);
2017-05-19 08:37:57 -07:00
magiskUpdateIcon.setVisibility(View.GONE);
2016-12-25 03:05:22 +08:00
2017-01-07 01:19:18 +08:00
safetyNetStatusText.setText(R.string.safetyNet_check_text);
2017-05-19 08:37:57 -07:00
magiskManager.safetyNetDone.isTriggered = false;
2017-05-20 03:04:14 +08:00
collapse();
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();
2017-02-12 19:49:46 +08:00
new CheckUpdates(getActivity()).exec();
2016-12-25 15:11:59 +08:00
});
2016-12-25 03:05:22 +08:00
2017-05-19 08:37:57 -07:00
if (magiskManager.magiskVersionCode < 0 && Shell.rootAccess() && !noDialog) {
2017-01-10 22:30:05 +08:00
noDialog = true;
2017-02-15 05:24:02 +08:00
new AlertDialogBuilder(getActivity())
2016-12-25 03:05:22 +08:00
.setTitle(R.string.no_magisk_title)
.setMessage(R.string.no_magisk_msg)
.setCancelable(true)
2017-02-22 04:58:03 +08:00
.setPositiveButton(R.string.goto_install, (d, i) -> gotoInstall())
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();
2017-05-19 08:37:57 -07:00
if (magiskManager.updateCheckDone.isTriggered)
2017-02-07 04:09:49 +08:00
updateCheckUI();
2017-05-19 08:37:57 -07:00
if (magiskManager.safetyNetDone.isTriggered)
2017-02-07 04:09:49 +08:00
updateSafetyNetUI();
2016-12-25 03:05:22 +08:00
return v;
}
2016-12-25 15:11:59 +08:00
@Override
2017-02-07 04:09:49 +08:00
public void onTrigger(CallbackEvent<Void> event) {
2017-05-19 08:37:57 -07:00
if (event == magiskManager.updateCheckDone) {
2016-12-25 15:11:59 +08:00
Logger.dev("StatusFragment: Update Check UI refresh triggered");
updateCheckUI();
2017-05-19 08:37:57 -07:00
} else if (event == magiskManager.safetyNetDone) {
2016-12-25 15:11:59 +08:00
Logger.dev("StatusFragment: SafetyNet UI refresh triggered");
updateSafetyNetUI();
}
}
2016-12-25 03:05:22 +08:00
@Override
public void onStart() {
super.onStart();
2017-05-19 08:37:57 -07:00
magiskManager.updateCheckDone.register(this);
magiskManager.safetyNetDone.register(this);
2016-12-25 03:05:22 +08:00
getActivity().setTitle(R.string.status);
}
@Override
public void onStop() {
2017-05-19 08:37:57 -07:00
magiskManager.updateCheckDone.unRegister(this);
magiskManager.safetyNetDone.unRegister(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 void updateUI() {
int image, color;
2017-05-19 08:37:57 -07:00
magiskManager.updateMagiskInfo();
2016-12-25 22:36:51 +08:00
2017-05-19 08:37:57 -07:00
if (magiskManager.magiskVersionCode < 0) {
color = colorBad;
image = R.drawable.ic_cancel;
2016-12-25 22:36:51 +08:00
magiskVersionText.setText(R.string.magisk_version_error);
} else {
2017-05-19 08:37:57 -07:00
color = colorOK;
image = R.drawable.ic_check_circle;
magiskVersionText.setText(getString(R.string.current_magisk_title, "v" + magiskManager.magiskVersionString));
2016-12-25 03:05:22 +08:00
}
2017-05-19 08:37:57 -07:00
magiskStatusIcon.setImageResource(image);
magiskStatusIcon.setColorFilter(color);
2017-01-26 13:46:54 +08:00
switch (Shell.rootStatus) {
case 0:
2016-12-25 03:05:22 +08:00
color = colorBad;
image = R.drawable.ic_cancel;
rootStatusText.setText(R.string.not_rooted);
2017-01-26 13:46:54 +08:00
break;
case 1:
2017-05-19 08:37:57 -07:00
if (magiskManager.suVersion != null) {
2017-01-26 13:46:54 +08:00
color = colorOK;
image = R.drawable.ic_check_circle;
2017-05-19 08:37:57 -07:00
rootStatusText.setText(magiskManager.suVersion);
2017-01-26 13:46:54 +08:00
break;
}
case -1:
default:
2016-12-25 03:05:22 +08:00
color = colorNeutral;
image = R.drawable.ic_help;
rootStatusText.setText(R.string.root_error);
}
2017-05-19 08:37:57 -07:00
2016-12-25 03:05:22 +08:00
rootStatusIcon.setImageResource(image);
2017-05-19 08:37:57 -07:00
rootStatusIcon.setColorFilter(color);
2016-12-25 03:05:22 +08:00
}
private void updateCheckUI() {
int image, color;
2017-05-19 08:37:57 -07:00
if (magiskManager.remoteMagiskVersionCode < 0) {
2016-12-25 03:05:22 +08:00
color = colorNeutral;
image = R.drawable.ic_help;
magiskUpdateText.setText(R.string.cannot_check_updates);
} else {
color = colorOK;
image = R.drawable.ic_check_circle;
2017-05-19 08:37:57 -07:00
magiskUpdateText.setText(getString(R.string.install_magisk_title, "v" + magiskManager.remoteMagiskVersionString));
2016-12-25 03:05:22 +08:00
}
2017-02-05 04:40:52 +08:00
2017-05-19 08:37:57 -07:00
magiskUpdateIcon.setImageResource(image);
magiskUpdateIcon.setColorFilter(color);
magiskUpdateIcon.setVisibility(View.VISIBLE);
2016-12-25 03:05:22 +08:00
magiskCheckUpdatesProgress.setVisibility(View.GONE);
mSwipeRefreshLayout.setRefreshing(false);
}
private void updateSafetyNetUI() {
int image, color;
safetyNetProgress.setVisibility(View.GONE);
2017-05-20 03:04:14 +08:00
safetyNetRefreshIcon.setVisibility(View.VISIBLE);
if (magiskManager.SNCheckResult.failed) {
safetyNetStatusText.setText(magiskManager.SNCheckResult.errmsg);
collapse();
} else {
safetyNetStatusText.setText(R.string.safetyNet_check_success);
if (magiskManager.SNCheckResult.ctsProfile) {
color = colorOK;
image = R.drawable.ic_check_circle;
} else {
2016-12-25 03:05:22 +08:00
color = colorBad;
image = R.drawable.ic_cancel;
2017-05-20 03:04:14 +08:00
}
ctsStatusText.setText("ctsProfile: " + magiskManager.SNCheckResult.ctsProfile);
ctsStatusIcon.setImageResource(image);
ctsStatusIcon.setColorFilter(color);
if (magiskManager.SNCheckResult.basicIntegrity) {
2016-12-25 03:05:22 +08:00
color = colorOK;
image = R.drawable.ic_check_circle;
2017-05-20 03:04:14 +08:00
} else {
color = colorBad;
image = R.drawable.ic_cancel;
}
basicStatusText.setText("basicIntegrity: " + magiskManager.SNCheckResult.basicIntegrity);
basicStatusIcon.setImageResource(image);
basicStatusIcon.setColorFilter(color);
expand();
2016-12-25 03:05:22 +08:00
}
2017-05-20 03:04:14 +08:00
}
private void setExpanded() {
ViewGroup.LayoutParams layoutParams = expandLayout.getLayoutParams();
layoutParams.height = mExpanded ? expandHeight : 0;
expandLayout.setLayoutParams(layoutParams);
expandLayout.setVisibility(mExpanded ? View.VISIBLE : View.GONE);
}
private void expand() {
if (mExpanded) return;
expandLayout.setVisibility(View.VISIBLE);
ValueAnimator mAnimator = slideAnimator(0, expandHeight);
mAnimator.start();
mExpanded = true;
}
private void collapse() {
if (!mExpanded) return;
int finalHeight = expandLayout.getHeight();
ValueAnimator mAnimator = slideAnimator(finalHeight, 0);
mAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationEnd(Animator animator) {
expandLayout.setVisibility(View.GONE);
}
@Override
public void onAnimationStart(Animator animator) {}
@Override
public void onAnimationCancel(Animator animator) {}
@Override
public void onAnimationRepeat(Animator animator) {}
});
mAnimator.start();
mExpanded = false;
}
private ValueAnimator slideAnimator(int start, int end) {
ValueAnimator animator = ValueAnimator.ofInt(start, end);
animator.addUpdateListener(valueAnimator -> {
int value = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = expandLayout.getLayoutParams();
layoutParams.height = value;
expandLayout.setLayoutParams(layoutParams);
});
return animator;
2016-12-25 03:05:22 +08:00
}
}