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 {
13
+
14
+ private const val TAG = " SVGACache"
15
+
10
16
enum class Type {
11
17
DEFAULT ,
12
18
FILE
@@ -36,11 +42,39 @@ object SVGACache {
36
42
this .type = type
37
43
}
38
44
39
- // fun clearCache(context: Context?){
40
- // context ?: return
41
- // cacheDir = "${context.cacheDir.absolutePath}/svga/"
42
- // File(cacheDir).takeIf { it.exists() }?.delete()
43
- // }
45
+ /* *
46
+ * 清理缓存
47
+ */
48
+ fun clearCache () {
49
+ if (! isInitialized()) {
50
+ LogUtils .error(TAG , " SVGACache is not init!" )
51
+ return
52
+ }
53
+ SVGAParser .threadPoolExecutor.execute {
54
+ clearDir(cacheDir)
55
+ LogUtils .info(TAG , " Clear svga cache done!" )
56
+ }
57
+ }
58
+
59
+ // 清除目录下的所有文件
60
+ private fun clearDir (path : String ) {
61
+ try {
62
+ val dir = File (path)
63
+ dir.takeIf { it.exists() }?.let { parentDir ->
64
+ parentDir.listFiles()?.forEach { file ->
65
+ if (! file.exists()) {
66
+ return @forEach
67
+ }
68
+ if (file.isDirectory) {
69
+ clearDir(file.absolutePath)
70
+ }
71
+ file.delete()
72
+ }
73
+ }
74
+ } catch (e: Exception ) {
75
+ LogUtils .error(TAG , " Clear svga cache path: $path fail" , e)
76
+ }
77
+ }
44
78
45
79
fun isInitialized (): Boolean {
46
80
return " /" != cacheDir
@@ -49,9 +83,11 @@ object SVGACache {
49
83
fun isDefaultCache (): Boolean = type == Type .DEFAULT
50
84
51
85
fun isCached (cacheKey : String ): Boolean {
52
- return (if (isDefaultCache()) buildCacheDir(cacheKey) else buildSvgaFile(
53
- cacheKey
54
- )).exists()
86
+ return (if (isDefaultCache()) {
87
+ buildCacheDir(cacheKey)
88
+ } else {
89
+ buildSvgaFile(cacheKey)
90
+ }).exists()
55
91
}
56
92
57
93
fun buildCacheKey (str : String ): String {
0 commit comments