1
1
package com.opensource.svgaplayer
2
2
3
3
import android.content.Context
4
+ import com.opensource.svgaplayer.utils.log.LogUtils
4
5
import java.io.File
5
6
import java.net.URL
6
7
import java.security.MessageDigest
7
8
8
-
9
+ /* *
10
+ * SVGA 缓存管理
11
+ */
9
12
object SVGACache {
10
13
enum class Type {
11
14
DEFAULT ,
12
15
FILE
13
16
}
14
17
18
+ private const val TAG = " SVGACache"
15
19
private var type: Type = Type .DEFAULT
16
20
private var cacheDir: String = " /"
21
+ get() {
22
+ if (field != " /" ) {
23
+ val dir = File (field)
24
+ if (! dir.exists()) {
25
+ dir.mkdirs()
26
+ }
27
+ }
28
+ return field
29
+ }
30
+
17
31
18
32
fun onCreate (context : Context ? ) {
19
33
onCreate(context, Type .DEFAULT )
@@ -27,22 +41,54 @@ object SVGACache {
27
41
this .type = type
28
42
}
29
43
30
- // fun clearCache(context: Context?){
31
- // context ?: return
32
- // cacheDir = "${context.cacheDir.absolutePath}/svga/"
33
- // File(cacheDir).takeIf { it.exists() }?.delete()
34
- // }
44
+ /* *
45
+ * 清理缓存
46
+ */
47
+ fun clearCache () {
48
+ if (! isInitialized()) {
49
+ LogUtils .error(TAG , " SVGACache is not init!" )
50
+ return
51
+ }
52
+ SVGAParser .threadPoolExecutor.execute {
53
+ clearDir(cacheDir)
54
+ LogUtils .info(TAG , " Clear svga cache done!" )
55
+ }
56
+ }
57
+
58
+ // 清除目录下的所有文件
59
+ private fun clearDir (path : String ) {
60
+ try {
61
+ val dir = File (path)
62
+ dir.takeIf { it.exists() }?.let { parentDir ->
63
+ parentDir.listFiles()?.forEach { file ->
64
+ if (! file.exists()) {
65
+ return @forEach
66
+ }
67
+ if (file.isDirectory) {
68
+ clearDir(file.absolutePath)
69
+ }
70
+ file.delete()
71
+ }
72
+ }
73
+ } catch (e: Exception ) {
74
+ LogUtils .error(TAG , " Clear svga cache path: $path fail" , e)
75
+ }
76
+ }
35
77
36
78
fun isInitialized (): Boolean {
37
- return " /" != cacheDir&& File (cacheDir).exists()
79
+ return " /" != cacheDir && File (cacheDir).exists()
38
80
}
39
81
40
82
fun isDefaultCache (): Boolean = type == Type .DEFAULT
41
83
42
84
fun isCached (cacheKey : String ): Boolean {
43
- return (if (isDefaultCache()) buildCacheDir(cacheKey) else buildSvgaFile(
44
- cacheKey
45
- )).exists()
85
+ return if (isDefaultCache()) {
86
+ buildCacheDir(cacheKey)
87
+ } else {
88
+ buildSvgaFile(
89
+ cacheKey
90
+ )
91
+ }.exists()
46
92
}
47
93
48
94
fun buildCacheKey (str : String ): String {
0 commit comments