@@ -4,7 +4,6 @@ import android.content.Context
4
4
import android.net.http.HttpResponseCache
5
5
import android.os.Handler
6
6
import android.os.Looper
7
- import android.util.Log
8
7
import com.opensource.svgaplayer.proto.MovieEntity
9
8
import com.opensource.svgaplayer.utils.log.LogUtils
10
9
import org.json.JSONObject
@@ -46,7 +45,11 @@ class SVGAParser(context: Context?) {
46
45
47
46
var noCache = false
48
47
49
- open fun resume (url : URL , complete : (inputStream: InputStream ) -> Unit , failure : (e: Exception ) -> Unit ): () -> Unit {
48
+ open fun resume (
49
+ url : URL ,
50
+ complete : (inputStream: InputStream ) -> Unit ,
51
+ failure : (e: Exception ) -> Unit
52
+ ): () -> Unit {
50
53
var cancelled = false
51
54
val cancelBlock = {
52
55
cancelled = true
@@ -55,8 +58,14 @@ class SVGAParser(context: Context?) {
55
58
try {
56
59
LogUtils .info(TAG , " ================ svga file download start ================" )
57
60
if (HttpResponseCache .getInstalled() == null && ! noCache) {
58
- LogUtils .error(TAG , " SVGAParser can not handle cache before install HttpResponseCache. see https://github.com/yyued/SVGAPlayer-Android#cache" )
59
- LogUtils .error(TAG , " 在配置 HttpResponseCache 前 SVGAParser 无法缓存. 查看 https://github.com/yyued/SVGAPlayer-Android#cache " )
61
+ LogUtils .error(
62
+ TAG ,
63
+ " SVGAParser can not handle cache before install HttpResponseCache. see https://github.com/yyued/SVGAPlayer-Android#cache"
64
+ )
65
+ LogUtils .error(
66
+ TAG ,
67
+ " 在配置 HttpResponseCache 前 SVGAParser 无法缓存. 查看 https://github.com/yyued/SVGAPlayer-Android#cache "
68
+ )
60
69
}
61
70
(url.openConnection() as ? HttpURLConnection )?.let {
62
71
it.connectTimeout = 20 * 1000
@@ -68,7 +77,10 @@ class SVGAParser(context: Context?) {
68
77
var count: Int
69
78
while (true ) {
70
79
if (cancelled) {
71
- LogUtils .warn(TAG , " ================ svga file download canceled ================" )
80
+ LogUtils .warn(
81
+ TAG ,
82
+ " ================ svga file download canceled ================"
83
+ )
72
84
break
73
85
}
74
86
count = inputStream.read(buffer, 0 , 4096 )
@@ -78,11 +90,17 @@ class SVGAParser(context: Context?) {
78
90
outputStream.write(buffer, 0 , count)
79
91
}
80
92
if (cancelled) {
81
- LogUtils .warn(TAG , " ================ svga file download canceled ================" )
93
+ LogUtils .warn(
94
+ TAG ,
95
+ " ================ svga file download canceled ================"
96
+ )
82
97
return @execute
83
98
}
84
99
ByteArrayInputStream (outputStream.toByteArray()).use {
85
- LogUtils .info(TAG , " ================ svga file download complete ================" )
100
+ LogUtils .info(
101
+ TAG ,
102
+ " ================ svga file download complete ================"
103
+ )
86
104
complete(it)
87
105
}
88
106
}
@@ -135,15 +153,20 @@ class SVGAParser(context: Context?) {
135
153
LogUtils .error(TAG , " 在配置 SVGAParser context 前, 无法解析 SVGA 文件。" )
136
154
return
137
155
}
138
- try {
139
- LogUtils .info( TAG , " ================ decode from assets ================ " )
140
- threadPoolExecutor.execute {
156
+ LogUtils .info( TAG , " ================ decode from assets ================ " )
157
+ threadPoolExecutor.execute {
158
+ try {
141
159
mContext?.assets?.open(name)?.let {
142
- this .decodeFromInputStream(it, SVGACache .buildCacheKey(" file:///assets/$name " ), callback, true )
160
+ this .decodeFromInputStream(
161
+ it,
162
+ SVGACache .buildCacheKey(" file:///assets/$name " ),
163
+ callback,
164
+ true
165
+ )
143
166
}
167
+ } catch (e: java.lang.Exception ) {
168
+ this .invokeErrorCallback(e, callback)
144
169
}
145
- } catch (e: java.lang.Exception ) {
146
- this .invokeErrorCallback(e, callback)
147
170
}
148
171
}
149
172
@@ -192,10 +215,10 @@ class SVGAParser(context: Context?) {
192
215
inflate(bytes)?.let { inflateBytes ->
193
216
LogUtils .info(TAG , " cache.inflate success" )
194
217
val videoItem = SVGAVideoEntity (
195
- MovieEntity .ADAPTER .decode(inflateBytes),
196
- File (cacheKey),
197
- mFrameWidth,
198
- mFrameHeight
218
+ MovieEntity .ADAPTER .decode(inflateBytes),
219
+ File (cacheKey),
220
+ mFrameWidth,
221
+ mFrameHeight
199
222
)
200
223
videoItem.prepare {
201
224
LogUtils .info(TAG , " cache.prepare success" )
@@ -219,18 +242,18 @@ class SVGAParser(context: Context?) {
219
242
fun doError (error : String , callback : ParseCompletion ? ) {
220
243
LogUtils .info(TAG , error)
221
244
this .invokeErrorCallback(
222
- Exception (error),
223
- callback
245
+ Exception (error),
246
+ callback
224
247
)
225
248
}
226
249
227
250
/* *
228
251
* 读取解析来自URL的svga文件.并缓存成本地文件
229
252
*/
230
253
fun _decodeFromInputStream (
231
- inputStream : InputStream ,
232
- cacheKey : String ,
233
- callback : ParseCompletion ?
254
+ inputStream : InputStream ,
255
+ cacheKey : String ,
256
+ callback : ParseCompletion ?
234
257
) {
235
258
threadPoolExecutor.execute {
236
259
try {
@@ -251,10 +274,10 @@ class SVGAParser(context: Context?) {
251
274
inflate(bytes)?.let { inflateBytes ->
252
275
LogUtils .info(TAG , " Input.inflate success" )
253
276
val videoItem = SVGAVideoEntity (
254
- MovieEntity .ADAPTER .decode(inflateBytes),
255
- File (cacheKey),
256
- mFrameWidth,
257
- mFrameHeight
277
+ MovieEntity .ADAPTER .decode(inflateBytes),
278
+ File (cacheKey),
279
+ mFrameWidth,
280
+ mFrameHeight
258
281
)
259
282
// 里面soundPool如果解析时load同一个svga的声音文件会出现无回调的情况,导致这里的callback不执行,
260
283
// 原因暂时未知.目前解决方案是公开imageview,drawable,entity的clear(),然后在播放带声音
@@ -276,10 +299,10 @@ class SVGAParser(context: Context?) {
276
299
}
277
300
278
301
fun decodeFromInputStream (
279
- inputStream : InputStream ,
280
- cacheKey : String ,
281
- callback : ParseCompletion ? ,
282
- closeInputStream : Boolean = false
302
+ inputStream : InputStream ,
303
+ cacheKey : String ,
304
+ callback : ParseCompletion ? ,
305
+ closeInputStream : Boolean = false
283
306
) {
284
307
if (mContext == null ) {
285
308
LogUtils .error(TAG , " 在配置 SVGAParser context 前, 无法解析 SVGA 文件。" )
@@ -309,24 +332,24 @@ class SVGAParser(context: Context?) {
309
332
LogUtils .info(TAG , " decode from input stream, inflate start" )
310
333
inflate(bytes)?.let {
311
334
val videoItem = SVGAVideoEntity (
312
- MovieEntity .ADAPTER .decode(it),
313
- File (cacheKey),
314
- mFrameWidth,
315
- mFrameHeight
335
+ MovieEntity .ADAPTER .decode(it),
336
+ File (cacheKey),
337
+ mFrameWidth,
338
+ mFrameHeight
316
339
)
317
340
videoItem.prepare {
318
341
LogUtils .info(TAG , " decode from input stream, inflate end" )
319
342
this .invokeCompleteCallback(videoItem, callback)
320
343
}
321
344
322
345
} ? : this .invokeErrorCallback(
323
- Exception (" inflate(bytes) cause exception" ),
324
- callback
346
+ Exception (" inflate(bytes) cause exception" ),
347
+ callback
325
348
)
326
349
}
327
350
} ? : this .invokeErrorCallback(
328
- Exception (" readAsBytes(inputStream) cause exception" ),
329
- callback
351
+ Exception (" readAsBytes(inputStream) cause exception" ),
352
+ callback
330
353
)
331
354
} catch (e: java.lang.Exception ) {
332
355
this .invokeErrorCallback(e, callback)
@@ -341,24 +364,38 @@ class SVGAParser(context: Context?) {
341
364
/* *
342
365
* @deprecated from 2.4.0
343
366
*/
344
- @Deprecated(" This method has been deprecated from 2.4.0." , ReplaceWith (" this.decodeFromAssets(assetsName, callback)" ))
367
+ @Deprecated(
368
+ " This method has been deprecated from 2.4.0." ,
369
+ ReplaceWith (" this.decodeFromAssets(assetsName, callback)" )
370
+ )
345
371
fun parse (assetsName : String , callback : ParseCompletion ? ) {
346
372
this .decodeFromAssets(assetsName, callback)
347
373
}
348
374
349
375
/* *
350
376
* @deprecated from 2.4.0
351
377
*/
352
- @Deprecated(" This method has been deprecated from 2.4.0." , ReplaceWith (" this.decodeFromURL(url, callback)" ))
378
+ @Deprecated(
379
+ " This method has been deprecated from 2.4.0." ,
380
+ ReplaceWith (" this.decodeFromURL(url, callback)" )
381
+ )
353
382
fun parse (url : URL , callback : ParseCompletion ? ) {
354
383
this .decodeFromURL(url, callback)
355
384
}
356
385
357
386
/* *
358
387
* @deprecated from 2.4.0
359
388
*/
360
- @Deprecated(" This method has been deprecated from 2.4.0." , ReplaceWith (" this.decodeFromInputStream(inputStream, cacheKey, callback, closeInputStream)" ))
361
- fun parse (inputStream : InputStream , cacheKey : String , callback : ParseCompletion ? , closeInputStream : Boolean = false) {
389
+ @Deprecated(
390
+ " This method has been deprecated from 2.4.0." ,
391
+ ReplaceWith (" this.decodeFromInputStream(inputStream, cacheKey, callback, closeInputStream)" )
392
+ )
393
+ fun parse (
394
+ inputStream : InputStream ,
395
+ cacheKey : String ,
396
+ callback : ParseCompletion ? ,
397
+ closeInputStream : Boolean = false
398
+ ) {
362
399
this .decodeFromInputStream(inputStream, cacheKey, callback, closeInputStream)
363
400
}
364
401
@@ -392,7 +429,14 @@ class SVGAParser(context: Context?) {
392
429
LogUtils .info(TAG , " binary change to entity" )
393
430
FileInputStream (binaryFile).use {
394
431
LogUtils .info(TAG , " binary change to entity success" )
395
- this .invokeCompleteCallback(SVGAVideoEntity (MovieEntity .ADAPTER .decode(it), cacheDir, mFrameWidth, mFrameHeight), callback)
432
+ this .invokeCompleteCallback(
433
+ SVGAVideoEntity (
434
+ MovieEntity .ADAPTER .decode(it),
435
+ cacheDir,
436
+ mFrameWidth,
437
+ mFrameHeight
438
+ ), callback
439
+ )
396
440
}
397
441
} catch (e: Exception ) {
398
442
LogUtils .error(TAG , " binary change to entity fail" , e)
@@ -417,7 +461,14 @@ class SVGAParser(context: Context?) {
417
461
byteArrayOutputStream.toString().let {
418
462
JSONObject (it).let {
419
463
LogUtils .info(TAG , " spec change to entity success" )
420
- this .invokeCompleteCallback(SVGAVideoEntity (it, cacheDir, mFrameWidth, mFrameHeight), callback)
464
+ this .invokeCompleteCallback(
465
+ SVGAVideoEntity (
466
+ it,
467
+ cacheDir,
468
+ mFrameWidth,
469
+ mFrameHeight
470
+ ), callback
471
+ )
421
472
}
422
473
}
423
474
}
@@ -494,7 +545,7 @@ class SVGAParser(context: Context?) {
494
545
fileOutputStream.write(buff, 0 , readBytes)
495
546
}
496
547
}
497
- LogUtils .error (TAG , " ================ unzip complete ================" )
548
+ LogUtils .info (TAG , " ================ unzip complete ================" )
498
549
zipInputStream.closeEntry()
499
550
}
500
551
}
0 commit comments