Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit 2d731df

Browse files
committed
feat: SVGACache 开放清理缓存方法
1 parent a4cc84e commit 2d731df

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

library/src/main/java/com/opensource/svgaplayer/SVGACache.kt

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package com.opensource.svgaplayer
22

33
import android.content.Context
4+
import com.opensource.svgaplayer.utils.log.LogUtils
45
import java.io.File
56
import java.net.URL
67
import java.security.MessageDigest
78

8-
9+
/**
10+
* SVGA 缓存管理
11+
*/
912
object SVGACache {
13+
14+
private const val TAG = "SVGACache"
15+
1016
enum class Type {
1117
DEFAULT,
1218
FILE
@@ -36,11 +42,39 @@ object SVGACache {
3642
this.type = type
3743
}
3844

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+
}
4478

4579
fun isInitialized(): Boolean {
4680
return "/" != cacheDir
@@ -49,9 +83,11 @@ object SVGACache {
4983
fun isDefaultCache(): Boolean = type == Type.DEFAULT
5084

5185
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()
5591
}
5692

5793
fun buildCacheKey(str: String): String {

0 commit comments

Comments
 (0)