Skip to content

Commit 407c1af

Browse files
committed
async parse always on child thread, and callback always on UI thread while context as Activity.
1 parent 9e7e1fc commit 407c1af

File tree

1 file changed

+62
-25
lines changed

1 file changed

+62
-25
lines changed

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

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@ import android.app.Activity
44
import android.content.Context
55

66
import org.json.JSONObject
7+
import java.io.*
78

8-
import java.io.BufferedInputStream
9-
import java.io.ByteArrayOutputStream
10-
import java.io.File
11-
import java.io.FileInputStream
12-
import java.io.FileOutputStream
13-
import java.io.InputStream
149
import java.net.HttpURLConnection
1510
import java.net.URL
1611
import java.security.MessageDigest
@@ -45,6 +40,14 @@ class SVGAParser(val context: Context) {
4540
return null
4641
}
4742

43+
fun parse(assetsName: String, callback: ParseCompletion) {
44+
try {
45+
context.assets.open(assetsName)?.let {
46+
parse(it, cacheKey("file:///assets/" + assetsName), callback)
47+
}
48+
} catch (e: Exception) {}
49+
}
50+
4851
fun parse(url: URL): SVGAVideoEntity? {
4952
try {
5053
if (cacheDir(cacheKey(url)).exists()) {
@@ -63,15 +66,33 @@ class SVGAParser(val context: Context) {
6366
}
6467

6568
fun parse(url: URL, callback: ParseCompletion) {
66-
Thread(Runnable {
67-
parse(url)?.let {
68-
(context as? Activity)?.runOnUiThread {
69-
callback.onComplete(it)
69+
Thread({
70+
val BUFFER_SIZE = 4096
71+
try {
72+
if (cacheDir(cacheKey(url)).exists()) {
73+
parse(null, cacheKey(url), callback)
7074
}
71-
return@Runnable
72-
}
73-
(context as? Activity)?.runOnUiThread {
74-
callback.onError()
75+
else {
76+
(url.openConnection() as? HttpURLConnection)?.let {
77+
it.connectTimeout = 20 * 1000
78+
it.requestMethod = "GET"
79+
it.connect()
80+
val inputStream = it.inputStream
81+
val outputStream = ByteArrayOutputStream()
82+
val buffer = ByteArray(BUFFER_SIZE)
83+
var count: Int
84+
while (true) {
85+
count = inputStream.read(buffer, 0, BUFFER_SIZE)
86+
if (count == -1) {
87+
break
88+
}
89+
outputStream.write(buffer, 0, count)
90+
}
91+
parse(ByteArrayInputStream(outputStream.toByteArray()), cacheKey(url), callback)
92+
}
93+
}
94+
} catch (e: Exception) {
95+
print(e)
7596
}
7697
}).start()
7798
}
@@ -111,17 +132,33 @@ class SVGAParser(val context: Context) {
111132
}
112133

113134
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-
})
135+
Thread({
136+
synchronized(sharedLock, {
137+
val videoItem = parse(inputStream, cacheKey)
138+
Thread({
139+
if (videoItem != null) {
140+
if (context as? Activity != null) {
141+
(context as? Activity)?.runOnUiThread {
142+
callback.onComplete(videoItem)
143+
}
144+
}
145+
else {
146+
callback.onComplete(videoItem)
147+
}
148+
}
149+
else {
150+
if (context as? Activity != null) {
151+
(context as? Activity)?.runOnUiThread {
152+
callback.onError()
153+
}
154+
}
155+
else {
156+
callback.onError()
157+
}
158+
}
159+
}).start()
160+
})
161+
}).start()
125162
}
126163

127164
private fun cacheKey(str: String): String {

0 commit comments

Comments
 (0)