@@ -10,7 +10,6 @@ import org.json.JSONObject
10
10
import java.io.*
11
11
import java.net.HttpURLConnection
12
12
import java.net.URL
13
- import java.security.MessageDigest
14
13
import java.util.concurrent.Executors
15
14
import java.util.concurrent.ThreadPoolExecutor
16
15
import java.util.concurrent.atomic.AtomicInteger
@@ -108,7 +107,7 @@ class SVGAParser(context: Context?) {
108
107
private var mShareParser = SVGAParser (null )
109
108
110
109
internal var threadPoolExecutor = Executors .newCachedThreadPool { r ->
111
- Thread (r, " SVGAParser-Thread-${threadNum.getAndIncrement()} " )
110
+ Thread (r, " SVGAParser-Thread-${threadNum.getAndIncrement()} " )
112
111
}
113
112
114
113
fun setThreadPoolExecutor (executor : ThreadPoolExecutor ) {
@@ -178,63 +177,90 @@ class SVGAParser(context: Context?) {
178
177
}
179
178
}
180
179
180
+ /* *
181
+ * 读取解析本地缓存的svga文件.
182
+ */
181
183
fun _decodeFromCacheKey (cacheKey : String , callback : ParseCompletion ? ) {
182
- val svga = SVGACache .buildCacheFile (cacheKey)
184
+ val svga = SVGACache .buildSvgaFile (cacheKey)
183
185
try {
184
- LogUtils .info(TAG , " binary change to entity" )
185
- FileInputStream (svga).use {
186
- LogUtils .info(TAG , " binary change to entity success" )
187
- this .invokeCompleteCallback(
188
- SVGAVideoEntity (
189
- MovieEntity .ADAPTER .decode(it),
190
- SVGACache .buildCacheDir(cacheKey),
191
- mFrameWidth,
192
- mFrameHeight
193
- ), callback
194
- )
186
+ LogUtils .info(TAG , " cache.binary change to entity" )
187
+ FileInputStream (svga).use { inputStream ->
188
+ try {
189
+ readAsBytes(inputStream)?.let { bytes ->
190
+ LogUtils .info(TAG , " cache.inflate start" )
191
+ inflate(bytes)?.let { inflateBytes ->
192
+ LogUtils .info(TAG , " cache.inflate success" )
193
+ val videoItem = SVGAVideoEntity (
194
+ MovieEntity .ADAPTER .decode(inflateBytes),
195
+ File (cacheKey),
196
+ mFrameWidth,
197
+ mFrameHeight
198
+ )
199
+ videoItem.prepare {
200
+ LogUtils .info(TAG , " cache.prepare success" )
201
+ this .invokeCompleteCallback(videoItem, callback)
202
+ }
203
+ } ? : doError(" cache.inflate(bytes) cause exception" , callback)
204
+ } ? : doError(" cache.readAsBytes(inputStream) cause exception" , callback)
205
+ } catch (e: Exception ) {
206
+ this .invokeErrorCallback(e, callback)
207
+ } finally {
208
+ inputStream.close()
209
+ }
195
210
}
196
211
} catch (e: Exception ) {
197
- LogUtils .error(TAG , " binary change to entity fail" , e)
198
- SVGACache .buildCacheDir(cacheKey).delete()
199
- svga.delete()
212
+ LogUtils .error(TAG , " cache.binary change to entity fail" , e)
213
+ svga.takeIf { it.exists() }?.delete()
200
214
this .invokeErrorCallback(e, callback)
201
215
}
202
216
}
203
217
218
+ fun doError (error : String , callback : ParseCompletion ? ) {
219
+ LogUtils .info(TAG , error)
220
+ this .invokeErrorCallback(
221
+ Exception (error),
222
+ callback
223
+ )
224
+ }
225
+
226
+ /* *
227
+ * 读取解析来自URL的svga文件.并缓存成本地文件
228
+ */
204
229
fun _decodeFromInputStream (
205
- inputStream : InputStream ,
206
- cacheKey : String ,
207
- callback : ParseCompletion ?
230
+ inputStream : InputStream ,
231
+ cacheKey : String ,
232
+ callback : ParseCompletion ?
208
233
) {
209
234
threadPoolExecutor.execute {
210
235
try {
236
+ LogUtils .info(TAG , " Input.binary change to entity" )
211
237
readAsBytes(inputStream)?.let { bytes ->
212
- LogUtils .info(TAG , " decode from input stream, inflate start" )
213
- inflate(bytes)?.let { inflateBytes ->
214
- threadPoolExecutor.execute {
215
- SVGACache .buildCacheFile(cacheKey).let { cacheFile ->
216
- cacheFile.takeIf { ! it.exists() }?.createNewFile()
217
- FileOutputStream (cacheFile).write(inflateBytes)
218
- }
238
+ threadPoolExecutor.execute {
239
+ SVGACache .buildSvgaFile(cacheKey).let { cacheFile ->
240
+ cacheFile.takeIf { ! it.exists() }?.createNewFile()
241
+ FileOutputStream (cacheFile).write(bytes)
219
242
}
243
+ }
244
+ LogUtils .info(TAG , " Input.inflate start" )
245
+ inflate(bytes)?.let { inflateBytes ->
246
+ LogUtils .info(TAG , " Input.inflate success" )
220
247
val videoItem = SVGAVideoEntity (
221
- MovieEntity .ADAPTER .decode(inflateBytes),
222
- File (cacheKey),
223
- mFrameWidth,
224
- mFrameHeight
248
+ MovieEntity .ADAPTER .decode(inflateBytes),
249
+ File (cacheKey),
250
+ mFrameWidth,
251
+ mFrameHeight
225
252
)
253
+ // 里面soundPool如果解析时load同一个svga的声音文件会出现无回调的情况,导致这里的callback不执行,
254
+ // 原因暂时未知.目前解决方案是公开imageview,drawable,entity的clear(),然后在播放带声音
255
+ // 的svgaimageview处,把解析完的drawable或者entity缓存下来,下次直接播放.用完再调用clear()
256
+ // 在ImageView添加clearsAfterDetached,用于控制imageview在onDetach的时候是否要自动调用clear.
257
+ // 以暂时缓解需要为RecyclerView缓存drawable或者entity的人士.用完记得调用clear()
226
258
videoItem.prepare {
227
- LogUtils .info(TAG , " decode from input stream, inflate end " )
259
+ LogUtils .info(TAG , " Input.prepare success " )
228
260
this .invokeCompleteCallback(videoItem, callback)
229
261
}
230
- } ? : this .invokeErrorCallback(
231
- Exception (" inflate(bytes) cause exception" ),
232
- callback
233
- )
234
- } ? : this .invokeErrorCallback(
235
- Exception (" readAsBytes(inputStream) cause exception" ),
236
- callback
237
- )
262
+ } ? : doError(" Input.inflate(bytes) cause exception" , callback)
263
+ } ? : doError(" Input.readAsBytes(inputStream) cause exception" , callback)
238
264
} catch (e: Exception ) {
239
265
this .invokeErrorCallback(e, callback)
240
266
} finally {
@@ -244,10 +270,10 @@ class SVGAParser(context: Context?) {
244
270
}
245
271
246
272
fun decodeFromInputStream (
247
- inputStream : InputStream ,
248
- cacheKey : String ,
249
- callback : ParseCompletion ? ,
250
- closeInputStream : Boolean = false
273
+ inputStream : InputStream ,
274
+ cacheKey : String ,
275
+ callback : ParseCompletion ? ,
276
+ closeInputStream : Boolean = false
251
277
) {
252
278
if (mContext == null ) {
253
279
LogUtils .error(TAG , " 在配置 SVGAParser context 前, 无法解析 SVGA 文件。" )
@@ -277,24 +303,24 @@ class SVGAParser(context: Context?) {
277
303
LogUtils .info(TAG , " decode from input stream, inflate start" )
278
304
inflate(bytes)?.let {
279
305
val videoItem = SVGAVideoEntity (
280
- MovieEntity .ADAPTER .decode(it),
281
- File (cacheKey),
282
- mFrameWidth,
283
- mFrameHeight
306
+ MovieEntity .ADAPTER .decode(it),
307
+ File (cacheKey),
308
+ mFrameWidth,
309
+ mFrameHeight
284
310
)
285
311
videoItem.prepare {
286
312
LogUtils .info(TAG , " decode from input stream, inflate end" )
287
313
this .invokeCompleteCallback(videoItem, callback)
288
314
}
289
315
290
316
} ? : this .invokeErrorCallback(
291
- Exception (" inflate(bytes) cause exception" ),
292
- callback
317
+ Exception (" inflate(bytes) cause exception" ),
318
+ callback
293
319
)
294
320
}
295
321
} ? : this .invokeErrorCallback(
296
- Exception (" readAsBytes(inputStream) cause exception" ),
297
- callback
322
+ Exception (" readAsBytes(inputStream) cause exception" ),
323
+ callback
298
324
)
299
325
} catch (e: java.lang.Exception ) {
300
326
this .invokeErrorCallback(e, callback)
0 commit comments