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

220 lines
8.8 KiB
Java
Raw Normal View History

2016-08-23 09:39:18 +00:00
package com.topjohnwu.magisk;
2016-08-25 10:08:07 +00:00
import android.os.AsyncTask;
2016-08-23 09:39:18 +00:00
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
2016-08-25 10:08:07 +00:00
import android.widget.ProgressBar;
2016-08-23 09:39:18 +00:00
import android.widget.Switch;
import android.widget.TextView;
import com.topjohnwu.magisk.utils.Shell;
2016-08-23 09:39:18 +00:00
import java.io.File;
2016-08-25 10:08:07 +00:00
import java.util.List;
2016-08-23 09:39:18 +00:00
import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MagiskFragment extends Fragment {
2016-08-25 10:08:07 +00:00
@BindView(R.id.progressBar) ProgressBar progressBar;
2016-08-23 09:39:18 +00:00
2016-08-25 10:08:07 +00:00
@BindView(R.id.rootSwitchView) View rootToggleView;
2016-08-23 09:39:18 +00:00
@BindView(R.id.selinuxSwitchView) View selinuxToggleView;
2016-08-25 10:08:07 +00:00
@BindView(R.id.magiskStatusView) View magiskStatusView;
@BindView(R.id.rootStatusView) View rootStatusView;
@BindView(R.id.safetynetStatusView) View safetynetStatusView;
@BindView(R.id.selinuxStatusView) View selinuxStatusView;
@BindView(R.id.root_toggle) Switch rootToggle;
2016-08-23 09:39:18 +00:00
@BindView(R.id.selinux_toggle) Switch selinuxToggle;
@BindView(R.id.magisk_status_container) View magiskStatusContainer;
@BindView(R.id.magisk_status_icon) ImageView magiskStatusIcon;
@BindView(R.id.magisk_version) TextView magiskVersion;
@BindView(R.id.root_status_container) View rootStatusContainer;
@BindView(R.id.root_status_icon) ImageView rootStatusIcon;
@BindView(R.id.root_status) TextView rootStatus;
@BindView(R.id.selinux_status_container) View selinuxStatusContainer;
@BindView(R.id.selinux_status_icon) ImageView selinuxStatusIcon;
@BindView(R.id.selinux_status) TextView selinuxStatus;
@BindView(R.id.safety_net_status) TextView safetyNetStatus;
@BindView(R.id.safety_net_icon) ImageView safetyNetStatusIcon;
@BindColor(R.color.red500) int red500;
@BindColor(R.color.green500) int green500;
@BindColor(R.color.grey500) int grey500;
@BindColor(R.color.accent) int accent;
2016-08-23 09:39:18 +00:00
int statusOK = R.drawable.ic_check_circle;
int statusError = R.drawable.ic_error;
int statusUnknown = R.drawable.ic_help;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.magisk_fragment, container, false);
ButterKnife.bind(this, view);
2016-08-25 10:08:07 +00:00
new updateUI().execute();
2016-08-23 09:39:18 +00:00
rootToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Shell.su(b ? "setprop magisk.root 1" : "setprop magisk.root 0");
2016-08-25 10:08:07 +00:00
new updateUI().execute();
2016-08-23 09:39:18 +00:00
}
});
selinuxToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Shell.su(b ? "setenforce 1" : "setenforce 0");
2016-08-25 10:08:07 +00:00
new updateUI().execute();
2016-08-23 09:39:18 +00:00
}
});
return view;
}
2016-08-25 10:08:07 +00:00
private class updateUI extends AsyncTask<Void, Void, Void> {
2016-08-23 09:39:18 +00:00
2016-08-25 10:08:07 +00:00
@Override
protected Void doInBackground(Void... voids) {
// Make sure static block invoked
Shell.rootAccess();
return null;
2016-08-23 09:39:18 +00:00
}
2016-08-25 10:08:07 +00:00
@Override
protected void onPostExecute(Void v) {
super.onPostExecute(v);
2016-08-23 09:39:18 +00:00
2016-08-25 10:08:07 +00:00
progressBar.setVisibility(View.GONE);
2016-08-23 09:39:18 +00:00
2016-08-25 10:08:07 +00:00
magiskStatusView.setVisibility(View.VISIBLE);
rootStatusView.setVisibility(View.VISIBLE);
safetynetStatusView.setVisibility(View.VISIBLE);
selinuxStatusView.setVisibility(View.VISIBLE);
2016-08-23 09:39:18 +00:00
2016-08-25 10:08:07 +00:00
if (Shell.rootAccess()) {
rootToggleView.setVisibility(View.VISIBLE);
selinuxToggleView.setVisibility(View.VISIBLE);
}
2016-08-23 09:39:18 +00:00
2016-08-25 10:08:07 +00:00
List<String> selinux = Shell.sh("getenforce");
List<String> version = Shell.sh("getprop magisk.version");
2016-08-23 09:39:18 +00:00
2016-08-25 10:08:07 +00:00
if (version.isEmpty()) {
magiskStatusContainer.setBackgroundColor(grey500);
magiskStatusIcon.setImageResource(statusUnknown);
2016-08-23 09:39:18 +00:00
2016-08-25 10:08:07 +00:00
magiskVersion.setTextColor(grey500);
magiskVersion.setText(R.string.magisk_version_error);
2016-08-23 09:39:18 +00:00
} else {
2016-08-25 10:08:07 +00:00
magiskStatusContainer.setBackgroundColor(green500);
magiskStatusIcon.setImageResource(statusOK);
2016-08-23 09:39:18 +00:00
2016-08-25 10:08:07 +00:00
magiskVersion.setTextColor(green500);
magiskVersion.setText(getString(R.string.magisk_version, version.get(0)));
2016-08-23 09:39:18 +00:00
}
2016-08-25 10:08:07 +00:00
if (selinux.isEmpty()) {
selinuxStatusContainer.setBackgroundColor(grey500);
selinuxStatusIcon.setImageResource(statusUnknown);
2016-08-23 09:39:18 +00:00
2016-08-25 10:08:07 +00:00
selinuxStatus.setText(R.string.selinux_error_info);
selinuxStatus.setTextColor(grey500);
selinuxToggle.setChecked(false);
} else if (selinux.get(0).equals("Enforcing")) {
selinuxStatusContainer.setBackgroundColor(green500);
selinuxStatusIcon.setImageResource(statusOK);
2016-08-23 09:39:18 +00:00
2016-08-25 10:08:07 +00:00
selinuxStatus.setText(R.string.selinux_enforcing_info);
selinuxStatus.setTextColor(green500);
selinuxToggle.setChecked(true);
} else {
selinuxStatusContainer.setBackgroundColor(red500);
selinuxStatusIcon.setImageResource(statusError);
2016-08-23 09:39:18 +00:00
2016-08-25 10:08:07 +00:00
selinuxStatus.setText(R.string.selinux_permissive_info);
selinuxStatus.setTextColor(red500);
selinuxToggle.setChecked(false);
}
if (new File("/system/framework/twframework.jar").exists()) {
2016-08-23 09:39:18 +00:00
selinuxToggleView.setVisibility(View.GONE);
2016-08-25 10:08:07 +00:00
selinuxStatus.append("\n" + getString(R.string.selinux_samsung_info));
}
2016-08-23 09:39:18 +00:00
2016-08-25 10:08:07 +00:00
switch (Shell.rootStatus) {
case -1:
// Root Error
rootStatusContainer.setBackgroundColor(grey500);
rootStatusIcon.setImageResource(statusUnknown);
rootStatus.setTextColor(grey500);
rootStatus.setText(R.string.root_error);
rootToggle.setChecked(false);
safetyNetStatusIcon.setImageResource(statusUnknown);
safetyNetStatus.setText(R.string.root_error_info);
break;
case 0:
// Not rooted
rootStatusContainer.setBackgroundColor(green500);
rootStatusIcon.setImageResource(statusOK);
rootStatus.setTextColor(green500);
rootStatus.setText(R.string.root_none);
rootToggle.setChecked(false);
safetyNetStatusIcon.setImageResource(statusOK);
safetyNetStatus.setText(R.string.root_none_info);
break;
case 1:
// Proper root
if (new File("/system/xbin/su").exists()) {
// Mounted
rootStatusContainer.setBackgroundColor(accent);
2016-08-25 10:08:07 +00:00
rootStatusIcon.setImageResource(statusError);
rootStatus.setTextColor(accent);
2016-08-25 10:08:07 +00:00
rootStatus.setText(R.string.root_mounted);
rootToggle.setChecked(true);
safetyNetStatusIcon.setImageResource(statusError);
safetyNetStatus.setText(R.string.root_mounted_info);
break;
} else {
// Not Mounted
rootStatusContainer.setBackgroundColor(green500);
rootStatusIcon.setImageResource(statusOK);
rootStatus.setTextColor(green500);
rootStatus.setText(R.string.root_unmounted);
rootToggle.setChecked(false);
safetyNetStatusIcon.setImageResource(statusOK);
safetyNetStatus.setText(R.string.root_unmounted_info);
break;
}
case 2:
// Improper root
rootStatusContainer.setBackgroundColor(red500);
rootStatusIcon.setImageResource(statusError);
rootStatus.setTextColor(red500);
rootStatus.setText(R.string.root_system);
rootToggle.setChecked(true);
safetyNetStatusIcon.setImageResource(statusError);
safetyNetStatus.setText(R.string.root_system_info);
rootToggleView.setVisibility(View.GONE);
break;
2016-08-23 09:39:18 +00:00
}
}
}
}