From 7737c6aee1be1b89206b763efb17af6e3b4124ea Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 17 Aug 2016 00:56:00 +0800 Subject: [PATCH] v4: change root switch method; massive refactor --- .idea/vcs.xml | 6 - app/build.gradle | 4 +- .../com/topjohnwu/magisk/MainActivity.java | 152 +++++++++++------- app/src/main/res/layout/activity_main.xml | 105 ++++++------ app/src/main/res/layout/no_root.xml | 14 -- app/src/main/res/values/strings.xml | 18 +++ app/src/main/res/values/styles.xml | 2 +- 7 files changed, 165 insertions(+), 136 deletions(-) delete mode 100644 .idea/vcs.xml delete mode 100644 app/src/main/res/layout/no_root.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1ddfb..000000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 8507dad80..f73ce5ad7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,8 +8,8 @@ android { applicationId "com.topjohnwu.magisk" minSdkVersion 21 targetSdkVersion 24 - versionCode 2 - versionName "1.1" + versionCode 3 + versionName "v4" } buildTypes { release { diff --git a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java index 5e9b4605e..5fff5f685 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java @@ -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 { @@ -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"); } - }); - } + } + }); } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 34bb26dbe..86c8bb8d8 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -10,12 +10,11 @@ android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.topjohnwu.magisk.MainActivity"> - + + + + + android:layout_marginLeft="10dp" + android:layout_alignTop="@+id/root_label" + android:layout_toEndOf="@+id/root_label" /> @@ -47,19 +68,18 @@ + android:layout_marginLeft="10dp" + android:layout_alignTop="@+id/selinux_label" + android:layout_toEndOf="@+id/selinux_label" /> + + - - - - - -