blob: 886e07fe099f1f8d793d0ebb12a2b3f8d43532a6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
package com.example.mylauncher;
import java.io.DataOutputStream;
import java.io.IOException;
public class RootHelper {
public static boolean executeRootCommand(String command) {
Process process = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
return process.waitFor() == 0;
} catch (IOException | InterruptedException e) {
e.printStackTrace();
return false;
} finally {
try {
if (os != null) {
os.close();
}
if (process != null) {
process.destroy();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static boolean hasRoot() {
return executeRootCommand("echo test");
}
/**
* Execute a shell command as root with a return value
*/
public static String executeRootCommandWithOutput(String command) {
Process process = null;
StringBuilder output = new StringBuilder();
try {
process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
java.io.BufferedReader reader = new java.io.BufferedReader(
new java.io.InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append('\n');
}
process.waitFor();
return output.toString().trim();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
return null;
} finally {
if (process != null) {
process.destroy();
}
}
}
/**
* Check if a specific package is installed
*/
public static boolean isPackageInstalled(String packageName) {
String command = "pm list packages " + packageName;
String result = executeRootCommandWithOutput(command);
return result != null && result.contains(packageName);
}
/**
* Execute a specific app with root permissions
*/
public static boolean launchApp(String packageName) {
String command = "monkey -p " + packageName + " -c android.intent.category.LAUNCHER 1";
return executeRootCommand(command);
}
/**
* Reboot device
*/
public static void reboot() {
executeRootCommand("reboot");
}
/**
* Power off device
*/
public static void powerOff() {
executeRootCommand("reboot -p");
}
/**
* Lock screen
*/
public static void lockScreen() {
executeRootCommand("input keyevent 26");
}
/**
* Change system settings (requires root)
*/
public static boolean changeSystemSetting(String setting, String value) {
return executeRootCommand("settings put system " + setting + " " + value);
}
/**
* Kill a specific app
*/
public static boolean killApp(String packageName) {
return executeRootCommand("am force-stop " + packageName);
}
/**
* Clear app data
*/
public static boolean clearAppData(String packageName) {
return executeRootCommand("pm clear " + packageName);
}
}
|