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

issue 问题修复 #365

Merged
merged 4 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 45 additions & 9 deletions library/src/main/java/com/opensource/svgaplayer/SVGACache.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.opensource.svgaplayer

import android.content.Context
import com.opensource.svgaplayer.utils.log.LogUtils
import java.io.File
import java.net.URL
import java.security.MessageDigest


/**
* SVGA 缓存管理
*/
object SVGACache {

private const val TAG = "SVGACache"

enum class Type {
DEFAULT,
FILE
Expand Down Expand Up @@ -36,11 +42,39 @@ object SVGACache {
this.type = type
}

// fun clearCache(context: Context?){
// context ?: return
// cacheDir = "${context.cacheDir.absolutePath}/svga/"
// File(cacheDir).takeIf { it.exists() }?.delete()
// }
/**
* 清理缓存
*/
fun clearCache() {
if (!isInitialized()) {
LogUtils.error(TAG, "SVGACache is not init!")
return
}
SVGAParser.threadPoolExecutor.execute {
clearDir(cacheDir)
LogUtils.info(TAG, "Clear svga cache done!")
}
}

// 清除目录下的所有文件
private fun clearDir(path: String) {
try {
val dir = File(path)
dir.takeIf { it.exists() }?.let { parentDir ->
parentDir.listFiles()?.forEach { file ->
if (!file.exists()) {
return@forEach
}
if (file.isDirectory) {
clearDir(file.absolutePath)
}
file.delete()
}
}
} catch (e: Exception) {
LogUtils.error(TAG, "Clear svga cache path: $path fail", e)
}
}

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

fun isCached(cacheKey: String): Boolean {
return (if (isDefaultCache()) buildCacheDir(cacheKey) else buildSvgaFile(
cacheKey
)).exists()
return (if (isDefaultCache()) {
buildCacheDir(cacheKey)
} else {
buildSvgaFile(cacheKey)
}).exists()
}

fun buildCacheKey(str: String): String {
Expand Down
Loading