Skip to content

Commit e3216a7

Browse files
committed
Merge remote-tracking branch 'origin/master' into okio_1.13.0
2 parents 0f0af43 + 1294373 commit e3216a7

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package com.opensource.svgaplayer
22

33
import android.app.Activity
44
import android.content.Context
5+
import android.net.http.HttpResponseCache
56
import android.os.Handler
7+
import android.util.Log
68
import com.opensource.svgaplayer.proto.MovieEntity
79

810
import org.json.JSONObject
@@ -31,9 +33,15 @@ class SVGAParser(private val context: Context) {
3133

3234
open class FileDownloader {
3335

36+
var noCache = false
37+
3438
open fun resume(url: URL, complete: (inputStream: InputStream) -> Unit, failure: (e: Exception) -> Unit) {
3539
Thread({
3640
try {
41+
if (HttpResponseCache.getInstalled() == null && !noCache) {
42+
Log.e("SVGAParser", "SVGAParser can not handle cache before install HttpResponseCache. see https://github.com/yyued/SVGAPlayer-Android#cache")
43+
Log.e("SVGAParser", "在配置 HttpResponseCache 前 SVGAParser 无法缓存. 查看 https://github.com/yyued/SVGAPlayer-Android#cache ")
44+
}
3745
(url.openConnection() as? HttpURLConnection)?.let {
3846
it.connectTimeout = 20 * 1000
3947
it.requestMethod = "GET"
@@ -121,9 +129,9 @@ class SVGAParser(private val context: Context) {
121129
})
122130
try {
123131
val cacheDir = File(context.cacheDir.absolutePath + "/" + cacheKey + "/")
124-
File(cacheDir, "movie.binary")?.takeIf { it.isFile }?.let { binaryFile ->
132+
File(cacheDir, "movie.binary").takeIf { it.isFile }?.let { binaryFile ->
125133
try {
126-
FileInputStream(binaryFile)?.let {
134+
FileInputStream(binaryFile).let {
127135
val videoItem = SVGAVideoEntity(MovieEntity.ADAPTER.decode(it), cacheDir)
128136
it.close()
129137
return videoItem
@@ -134,9 +142,9 @@ class SVGAParser(private val context: Context) {
134142
throw e
135143
}
136144
}
137-
File(cacheDir, "movie.spec")?.takeIf { it.isFile }?.let { jsonFile ->
145+
File(cacheDir, "movie.spec").takeIf { it.isFile }?.let { jsonFile ->
138146
try {
139-
FileInputStream(jsonFile)?.let { fileInputStream ->
147+
FileInputStream(jsonFile).let { fileInputStream ->
140148
val byteArrayOutputStream = ByteArrayOutputStream()
141149
val buffer = ByteArray(2048)
142150
while (true) {
@@ -146,8 +154,8 @@ class SVGAParser(private val context: Context) {
146154
}
147155
byteArrayOutputStream.write(buffer, 0, size)
148156
}
149-
byteArrayOutputStream.toString()?.let {
150-
JSONObject(it)?.let {
157+
byteArrayOutputStream.toString().let {
158+
JSONObject(it).let {
151159
fileInputStream.close()
152160
return SVGAVideoEntity(it, cacheDir)
153161
}
@@ -178,9 +186,9 @@ class SVGAParser(private val context: Context) {
178186
private fun parseWithCacheKey(cacheKey: String): SVGAVideoEntity? {
179187
try {
180188
val cacheDir = File(context.cacheDir.absolutePath + "/" + cacheKey + "/")
181-
File(cacheDir, "movie.binary")?.takeIf { it.isFile }?.let { binaryFile ->
189+
File(cacheDir, "movie.binary").takeIf { it.isFile }?.let { binaryFile ->
182190
try {
183-
FileInputStream(binaryFile)?.let {
191+
FileInputStream(binaryFile).let {
184192
val videoItem = SVGAVideoEntity(MovieEntity.ADAPTER.decode(it), cacheDir)
185193
it.close()
186194
return videoItem
@@ -191,9 +199,9 @@ class SVGAParser(private val context: Context) {
191199
throw e
192200
}
193201
}
194-
File(cacheDir, "movie.spec")?.takeIf { it.isFile }?.let { jsonFile ->
202+
File(cacheDir, "movie.spec").takeIf { it.isFile }?.let { jsonFile ->
195203
try {
196-
FileInputStream(jsonFile)?.let { fileInputStream ->
204+
FileInputStream(jsonFile).let { fileInputStream ->
197205
val byteArrayOutputStream = ByteArrayOutputStream()
198206
val buffer = ByteArray(2048)
199207
while (true) {
@@ -203,8 +211,8 @@ class SVGAParser(private val context: Context) {
203211
}
204212
byteArrayOutputStream.write(buffer, 0, size)
205213
}
206-
byteArrayOutputStream.toString()?.let {
207-
JSONObject(it)?.let {
214+
byteArrayOutputStream.toString().let {
215+
JSONObject(it).let {
208216
fileInputStream.close()
209217
return SVGAVideoEntity(it, cacheDir)
210218
}
@@ -226,20 +234,16 @@ class SVGAParser(private val context: Context) {
226234
val messageDigest = MessageDigest.getInstance("MD5")
227235
messageDigest.update(str.toByteArray(charset("UTF-8")))
228236
val digest = messageDigest.digest()
229-
val sb = StringBuffer()
237+
var sb = ""
230238
for (b in digest) {
231-
sb.append(String.format("%02x", b))
239+
sb += String.format("%02x", b)
232240
}
233-
return sb.toString()
241+
return sb
234242
}
235243

236-
private fun cacheKey(url: URL): String {
237-
return cacheKey(url.toString())
238-
}
244+
private fun cacheKey(url: URL): String = cacheKey(url.toString())
239245

240-
private fun cacheDir(cacheKey: String): File {
241-
return File(context.cacheDir.absolutePath + "/" + cacheKey + "/")
242-
}
246+
private fun cacheDir(cacheKey: String): File = File(context.cacheDir.absolutePath + "/" + cacheKey + "/")
243247

244248
private fun readAsBytes(inputStream: InputStream): ByteArray {
245249
val byteArrayOutputStream = ByteArrayOutputStream()

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ class SVGAVideoEntity {
5353
FPS = movieParams.fps ?: 20
5454
frames = movieParams.frames ?: 0
5555
}
56-
resetImages(obj)
56+
try {
57+
resetImages(obj)
58+
} catch (e: Exception) {
59+
e.printStackTrace()
60+
}
5761
resetSprites(obj)
5862
}
5963

0 commit comments

Comments
 (0)