Skip to content

Commit 1442c65

Browse files
committed
feat: When SVGAParser is in the parsing process, it outputs the file name or url, which is convenient for troubleshooting file problems
1 parent 2d731df commit 1442c65

File tree

1 file changed

+76
-53
lines changed

1 file changed

+76
-53
lines changed

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

Lines changed: 76 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ class SVGAParser(context: Context?) {
5656
}
5757
threadPoolExecutor.execute {
5858
try {
59-
LogUtils.info(TAG, "================ svga file download start ================")
59+
LogUtils.info(
60+
TAG,
61+
"================ svga file: $url download start ================"
62+
)
6063
if (HttpResponseCache.getInstalled() == null && !noCache) {
6164
LogUtils.error(
6265
TAG,
@@ -80,7 +83,7 @@ class SVGAParser(context: Context?) {
8083
if (cancelled) {
8184
LogUtils.warn(
8285
TAG,
83-
"================ svga file download canceled ================"
86+
"================ svga file: $url download canceled ================"
8487
)
8588
break
8689
}
@@ -93,24 +96,21 @@ class SVGAParser(context: Context?) {
9396
if (cancelled) {
9497
LogUtils.warn(
9598
TAG,
96-
"================ svga file download canceled ================"
99+
"================ svga file: $url download canceled ================"
97100
)
98101
return@execute
99102
}
100103
ByteArrayInputStream(outputStream.toByteArray()).use {
101104
LogUtils.info(
102105
TAG,
103-
"================ svga file download complete ================"
106+
"================ svga file: $url download complete ================"
104107
)
105108
complete(it)
106109
}
107110
}
108111
}
109112
}
110113
} catch (e: Exception) {
111-
LogUtils.error(TAG, "================ svga file download fail ================")
112-
LogUtils.error(TAG, "error: ${e.message}")
113-
e.printStackTrace()
114114
failure(e)
115115
}
116116
}
@@ -154,19 +154,20 @@ class SVGAParser(context: Context?) {
154154
LogUtils.error(TAG, "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
155155
return
156156
}
157-
LogUtils.info(TAG, "================ decode from assets ================")
157+
LogUtils.info(TAG, "================ decode $name from assets ================")
158158
threadPoolExecutor.execute {
159159
try {
160160
mContext?.assets?.open(name)?.let {
161161
this.decodeFromInputStream(
162162
it,
163163
SVGACache.buildCacheKey("file:///assets/$name"),
164164
callback,
165-
true
165+
true,
166+
alias = name
166167
)
167168
}
168169
} catch (e: java.lang.Exception) {
169-
this.invokeErrorCallback(e, callback)
170+
this.invokeErrorCallback(e, callback, name)
170171
}
171172
}
172173
}
@@ -176,39 +177,44 @@ class SVGAParser(context: Context?) {
176177
LogUtils.error(TAG, "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
177178
return null
178179
}
179-
LogUtils.info(TAG, "================ decode from url ================")
180+
val urlPath = url.toString()
181+
LogUtils.info(TAG, "================ decode $urlPath from url ================")
180182
val cacheKey = SVGACache.buildCacheKey(url);
181183
return if (SVGACache.isCached(cacheKey)) {
182184
LogUtils.info(TAG, "this url cached")
183185
threadPoolExecutor.execute {
184186
if (SVGACache.isDefaultCache()) {
185-
this.decodeFromCacheKey(cacheKey, callback)
187+
this.decodeFromCacheKey(cacheKey, callback, alias = urlPath)
186188
} else {
187-
this._decodeFromCacheKey(cacheKey, callback)
189+
this._decodeFromCacheKey(cacheKey, callback, alias = urlPath)
188190
}
189191
}
190192
return null
191193
} else {
192194
LogUtils.info(TAG, "no cached, prepare to download")
193195
fileDownloader.resume(url, {
194196
if (SVGACache.isDefaultCache()) {
195-
this.decodeFromInputStream(it, cacheKey, callback)
197+
this.decodeFromInputStream(it, cacheKey, callback, alias = urlPath)
196198
} else {
197-
this._decodeFromInputStream(it, cacheKey, callback)
199+
this._decodeFromInputStream(it, cacheKey, callback, alias = urlPath)
198200
}
199201
}, {
200-
this.invokeErrorCallback(it, callback)
202+
LogUtils.error(
203+
TAG,
204+
"================ svga file: $url download fail ================"
205+
)
206+
this.invokeErrorCallback(it, callback, alias = urlPath)
201207
})
202208
}
203209
}
204210

205211
/**
206212
* 读取解析本地缓存的svga文件.
207213
*/
208-
fun _decodeFromCacheKey(cacheKey: String, callback: ParseCompletion?) {
214+
fun _decodeFromCacheKey(cacheKey: String, callback: ParseCompletion?, alias: String) {
209215
val svga = SVGACache.buildSvgaFile(cacheKey)
210216
try {
211-
LogUtils.info(TAG, "cache.binary change to entity")
217+
LogUtils.info(TAG, "$alias cache.binary change to entity")
212218
FileInputStream(svga).use { inputStream ->
213219
try {
214220
readAsBytes(inputStream)?.let { bytes ->
@@ -223,28 +229,29 @@ class SVGAParser(context: Context?) {
223229
)
224230
videoItem.prepare {
225231
LogUtils.info(TAG, "cache.prepare success")
226-
this.invokeCompleteCallback(videoItem, callback)
232+
this.invokeCompleteCallback(videoItem, callback, alias)
227233
}
228-
} ?: doError("cache.inflate(bytes) cause exception", callback)
229-
} ?: doError("cache.readAsBytes(inputStream) cause exception", callback)
234+
} ?: doError("cache.inflate(bytes) cause exception", callback, alias)
235+
} ?: doError("cache.readAsBytes(inputStream) cause exception", callback, alias)
230236
} catch (e: Exception) {
231-
this.invokeErrorCallback(e, callback)
237+
this.invokeErrorCallback(e, callback, alias)
232238
} finally {
233239
inputStream.close()
234240
}
235241
}
236242
} catch (e: Exception) {
237-
LogUtils.error(TAG, "cache.binary change to entity fail", e)
243+
LogUtils.error(TAG, "$alias cache.binary change to entity fail", e)
238244
svga.takeIf { it.exists() }?.delete()
239-
this.invokeErrorCallback(e, callback)
245+
this.invokeErrorCallback(e, callback, alias)
240246
}
241247
}
242248

