/
CleanUtils.java
104 lines (91 loc) · 3.09 KB
/
CleanUtils.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
package com.blankj.utilcode.util;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import java.io.File;
import androidx.annotation.RequiresApi;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2016/09/27
* desc : utils about clean
* </pre>
*/
public final class CleanUtils {
private CleanUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Clean the internal cache.
* <p>directory: /data/data/package/cache</p>
*
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean cleanInternalCache() {
return UtilsBridge.deleteAllInDir(Utils.getApp().getCacheDir());
}
/**
* Clean the internal files.
* <p>directory: /data/data/package/files</p>
*
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean cleanInternalFiles() {
return UtilsBridge.deleteAllInDir(Utils.getApp().getFilesDir());
}
/**
* Clean the internal databases.
* <p>directory: /data/data/package/databases</p>
*
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean cleanInternalDbs() {
return UtilsBridge.deleteAllInDir(new File(Utils.getApp().getFilesDir().getParent(), "databases"));
}
/**
* Clean the internal database by name.
* <p>directory: /data/data/package/databases/dbName</p>
*
* @param dbName The name of database.
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean cleanInternalDbByName(final String dbName) {
return Utils.getApp().deleteDatabase(dbName);
}
/**
* Clean the internal shared preferences.
* <p>directory: /data/data/package/shared_prefs</p>
*
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean cleanInternalSp() {
return UtilsBridge.deleteAllInDir(new File(Utils.getApp().getFilesDir().getParent(), "shared_prefs"));
}
/**
* Clean the external cache.
* <p>directory: /storage/emulated/0/android/data/package/cache</p>
*
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean cleanExternalCache() {
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
&& UtilsBridge.deleteAllInDir(Utils.getApp().getExternalCacheDir());
}
/**
* Clean the custom directory.
*
* @param dirPath The path of directory.
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean cleanCustomDir(final String dirPath) {
return UtilsBridge.deleteAllInDir(UtilsBridge.getFileByPath(dirPath));
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static void cleanAppUserData() {
ActivityManager am = (ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
//noinspection ConstantConditions
am.clearApplicationUserData();
}
}