v4: change root switch method; massive refactor

This commit is contained in:
topjohnwu 2016-08-17 00:56:00 +08:00
parent b18b5c4f43
commit 7737c6aee1
7 changed files with 165 additions and 136 deletions

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -8,8 +8,8 @@ android {
applicationId "com.topjohnwu.magisk"
minSdkVersion 21
targetSdkVersion 24
versionCode 2
versionName "1.1"
versionCode 3
versionName "v4"
}
buildTypes {
release {

View File

@ -7,8 +7,6 @@ import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import java.io.BufferedReader;
@ -19,14 +17,13 @@ import java.io.InputStreamReader;
public class MainActivity extends Activity {
private Switch selinuxSwitch;
private TextView rootStatus, selinuxStatus, safetyNet, permissive;
private Button rootButton;
private EditText countdown;
private Switch rootToggle, selinuxToggle;
private TextView magiskVersion, rootStatus, selinuxStatus, safetyNet, permissive;
private String suPath;
private String execute(String command) {
StringBuffer output = new StringBuffer();
StringBuilder output = new StringBuilder();
Process p;
try {
@ -34,49 +31,71 @@ public class MainActivity extends Activity {
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
String line;
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
if(output.length() != 0) output.append("\n");
output.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
String response = output.toString();
return response;
return output.toString();
}
private void updateStatus() {
String selinux = execute("getenforce");
if((new File("/system/xbin/su").exists())) {
rootStatus.setText("Mounted");
rootStatus.setTextColor(Color.RED);
safetyNet.setText("Root mounted and enabled. Safety Net (Android Pay) will NOT work");
safetyNet.setTextColor(Color.RED);
rootButton.setEnabled(true);
} else {
rootStatus.setText("Not Mounted");
rootStatus.setTextColor(Color.GREEN);
safetyNet.setText("Safety Net (Android Pay) should work, but no root temporarily");
safetyNet.setTextColor(Color.GREEN);
rootButton.setEnabled(false);
}
magiskVersion.setText(getString(R.string.magisk_version, execute("getprop magisk.version")));
selinuxStatus.setText(selinux);
if(selinux.equals("Enforcing\n")) {
if(selinux.equals("Enforcing")) {
selinuxStatus.setTextColor(Color.GREEN);
selinuxSwitch.setChecked(true);
permissive.setText("SELinux is enforced");
selinuxToggle.setChecked(true);
permissive.setText(R.string.selinux_enforcing_info);
permissive.setTextColor(Color.GREEN);
} else {
selinuxStatus.setTextColor(Color.RED);
selinuxSwitch.setChecked(false);
permissive.setText("Only turn off SELinux if necessary!");
selinuxToggle.setChecked(false);
permissive.setText(R.string.selinux_permissive_info);
permissive.setTextColor(Color.RED);
}
if((new File("/system/framework/twframework.jar")).exists()) {
selinuxToggle.setEnabled(false);
permissive.setText(R.string.selinux_samsung);
}
if((new File("/system/xbin/su").exists())) {
rootStatus.setTextColor(Color.RED);
safetyNet.setTextColor(Color.RED);
rootToggle.setChecked(true);
if(!(new File(suPath + "/su")).exists()) {
rootStatus.setText(R.string.root_system);
safetyNet.setText(R.string.root_system_info);
rootToggle.setEnabled(false);
selinuxToggle.setEnabled(false);
} else {
rootStatus.setText(R.string.root_mounted);
safetyNet.setText(R.string.root_mounted_info);
}
} else {
rootStatus.setTextColor(Color.GREEN);
safetyNet.setTextColor(Color.GREEN);
rootToggle.setChecked(false);
if(!(new File(suPath + "/su")).exists()) {
rootStatus.setText(R.string.root_none);
safetyNet.setText(R.string.root_none_info);
rootToggle.setEnabled(false);
selinuxToggle.setEnabled(false);
} else {
rootStatus.setText(R.string.root_unmounted);
safetyNet.setText(R.string.root_unmounted_info);
}
}
}
protected class SU extends AsyncTask<String, Void, Void> {
@ -84,7 +103,7 @@ public class MainActivity extends Activity {
@Override
protected Void doInBackground(String... params) {
try {
Process su = Runtime.getRuntime().exec("su");
Process su = Runtime.getRuntime().exec(suPath + "/su");
DataOutputStream out = new DataOutputStream(su.getOutputStream());
for(String command : params) {
out.writeBytes(command + "\n");
@ -111,43 +130,52 @@ public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(!(new File("/magisk/phh/su")).exists()) {
setContentView(R.layout.no_root);
} else {
magiskVersion = (TextView) findViewById(R.id.magisk_version);
rootToggle = (Switch) findViewById(R.id.root_toggle);
selinuxToggle = (Switch) findViewById(R.id.selinux_toggle);
rootStatus = (TextView) findViewById(R.id.root_status);
selinuxStatus = (TextView) findViewById(R.id.selinux_status);
safetyNet = (TextView) findViewById(R.id.safety_net);
permissive = (TextView) findViewById(R.id.permissive);
setContentView(R.layout.activity_main);
suPath = execute("getprop magisk.supath");
selinuxSwitch = (Switch) findViewById(R.id.permissive_switch);
rootStatus = (TextView) findViewById(R.id.root_status);
selinuxStatus = (TextView) findViewById(R.id.selinux_status);
safetyNet = (TextView) findViewById(R.id.safety_net);
permissive = (TextView) findViewById(R.id.permissive);
countdown = (EditText) findViewById(R.id.countdown);
rootButton = (Button) findViewById(R.id.rootButton);
setContentView(R.layout.activity_main);
updateStatus();
magiskVersion = (TextView) findViewById(R.id.magisk_version);
rootToggle = (Switch) findViewById(R.id.root_toggle);
selinuxToggle = (Switch) findViewById(R.id.selinux_toggle);
rootStatus = (TextView) findViewById(R.id.root_status);
selinuxStatus = (TextView) findViewById(R.id.selinux_status);
safetyNet = (TextView) findViewById(R.id.safety_net);
permissive = (TextView) findViewById(R.id.permissive);
rootButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int timeout;
timeout = Integer.parseInt(countdown.getText().toString()) * 60;
(new SU()).execute("setprop magisk.timeout " + String.valueOf(timeout), "setprop magisk.phhsu 0");
updateStatus();
rootToggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Switch s = (Switch) view;
if(s.isChecked()) {
(new SU()).execute("setprop magisk.root 1");
} else {
(new SU()).execute("setprop magisk.root 0");
}
});
}
});
selinuxSwitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Switch s = (Switch) view;
if(s.isChecked()) {
(new SU()).execute("setenforce 1");
} else {
(new SU()).execute("setenforce 0");
}
selinuxToggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Switch s = (Switch) view;
if(s.isChecked()) {
(new SU()).execute("setenforce 1");
} else {
(new SU()).execute("setenforce 0");
}
});
}
}
});
}
}

