From b18b5c4f43a04e9b5b8b05af7dacca7b4a7abe7b Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 8 Aug 2016 00:26:39 +0800 Subject: [PATCH] Update disable method (requires Magisk v2) --- .idea/vcs.xml | 2 +- app/build.gradle | 6 +- .../com/topjohnwu/magisk/MainActivity.java | 167 ++++++++++-------- app/src/main/res/layout/activity_main.xml | 55 ++++-- 4 files changed, 131 insertions(+), 99 deletions(-) diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7f4..35eb1ddfb 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 6e0179dc7..8507dad80 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,14 +2,14 @@ apply plugin: 'com.android.application' android { compileSdkVersion 24 - buildToolsVersion "24.0.0" + buildToolsVersion "24.0.1" defaultConfig { applicationId "com.topjohnwu.magisk" minSdkVersion 21 targetSdkVersion 24 - versionCode 1 - versionName "1.0" + versionCode 2 + versionName "1.1" } 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 6318afe3a..5e9b4605e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java @@ -4,76 +4,107 @@ import android.app.Activity; import android.graphics.Color; import android.os.AsyncTask; 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.DataInputStream; +import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.IOException; +import java.io.InputStreamReader; public class MainActivity extends Activity { - private String suPATH; - private String xbinPATH; - - private Switch rootSwitch, selinuxSwitch; + private Switch selinuxSwitch; private TextView rootStatus, selinuxStatus, safetyNet, permissive; + private Button rootButton; + private EditText countdown; - protected class callSU extends AsyncTask { + private String execute(String command) { + + StringBuffer output = new StringBuffer(); + + Process p; + try { + p = Runtime.getRuntime().exec(command); + p.waitFor(); + BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); + + String line = ""; + while ((line = reader.readLine())!= null) { + output.append(line + "\n"); + } + + } catch (Exception e) { + e.printStackTrace(); + } + String response = output.toString(); + return response; + + } + + 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); + } + + selinuxStatus.setText(selinux); + + if(selinux.equals("Enforcing\n")) { + selinuxStatus.setTextColor(Color.GREEN); + selinuxSwitch.setChecked(true); + permissive.setText("SELinux is enforced"); + permissive.setTextColor(Color.GREEN); + } else { + selinuxStatus.setTextColor(Color.RED); + selinuxSwitch.setChecked(false); + permissive.setText("Only turn off SELinux if necessary!"); + permissive.setTextColor(Color.RED); + } + } + + protected class SU extends AsyncTask { @Override - protected String[] doInBackground(String... params) { - String[] results = new String[2]; + protected Void doInBackground(String... params) { try { - Process su = Runtime.getRuntime().exec(suPATH); + Process su = Runtime.getRuntime().exec("su"); DataOutputStream out = new DataOutputStream(su.getOutputStream()); - DataInputStream in = new DataInputStream(su.getInputStream()); - for(int i = 0; i < params.length; ++i) { - out.writeBytes(params[i] + "\n"); + for(String command : params) { + out.writeBytes(command + "\n"); out.flush(); } - out.writeBytes("if [ -z $(which su) ]; then echo 0; else echo 1; fi;\n"); - out.flush(); - results[0] = in.readLine(); - out.writeBytes("getenforce\n"); - out.flush(); - results[1] = in.readLine(); out.writeBytes("exit\n"); out.flush(); } catch (IOException e) { e.printStackTrace(); } - return results; + return null; } + @Override - protected void onPostExecute(String[] results) { - if(results[0].equals("1")) { - rootStatus.setText("Mounted"); - rootStatus.setTextColor(Color.RED); - rootSwitch.setChecked(true); - safetyNet.setText("Root mounted and enabled. Safety Net (Android Pay) will NOT work"); - safetyNet.setTextColor(Color.RED); - } else { - rootStatus.setText("Not Mounted"); - rootStatus.setTextColor(Color.GREEN); - rootSwitch.setChecked(false); - safetyNet.setText("Safety Net (Android Pay) should work, but no root temporarily"); - safetyNet.setTextColor(Color.GREEN); - } - - selinuxStatus.setText(results[1]); - - if(results[1].equals("Enforcing")) { - selinuxStatus.setTextColor(Color.GREEN); - selinuxSwitch.setChecked(true); - permissive.setText("SELinux is enforced"); - permissive.setTextColor(Color.GREEN); - } else { - selinuxStatus.setTextColor(Color.RED); - selinuxSwitch.setChecked(false); - permissive.setText("Only turn off SELinux if necessary!"); - permissive.setTextColor(Color.RED); - } + protected void onPostExecute(Void aVoid) { + final Handler handler = new Handler(); + handler.postDelayed(new Runnable() { + @Override + public void run() { + updateStatus(); + } + }, 1500); } } @@ -81,46 +112,28 @@ public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - boolean rooted = true; - - File phh = new File("/magisk/phh/su"); - File supersu = new File("/su/bin/su"); - - if(!supersu.exists()) { - if(!phh.exists()) { - setContentView(R.layout.no_root); - rooted = false; - } else { - suPATH = "/magisk/phh/su"; - xbinPATH = "/magisk/phh/xbin"; - } + if(!(new File("/magisk/phh/su")).exists()) { + setContentView(R.layout.no_root); } else { - suPATH = "/su/bin/su"; - xbinPATH = "/su/xbin"; - } - - if(rooted) { setContentView(R.layout.activity_main); - rootSwitch = (Switch) findViewById(R.id.root_switch); 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); - (new callSU()).execute(); + updateStatus(); - rootSwitch.setOnClickListener(new View.OnClickListener() { + rootButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - Switch s = (Switch) view; - if(s.isChecked()) { - (new callSU()).execute("mount -o bind " + xbinPATH + " /system/xbin"); - } else { - (new callSU()).execute("umount /system/xbin"); - } + int timeout; + timeout = Integer.parseInt(countdown.getText().toString()) * 60; + (new SU()).execute("setprop magisk.timeout " + String.valueOf(timeout), "setprop magisk.phhsu 0"); } }); @@ -129,9 +142,9 @@ public class MainActivity extends Activity { public void onClick(View view) { Switch s = (Switch) view; if(s.isChecked()) { - (new callSU()).execute("setenforce 1"); + (new SU()).execute("setenforce 1"); } else { - (new callSU()).execute("setenforce 0"); + (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 51abb8153..34bb26dbe 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,6 +1,7 @@ - - - + + + + +