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

297 lines
12 KiB
Java
Raw Normal View History

2016-12-24 19:05:22 +00:00
package com.topjohnwu.magisk;
2017-01-06 18:46:50 +00:00
import android.content.Intent;
import android.net.Uri;
2016-12-24 19:05:22 +00:00
import android.os.Bundle;
import android.support.annotation.Nullable;
2017-01-28 16:20:43 +00:00
import android.support.v4.app.FragmentTransaction;
2016-12-24 19:05:22 +00:00
import android.support.v4.widget.SwipeRefreshLayout;
2017-02-12 11:49:46 +00:00
import android.support.v7.app.AlertDialog;
2016-12-24 19:05:22 +00:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
2017-02-12 11:49:46 +00:00
import com.topjohnwu.magisk.asyncs.CheckUpdates;
2017-02-14 21:24:02 +00:00
import com.topjohnwu.magisk.components.AlertDialogBuilder;
2017-02-06 18:01:32 +00:00
import com.topjohnwu.magisk.components.Fragment;
2017-02-06 20:09:49 +00:00
import com.topjohnwu.magisk.utils.CallbackEvent;
2016-12-24 19:05:22 +00:00
import com.topjohnwu.magisk.utils.Logger;
import com.topjohnwu.magisk.utils.Shell;
2017-01-10 14:30:05 +00:00
import com.topjohnwu.magisk.utils.Utils;
2016-12-24 19:05:22 +00:00
import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
2016-12-24 19:05:22 +00:00
2017-02-06 20:09:49 +00:00
public class StatusFragment extends Fragment implements CallbackEvent.Listener<Void> {
2016-12-24 19:05:22 +00:00
2017-01-06 18:46:50 +00:00
private static boolean noDialog = false;
private Unbinder unbinder;
2016-12-24 19:05:22 +00: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-06 17:19:18 +00:00
int defaultColor;
2016-12-24 19:05:22 +00:00
2017-01-06 18:46:50 +00:00
private AlertDialog updateMagisk;
2016-12-24 19:05:22 +00:00
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
2017-01-25 09:07:23 +00:00
View v = inflater.inflate(R.layout.fragment_status, container, false);
unbinder = ButterKnife.bind(this, v);
2016-12-24 19:05:22 +00:00
2017-01-06 17:19:18 +00:00
defaultColor = magiskUpdateText.getCurrentTextColor();
2016-12-24 19:05:22 +00:00
mSwipeRefreshLayout.setOnRefreshListener(() -> {
magiskStatusContainer.setBackgroundColor(trans);
magiskStatusIcon.setImageResource(0);
magiskUpdateText.setText(R.string.checking_for_updates);
magiskCheckUpdatesProgress.setVisibility(View.VISIBLE);
2017-01-06 17:19:18 +00:00
magiskUpdateText.setTextColor(defaultColor);
2016-12-24 19:05:22 +00:00
2017-01-06 17:19:18 +00:00
safetyNetProgress.setVisibility(View.GONE);
safetyNetContainer.setBackgroundColor(colorNeutral);
safetyNetIcon.setImageResource(R.drawable.ic_safetynet);
safetyNetStatusText.setText(R.string.safetyNet_check_text);
safetyNetStatusText.setTextColor(defaultColor);
2017-02-06 18:01:32 +00:00
getApplication().safetyNetDone.isTriggered = false;
2017-01-06 18:46:50 +00:00
noDialog = false;
2016-12-24 19:05:22 +00:00
2016-12-25 07:11:59 +00:00
updateUI();
2017-02-12 11:49:46 +00:00
new CheckUpdates(getActivity()).exec();
2016-12-25 07:11:59 +00:00
});
2016-12-24 19:05:22 +00:00
2017-01-06 17:19:18 +00:00
safetyNetContainer.setOnClickListener(view -> {
safetyNetProgress.setVisibility(View.VISIBLE);
safetyNetContainer.setBackgroundColor(trans);
safetyNetIcon.setImageResource(0);
safetyNetStatusText.setText(R.string.checking_safetyNet_status);
2017-02-12 11:49:46 +00:00
Utils.checkSafetyNet(getApplication());
2017-01-06 17:19:18 +00:00
});
2016-12-24 19:05:22 +00:00
2017-02-06 18:01:32 +00:00
if (getApplication().magiskVersion < 0 && Shell.rootAccess() && !noDialog) {
2017-01-10 14:30:05 +00:00
noDialog = true;
2017-02-14 21:24:02 +00:00
new AlertDialogBuilder(getActivity())
2016-12-24 19:05:22 +00:00
.setTitle(R.string.no_magisk_title)
.setMessage(R.string.no_magisk_msg)
.setCancelable(true)
2016-12-27 20:48:40 +00:00
.setPositiveButton(R.string.goto_install, (dialogInterface, i) -> {
2016-12-24 19:05:22 +00:00
((MainActivity) getActivity()).navigationView.setCheckedItem(R.id.install);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
2017-02-06 18:01:32 +00:00
transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
2016-12-24 19:05:22 +00:00
try {
transaction.replace(R.id.content_frame, new InstallFragment(), "install").commit();
} catch (IllegalStateException ignored) {}
})
2017-01-10 14:30:05 +00:00
.setNegativeButton(R.string.no_thanks, null)
2016-12-24 19:05:22 +00:00
.show();
}
2017-01-06 18:46:50 +00:00
updateUI();
2017-02-06 20:09:49 +00:00
if (getApplication().updateCheckDone.isTriggered)
updateCheckUI();
if (getApplication().safetyNetDone.isTriggered)
updateSafetyNetUI();
2016-12-24 19:05:22 +00:00
return v;
}
2016-12-25 07:11:59 +00:00
@Override
2017-02-06 20:09:49 +00:00
public void onTrigger(CallbackEvent<Void> event) {
2017-02-06 18:01:32 +00:00
if (event == getApplication().updateCheckDone) {
2016-12-25 07:11:59 +00:00
Logger.dev("StatusFragment: Update Check UI refresh triggered");
updateCheckUI();
2017-02-06 18:01:32 +00:00
} else if (event == getApplication().safetyNetDone) {
2016-12-25 07:11:59 +00:00
Logger.dev("StatusFragment: SafetyNet UI refresh triggered");
updateSafetyNetUI();
}
}
2016-12-24 19:05:22 +00:00
@Override
public void onStart() {
super.onStart();
2017-02-06 20:09:49 +00:00
getApplication().updateCheckDone.register(this);
getApplication().safetyNetDone.register(this);
2016-12-24 19:05:22 +00:00
getActivity().setTitle(R.string.status);
}
@Override
public void onStop() {
2017-02-06 20:09:49 +00:00
getApplication().updateCheckDone.unRegister(this);
getApplication().safetyNetDone.unRegister(this);
super.onStop();
2016-12-24 19:05:22 +00:00
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
2016-12-25 14:36:51 +00:00
private void updateUI() {
int image, color;
2017-02-06 18:01:32 +00:00
getApplication().updateMagiskInfo();
2016-12-25 14:36:51 +00:00
2017-02-06 18:01:32 +00:00
if (getApplication().magiskVersion < 0) {
2016-12-25 14:36:51 +00:00
magiskVersionText.setText(R.string.magisk_version_error);
2017-02-06 18:01:32 +00:00
} else if (getApplication().disabled) {
magiskVersionText.setText(getString(R.string.magisk_version_disable, getApplication().magiskVersionString));
2016-12-25 14:36:51 +00:00
} else {
2017-02-06 18:01:32 +00:00
magiskVersionText.setText(getString(R.string.magisk_version, getApplication().magiskVersionString));
2016-12-24 19:05:22 +00:00
}
2017-01-26 05:46:54 +00:00
switch (Shell.rootStatus) {
case 0:
2016-12-24 19:05:22 +00:00
color = colorBad;
image = R.drawable.ic_cancel;
rootStatusText.setText(R.string.not_rooted);
2017-01-27 22:13:07 +00:00
rootInfoText.setText(R.string.root_info_warning);
2017-01-26 05:46:54 +00:00
break;
case 1:
2017-02-06 18:01:32 +00:00
if (getApplication().suVersion != null) {
2017-01-26 05:46:54 +00:00
color = colorOK;
image = R.drawable.ic_check_circle;
rootStatusText.setText(R.string.proper_root);
2017-02-06 18:01:32 +00:00
rootInfoText.setText(getApplication().suVersion);
2017-01-26 05:46:54 +00:00
break;
}
case -1:
default:
2016-12-24 19:05:22 +00:00
color = colorNeutral;
image = R.drawable.ic_help;
rootStatusText.setText(R.string.root_error);
2017-01-27 22:13:07 +00:00
rootInfoText.setText(R.string.root_info_warning);
2016-12-24 19:05:22 +00:00
}
rootStatusContainer.setBackgroundColor(color);
rootStatusText.setTextColor(color);
rootInfoText.setTextColor(color);
rootStatusIcon.setImageResource(image);
}
private void updateCheckUI() {
int image, color;
2017-02-06 18:01:32 +00:00
if (getApplication().remoteMagiskVersion < 0) {
2016-12-24 19:05:22 +00:00
color = colorNeutral;
image = R.drawable.ic_help;
magiskUpdateText.setText(R.string.cannot_check_updates);
2017-02-06 18:01:32 +00:00
} else if (getApplication().remoteMagiskVersion > getApplication().magiskVersion) {
2016-12-24 19:05:22 +00:00
color = colorInfo;
image = R.drawable.ic_update;
2017-02-06 18:01:32 +00:00
magiskUpdateText.setText(getString(R.string.magisk_update_available, getApplication().remoteMagiskVersion));
2016-12-24 19:05:22 +00:00
} else {
color = colorOK;
image = R.drawable.ic_check_circle;
magiskUpdateText.setText(getString(R.string.up_to_date, getString(R.string.magisk)));
}
2017-02-06 18:01:32 +00:00
if (getApplication().magiskVersion < 0) {
2016-12-24 19:05:22 +00:00
color = colorBad;
image = R.drawable.ic_cancel;
2017-02-06 18:01:32 +00:00
} else if (getApplication().disabled) {
2017-02-04 20:40:52 +00:00
color = colorNeutral;
image = R.drawable.ic_cancel;
2016-12-24 19:05:22 +00:00
}
2017-02-04 20:40:52 +00:00
2016-12-24 19:05:22 +00:00
magiskStatusContainer.setBackgroundColor(color);
magiskVersionText.setTextColor(color);
magiskUpdateText.setTextColor(color);
magiskStatusIcon.setImageResource(image);
magiskCheckUpdatesProgress.setVisibility(View.GONE);
mSwipeRefreshLayout.setRefreshing(false);
2017-01-06 18:46:50 +00:00
2017-02-14 21:24:02 +00:00
updateMagisk = new AlertDialogBuilder(getActivity())
2017-01-06 18:46:50 +00:00
.setTitle(R.string.magisk_update_title)
2017-02-06 18:01:32 +00:00
.setMessage(getString(R.string.magisk_update_message, getApplication().remoteMagiskVersion))
2017-01-06 18:46:50 +00:00
.setCancelable(true)
.setPositiveButton(R.string.goto_install, (dialogInterface, i) -> {
((MainActivity) getActivity()).navigationView.setCheckedItem(R.id.install);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
2017-02-04 20:40:52 +00:00
transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
2017-01-06 18:46:50 +00:00
try {
transaction.replace(R.id.content_frame, new InstallFragment(), "install").commit();
} catch (IllegalStateException ignored) {}
})
2017-02-15 21:45:31 +00:00
.setNeutralButton(R.string.release_notes, (dialog, which) -> {
2017-02-06 18:01:32 +00:00
getActivity().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getApplication().releaseNoteLink)));
2017-01-06 18:46:50 +00:00
})
2017-01-10 14:30:05 +00:00
.setNegativeButton(R.string.no_thanks, null)
2017-01-06 18:46:50 +00:00
.create();
2017-02-06 18:01:32 +00:00
if (getApplication().magiskVersion < getApplication().remoteMagiskVersion && Shell.rootAccess()) {
2017-01-06 18:46:50 +00:00
magiskStatusContainer.setOnClickListener(view -> updateMagisk.show());
if (!noDialog) {
2017-01-10 14:30:05 +00:00
noDialog = true;
2017-01-06 18:46:50 +00:00
updateMagisk.show();
}
}
2016-12-24 19:05:22 +00:00
}
private void updateSafetyNetUI() {
int image, color;
safetyNetProgress.setVisibility(View.GONE);
2017-02-06 18:01:32 +00:00
switch (getApplication().SNCheckResult) {
2017-01-03 10:01:57 +00: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-24 19:05:22 +00: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);
}
}