View File

@ -10,12 +10,11 @@
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.topjohnwu.magisk.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Root Status:"
android:id="@+id/root_label"
android:text="@string/magisk_label"
android:id="@+id/magisk_label"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:textSize="20sp"
@ -25,21 +24,43 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="(unavailable)"
android:text="@string/unavailable"
android:id="@+id/magisk_version"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:layout_alignTop="@+id/magisk_label"
android:layout_toEndOf="@+id/magisk_label" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/root_label"
android:id="@+id/root_label"
android:layout_below="@+id/magisk_label"
android:layout_alignParentStart="true"
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/unavailable"
android:id="@+id/root_status"
android:textStyle="bold"
android:layout_toEndOf="@+id/root_label"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp" />
android:layout_marginLeft="10dp"
android:layout_alignTop="@+id/root_label"
android:layout_toEndOf="@+id/root_label" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selinux Status:"
android:text="@string/selinux_label"
android:id="@+id/selinux_label"
android:layout_alignParentStart="true"
android:layout_below="@+id/root_status"
android:layout_below="@+id/root_label"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp" />
@ -47,19 +68,18 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="(unavailable)"
android:text="@string/unavailable"
android:id="@+id/selinux_status"
android:textStyle="bold"
android:layout_below="@+id/root_status"
android:layout_toEndOf="@+id/selinux_label"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp" />
android:layout_marginLeft="10dp"
android:layout_alignTop="@+id/selinux_label"
android:layout_toEndOf="@+id/selinux_label" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="(unavailable)"
android:text="@string/unavailable"
android:id="@+id/safety_net"
android:layout_below="@+id/selinux_label"
android:layout_alignParentStart="true"
@ -70,7 +90,7 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="(unavailable)"
android:text="@string/unavailable"
android:id="@+id/permissive"
android:layout_below="@+id/safety_net"
android:layout_alignParentStart="true"
@ -81,44 +101,27 @@
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selinux Toggle"
android:id="@+id/permissive_switch"
android:text="@string/root_toggle"
android:id="@+id/root_toggle"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:switchPadding="20dp"
android:layout_below="@+id/permissive"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp"
android:checked="false" />
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/selinux_toggle"
android:id="@+id/selinux_toggle"
android:layout_marginTop="20dp"
android:textSize="20sp"
android:switchPadding="20dp"
android:layout_below="@+id/countdown"
android:layout_below="@+id/root_toggle"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp" />
<EditText
android:layout_width="50dp"
android:layout_height="wrap_content"
android:inputType="number"
android:id="@+id/countdown"
android:layout_below="@+id/permissive"
android:layout_alignParentStart="true"
android:textAlignment="center"
android:text="5"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="minutes"
android:id="@+id/textMin"
android:layout_alignBottom="@+id/countdown"
android:layout_toEndOf="@+id/countdown"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Disable root"
android:id="@+id/rootButton"
android:layout_alignTop="@+id/countdown"
android:layout_toEndOf="@+id/textMin" />
android:layout_marginLeft="10dp"
android:checked="false" />
</RelativeLayout>

