@@ -142,7 +142,7 @@ class SVGAParser(context: Context?) {
142
142
LogUtils .error(TAG , " 在配置 SVGAParser context 前, 无法解析 SVGA 文件。" )
143
143
return
144
144
}
145
- LogUtils .info(TAG , " ================ decode from assets ================" )
145
+ LogUtils .info(TAG , " ================ decode $name from assets ================" )
146
146
threadPoolExecutor.execute {
147
147
try {
148
148
mContext?.assets?.open(name)?.let {
@@ -151,54 +151,82 @@ class SVGAParser(context: Context?) {
151
151
SVGACache .buildCacheKey(" file:///assets/$name " ),
152
152
callback,
153
153
true ,
154
- playCallback
154
+ playCallback,
155
+ alias = name
155
156
)
156
157
}
157
158
} catch (e: Exception ) {
158
- this .invokeErrorCallback(e, callback)
159
+ this .invokeErrorCallback(e, callback, name )
159
160
}
160
161
}
161
162
162
163
}
163
164
164
- fun decodeFromURL (url : URL , callback : ParseCompletion ? ,playCallback : PlayCallback ? =null): (() -> Unit )? {
165
+ fun decodeFromURL (
166
+ url : URL ,
167
+ callback : ParseCompletion ? ,
168
+ playCallback : PlayCallback ? = null
169
+ ): (() -> Unit )? {
165
170
if (mContext == null ) {
166
171
LogUtils .error(TAG , " 在配置 SVGAParser context 前, 无法解析 SVGA 文件。" )
167
172
return null
168
173
}
169
- LogUtils .info(TAG , " ================ decode from url ================" )
174
+ val urlPath = url.toString()
175
+ LogUtils .info(TAG , " ================ decode from url: $urlPath ================" )
170
176
val cacheKey = SVGACache .buildCacheKey(url);
171
177
return if (SVGACache .isCached(cacheKey)) {
172
178
LogUtils .info(TAG , " this url cached" )
173
179
threadPoolExecutor.execute {
174
180
if (SVGACache .isDefaultCache()) {
175
- this .decodeFromCacheKey(cacheKey, callback)
181
+ this .decodeFromCacheKey(cacheKey, callback, alias = urlPath )
176
182
} else {
177
- this ._decodeFromCacheKey (cacheKey, callback,playCallback)
183
+ this ._decodeFromCacheKey (cacheKey, callback, playCallback, alias = urlPath )
178
184
}
179
185
}
180
186
return null
181
187
} else {
182
188
LogUtils .info(TAG , " no cached, prepare to download" )
183
189
fileDownloader.resume(url, {
184
190
if (SVGACache .isDefaultCache()) {
185
- this .decodeFromInputStream(it, cacheKey, callback,false ,playCallback)
191
+ this .decodeFromInputStream(
192
+ it,
193
+ cacheKey,
194
+ callback,
195
+ false ,
196
+ playCallback,
197
+ alias = urlPath
198
+ )
186
199
} else {
187
- this ._decodeFromInputStream (it, cacheKey, callback,playCallback)
200
+ this ._decodeFromInputStream (
201
+ it,
202
+ cacheKey,
203
+ callback,
204
+ playCallback,
205
+ alias = urlPath
206
+ )
188
207
}
189
208
}, {
190
- this .invokeErrorCallback(it, callback)
209
+ LogUtils .error(
210
+ TAG ,
211
+ " ================ svga file: $url download fail ================"
212
+ )
213
+ this .invokeErrorCallback(it, callback, alias = urlPath)
191
214
})
192
215
}
193
216
}
194
217
195
218
/* *
196
219
* 读取解析本地缓存的svga文件.
197
220
*/
198
- fun _decodeFromCacheKey (cacheKey : String , callback : ParseCompletion ? ,playCallback : PlayCallback ? ) {
221
+ fun _decodeFromCacheKey (
222
+ cacheKey : String ,
223
+ callback : ParseCompletion ? ,
224
+ playCallback : PlayCallback ? ,
225
+ alias : String? = null
226
+ ) {
199
227
val svga = SVGACache .buildSvgaFile(cacheKey)
200
228
try {
201
- LogUtils .info(TAG , " cache.binary change to entity" )
229
+ LogUtils .info(TAG , " $alias cache.binary change to entity" )
202
230
FileInputStream (svga).use { inputStream ->
203
231
try {
204
232
readAsBytes(inputStream)?.let { bytes ->
@@ -213,28 +241,33 @@ class SVGAParser(context: Context?) {
213
241
)
214
242
videoItem.prepare({
215
243
LogUtils .info(TAG , " cache.prepare success" )
216
- this .invokeCompleteCallback(videoItem, callback)
244
+ this .invokeCompleteCallback(videoItem, callback, alias )
217
245
},playCallback)
218
- } ? : doError(" cache.inflate(bytes) cause exception" , callback)
219
- } ? : doError(" cache.readAsBytes(inputStream) cause exception" , callback)
246
+ } ? : doError(" cache.inflate(bytes) cause exception" , callback, alias )
247
+ } ? : doError(" cache.readAsBytes(inputStream) cause exception" , callback, alias )
220
248
} catch (e: Exception ) {
221
- this .invokeErrorCallback(e, callback)
249
+ this .invokeErrorCallback(e, callback, alias )
222
250
} finally {
223
251
inputStream.close()
224
252
}
225
253
}
226
254
} catch (e: Exception ) {
227
- LogUtils .error(TAG , " cache.binary change to entity fail" , e)
255
+ LogUtils .error(TAG , " $alias cache.binary change to entity fail" , e)
228
256
svga.takeIf { it.exists() }?.delete()
229
- this .invokeErrorCallback(e, callback)
257
+ this .invokeErrorCallback(e, callback, alias )
230
258
}
231
259
}
232
260
233
- fun doError (error : String , callback : ParseCompletion ? ) {
261
+ private fun doError (
262
+ error : String ,
263
+ callback : ParseCompletion ? ,
264
+ alias : String?
265
+ ) {
234
266
LogUtils .info(TAG , error)
235
267
this .invokeErrorCallback(
236
268
Exception (error),
237
- callback
269
+ callback,
270
+ alias
238
271
)
239
272
}
240
273
@@ -245,11 +278,12 @@ class SVGAParser(context: Context?) {
245
278
inputStream : InputStream ,
246
279
cacheKey : String ,
247
280
callback : ParseCompletion ? ,
248
- playCallback : PlayCallback ?
281
+ playCallback : PlayCallback ? ,
282
+ alias : String? = null
249
283
) {
250
284
threadPoolExecutor.execute {
251
285
try {
252
- LogUtils .info(TAG , " Input.binary change to entity" )
286
+ LogUtils .info(TAG , " ================ $alias _inputStream change to entity ================ " )
253
287
readAsBytes(inputStream)?.let { bytes ->
254
288
threadPoolExecutor.execute {
255
289
SVGACache .buildSvgaFile(cacheKey).let { cacheFile ->
@@ -262,9 +296,9 @@ class SVGAParser(context: Context?) {
262
296
}
263
297
}
264
298
}
265
- LogUtils .info(TAG , " Input. inflate start" )
299
+ LogUtils .info(TAG , " inputStream inflate start" )
266
300
inflate(bytes)?.let { inflateBytes ->
267
- LogUtils .info(TAG , " Input. inflate success" )
301
+ LogUtils .info(TAG , " inputStream inflate success" )
268
302
val videoItem = SVGAVideoEntity (
269
303
MovieEntity .ADAPTER .decode(inflateBytes),
270
304
File (cacheKey),
@@ -276,16 +310,18 @@ class SVGAParser(context: Context?) {
276
310
// 的svgaimageview处,把解析完的drawable或者entity缓存下来,下次直接播放.用完再调用clear()
277
311
// 在ImageView添加clearsAfterDetached,用于控制imageview在onDetach的时候是否要自动调用clear.
278
312
// 以暂时缓解需要为RecyclerView缓存drawable或者entity的人士.用完记得调用clear()
313
+ LogUtils .info(TAG , " SVGAVideoEntity prepare start" )
279
314
videoItem.prepare({
280
- LogUtils .info(TAG , " cache. prepare success" )
281
- this .invokeCompleteCallback(videoItem, callback)
315
+ LogUtils .info(TAG , " SVGAVideoEntity prepare success" )
316
+ this .invokeCompleteCallback(videoItem, callback, alias )
282
317
}, playCallback)
283
- } ? : doError(" Input. inflate(bytes) cause exception" , callback)
284
- } ? : doError(" Input. readAsBytes(inputStream) cause exception" , callback)
318
+ } ? : doError(" inflate(bytes) cause exception" , callback, alias )
319
+ } ? : doError(" readAsBytes(inputStream) cause exception" , callback, alias )
285
320
} catch (e: Exception ) {
286
- this .invokeErrorCallback(e, callback)
321
+ this .invokeErrorCallback(e, callback, alias )
287
322
} finally {
288
323
inputStream.close()
324
+ LogUtils .info(TAG , " ================ $alias _inputStream change to entity end ================" )
289
325
}
290
326
}
291
327
}
@@ -295,13 +331,14 @@ class SVGAParser(context: Context?) {
295
331
cacheKey : String ,
296
332
callback : ParseCompletion ? ,
297
333
closeInputStream : Boolean = false,
298
- playCallback : PlayCallback ? =null
334
+ playCallback : PlayCallback ? =null,
335
+ alias : String? = null
299
336
) {
300
337
if (mContext == null ) {
301
338
LogUtils .error(TAG , " 在配置 SVGAParser context 前, 无法解析 SVGA 文件。" )
302
339
return
303
340
}
304
- LogUtils .info(TAG , " ================ decode from input stream ================" )
341
+ LogUtils .info(TAG , " ================ decode $alias from input stream ================" )
305
342
threadPoolExecutor.execute {
306
343
try {
307
344
readAsBytes(inputStream)?.let { bytes ->
@@ -320,36 +357,41 @@ class SVGAParser(context: Context?) {
320
357
}
321
358
}
322
359
}
323
- this .decodeFromCacheKey(cacheKey, callback)
360
+ this .decodeFromCacheKey(cacheKey, callback, alias )
324
361
} else {
325
- LogUtils .info(TAG , " decode from input stream, inflate start" )
362
+ LogUtils .info(TAG , " inflate start" )
326
363
inflate(bytes)?.let {
364
+ LogUtils .info(TAG , " inflate complete" )
327
365
val videoItem = SVGAVideoEntity (
328
366
MovieEntity .ADAPTER .decode(it),
329
367
File (cacheKey),
330
368
mFrameWidth,
331
369
mFrameHeight
332
370
)
371
+ LogUtils .info(TAG , " SVGAVideoEntity prepare start" )
333
372
videoItem.prepare({
334
- LogUtils .info(TAG , " cache. prepare success" )
335
- this .invokeCompleteCallback(videoItem, callback)
373
+ LogUtils .info(TAG , " SVGAVideoEntity prepare success" )
374
+ this .invokeCompleteCallback(videoItem, callback, alias )
336
375
},playCallback)
337
376
338
377
} ? : this .invokeErrorCallback(
339
378
Exception (" inflate(bytes) cause exception" ),
340
- callback
379
+ callback,
380
+ alias
341
381
)
342
382
}
343
383
} ? : this .invokeErrorCallback(
344
384
Exception (" readAsBytes(inputStream) cause exception" ),
345
- callback
385
+ callback,
386
+ alias
346
387
)
347
388
} catch (e: java.lang.Exception ) {
348
- this .invokeErrorCallback(e, callback)
389
+ this .invokeErrorCallback(e, callback, alias )
349
390
} finally {
350
391
if (closeInputStream) {
351
392
inputStream.close()
352
393
}
394
+ LogUtils .info(TAG , " ================ decode $alias from input stream end ================" )
353
395
}
354
396
}
355
397
}
@@ -374,28 +416,45 @@ class SVGAParser(context: Context?) {
374
416
* @deprecated from 2.4.0
375
417
*/
376
418
@Deprecated(" This method has been deprecated from 2.4.0." , ReplaceWith (" this.decodeFromInputStream(inputStream, cacheKey, callback, closeInputStream)" ))
377
- fun parse (inputStream : InputStream , cacheKey : String , callback : ParseCompletion ? , closeInputStream : Boolean = false) {
419
+ fun parse (
420
+ inputStream : InputStream ,
421
+ cacheKey : String ,
422
+ callback : ParseCompletion ? ,
423
+ closeInputStream : Boolean = false
424
+ ) {
378
425
this .decodeFromInputStream(inputStream, cacheKey, callback, closeInputStream,null )
379
426
}
380
427
381
- private fun invokeCompleteCallback (videoItem : SVGAVideoEntity , callback : ParseCompletion ? ) {
428
+ private fun invokeCompleteCallback (
429
+ videoItem : SVGAVideoEntity ,
430
+ callback : ParseCompletion ? ,
431
+ alias : String?
432
+ ) {
382
433
Handler (Looper .getMainLooper()).post {
383
- LogUtils .info(TAG , " ================ parser complete ================" )
434
+ LogUtils .info(TAG , " ================ $alias parser complete ================" )
384
435
callback?.onComplete(videoItem)
385
436
}
386
437
}
387
438
388
- private fun invokeErrorCallback (e : java.lang.Exception , callback : ParseCompletion ? ) {
439
+ private fun invokeErrorCallback (
440
+ e : Exception ,
441
+ callback : ParseCompletion ? ,
442
+ alias : String?
443
+ ) {
389
444
e.printStackTrace()
390
- LogUtils .error(TAG , " ================ parser error ================" )
391
- LogUtils .error(TAG , " error" , e)
445
+ LogUtils .error(TAG , " ================ $alias parser error ================" )
446
+ LogUtils .error(TAG , " $alias parse error" , e)
392
447
Handler (Looper .getMainLooper()).post {
393
448
callback?.onError()
394
449
}
395
450
}
396
451
397
- private fun decodeFromCacheKey (cacheKey : String , callback : ParseCompletion ? ) {
398
- LogUtils .info(TAG , " ================ decode from cache ================" )
452
+ private fun decodeFromCacheKey (
453
+ cacheKey : String ,
454
+ callback : ParseCompletion ? ,
455
+ alias : String?
456
+ ) {
457
+ LogUtils .info(TAG , " ================ decode $alias from cache ================" )
399
458
LogUtils .debug(TAG , " decodeFromCacheKey called with cacheKey : $cacheKey " )
400
459
if (mContext == null ) {
401
460
LogUtils .error(TAG , " 在配置 SVGAParser context 前, 无法解析 SVGA 文件。" )
@@ -408,7 +467,16 @@ class SVGAParser(context: Context?) {
408
467
LogUtils .info(TAG , " binary change to entity" )
409
468
FileInputStream (binaryFile).use {
410
469
LogUtils .info(TAG , " binary change to entity success" )
411
- this .invokeCompleteCallback(SVGAVideoEntity (MovieEntity .ADAPTER .decode(it), cacheDir, mFrameWidth, mFrameHeight), callback)
470
+ this .invokeCompleteCallback(
471
+ SVGAVideoEntity (
472
+ MovieEntity .ADAPTER .decode(it),
473
+ cacheDir,
474
+ mFrameWidth,
475
+ mFrameHeight
476
+ ),
477
+ callback,
478
+ alias
479
+ )
412
480
}
413
481
414
482
} catch (e: Exception ) {
@@ -434,20 +502,29 @@ class SVGAParser(context: Context?) {
434
502
byteArrayOutputStream.toString().let {
435
503
JSONObject (it).let {
436
504
LogUtils .info(TAG , " spec change to entity success" )
437
- this .invokeCompleteCallback(SVGAVideoEntity (it, cacheDir, mFrameWidth, mFrameHeight), callback)
505
+ this .invokeCompleteCallback(
506
+ SVGAVideoEntity (
507
+ it,
508
+ cacheDir,
509
+ mFrameWidth,
510
+ mFrameHeight
511
+ ),
512
+ callback,
513
+ alias
514
+ )
438
515
}
439
516
}
440
517
}
441
518
}
442
519
} catch (e: Exception ) {
443
- LogUtils .error(TAG , " spec change to entity fail" , e)
520
+ LogUtils .error(TAG , " $alias movie. spec change to entity fail" , e)
444
521
cacheDir.delete()
445
522
jsonFile.delete()
446
523
throw e
447
524
}
448
525
}
449
526
} catch (e: Exception ) {
450
- this .invokeErrorCallback(e, callback)
527
+ this .invokeErrorCallback(e, callback, alias )
451
528
}
452
529
}
453
530
0 commit comments