16
16
17
17
package com .code19 .library ;
18
18
19
+ import android .app .ActivityManager ;
20
+ import android .app .ActivityManager .RunningServiceInfo ;
19
21
import android .content .Context ;
20
22
import android .content .Intent ;
21
23
import android .content .pm .ApplicationInfo ;
26
28
import android .text .TextUtils ;
27
29
import android .util .Log ;
28
30
31
+ import java .io .BufferedReader ;
32
+ import java .io .DataOutputStream ;
29
33
import java .io .File ;
34
+ import java .io .FileFilter ;
35
+ import java .io .IOException ;
36
+ import java .io .InputStreamReader ;
37
+ import java .lang .reflect .Method ;
38
+ import java .util .Iterator ;
30
39
import java .util .List ;
40
+ import java .util .regex .Pattern ;
31
41
32
42
/**
33
43
* Create by h4de5ing 2016/5/18 018
34
44
* checked
35
45
*/
36
46
public class AppUtils {
37
47
38
- public static String getAppName (Context context ,String packageName ) {
48
+ public static String getAppName (Context context , String packageName ) {
39
49
PackageManager pm = context .getPackageManager ();
40
50
String appName = null ;
41
51
try {
@@ -48,7 +58,7 @@ public static String getAppName(Context context,String packageName) {
48
58
}
49
59
50
60
51
- public static Drawable getAppIcon (Context context ,String packageName ) {
61
+ public static Drawable getAppIcon (Context context , String packageName ) {
52
62
PackageManager pm = context .getPackageManager ();
53
63
Drawable appIcon = null ;
54
64
try {
@@ -61,7 +71,7 @@ public static Drawable getAppIcon(Context context,String packageName) {
61
71
}
62
72
63
73
64
- public static long getAppDate (Context context ,String packageName ) {
74
+ public static long getAppDate (Context context , String packageName ) {
65
75
long lastUpdateTime = 0 ;
66
76
try {
67
77
PackageInfo packageInfo = context .getPackageManager ().getPackageInfo (packageName , 0 );
@@ -73,7 +83,7 @@ public static long getAppDate(Context context,String packageName ) {
73
83
}
74
84
75
85
76
- public static long getAppSize (Context context ,String packageName ) {
86
+ public static long getAppSize (Context context , String packageName ) {
77
87
long appSize = 0 ;
78
88
try {
79
89
ApplicationInfo applicationInfo = context .getPackageManager ().getApplicationInfo (packageName , 0 );
@@ -84,7 +94,7 @@ public static long getAppSize(Context context,String packageName ) {
84
94
return appSize ;
85
95
}
86
96
87
- public static String getAppApk (Context context ,String packageName ) {
97
+ public static String getAppApk (Context context , String packageName ) {
88
98
String sourceDir = null ;
89
99
try {
90
100
ApplicationInfo applicationInfo = context .getPackageManager ().getApplicationInfo (packageName , 0 );
@@ -95,7 +105,7 @@ public static String getAppApk(Context context,String packageName) {
95
105
return sourceDir ;
96
106
}
97
107
98
- public static String getAppVersionName (Context context ,String packageName ) {
108
+ public static String getAppVersionName (Context context , String packageName ) {
99
109
String appVersion = null ;
100
110
try {
101
111
PackageInfo packageInfo = context .getPackageManager ().getPackageInfo (packageName , 0 );
@@ -106,7 +116,7 @@ public static String getAppVersionName(Context context,String packageName) {
106
116
return appVersion ;
107
117
}
108
118
109
- public static int getAppVersionCode (Context context ,String packageName ) {
119
+ public static int getAppVersionCode (Context context , String packageName ) {
110
120
int appVersionCode = 0 ;
111
121
try {
112
122
PackageInfo packageInfo = context .getPackageManager ().getPackageInfo (packageName , 0 );
@@ -199,4 +209,172 @@ public static boolean isSystemApp(Context context, String packageName) {
199
209
return isSys ;
200
210
}
201
211
212
+ /**
213
+ * className "com.xxx.xx..XXXService"
214
+ */
215
+ public static boolean isServiceRunning (Context context , String className ) {
216
+ boolean isRunning = false ;
217
+ ActivityManager activityManager = (ActivityManager ) context .getSystemService (Context .ACTIVITY_SERVICE );
218
+ List <RunningServiceInfo > servicesList = activityManager .getRunningServices (Integer .MAX_VALUE );
219
+ Iterator <RunningServiceInfo > l = servicesList .iterator ();
220
+ while (l .hasNext ()) {
221
+ RunningServiceInfo si = (RunningServiceInfo ) l .next ();
222
+ if (className .equals (si .service .getClassName ())) {
223
+ isRunning = true ;
224
+ }
225
+ }
226
+ return isRunning ;
227
+ }
228
+
229
+ public static boolean stopRunningService (Context context , String className ) {
230
+ Intent intent_service = null ;
231
+ boolean ret = false ;
232
+ try {
233
+ intent_service = new Intent (context , Class .forName (className ));
234
+ } catch (Exception e ) {
235
+ e .printStackTrace ();
236
+ }
237
+ if (intent_service != null ) {
238
+ ret = context .stopService (intent_service );
239
+ }
240
+ return ret ;
241
+ }
242
+
243
+ public static int getNumCores () {
244
+ try {
245
+ //Get directory containing CPU info
246
+ File dir = new File ("/sys/devices/system/cpu/" );
247
+ //Filter to only list the devices we care about
248
+ File [] files = dir .listFiles (new FileFilter () {
249
+
250
+ @ Override
251
+ public boolean accept (File pathname ) {
252
+ //Check if filename is "cpu", followed by a single digit number
253
+ if (Pattern .matches ("cpu[0-9]" , pathname .getName ())) {
254
+ return true ;
255
+ }
256
+ return false ;
257
+ }
258
+
259
+ });
260
+ //Return the number of cores (virtual CPU devices)
261
+ return files .length ;
262
+ } catch (Exception e ) {
263
+ e .printStackTrace ();
264
+ return 1 ;
265
+ }
266
+ }
267
+
268
+ public static void killProcesses (Context context , int pid , String processName ) {
269
+ ActivityManager activityManager = (ActivityManager ) context .getSystemService (Context .ACTIVITY_SERVICE );
270
+ String packageName = null ;
271
+ try {
272
+ if (processName .indexOf (":" ) == -1 ) {
273
+ packageName = processName ;
274
+ } else {
275
+ packageName = processName .split (":" )[0 ];
276
+ }
277
+ activityManager .killBackgroundProcesses (packageName );
278
+ Method forceStopPackage = activityManager .getClass ().getDeclaredMethod ("forceStopPackage" , String .class );
279
+ forceStopPackage .setAccessible (true );
280
+ forceStopPackage .invoke (activityManager , packageName );
281
+ } catch (Exception e ) {
282
+ e .printStackTrace ();
283
+ }
284
+ }
285
+
286
+ public static String runScript (String script ) {
287
+ String sRet = "" ;
288
+ try {
289
+ final Process m_process = Runtime .getRuntime ().exec (script );
290
+ final StringBuilder sbread = new StringBuilder ();
291
+ Thread tout = new Thread (new Runnable () {
292
+ public void run () {
293
+ BufferedReader bufferedReader = new BufferedReader (
294
+ new InputStreamReader (m_process .getInputStream ()),
295
+ 8192 );
296
+ String ls_1 = null ;
297
+ try {
298
+ while ((ls_1 = bufferedReader .readLine ()) != null ) {
299
+ sbread .append (ls_1 ).append ("\n " );
300
+ }
301
+ } catch (IOException e ) {
302
+ e .printStackTrace ();
303
+ } finally {
304
+ try {
305
+ bufferedReader .close ();
306
+ } catch (IOException e ) {
307
+ e .printStackTrace ();
308
+ }
309
+ }
310
+ }
311
+ });
312
+ tout .start ();
313
+
314
+ final StringBuilder sberr = new StringBuilder ();
315
+ Thread terr = new Thread (new Runnable () {
316
+ public void run () {
317
+ BufferedReader bufferedReader = new BufferedReader (
318
+ new InputStreamReader (m_process .getErrorStream ()),
319
+ 8192 );
320
+ String ls_1 = null ;
321
+ try {
322
+ while ((ls_1 = bufferedReader .readLine ()) != null ) {
323
+ sberr .append (ls_1 ).append ("\n " );
324
+ }
325
+ } catch (IOException e ) {
326
+ e .printStackTrace ();
327
+ } finally {
328
+ try {
329
+ bufferedReader .close ();
330
+ } catch (IOException e ) {
331
+ e .printStackTrace ();
332
+ }
333
+ }
334
+ }
335
+ });
336
+ terr .start ();
337
+
338
+ int retvalue = m_process .waitFor ();
339
+ while (tout .isAlive ()) {
340
+ Thread .sleep (50 );
341
+ }
342
+ if (terr .isAlive ())
343
+ terr .interrupt ();
344
+ String stdout = sbread .toString ();
345
+ String stderr = sberr .toString ();
346
+ sRet = stdout + stderr ;
347
+ } catch (Exception e ) {
348
+ e .printStackTrace ();
349
+ return null ;
350
+ }
351
+ return sRet ;
352
+ }
353
+
354
+ public static boolean getRootPermission (Context context ) {
355
+ String packageCodePath = context .getPackageCodePath ();
356
+ Process process = null ;
357
+ DataOutputStream os = null ;
358
+ try {
359
+ String cmd = "chmod 777 " + packageCodePath ;
360
+ process = Runtime .getRuntime ().exec ("su" );
361
+ os = new DataOutputStream (process .getOutputStream ());
362
+ os .writeBytes (cmd + "\n " );
363
+ os .writeBytes ("exit\n " );
364
+ os .flush ();
365
+ process .waitFor ();
366
+ } catch (Exception e ) {
367
+ return false ;
368
+ } finally {
369
+ try {
370
+ if (os != null ) {
371
+ os .close ();
372
+ }
373
+ process .destroy ();
374
+ } catch (Exception e ) {
375
+ e .printStackTrace ();
376
+ }
377
+ }
378
+ return true ;
379
+ }
202
380
}
0 commit comments