Skip to content

Commit 9e7e1fc

Browse files
committed
add sync lock.
1 parent 55f5ca7 commit 9e7e1fc

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ class SVGAParser(val context: Context) {
3030

3131
}
3232

33+
companion object {
34+
35+
private var sharedLock: Int = 0
36+
37+
}
38+
3339
fun parse(assetsName: String): SVGAVideoEntity? {
3440
try {
3541
context.assets.open(assetsName)?.let {
@@ -104,6 +110,20 @@ class SVGAParser(val context: Context) {
104110
return null
105111
}
106112

113+
fun parse(inputStream: InputStream?, cacheKey: String, callback: ParseCompletion) {
114+
synchronized(sharedLock, {
115+
val videoItem = parse(inputStream, cacheKey)
116+
Thread({
117+
if (videoItem != null) {
118+
callback.onComplete(videoItem)
119+
}
120+
else {
121+
callback.onError()
122+
}
123+
}).start()
124+
})
125+
}
126+
107127
private fun cacheKey(str: String): String {
108128
val messageDigest = MessageDigest.getInstance("MD5")
109129
messageDigest.update(str.toByteArray(charset("UTF-8")))

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ class SVGAVideoEntity(obj: JSONObject, val cacheDir: File) {
5353
it.keys().forEach {
5454
val imageKey = it
5555
var filePath = cacheDir.absolutePath + "/" + imgObjects[imageKey]
56-
var bitmap = BitmapFactory.decodeFile(filePath)
56+
var bitmap = if (File(filePath).exists()) BitmapFactory.decodeFile(filePath) else null
5757
if (bitmap != null) {
5858
images.put(imageKey, bitmap)
5959
}
6060
else {
6161
filePath = cacheDir.absolutePath + "/" + imageKey + ".png"
62-
bitmap = BitmapFactory.decodeFile(filePath)
62+
bitmap = if (File(filePath).exists()) BitmapFactory.decodeFile(filePath) else null
6363
if (bitmap != null) {
6464
images.put(imageKey, bitmap)
6565
}

0 commit comments

Comments
 (0)