/
DangerousUtils.java
355 lines (324 loc) · 12.9 KB
/
DangerousUtils.java
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package com.blankj.subutil.util;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.PowerManager;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import com.blankj.utilcode.util.IntentUtils;
import com.blankj.utilcode.util.ShellUtils;
import com.blankj.utilcode.util.Utils;
import java.io.File;
import java.lang.reflect.Method;
import java.util.List;
import androidx.annotation.RequiresPermission;
import static android.Manifest.permission.MODIFY_PHONE_STATE;
import static android.Manifest.permission.SEND_SMS;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2019/06/29
* desc :
* </pre>
*/
public class DangerousUtils {
private DangerousUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
///////////////////////////////////////////////////////////////////////////
// AppUtils
///////////////////////////////////////////////////////////////////////////
/**
* Install the app silently.
* <p>Without root permission must hold
* {@code android:sharedUserId="android.uid.shell"} and
* {@code <uses-permission android:name="android.permission.INSTALL_PACKAGES" />}</p>
*
* @param filePath The path of file.
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean installAppSilent(final String filePath) {
return installAppSilent(getFileByPath(filePath), null);
}
/**
* Install the app silently.
* <p>Without root permission must hold
* {@code android:sharedUserId="android.uid.shell"} and
* {@code <uses-permission android:name="android.permission.INSTALL_PACKAGES" />}</p>
*
* @param file The file.
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean installAppSilent(final File file) {
return installAppSilent(file, null);
}
/**
* Install the app silently.
* <p>Without root permission must hold
* {@code android:sharedUserId="android.uid.shell"} and
* {@code <uses-permission android:name="android.permission.INSTALL_PACKAGES" />}</p>
*
* @param filePath The path of file.
* @param params The params of installation(e.g.,<code>-r</code>, <code>-s</code>).
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean installAppSilent(final String filePath, final String params) {
return installAppSilent(getFileByPath(filePath), params);
}
/**
* Install the app silently.
* <p>Without root permission must hold
* {@code android:sharedUserId="android.uid.shell"} and
* {@code <uses-permission android:name="android.permission.INSTALL_PACKAGES" />}</p>
*
* @param file The file.
* @param params The params of installation(e.g.,<code>-r</code>, <code>-s</code>).
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean installAppSilent(final File file, final String params) {
return installAppSilent(file, params, isDeviceRooted());
}
/**
* Install the app silently.
* <p>Without root permission must hold
* {@code android:sharedUserId="android.uid.shell"} and
* {@code <uses-permission android:name="android.permission.INSTALL_PACKAGES" />}</p>
*
* @param file The file.
* @param params The params of installation(e.g.,<code>-r</code>, <code>-s</code>).
* @param isRooted True to use root, false otherwise.
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean installAppSilent(final File file,
final String params,
final boolean isRooted) {
if (!isFileExists(file)) return false;
String filePath = '"' + file.getAbsolutePath() + '"';
String command = "LD_LIBRARY_PATH=/vendor/lib*:/system/lib* pm install " +
(params == null ? "" : params + " ")
+ filePath;
ShellUtils.CommandResult commandResult = ShellUtils.execCmd(command, isRooted);
if (commandResult.successMsg != null
&& commandResult.successMsg.toLowerCase().contains("success")) {
return true;
} else {
Log.e("AppUtils", "installAppSilent successMsg: " + commandResult.successMsg +
", errorMsg: " + commandResult.errorMsg);
return false;
}
}
/**
* Uninstall the app silently.
* <p>Without root permission must hold
* {@code android:sharedUserId="android.uid.shell"} and
* {@code <uses-permission android:name="android.permission.DELETE_PACKAGES" />}</p>
*
* @param packageName The name of the package.
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean uninstallAppSilent(final String packageName) {
return uninstallAppSilent(packageName, false);
}
/**
* Uninstall the app silently.
* <p>Without root permission must hold
* {@code android:sharedUserId="android.uid.shell"} and
* {@code <uses-permission android:name="android.permission.DELETE_PACKAGES" />}</p>
*
* @param packageName The name of the package.
* @param isKeepData Is keep the data.
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean uninstallAppSilent(final String packageName, final boolean isKeepData) {
return uninstallAppSilent(packageName, isKeepData, isDeviceRooted());
}
/**
* Uninstall the app silently.
* <p>Without root permission must hold
* {@code android:sharedUserId="android.uid.shell"} and
* {@code <uses-permission android:name="android.permission.DELETE_PACKAGES" />}</p>
*
* @param packageName The name of the package.
* @param isKeepData Is keep the data.
* @param isRooted True to use root, false otherwise.
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean uninstallAppSilent(final String packageName,
final boolean isKeepData,
final boolean isRooted) {
if (isSpace(packageName)) return false;
String command = "LD_LIBRARY_PATH=/vendor/lib*:/system/lib* pm uninstall "
+ (isKeepData ? "-k " : "")
+ packageName;
ShellUtils.CommandResult commandResult = ShellUtils.execCmd(command, isRooted);
if (commandResult.successMsg != null
&& commandResult.successMsg.toLowerCase().contains("success")) {
return true;
} else {
Log.e("AppUtils", "uninstallAppSilent successMsg: " + commandResult.successMsg +
", errorMsg: " + commandResult.errorMsg);
return false;
}
}
private static boolean isFileExists(final File file) {
return file != null && file.exists();
}
private static File getFileByPath(final String filePath) {
return isSpace(filePath) ? null : new File(filePath);
}
private static boolean isSpace(final String s) {
if (s == null) return true;
for (int i = 0, len = s.length(); i < len; ++i) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
}
return true;
}
private static boolean isDeviceRooted() {
String su = "su";
String[] locations = {"/system/bin/", "/system/xbin/", "/sbin/", "/system/sd/xbin/",
"/system/bin/failsafe/", "/data/local/xbin/", "/data/local/bin/", "/data/local/",
"/system/sbin/", "/usr/bin/", "/vendor/bin/"};
for (String location : locations) {
if (new File(location + su).exists()) {
return true;
}
}
return false;
}
///////////////////////////////////////////////////////////////////////////
// DeviceUtils
///////////////////////////////////////////////////////////////////////////
/**
* Shutdown the device
* <p>Requires root permission
* or hold {@code android:sharedUserId="android.uid.system"},
* {@code <uses-permission android:name="android.permission.SHUTDOWN" />}
* in manifest.</p>
*
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean shutdown() {
try {
ShellUtils.CommandResult result = ShellUtils.execCmd("reboot -p", true);
if (result.result == 0) return true;
Utils.getApp().startActivity(IntentUtils.getShutdownIntent());
return true;
} catch (Exception e) {
return false;
}
}
/**
* Reboot the device.
* <p>Requires root permission
* or hold {@code android:sharedUserId="android.uid.system"} in manifest.</p>
*
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean reboot() {
try {
ShellUtils.CommandResult result = ShellUtils.execCmd("reboot", true);
if (result.result == 0) return true;
Intent intent = new Intent(Intent.ACTION_REBOOT);
intent.putExtra("nowait", 1);
intent.putExtra("interval", 1);
intent.putExtra("window", 0);
Utils.getApp().sendBroadcast(intent);
return true;
} catch (Exception e) {
return false;
}
}
/**
* Reboot the device.
* <p>Requires root permission
* or hold {@code android:sharedUserId="android.uid.system"},
* {@code <uses-permission android:name="android.permission.REBOOT" />}</p>
*
* @param reason code to pass to the kernel (e.g., "recovery") to
* request special boot modes, or null.
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean reboot(final String reason) {
try {
PowerManager pm = (PowerManager) Utils.getApp().getSystemService(Context.POWER_SERVICE);
pm.reboot(reason);
return true;
} catch (Exception e) {
return false;
}
}
/**
* Reboot the device to recovery.
* <p>Requires root permission.</p>
*
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean reboot2Recovery() {
ShellUtils.CommandResult result = ShellUtils.execCmd("reboot recovery", true);
return result.result == 0;
}
/**
* Reboot the device to bootloader.
* <p>Requires root permission.</p>
*
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean reboot2Bootloader() {
ShellUtils.CommandResult result = ShellUtils.execCmd("reboot bootloader", true);
return result.result == 0;
}
/**
* Enable or disable mobile data.
* <p>Must hold {@code android:sharedUserId="android.uid.system"},
* {@code <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />}</p>
*
* @param enabled True to enabled, false otherwise.
* @return {@code true}: success<br>{@code false}: fail
*/
@RequiresPermission(MODIFY_PHONE_STATE)
public static boolean setMobileDataEnabled(final boolean enabled) {
try {
TelephonyManager tm =
(TelephonyManager) Utils.getApp().getSystemService(Context.TELEPHONY_SERVICE);
if (tm == null) return false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
tm.setDataEnabled(enabled);
return true;
}
Method setDataEnabledMethod =
tm.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
setDataEnabledMethod.invoke(tm, enabled);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* Send sms silently.
* <p>Must hold {@code <uses-permission android:name="android.permission.SEND_SMS" />}</p>
*
* @param phoneNumber The phone number.
* @param content The content.
*/
@RequiresPermission(SEND_SMS)
public static void sendSmsSilent(final String phoneNumber, final String content) {
if (TextUtils.isEmpty(content)) return;
PendingIntent sentIntent = PendingIntent.getBroadcast(Utils.getApp(), 0, new Intent("send"), 0);
SmsManager smsManager = SmsManager.getDefault();
if (content.length() >= 70) {
List<String> ms = smsManager.divideMessage(content);
for (String str : ms) {
smsManager.sendTextMessage(phoneNumber, null, str, sentIntent, null);
}
} else {
smsManager.sendTextMessage(phoneNumber, null, content, sentIntent, null);
}
}
}