Skip to content

Commit ec7f7ad

Browse files
committed
see 10/13 log
1 parent 534b2ad commit ec7f7ad

File tree

9 files changed

+81
-134
lines changed

9 files changed

+81
-134
lines changed

utilcode/src/androidTest/java/com/blankj/utilcode/ApplicationTest.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

utilcode/src/main/AndroidManifest.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,5 @@
33
<application android:allowBackup="true"
44
android:label="@string/app_name"
55
android:supportsRtl="true">
6-
76
</application>
8-
97
</manifest>

utilcode/src/main/java/com/blankj/utilcode/utils/ActivityUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private ActivityUtils() {
2929
* @param className activity全路径类名
3030
* @return {@code true}: 是<br>{@code false}: 否
3131
*/
32-
public static boolean isExistActivity(Context context, String packageName, String className) {
32+
public static boolean isActivityExists(Context context, String packageName, String className) {
3333
Intent intent = new Intent();
3434
intent.setClassName(packageName, className);
3535
return !(context.getPackageManager().resolveActivity(intent, 0) == null ||

utilcode/src/main/java/com/blankj/utilcode/utils/AppUtils.java

Lines changed: 45 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@
99
import android.content.pm.Signature;
1010
import android.graphics.drawable.Drawable;
1111

12-
import java.security.MessageDigest;
13-
import java.security.NoSuchAlgorithmException;
14-
import java.io.BufferedReader;
1512
import java.io.File;
16-
import java.io.IOException;
17-
import java.io.InputStreamReader;
18-
import java.io.OutputStream;
1913
import java.util.ArrayList;
2014
import java.util.Arrays;
2115
import java.util.List;
@@ -342,6 +336,35 @@ public static int getAppVersionCode(Context context, String packageName) {
342336
}
343337
}
344338

339+
/**
340+
* 判断App是否是系统应用
341+
*
342+
* @param context 上下文
343+
* @return {@code true}: 是<br>{@code false}: 否
344+
*/
345+
public static boolean isSystemApp(Context context) {
346+
return isSystemApp(context, context.getPackageName());
347+
}
348+
349+
/**
350+
* 判断App是否是系统应用
351+
*
352+
* @param context 上下文
353+
* @param packageName 包名
354+
* @return {@code true}: 是<br>{@code false}: 否
355+
*/
356+
public static boolean isSystemApp(Context context, String packageName) {
357+
if (StringUtils.isSpace(packageName)) return false;
358+
try {
359+
PackageManager pm = context.getPackageManager();
360+
ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
361+
return ai != null && (ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
362+
} catch (PackageManager.NameNotFoundException e) {
363+
e.printStackTrace();
364+
}
365+
return false;
366+
}
367+
345368
/**
346369
* 获取App签名
347370
*
@@ -397,77 +420,19 @@ public static String getAppSignatureSHA1(Context context, String packageName) {
397420
replaceAll("(?<=[0-9A-F]{2})[0-9A-F]{2}", ":$0");
398421
}
399422

400-
/**
401-
* 判断App是否是系统应用
402-
*
403-
* @param context 上下文
404-
* @return {@code true}: 是<br>{@code false}: 否
405-
*/
406-
public static boolean isSystemApp(Context context) {
407-
return isSystemApp(context, context.getPackageName());
408-
}
409-
410-
/**
411-
* 判断App是否是系统应用
412-
*
413-
* @param context 上下文
414-
* @param packageName 包名
415-
* @return {@code true}: 是<br>{@code false}: 否
416-
*/
417-
public static boolean isSystemApp(Context context, String packageName) {
418-
if (StringUtils.isSpace(packageName)) return false;
419-
try {
420-
PackageManager pm = context.getPackageManager();
421-
ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
422-
return ai != null && (ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
423-
} catch (PackageManager.NameNotFoundException e) {
424-
e.printStackTrace();
425-
}
426-
return false;
427-
}
428-
429423
/**
430424
* 判断App是否处于前台
431425
*
432426
* @param context 上下文
433427
* @return {@code true}: 是<br>{@code false}: 否
434428
*/
435429
public static boolean isAppForeground(Context context) {
436-
return isApplicationForeground(context, context.getPackageName());
437-
}
438-
439-
/**
440-
* 判断App是否处于前台
441-
* <p>需添加权限 {@code <uses-permission android:name="android.permission.GET_TASKS"/>}</p>
442-
* <p>该方法在 API 21 被遗弃,已经不能使用</p>
443-
*
444-
* @param context 上下文
445-
* @param packageName 包名
446-
* @return {@code true}: 是<br>{@code false}: 否
447-
*/
448-
@Deprecated
449-
public static boolean isAppForeground(Context context, String packageName) {
450430
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
451-
@SuppressWarnings("deprecation")
452-
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
453-
return tasks != null && !tasks.isEmpty()
454-
&& tasks.get(0).topActivity.getPackageName().equals(packageName);
455-
}
456-
457-
/**
458-
* 判断 App 是否处于前台
459-
*
460-
* @param context 上下文
461-
* @param packageName 包名
462-
* @return {@code true}: 是<br>{@code false}: 否
463-
*/
464-
public static boolean isApplicationForeground(final Context context, String packageName) {
465-
final ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
466-
final List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
431+
List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
467432
if (processInfos != null) {
468433
for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
469434
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
470-
&& Arrays.asList(processInfo.pkgList).contains(packageName)) {
435+
&& Arrays.asList(processInfo.pkgList).contains(context.getPackageName())) {
471436
return true;
472437
}
473438
}
@@ -550,10 +515,10 @@ public void setVersionName(String versionName) {
550515
* @param packageName 包名
551516
* @param packagePath 包路径
552517
* @param versionName 版本号
553-
* @param versionCode 版本Code
518+
* @param versionCode 版本码
554519
* @param isSystem 是否系统应用
555520
*/
556-
public AppInfo(String name, Drawable icon, String packageName, String packagePath,
521+
public AppInfo(String packageName, String name, Drawable icon, String packagePath,
557522
String versionName, int versionCode, boolean isSystem) {
558523
this.setName(name);
559524
this.setIcon(icon);
@@ -564,16 +529,16 @@ public AppInfo(String name, Drawable icon, String packageName, String packagePat
564529
this.setSystem(isSystem);
565530
}
566531

567-
// @Override
568-
// public String toString() {
569-
// return getName() + "\n"
570-
// + getIcon() + "\n"
571-
// + getPackageName() + "\n"
572-
// + getPackagePath() + "\n"
573-
// + getVersionName() + "\n"
574-
// + getVersionCode() + "\n"
575-
// + isSystem() + "\n"
576-
// }
532+
@Override
533+
public String toString() {
534+
return "App包名:" + getPackageName() + "\n" +
535+
"App名称:" + getName() + "\n" +
536+
"App图标:" + getIcon() + "\n" +
537+
"App路径:" + getPackagePath() + "\n" +
538+
"App版本号:" + getVersionName() + "\n" +
539+
"App版本码:" + getVersionCode() + "\n" +
540+
"是否系统App:" + isSystem() + "\n";
541+
}
577542
}
578543

579544
/**
@@ -616,14 +581,14 @@ public static AppInfo getAppInfo(Context context, String packageName) {
616581
private static AppInfo getBean(PackageManager pm, PackageInfo pi) {
617582
if (pm == null || pi == null) return null;
618583
ApplicationInfo ai = pi.applicationInfo;
584+
String packageName = pi.packageName;
619585
String name = ai.loadLabel(pm).toString();
620586
Drawable icon = ai.loadIcon(pm);
621-
String packageName = pi.packageName;
622587
String packagePath = ai.sourceDir;
623588
String versionName = pi.versionName;
624589
int versionCode = pi.versionCode;
625590
boolean isSystem = (ApplicationInfo.FLAG_SYSTEM & ai.flags) != 0;
626-
return new AppInfo(name, icon, packageName, packagePath, versionName, versionCode, isSystem);
591+
return new AppInfo(packageName, name, icon, packagePath, versionName, versionCode, isSystem);
627592
}
628593

629594
/**

utilcode/src/main/java/com/blankj/utilcode/utils/CrashUtils.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
import android.os.Environment;
88

99
import java.io.File;
10+
import java.io.FileWriter;
11+
import java.io.IOException;
1012
import java.io.PrintWriter;
11-
import java.io.StringWriter;
12-
import java.io.Writer;
1313
import java.lang.Thread.UncaughtExceptionHandler;
14-
import java.lang.ref.WeakReference;
14+
import java.text.SimpleDateFormat;
15+
import java.util.Date;
16+
import java.util.Locale;
1517

1618
/**
1719
* <pre>
@@ -52,9 +54,9 @@ public static CrashUtils getInstance() {
5254
public boolean init(Context context) {
5355
if (mInitialized) return true;
5456
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
55-
dir = context.getExternalCacheDir().getPath();
57+
dir = context.getExternalCacheDir().getPath() + File.separator;
5658
} else {
57-
dir = context.getCacheDir().getPath();
59+
dir = context.getCacheDir().getPath() + File.separator;
5860
}
5961
try {
6062
PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
@@ -71,25 +73,24 @@ public boolean init(Context context) {
7173

7274
@Override
7375
public void uncaughtException(Thread thread, Throwable throwable) {
74-
String fullPath = dir + File.separator + "crash_" + TimeUtils.getCurTimeString() + ".txt";
76+
String now = new SimpleDateFormat("yy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date());
77+
String fullPath = dir + "crash_" + now + ".txt";
7578
if (!FileUtils.createOrExistsFile(fullPath)) return;
76-
StringBuilder sb = new StringBuilder();
77-
sb.append(getCrashHead());
78-
Writer writer = new StringWriter();
7979
PrintWriter pw = null;
8080
try {
81-
pw = new PrintWriter(writer);
81+
pw = new PrintWriter(new FileWriter(fullPath, false));
82+
pw.write(getCrashHead());
8283
throwable.printStackTrace(pw);
8384
Throwable cause = throwable.getCause();
8485
while (cause != null) {
8586
cause.printStackTrace(pw);
8687
cause = cause.getCause();
8788
}
89+
} catch (IOException e) {
90+
e.printStackTrace();
8891
} finally {
8992
CloseUtils.closeIO(pw);
9093
}
91-
sb.append(writer.toString());
92-
FileUtils.writeFileFromString(fullPath, sb.toString(), false);
9394
if (mHandler != null) {
9495
mHandler.uncaughtException(thread, throwable);
9596
}
@@ -100,16 +101,14 @@ public void uncaughtException(Thread thread, Throwable throwable) {
100101
*
101102
* @return 崩溃头
102103
*/
103-
private StringBuilder getCrashHead() {
104-
StringBuilder sb = new StringBuilder();
105-
sb.append("\n************* Crash Log Head ****************");
106-
sb.append("\nDevice Manufacturer: ").append(Build.MANUFACTURER);// 设备厂商
107-
sb.append("\nDevice Model : ").append(Build.MODEL);// 设备型号
108-
sb.append("\nAndroid Version : ").append(Build.VERSION.RELEASE);// 系统版本
109-
sb.append("\nAndroid SDK : ").append(Build.VERSION.SDK_INT);// SDK版本
110-
sb.append("\nApp VersionName : ").append(versionName);
111-
sb.append("\nApp VersionCode : ").append(versionCode);
112-
sb.append("\n************* Crash Log Head ****************\n\n");
113-
return sb;
104+
private String getCrashHead() {
105+
return "\n************* Crash Log Head ****************" +
106+
"\nDevice Manufacturer: " + Build.MANUFACTURER +// 设备厂商
107+
"\nDevice Model : " + Build.MODEL +// 设备型号
108+
"\nAndroid Version : " + Build.VERSION.RELEASE +// 系统版本
109+
"\nAndroid SDK : " + Build.VERSION.SDK_INT +// SDK版本
110+
"\nApp VersionName : " + versionName +
111+
"\nApp VersionCode : " + versionCode +
112+
"\n************* Crash Log Head ****************\n\n";
114113
}
115114
}

utilcode/src/main/java/com/blankj/utilcode/utils/ImageUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ public static Bitmap toRoundCorner(Bitmap src, float radius, boolean recycle) {
633633
*
634634
* @param context 上下文
635635
* @param src 源图片
636-
* @param scale 缩小倍数(0...1)
636+
* @param scale 缩放比例(0...1)
637637
* @param radius 模糊半径
638638
* @return 模糊后的图片
639639
*/
@@ -647,7 +647,7 @@ public static Bitmap fastBlur(Context context, Bitmap src, float scale, float ra
647647
*
648648
* @param context 上下文
649649
* @param src 源图片
650-
* @param scale 缩小倍数(0...1)
650+
* @param scale 缩放比例(0...1)
651651
* @param radius 模糊半径
652652
* @param recycle 是否回收
653653
* @return 模糊后的图片
@@ -1144,7 +1144,7 @@ public static Bitmap toGray(Bitmap src) {
11441144
public static Bitmap toGray(Bitmap src, boolean recycle) {
11451145
if (isEmptyBitmap(src)) return null;
11461146
Bitmap grayBitmap = Bitmap.createBitmap(src.getWidth(),
1147-
src.getHeight(), Bitmap.Config.RGB_565);
1147+
src.getHeight(), Bitmap.Config.ARGB_8888);
11481148
Canvas canvas = new Canvas(grayBitmap);
11491149
Paint paint = new Paint();
11501150
ColorMatrix colorMatrix = new ColorMatrix();

utilcode/src/main/java/com/blankj/utilcode/utils/LogUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,9 @@ private synchronized static void log2File(char type, String tag, String content)
291291
if (content == null) return;
292292
Date now = new Date();
293293
String date = new SimpleDateFormat("MM-dd", Locale.getDefault()).format(now);
294-
String time = new SimpleDateFormat("MM-dd HH:mm:ss.SSS", Locale.getDefault()).format(now);
295-
String fullPath = dir + date + ".log";
294+
String fullPath = dir + "log_" + date + ".txt";
296295
if (!FileUtils.createOrExistsFile(fullPath)) return;
296+
String time = new SimpleDateFormat("MM-dd HH:mm:ss.SSS", Locale.getDefault()).format(now);
297297
String dateLogContent = time + ":" + type + ":" + tag + ":" + content + '\n';
298298
BufferedWriter bw = null;
299299
try {

utilcode/src/main/java/com/blankj/utilcode/utils/NetworkUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ private NetworkUtils() {
5252
*/
5353
public static void openWirelessSettings(Context context) {
5454
if (android.os.Build.VERSION.SDK_INT > 10) {
55-
context.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
56-
} else {
5755
context.startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
56+
} else {
57+
context.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
5858
}
5959
}
6060

utilcode/src/test/java/com/blankj/utilcode/utils/TestUtils.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,24 @@ public void readme2Eng() throws Exception {
4343
File readme = new File(new File(System.getProperty("user.dir")).getParent() + SEP + "README-CN.md");
4444
File readmeEng = new File(new File(System.getProperty("user.dir")).getParent() + SEP + "README.md");
4545
List<String> list = FileUtils.readFile2List(readme, "UTF-8");
46-
StringBuilder sb = new StringBuilder();
47-
for (String line : list) {
46+
StringBuilder sb = new StringBuilder("## Android developers should collect the following utils\r\n" +
47+
"**[中文版README][readme-cn.md]→[How to get this README from README-CN][trans]**\r\n" +
48+
"***\r\n" +
49+
"Directory is shown below: \r\n");
50+
List<String> lines = list.subList(4, list.size());
51+
for (String line : lines) {
4852
if (line.length() >= 3 && line.startsWith("> -") && line.contains("Utils")) {
4953
String utilsName = line.substring(line.indexOf("[") + 1, line.indexOf("Utils"));
5054
sb.append("> - **About ").append(utilsName).append(line.substring(line.indexOf("→")));
5155
} else if (line.length() >= 3 && line.startsWith("> ")) {
5256
sb.append("> - ").append(line.substring(line.indexOf("*")));
53-
} else if (line.length() >= 3 && line.startsWith("## ")) {
54-
sb.append("## Android developers should collect the following utils")
55-
.append("\r\n").append("**[中文版README][readme.cn]**");
56-
} else if (line.length() >= 1 && line.startsWith("为")) {
57-
sb.append("Directory is shown below: ");
5857
} else if (line.length() >= 2 && line.startsWith("**做")) {
5958
sb.append("**I'm so sorry for that the code is annotated with Chinese.**");
6059
} else {
6160
sb.append(line);
6261
}
6362
sb.append("\r\n");
6463
}
65-
sb.append("\r\n[readme.cn]: https://github.com/Blankj/AndroidUtilCode/blob/master/README-CN.md");
6664
FileUtils.writeFileFromString(readmeEng, sb.toString(), false);
6765
}
6866
}

0 commit comments

Comments
 (0)