Skip to content

Commit 28dcee3

Browse files
committed
see 09/29 log
1 parent 8abb362 commit 28dcee3

File tree

12 files changed

+281
-135
lines changed

12 files changed

+281
-135
lines changed

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

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.app.Activity;
44
import android.app.ActivityManager;
5-
import android.content.ComponentName;
65
import android.content.Context;
76
import android.content.pm.ApplicationInfo;
87
import android.content.pm.PackageInfo;
@@ -36,8 +35,7 @@ private AppUtils() {
3635
* @return {@code true}: 已安装<br>{@code false}: 未安装
3736
*/
3837
public static boolean isInstallApp(Context context, String packageName) {
39-
return !StringUtils.isSpace(packageName)
40-
&& context.getPackageManager().getLaunchIntentForPackage(packageName) != null;
38+
return !StringUtils.isSpace(packageName) && IntentUtils.getLaunchAppIntent(context, packageName) != null;
4139
}
4240

4341
/**
@@ -335,21 +333,21 @@ public static Signature[] getAppSignature(Context context, String packageName) {
335333
}
336334

337335
/**
338-
* 判断是否是系统App
336+
* 判断App是否是系统应用
339337
*
340338
* @param context 上下文
341-
* @return {@code true}: 是<br>{@code false}: 不是
339+
* @return {@code true}: 是<br>{@code false}:
342340
*/
343341
public static boolean isSystemApp(Context context) {
344342
return isSystemApp(context, context.getPackageName());
345343
}
346344

347345
/**
348-
* 判断是否是系统App
346+
* 判断App是否是系统应用
349347
*
350348
* @param context 上下文
351349
* @param packageName 包名
352-
* @return {@code true}: 是<br>{@code false}: 不是
350+
* @return {@code true}: 是<br>{@code false}:
353351
*/
354352
public static boolean isSystemApp(Context context, String packageName) {
355353
if (StringUtils.isSpace(packageName)) return false;
@@ -363,6 +361,33 @@ public static boolean isSystemApp(Context context, String packageName) {
363361
return false;
364362
}
365363

364+
/**
365+
* 判断App是否处于前台
366+
* <p>需添加权限 {@code <uses-permission android:name="android.permission.GET_TASKS"/>}</p>
367+
* <p>并且必须是系统应用该方法才有效</p>
368+
*
369+
* @param context 上下文
370+
* @return {@code true}: 是<br>{@code false}: 否
371+
*/
372+
public static boolean isAppForeground(Context context) {
373+
return isAppForeground(context, context.getPackageName());
374+
}
375+
376+
/**
377+
* 判断App是否处于前台
378+
* <p>需添加权限 {@code <uses-permission android:name="android.permission.GET_TASKS"/>}</p>
379+
* <p>并且必须是系统应用该方法才有效</p>
380+
*
381+
* @param context 上下文
382+
* @return {@code true}: 是<br>{@code false}: 否
383+
*/
384+
public static boolean isAppForeground(Context context, String packageName) {
385+
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
386+
@SuppressWarnings("deprecation")
387+
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
388+
return !tasks.isEmpty() && tasks.get(0).topActivity.getPackageName().equals(packageName);
389+
}
390+
366391
/**
367392
* 封装App信息的Bean类
368393
*/
@@ -521,7 +546,7 @@ private static AppInfo getBean(PackageManager pm, PackageInfo pi) {
521546
* @param context 上下文
522547
* @return 所有已安装的AppInfo列表
523548
*/
524-
public static List<AppInfo> getAllAppsInfo(Context context) {
549+
public static List<AppInfo> getAppsInfo(Context context) {
525550
List<AppInfo> list = new ArrayList<>();
526551
PackageManager pm = context.getPackageManager();
527552
// 获取系统中安装的所有软件信息
@@ -535,23 +560,35 @@ public static List<AppInfo> getAllAppsInfo(Context context) {
535560
}
536561

537562
/**
538-
* 判断当前App处于前台还是后台
539-
* <p>需添加权限 {@code <uses-permission android:name="android.permission.GET_TASKS"/>}</p>
540-
* <p>并且必须是系统应用该方法才有效</p>
563+
* 清除App所有数据
541564
*
542-
* @param context 上下文
543-
* @return {@code true}: 后台<br>{@code false}: 前台
565+
* @param context 上下文
566+
* @param dirPaths 目录路径
544567
*/
545-
public static boolean isAppBackground(Context context) {
546-
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
547-
@SuppressWarnings("deprecation")
548-
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
549-
if (!tasks.isEmpty()) {
550-
ComponentName topActivity = tasks.get(0).topActivity;
551-
if (!topActivity.getPackageName().equals(context.getPackageName())) {
552-
return true;
553-
}
568+
public static boolean cleanAppData(Context context, String... dirPaths) {
569+
File[] dirs = new File[dirPaths.length];
570+
int i = 0;
571+
for (String dirPath : dirPaths) {
572+
dirs[i++] = new File(dirPath);
554573
}
555-
return false;
574+
return cleanAppData(context, dirs);
575+
}
576+
577+
/**
578+
* 清除App所有数据
579+
*
580+
* @param context 上下文
581+
* @param dirs 目录
582+
*/
583+
public static boolean cleanAppData(Context context, File... dirs) {
584+
boolean isSuccess = CleanUtils.cleanInternalCache(context);
585+
isSuccess &= CleanUtils.cleanInternalDbs(context);
586+
isSuccess &= CleanUtils.cleanInternalSP(context);
587+
isSuccess &= CleanUtils.cleanInternalFiles(context);
588+
isSuccess &= CleanUtils.cleanExternalCache(context);
589+
for (File dir : dirs) {
590+
isSuccess &= CleanUtils.cleanCustomCache(dir);
591+
}
592+
return isSuccess;
556593
}
557594
}
Lines changed: 39 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.blankj.utilcode.utils;
22

33
import android.content.Context;
4-
import android.os.Environment;
54

65
import java.io.File;
76

@@ -20,106 +19,89 @@ private CleanUtils() {
2019
}
2120

2221
/**
23-
* 清除本应用所有的数据
22+
* 清除内部缓存
23+
* <p>/data/data/com.xxx.xxx/cache</p>
2424
*
25-
* @param context 上下文
26-
* @param filePath 文件路径
25+
* @param context 上下文
26+
* @return {@code true}: 清除成功<br>{@code false}: 清除失败
2727
*/
28-
public static void cleanApplicationData(Context context, String... filePath) {
29-
cleanInternalCache(context);
30-
cleanExternalCache(context);
31-
cleanDatabases(context);
32-
cleanSharedPreference(context);
33-
cleanFiles(context);
34-
for (String fp : filePath) {
35-
cleanCustomCache(fp);
36-
}
28+
public static boolean cleanInternalCache(Context context) {
29+
return FileUtils.deleteFilesInDir(context.getCacheDir());
3730
}
3831

3932
/**
40-
* 清除应用内部缓存
41-
* <p>/data/data/com.xxx.xxx/cache</p>
33+
* 清除内部文件
34+
* <p>/data/data/com.xxx.xxx/files</p>
4235
*
4336
* @param context 上下文
37+
* @return {@code true}: 清除成功<br>{@code false}: 清除失败
4438
*/
45-
public static void cleanInternalCache(Context context) {
46-
deleteFilesByDirectory(context.getCacheDir());
39+
public static boolean cleanInternalFiles(Context context) {
40+
return FileUtils.deleteFilesInDir(context.getFilesDir());
4741
}
4842

4943
/**
50-
* 清除应用所有数据库
44+
* 清除内部数据库
5145
* <p>/data/data/com.xxx.xxx/databases</p>
5246
*
5347
* @param context 上下文
48+
* @return {@code true}: 清除成功<br>{@code false}: 清除失败
5449
*/
55-
public static void cleanDatabases(Context context) {
56-
deleteFilesByDirectory(new File(context.getFilesDir().getPath()
57-
+ context.getPackageName() + File.separator + "databases"));
50+
public static boolean cleanInternalDbs(Context context) {
51+
return FileUtils.deleteFilesInDir(context.getFilesDir().getParent() + File.separator + "databases");
5852
}
5953

6054
/**
61-
* 按名字清除应用数据库
55+
* 根据名称清除数据库
56+
* <p>/data/data/com.xxx.xxx/databases/dbName</p>
6257
*
6358
* @param context 上下文
6459
* @param dbName 数据库名称
60+
* @return {@code true}: 清除成功<br>{@code false}: 清除失败
6561
*/
66-
public static void cleanDatabaseByName(Context context, String dbName) {
67-
context.deleteDatabase(dbName);
62+
public static boolean cleanInternalDbByName(Context context, String dbName) {
63+
return context.deleteDatabase(dbName);
6864
}
6965

7066
/**
71-
* 清除应用sp
67+
* 清除内部SP
7268
* <p>/data/data/com.xxx.xxx/shared_prefs</p>
7369
*
7470
* @param context 上下文
71+
* @return {@code true}: 清除成功<br>{@code false}: 清除失败
7572
*/
76-
public static void cleanSharedPreference(Context context) {
77-
deleteFilesByDirectory(new File(context.getFilesDir().getPath()
78-
+ context.getPackageName() + File.separator + "shared_prefs"));
79-
}
80-
81-
/**
82-
* 清除应用文件下的内容
83-
* <p>/data/data/com.xxx.xxx/files</p>
84-
*
85-
* @param context 上下文
86-
*/
87-
public static void cleanFiles(Context context) {
88-
deleteFilesByDirectory(context.getFilesDir());
73+
public static boolean cleanInternalSP(Context context) {
74+
return FileUtils.deleteFilesInDir(context.getFilesDir().getParent() + File.separator + "shared_prefs");
8975
}
9076

9177
/**
92-
* 清除外部cache下的内容
93-
* <p>/mnt/sdcard/android/data/com.xxx.xxx/cache<p/>
78+
* 清除外部缓存
79+
* <p>/storage/emulated/0/android/data/com.xxx.xxx/cache<p/>
9480
*
9581
* @param context 上下文
82+
* @return {@code true}: 清除成功<br>{@code false}: 清除失败
9683
*/
97-
public static void cleanExternalCache(Context context) {
98-
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
99-
deleteFilesByDirectory(context.getExternalCacheDir());
100-
}
84+
public static boolean cleanExternalCache(Context context) {
85+
return SDCardUtils.isSDCardEnable() && FileUtils.deleteFilesInDir(context.getExternalCacheDir());
10186
}
10287

10388
/**
104-
* 清除自定义路径下的文件
89+
* 清除自定义目录下的文件
10590
*
106-
* @param dirPath 文件路径
91+
* @param dirPath 目录路径
92+
* @return {@code true}: 清除成功<br>{@code false}: 清除失败
10793
*/
108-
public static void cleanCustomCache(String dirPath) {
109-
if (StringUtils.isSpace(dirPath)) return;
110-
FileUtils.deleteDir(dirPath);
94+
public static boolean cleanCustomCache(String dirPath) {
95+
return FileUtils.deleteFilesInDir(dirPath);
11196
}
11297

11398
/**
114-
* 删除方法 这里只会删除某个文件夹下的文件,如果传入的directory是个文件,将不做处理
99+
* 清除自定义目录下的文件
115100
*
116-
* @param directory 文件夹File对象
101+
* @param dir 目录
102+
* @return {@code true}: 清除成功<br>{@code false}: 清除失败
117103
*/
118-
private static void deleteFilesByDirectory(File directory) {
119-
if (directory != null && directory.exists() && directory.isDirectory()) {
120-
for (File item : directory.listFiles()) {
121-
item.delete();
122-
}
123-
}
104+
public static boolean cleanCustomCache(File dir) {
105+
return FileUtils.deleteFilesInDir(dir);
124106
}
125107
}

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* author: Blankj
1616
* blog : http://blankj.com
1717
* time : 2016/9/28
18-
* desc :
18+
* desc : 判空相关工具类
1919
* </pre>
2020
*/
2121
public class EmptyUtils {
@@ -24,6 +24,12 @@ private EmptyUtils() {
2424
throw new UnsupportedOperationException("u can't instantiate me...");
2525
}
2626

27+
/**
28+
* 判断对象是否为空
29+
*
30+
* @param obj 对象
31+
* @return {@code true}: 为空<br>{@code false}: 不为空
32+
*/
2733
public static boolean isEmpty(Object obj) {
2834
if (obj == null) {
2935
return true;
@@ -49,7 +55,35 @@ public static boolean isEmpty(Object obj) {
4955
return false;
5056
}
5157

58+
/**
59+
* 判断对象是否非空
60+
* <p>不要问我为什么不直接调用{@code !isEmpty(obj);}</p>
61+
*
62+
* @param obj 对象
63+
* @return {@code true}: 非空<br>{@code false}: 空
64+
*/
5265
public static boolean isNotEmpty(Object obj) {
53-
return !isEmpty(obj);
66+
if (obj == null) {
67+
return false;
68+
} else if (obj instanceof String && obj.toString().length() == 0) {
69+
return false;
70+
} else if (obj.getClass().isArray() && Array.getLength(obj) == 0) {
71+
return false;
72+
} else if (obj instanceof Collection && ((Collection) obj).isEmpty()) {
73+
return false;
74+
} else if (obj instanceof Map && ((Map) obj).isEmpty()) {
75+
return false;
76+
} else if (obj instanceof SparseArray && ((SparseArray) obj).size() == 0) {
77+
return false;
78+
} else if (obj instanceof SparseBooleanArray && ((SparseBooleanArray) obj).size() == 0) {
79+
return false;
80+
} else if (obj instanceof SparseIntArray && ((SparseIntArray) obj).size() == 0) {
81+
return false;
82+
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
83+
if (obj instanceof SparseLongArray && ((SparseLongArray) obj).size() == 0) {
84+
return false;
85+
}
86+
}
87+
return true;
5488
}
5589
}

0 commit comments

Comments
 (0)