mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-10-16 09:52:31 +00:00
Changed root method
This commit is contained in:
@@ -25,4 +25,5 @@ android {
|
||||
|
||||
dependencies {
|
||||
compile fileTree(dir: 'libs', include: ['*.jar'])
|
||||
compile project(':lib:RootCommands')
|
||||
}
|
||||
|
@@ -1,13 +0,0 @@
|
||||
package com.topjohnwu.magisk;
|
||||
|
||||
import android.app.Application;
|
||||
import android.test.ApplicationTestCase;
|
||||
|
||||
/**
|
||||
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
|
||||
*/
|
||||
public class ApplicationTest extends ApplicationTestCase<Application> {
|
||||
public ApplicationTest() {
|
||||
super(Application.class);
|
||||
}
|
||||
}
|
@@ -8,14 +8,14 @@
|
||||
android:label="@string/app_name"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
<activity android:name=".MainActivity">
|
||||
<activity android:name=".ui.MainActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN"/>
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity android:name=".ModulesActivity">
|
||||
<activity android:name=".ui.ModulesActivity">
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package com.topjohnwu.magisk;
|
||||
package com.topjohnwu.magisk.model;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
@@ -0,0 +1,60 @@
|
||||
package com.topjohnwu.magisk.rv;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.topjohnwu.magisk.R;
|
||||
import com.topjohnwu.magisk.model.Module;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ModulesAdapter extends ArrayAdapter<Module> {
|
||||
|
||||
public ModulesAdapter(Context context, int resource, List<Module> modules) {
|
||||
super(context, resource, modules);
|
||||
}
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
ViewHolder vh;
|
||||
|
||||
if (convertView == null) {
|
||||
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
convertView = inflater.inflate(R.layout.row, null);
|
||||
|
||||
vh = new ViewHolder();
|
||||
vh.name = (TextView) convertView.findViewById(R.id.name);
|
||||
vh.version = (TextView) convertView.findViewById(R.id.version);
|
||||
vh.versionCode = (TextView) convertView.findViewById(R.id.versionCode);
|
||||
vh.description = (TextView) convertView.findViewById(R.id.description);
|
||||
vh.cache = (TextView) convertView.findViewById(R.id.cache);
|
||||
|
||||
convertView.setTag(vh);
|
||||
} else {
|
||||
vh = (ViewHolder) convertView.getTag();
|
||||
}
|
||||
|
||||
Module module = getItem(position);
|
||||
vh.name.setText("name= " + module.getName());
|
||||
vh.version.setText("version= " + module.getVersion());
|
||||
vh.versionCode.setText("versioncode= " + module.getVersionCode());
|
||||
vh.description.setText("description= " + module.getDescription());
|
||||
vh.cache.setText("is from cache= " + module.isCache());
|
||||
|
||||
return convertView;
|
||||
}
|
||||
|
||||
static class ViewHolder {
|
||||
public TextView name;
|
||||
public TextView version;
|
||||
public TextView versionCode;
|
||||
public TextView description;
|
||||
public TextView cache;
|
||||
}
|
||||
}
|
@@ -1,19 +1,17 @@
|
||||
package com.topjohnwu.magisk;
|
||||
package com.topjohnwu.magisk.ui;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Color;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.widget.Switch;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import com.topjohnwu.magisk.R;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import static com.topjohnwu.magisk.ui.utils.Utils.executeCommand;
|
||||
|
||||
public class MainActivity extends Activity {
|
||||
|
||||
@@ -34,59 +32,29 @@ public class MainActivity extends Activity {
|
||||
safetyNet = (TextView) findViewById(R.id.safety_net);
|
||||
permissive = (TextView) findViewById(R.id.permissive);
|
||||
|
||||
suPath = execute("getprop magisk.supath");
|
||||
suPath = executeCommand("getprop magisk.supath");
|
||||
updateStatus();
|
||||
|
||||
rootToggle.setOnClickListener(view -> {
|
||||
Switch s = (Switch) view;
|
||||
if (s.isChecked()) {
|
||||
(new SU()).execute("setprop magisk.root 1");
|
||||
} else {
|
||||
(new SU()).execute("setprop magisk.root 0");
|
||||
}
|
||||
rootToggle.setOnCheckedChangeListener((view, checked) -> {
|
||||
executeCommand(checked ? "setprop magisk.root 1" : "setprop magisk.root 0");
|
||||
updateStatus();
|
||||
});
|
||||
|
||||
selinuxToggle.setOnClickListener(view -> {
|
||||
Switch s = (Switch) view;
|
||||
if (s.isChecked()) {
|
||||
new SU().execute("setenforce 1");
|
||||
} else {
|
||||
new SU().execute("setenforce 0");
|
||||
}
|
||||
selinuxToggle.setOnCheckedChangeListener((view, checked) -> {
|
||||
executeCommand(checked ? "setenforce 1" : "setenforce 0");
|
||||
updateStatus();
|
||||
});
|
||||
|
||||
findViewById(R.id.modules).setOnClickListener(view -> startActivity(new Intent(this, ModulesActivity.class)));
|
||||
}
|
||||
|
||||
private String execute(String command) {
|
||||
|
||||
StringBuilder output = new StringBuilder();
|
||||
|
||||
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) {
|
||||
if (output.length() != 0) output.append("\n");
|
||||
output.append(line);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return output.toString();
|
||||
|
||||
}
|
||||
|
||||
private void updateStatus() {
|
||||
String selinux = executeCommand("getenforce");
|
||||
|
||||
String selinux = execute("getenforce");
|
||||
magiskVersion.setText(getString(R.string.magisk_version, execute("getprop magisk.version")));
|
||||
magiskVersion.setText(getString(R.string.magisk_version, executeCommand("getprop magisk.version")));
|
||||
selinuxStatus.setText(selinux);
|
||||
|
||||
assert selinux != null;
|
||||
if (selinux.equals("Enforcing")) {
|
||||
selinuxStatus.setTextColor(Color.GREEN);
|
||||
selinuxToggle.setChecked(true);
|
||||
@@ -109,7 +77,7 @@ public class MainActivity extends Activity {
|
||||
safetyNet.setTextColor(Color.RED);
|
||||
rootToggle.setChecked(true);
|
||||
|
||||
if (!(new File(suPath + "/su")).exists()) {
|
||||
if (!new File(suPath + "/su").exists()) {
|
||||
rootStatus.setText(R.string.root_system);
|
||||
safetyNet.setText(R.string.root_system_info);
|
||||
rootToggle.setEnabled(false);
|
||||
@@ -135,29 +103,4 @@ public class MainActivity extends Activity {
|
||||
}
|
||||
}
|
||||
|
||||
protected class SU extends AsyncTask<String, Void, Void> {
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(String... params) {
|
||||
try {
|
||||
Process su = Runtime.getRuntime().exec(suPath + "/su");
|
||||
DataOutputStream out = new DataOutputStream(su.getOutputStream());
|
||||
for (String command : params) {
|
||||
out.writeBytes(command + "\n");
|
||||
out.flush();
|
||||
}
|
||||
out.writeBytes("exit\n");
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void aVoid) {
|
||||
final Handler handler = new Handler();
|
||||
handler.postDelayed(MainActivity.this::updateStatus, 1500);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,17 +1,14 @@
|
||||
package com.topjohnwu.magisk;
|
||||
package com.topjohnwu.magisk.ui;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.topjohnwu.magisk.R;
|
||||
import com.topjohnwu.magisk.model.Module;
|
||||
import com.topjohnwu.magisk.rv.ModulesAdapter;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
@@ -90,49 +87,4 @@ public class ModulesActivity extends Activity {
|
||||
}
|
||||
}
|
||||
|
||||
private class ModulesAdapter extends ArrayAdapter<Module> {
|
||||
|
||||
public ModulesAdapter(Context context, int resource, List<Module> modules) {
|
||||
super(context, resource, modules);
|
||||
}
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
ViewHolder vh;
|
||||
|
||||
if (convertView == null) {
|
||||
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
convertView = inflater.inflate(R.layout.row, null);
|
||||
|
||||
vh = new ViewHolder();
|
||||
vh.name = (TextView) convertView.findViewById(R.id.name);
|
||||
vh.version = (TextView) convertView.findViewById(R.id.version);
|
||||
vh.versionCode = (TextView) convertView.findViewById(R.id.versionCode);
|
||||
vh.description = (TextView) convertView.findViewById(R.id.description);
|
||||
vh.cache = (TextView) convertView.findViewById(R.id.cache);
|
||||
|
||||
convertView.setTag(vh);
|
||||
} else {
|
||||
vh = (ViewHolder) convertView.getTag();
|
||||
}
|
||||
|
||||
Module module = getItem(position);
|
||||
vh.name.setText("name= " + module.getName());
|
||||
vh.version.setText("version= " + module.getVersion());
|
||||
vh.versionCode.setText("versioncode= " + module.getVersionCode());
|
||||
vh.description.setText("description= " + module.getDescription());
|
||||
vh.cache.setText("is from cache= " + module.isCache());
|
||||
|
||||
return convertView;
|
||||
}
|
||||
|
||||
private class ViewHolder {
|
||||
public TextView name;
|
||||
public TextView version;
|
||||
public TextView versionCode;
|
||||
public TextView description;
|
||||
public TextView cache;
|
||||
}
|
||||
}
|
||||
}
|
28
app/src/main/java/com/topjohnwu/magisk/ui/utils/Utils.java
Normal file
28
app/src/main/java/com/topjohnwu/magisk/ui/utils/Utils.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package com.topjohnwu.magisk.ui.utils;
|
||||
|
||||
import org.sufficientlysecure.rootcommands.Shell;
|
||||
import org.sufficientlysecure.rootcommands.command.SimpleCommand;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
public class Utils {
|
||||
|
||||
public static String executeCommand(String... commands) {
|
||||
try {
|
||||
Shell shell = Shell.startRootShell();
|
||||
SimpleCommand command = new SimpleCommand(commands);
|
||||
shell.add(command).waitForFinish();
|
||||
|
||||
String output = command.getOutput();
|
||||
output = output.replaceAll("\n", "");
|
||||
|
||||
shell.close();
|
||||
|
||||
return output;
|
||||
} catch (IOException | TimeoutException e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -4,7 +4,7 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:focusableInTouchMode="true"
|
||||
tools:context="com.topjohnwu.magisk.MainActivity">
|
||||
tools:context=".ui.MainActivity">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/magisk_label"
|
||||
|
@@ -4,7 +4,7 @@
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context="com.topjohnwu.magisk.ModulesActivity">
|
||||
tools:context=".ui.ModulesActivity">
|
||||
|
||||
<ListView
|
||||
android:id="@android:id/list"
|
||||
|
@@ -1,15 +0,0 @@
|
||||
package com.topjohnwu.magisk;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* To work on unit tests, switch the Test Artifact in the Build Variants view.
|
||||
*/
|
||||
public class ExampleUnitTest {
|
||||
@Test
|
||||
public void addition_isCorrect() throws Exception {
|
||||
assertEquals(4, 2 + 2);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user