243-
fun doError(error: String, callback: ParseCompletion?) {
244-
LogUtils.info(TAG, error)
249+
fun doError(error: String, callback: ParseCompletion?, alias: String) {
250+
LogUtils.error(TAG, error)
245251
this.invokeErrorCallback(
246252
Exception(error),
247-
callback
253+
callback,
254+
alias
248255
)
249256
}
250257

@@ -254,11 +261,12 @@ class SVGAParser(context: Context?) {
254261
fun _decodeFromInputStream(
255262
inputStream: InputStream,
256263
cacheKey: String,
257-
callback: ParseCompletion?
264+
callback: ParseCompletion?,
265+
alias: String
258266
) {
259267
threadPoolExecutor.execute {
260268
try {
261-
LogUtils.info(TAG, "Input.binary change to entity")
269+
LogUtils.info(TAG, "$alias input.binary change to entity")
262270
readAsBytes(inputStream)?.let { bytes ->
263271
threadPoolExecutor.execute {
264272
SVGACache.buildSvgaFile(cacheKey).let { cacheFile ->
@@ -271,7 +279,7 @@ class SVGAParser(context: Context?) {
271279
}
272280
}
273281
}
274-
LogUtils.info(TAG, "Input.inflate start")
282+
LogUtils.info(TAG, "input.inflate start")
275283
inflate(bytes)?.let { inflateBytes ->
276284
LogUtils.info(TAG, "Input.inflate success")
277285
val videoItem = SVGAVideoEntity(
@@ -286,13 +294,13 @@ class SVGAParser(context: Context?) {
286294
// 在ImageView添加clearsAfterDetached,用于控制imageview在onDetach的时候是否要自动调用clear.
287295
// 以暂时缓解需要为RecyclerView缓存drawable或者entity的人士.用完记得调用clear()
288296
videoItem.prepare {
289-
LogUtils.info(TAG, "Input.prepare success")
290-
this.invokeCompleteCallback(videoItem, callback)
297+
LogUtils.info(TAG, "input.prepare success")
298+
this.invokeCompleteCallback(videoItem, callback, alias)
291299
}
292-
} ?: doError("Input.inflate(bytes) cause exception", callback)
293-
} ?: doError("Input.readAsBytes(inputStream) cause exception", callback)
300+
} ?: doError("input.inflate(bytes) cause exception", callback, alias)
301+
} ?: doError("input.readAsBytes(inputStream) cause exception", callback, alias)
294302
} catch (e: Exception) {
295-
this.invokeErrorCallback(e, callback)
303+
this.invokeErrorCallback(e, callback, alias)
296304
} finally {
297305
inputStream.close()
298306
}
@@ -303,13 +311,14 @@ class SVGAParser(context: Context?) {
303311
inputStream: InputStream,
304312
cacheKey: String,
305313
callback: ParseCompletion?,
306-
closeInputStream: Boolean = false
314+
closeInputStream: Boolean = false,
315+
alias: String
307316
) {
308317
if (mContext == null) {
309318
LogUtils.error(TAG, "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
310319
return
311320
}
312-
LogUtils.info(TAG, "================ decode from input stream ================")
321+
LogUtils.info(TAG, "================ decode $alias from input stream ================")
313322
threadPoolExecutor.execute {
314323
try {
315324
readAsBytes(inputStream)?.let { bytes ->
@@ -328,7 +337,7 @@ class SVGAParser(context: Context?) {
328337
}
329338
}
330339
}
331-
this.decodeFromCacheKey(cacheKey, callback)
340+
this.decodeFromCacheKey(cacheKey, callback, alias)
332341
} else {
333342
LogUtils.info(TAG, "decode from input stream, inflate start")
334343
inflate(bytes)?.let {
@@ -340,20 +349,22 @@ class SVGAParser(context: Context?) {
340349
)
341350
videoItem.prepare {
342351
LogUtils.info(TAG, "decode from input stream, inflate end")
343-
this.invokeCompleteCallback(videoItem, callback)
352+
this.invokeCompleteCallback(videoItem, callback, alias)
344353
}
345354

346355
} ?: this.invokeErrorCallback(
347356
Exception("inflate(bytes) cause exception"),
348-
callback
357+
callback,
358+
alias
349359
)
350360
}
351361
} ?: this.invokeErrorCallback(
352362
Exception("readAsBytes(inputStream) cause exception"),
353-
callback
363+
callback,
364+
alias
354365
)
355366
} catch (e: java.lang.Exception) {
356-
this.invokeErrorCallback(e, callback)
367+
this.invokeErrorCallback(e, callback, alias)
357368
} finally {
358369
if (closeInputStream) {
359370
inputStream.close()
@@ -397,27 +408,35 @@ class SVGAParser(context: Context?) {
397408
callback: ParseCompletion?,
398409
closeInputStream: Boolean = false
399410
) {
400-
this.decodeFromInputStream(inputStream, cacheKey, callback, closeInputStream)
411+
this.decodeFromInputStream(inputStream, cacheKey, callback, closeInputStream, "")
401412
}
402413

403-
private fun invokeCompleteCallback(videoItem: SVGAVideoEntity, callback: ParseCompletion?) {
414+
private fun invokeCompleteCallback(
415+
videoItem: SVGAVideoEntity,
416+
callback: ParseCompletion?,
417+
alias: String
418+
) {
404419
Handler(Looper.getMainLooper()).post {
405-
LogUtils.info(TAG, "================ parser complete ================")
420+
LogUtils.info(TAG, "================ $alias parser complete ================")
406421
callback?.onComplete(videoItem)
407422
}
408423
}
409424

410-
private fun invokeErrorCallback(e: java.lang.Exception, callback: ParseCompletion?) {
425+
private fun invokeErrorCallback(
426+
e: java.lang.Exception,
427+
callback: ParseCompletion?,
428+
alias: String
429+
) {
411430
e.printStackTrace()
412-
LogUtils.error(TAG, "================ parser error ================")
413-
LogUtils.error(TAG, "error", e)
431+
LogUtils.error(TAG, "================ $alias parser error ================")
432+
LogUtils.error(TAG, "$alias parser error", e)
414433
Handler(Looper.getMainLooper()).post {
415434
callback?.onError()
416435
}
417436
}
418437

419-
private fun decodeFromCacheKey(cacheKey: String, callback: ParseCompletion?) {
420-
LogUtils.info(TAG, "================ decode from cache ================")
438+
private fun decodeFromCacheKey(cacheKey: String, callback: ParseCompletion?, alias: String) {
439+
LogUtils.info(TAG, "================ decode $alias from cache ================")
421440
LogUtils.debug(TAG, "decodeFromCacheKey called with cacheKey : $cacheKey")
422441
if (mContext == null) {
423442
LogUtils.error(TAG, "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
@@ -436,7 +455,9 @@ class SVGAParser(context: Context?) {
436455
cacheDir,
437456
mFrameWidth,
438457
mFrameHeight
439-
), callback
458+
),
459+
callback,
460+
alias
440461
)
441462
}
442463
} catch (e: Exception) {
@@ -468,7 +489,9 @@ class SVGAParser(context: Context?) {
468489
cacheDir,
469490
mFrameWidth,
470491
mFrameHeight
471-
), callback
492+
),
493+
callback,
494+
alias
472495
)
473496
}
474497
}
@@ -482,7 +505,7 @@ class SVGAParser(context: Context?) {
482505
}
483506
}
484507
} catch (e: Exception) {
485-
this.invokeErrorCallback(e, callback)
508+
this.invokeErrorCallback(e, callback, alias)
486509
}
487510
}
488511

0 commit comments

Comments
 (0)