-
Notifications
You must be signed in to change notification settings - Fork 802
/
AppApplicationMgr.java
665 lines (621 loc) · 21 KB
/
AppApplicationMgr.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
package com.jingewenku.abrahamcaijin.commonutil;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import java.io.*;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
/**
* 主要功能:获取App应用版本信息
* @Prject: CommonUtilLibrary
* @Package: com.jingewenku.abrahamcaijin.commonutil
* @author: AbrahamCaiJin
* @date: 2017年05月03日 16:37
* @Copyright: 个人版权所有
* @Company:
* @version: 1.0.0
*/
@SuppressWarnings("rawtypes")
public class AppApplicationMgr {
/**
* 获取本地apk的名称
* @param context 上下文
* @return String
*/
public static String getAppName(Context context) {
try {
PackageManager e = context.getPackageManager();
PackageInfo packageInfo = e.getPackageInfo(context.getPackageName(), 0);
int labelRes = packageInfo.applicationInfo.labelRes;
return context.getResources().getString(labelRes);
} catch (NameNotFoundException var4) {
var4.printStackTrace();
return "unKnown";
}
}
/**
* 获取本地Apk版本名称
* @param context 上下文
* @return String
*/
public static String getVersionName(Context context) {
String verName = "";
try {
verName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
} catch (NameNotFoundException e) {
AppLogMessageMgr.e("AppApplicationMgr-->>getVerName()", e.getMessage() + "获取本地Apk版本名称失败!");
e.printStackTrace();
}
return verName;
}
/**
* 获取本地Apk版本号
* @param context 上下文
* @return int
*/
public static int getVersionCode(Context context) {
int verCode = -1;
try {
verCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
} catch (NameNotFoundException e) {
AppLogMessageMgr.e("AppApplicationMgr-->>getVerCode()", e.getMessage() + "获取本地Apk版本号失败!");
e.printStackTrace();
}
return verCode;
}
/**
* 根据key获取xml中Meta的值
* @param context 上下文
* @param key
* @return
*/
public static String getMetaData(Context context, String key) {
String value = "";
try {
ApplicationInfo e = context.getPackageManager().getApplicationInfo(context.getPackageName(), 128);
if(null != e) {
Bundle metaData = e.metaData;
if(null != metaData) {
value = metaData.getString(key);
if(null == value || value.length() == 0) {
value = "";
}
}
}
} catch (NameNotFoundException var5) {
var5.printStackTrace();
}
return value;
}
/**
* 获取应用图标
* @param context
* @param packageName
* @return
*/
public static Drawable getAppIcon(Context context, String packageName) {
PackageManager pm = context.getPackageManager();
Drawable appIcon = null;
try {
ApplicationInfo applicationInfo = pm.getApplicationInfo(packageName, 0);
appIcon = applicationInfo.loadIcon(pm);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return appIcon;
}
/**
* 获取应用第一次安装日期
* @param context
* @param packageName
* @return
*/
public static long getAppFirstInstallTime(Context context, String packageName) {
long lastUpdateTime = 0;
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
lastUpdateTime = packageInfo.firstInstallTime;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return lastUpdateTime;
}
/**
* 获取应用更新日期
* @param context
* @param packageName
* @return
*/
public static long getAppLastUpdateTime(Context context, String packageName) {
long lastUpdateTime = 0;
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
lastUpdateTime = packageInfo.lastUpdateTime;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return lastUpdateTime;
}
/**
* 获取应用大小
* @param context
* @param packageName
* @return
*/
public static long getAppSize(Context context, String packageName) {
long appSize = 0;
try {
ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(packageName, 0);
appSize = new File(applicationInfo.sourceDir).length();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return appSize;
}
/**
* 获取应用apk文件
* @param context
* @param packageName
* @return
*/
public static String getAppApk(Context context, String packageName) {
String sourceDir = null;
try {
ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(packageName, 0);
sourceDir = applicationInfo.sourceDir;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return sourceDir;
}
/**
* 获取应用的安装市场
* @param context
* @param packageName
* @return
*/
public static String getAppInstaller(Context context, String packageName) {
return context.getPackageManager().getInstallerPackageName(packageName);
}
/**
* 获取应用签名
* @param context
* @param packageName
* @return
*/
public static String getAppSign(Context context, String packageName) {
try {
PackageInfo pis = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
return hexdigest(pis.signatures[0].toByteArray());
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(AppApplicationMgr.class.getName() + "the " + packageName + "'s application not found");
}
}
public static String hexdigest(byte[] paramArrayOfByte) {
final char[] hexDigits = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102};
try {
MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
localMessageDigest.update(paramArrayOfByte);
byte[] arrayOfByte = localMessageDigest.digest();
char[] arrayOfChar = new char[32];
for (int i = 0, j = 0; ; i++, j++) {
if (i >= 16) {
return new String(arrayOfChar);
}
int k = arrayOfByte[i];
arrayOfChar[j] = hexDigits[(0xF & k >>> 4)];
arrayOfChar[++j] = hexDigits[(k & 0xF)];
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* 获取应用兼容sdk
* @param context
* @param packageName
* @return
*/
public static int getAppTargetSdkVersion(Context context, String packageName) {
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
ApplicationInfo applicationInfo = packageInfo.applicationInfo;
return applicationInfo.targetSdkVersion;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return 0;
}
}
/**
* 获取应用uid
* @param context
* @param packageName
* @return
*/
public static int getAppUid(Context context, String packageName) {
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
ApplicationInfo applicationInfo = packageInfo.applicationInfo;
return applicationInfo.uid;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return 0;
}
}
/**
* 获取Cpu内核数
* @return
*/
public static int getNumCores() {
try {
File dir = new File("/sys/devices/system/cpu/");
File[] files = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return Pattern.matches("cpu[0-9]", pathname.getName());
}
});
return files.length;
} catch (Exception e) {
e.printStackTrace();
return 1;
}
}
/**
* 获得root权限
* @param context
* @return
*/
public static boolean getRootPermission(Context context) {
String packageCodePath = context.getPackageCodePath();
Process process = null;
DataOutputStream os = null;
try {
String cmd = "chmod 777 " + packageCodePath;
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(cmd + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
/**
* 获取应用的所有权限
* @param context
* @param packname
* @return
*/
public static String[] getAppPermissions(Context context, String packname) {
String[] requestedPermissions = null;
try {
PackageInfo info = context.getPackageManager().getPackageInfo(packname, PackageManager.GET_PERMISSIONS);
requestedPermissions = info.requestedPermissions;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return requestedPermissions;
}
/**
* 是否有权限
* @param context
* @param permission
* @return
*/
public static boolean hasPermission(Context context, String permission) {
if (context != null && !TextUtils.isEmpty(permission)) {
try {
PackageManager packageManager = context.getPackageManager();
if (packageManager != null) {
if (PackageManager.PERMISSION_GRANTED == packageManager.checkPermission(permission, context
.getPackageName())) {
return true;
}
Log.d("AppUtils", "Have you declared permission " + permission + " in AndroidManifest.xml ?");
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return false;
}
/**
* 应用是否安装
* @param context
* @param packageName
* @return
*/
public static boolean isInstalled(Context context, String packageName) {
boolean installed = false;
if (TextUtils.isEmpty(packageName)) {
return false;
}
List<ApplicationInfo> installedApplications = context.getPackageManager().getInstalledApplications(0);
for (ApplicationInfo in : installedApplications) {
if (packageName.equals(in.packageName)) {
installed = true;
break;
} else {
installed = false;
}
}
return installed;
}
/**
* 安装应用
* @param context
* @param filePath
* @return
*/
public static boolean installApk(Context context, String filePath) {
File file = new File(filePath);
if (!file.exists() || !file.isFile() || file.length() <= 0) {
return false;
}
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
return true;
}
/**
* 卸载应用
* @param context
* @param packageName
* @return
*/
public static boolean uninstallApk(Context context, String packageName) {
if (TextUtils.isEmpty(packageName)) {
return false;
}
Intent i = new Intent(Intent.ACTION_DELETE, Uri.parse("package:" + packageName));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
return true;
}
/**
* 是否是系统应用
* @param context
* @param packageName
* @return
*/
public static boolean isSystemApp(Context context, String packageName) {
boolean isSys = false;
PackageManager pm = context.getPackageManager();
try {
ApplicationInfo applicationInfo = pm.getApplicationInfo(packageName, 0);
if (applicationInfo != null && (applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {
isSys = true;
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
isSys = false;
}
return isSys;
}
/**
* 服务是否在运行
* @param context
* @param className
* @return
*/
public static boolean isServiceRunning(Context context, String className) {
boolean isRunning = false;
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> servicesList = activityManager.getRunningServices(Integer.MAX_VALUE);
for (RunningServiceInfo si : servicesList) {
if (className.equals(si.service.getClassName())) {
isRunning = true;
}
}
return isRunning;
}
/**
* 停止服务
* @param context
* @param className
* @return
*/
public static boolean stopRunningService(Context context, String className) {
Intent intent_service = null;
boolean ret = false;
try {
intent_service = new Intent(context, Class.forName(className));
} catch (Exception e) {
e.printStackTrace();
}
if (intent_service != null) {
ret = context.stopService(intent_service);
}
return ret;
}
/**
* 结束进程
* @param context
* @param pid
* @param processName
*/
public static void killProcesses(Context context, int pid, String processName) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
String packageName;
try {
if (!processName.contains(":")) {
packageName = processName;
} else {
packageName = processName.split(":")[0];
}
activityManager.killBackgroundProcesses(packageName);
Method forceStopPackage = activityManager.getClass().getDeclaredMethod("forceStopPackage", String.class);
forceStopPackage.setAccessible(true);
forceStopPackage.invoke(activityManager, packageName);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 运行脚本
* @param script
* @return
*/
public static String runScript(String script) {
String sRet;
try {
final Process m_process = Runtime.getRuntime().exec(script);
final StringBuilder sbread = new StringBuilder();
Thread tout = new Thread(new Runnable() {
public void run() {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(m_process.getInputStream()),
8192);
String ls_1;
try {
while ((ls_1 = bufferedReader.readLine()) != null) {
sbread.append(ls_1).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
tout.start();
final StringBuilder sberr = new StringBuilder();
Thread terr = new Thread(new Runnable() {
public void run() {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(m_process.getErrorStream()),
8192);
String ls_1;
try {
while ((ls_1 = bufferedReader.readLine()) != null) {
sberr.append(ls_1).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
terr.start();
m_process.waitFor();
while (tout.isAlive()) {
Thread.sleep(50);
}
if (terr.isAlive())
terr.interrupt();
String stdout = sbread.toString();
String stderr = sberr.toString();
sRet = stdout + stderr;
} catch (Exception e) {
e.printStackTrace();
return null;
}
return sRet;
}
/**
* 启动应用
* @param context
* @param packagename
*/
public static void runApp(Context context, String packagename) {
context.startActivity(new Intent(context.getPackageManager().getLaunchIntentForPackage(packagename)));
}
// /**
// * 获得当前版本信息
// * @param keyValues key信息
// * @return RequestParams
// */
// public static RequestParams getRequestParams(HashMap<String,String> keyValues){
// RequestParams params = new RequestParams();
// Iterator iterator = keyValues.entrySet().iterator();
// while(iterator.hasNext()){
// Map.Entry entry = (Map.Entry) iterator.next();
// Object key = entry.getKey();
// params.put((String) key, entry.getValue().toString());
// }
// return params;
// }
/**
* 获得包名
*
* @param context 上下文
* @return 包名
*/
public static String getPackageName(Context context) {
try {
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).packageName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取application层级的metadata
*
* @param context 上下文
* @param key key
* @return value
*/
public static String getApplicationMetaData(Context context, String key) {
try {
Bundle metaData = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA).metaData;
return metaData.get(key).toString();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* 获得应用申明的所有权限列表
* @param context 上下文
* @return 获得应用申明的所有权限列表
*/
public static List<String> getPermissions(Context context){
List<String> permissions=new ArrayList<String>();
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
permissions.addAll(Arrays.asList(packageInfo.requestedPermissions));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return permissions;
}
}