View File

@ -1,14 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You are not rooted!"
android:id="@+id/no_root"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="93dp"
android:textSize="40sp" />
</RelativeLayout>

View File

@ -1,3 +1,21 @@
<resources>
<string name="app_name">Magisk Manager</string>
<string name="root_mounted">Mounted</string>
<string name="root_mounted_info">Root mounted and enabled. Safety Net (Android Pay) will NOT work</string>
<string name="root_unmounted">Not Mounted</string>
<string name="root_unmounted_info">Safety Net (Android Pay) should work, but no root temporarily</string>
<string name="selinux_enforcing_info">SELinux is enforced</string>
<string name="selinux_permissive_info">Only turn off SELinux if necessary!</string>
<string name="unavailable">(unavailable)</string>
<string name="magisk_label">Boot Version:</string>
<string name="root_label">Root Status:</string>
<string name="selinux_label">Selinux Status:</string>
<string name="root_toggle">Root Toggle</string>
<string name="selinux_toggle">Selinux Toggle</string>
<string name="root_system">Improperly Installed</string>
<string name="root_system_info">Root improperly installed. Safety Net (Android Pay) will NOT work, and impossible to toggle</string>
<string name="root_none">Not Rooted</string>
<string name="root_none_info">Safety Net (Android Pay) should work</string>
<string name="magisk_version">Magisk v%1$s</string>
<string name="selinux_samsung">Samsung do not support switching SELinux status!</string>
</resources>

View File

@ -1,6 +1,6 @@
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar"/>
<style name="AppTheme" parent="android:Theme.Material"/>
</resources>