@@ -2,7 +2,9 @@ package com.opensource.svgaplayer
2
2
3
3
import android.app.Activity
4
4
import android.content.Context
5
+ import android.net.http.HttpResponseCache
5
6
import android.os.Handler
7
+ import android.util.Log
6
8
import com.opensource.svgaplayer.proto.MovieEntity
7
9
8
10
import org.json.JSONObject
@@ -31,9 +33,15 @@ class SVGAParser(private val context: Context) {
31
33
32
34
open class FileDownloader {
33
35
36
+ var noCache = false
37
+
34
38
open fun resume (url : URL , complete : (inputStream: InputStream ) -> Unit , failure : (e: Exception ) -> Unit ) {
35
39
Thread ({
36
40
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
+ }
37
45
(url.openConnection() as ? HttpURLConnection )?.let {
38
46
it.connectTimeout = 20 * 1000
39
47
it.requestMethod = " GET"
@@ -121,9 +129,9 @@ class SVGAParser(private val context: Context) {
121
129
})
122
130
try {
123
131
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 ->
125
133
try {
126
- FileInputStream (binaryFile)? .let {
134
+ FileInputStream (binaryFile).let {
127
135
val videoItem = SVGAVideoEntity (MovieEntity .ADAPTER .decode(it), cacheDir)
128
136
it.close()
129
137
return videoItem
@@ -134,9 +142,9 @@ class SVGAParser(private val context: Context) {
134
142
throw e
135
143
}
136
144
}
137
- File (cacheDir, " movie.spec" )? .takeIf { it.isFile }?.let { jsonFile ->
145
+ File (cacheDir, " movie.spec" ).takeIf { it.isFile }?.let { jsonFile ->
138
146
try {
139
- FileInputStream (jsonFile)? .let { fileInputStream ->
147
+ FileInputStream (jsonFile).let { fileInputStream ->
140
148
val byteArrayOutputStream = ByteArrayOutputStream ()
141
149
val buffer = ByteArray (2048 )
142
150
while (true ) {
@@ -146,8 +154,8 @@ class SVGAParser(private val context: Context) {
146
154
}
147
155
byteArrayOutputStream.write(buffer, 0 , size)
148
156
}
149
- byteArrayOutputStream.toString()? .let {
150
- JSONObject (it)? .let {
157
+ byteArrayOutputStream.toString().let {
158
+ JSONObject (it).let {
151
159
fileInputStream.close()
152
160
return SVGAVideoEntity (it, cacheDir)
153
161
}
@@ -178,9 +186,9 @@ class SVGAParser(private val context: Context) {
178
186
private fun parseWithCacheKey (cacheKey : String ): SVGAVideoEntity ? {
179
187
try {
180
188
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 ->
182
190
try {
183
- FileInputStream (binaryFile)? .let {
191
+ FileInputStream (binaryFile).let {
184
192
val videoItem = SVGAVideoEntity (MovieEntity .ADAPTER .decode(it), cacheDir)
185
193
it.close()
186
194
return videoItem
@@ -191,9 +199,9 @@ class SVGAParser(private val context: Context) {
191
199
throw e
192
200
}
193
201
}
194
- File (cacheDir, " movie.spec" )? .takeIf { it.isFile }?.let { jsonFile ->
202
+ File (cacheDir, " movie.spec" ).takeIf { it.isFile }?.let { jsonFile ->
195
203
try {
196
- FileInputStream (jsonFile)? .let { fileInputStream ->
204
+ FileInputStream (jsonFile).let { fileInputStream ->
197
205
val byteArrayOutputStream = ByteArrayOutputStream ()
198
206
val buffer = ByteArray (2048 )
199
207
while (true ) {
@@ -203,8 +211,8 @@ class SVGAParser(private val context: Context) {
203
211
}
204
212
byteArrayOutputStream.write(buffer, 0 , size)
205
213
}
206
- byteArrayOutputStream.toString()? .let {
207
- JSONObject (it)? .let {
214
+ byteArrayOutputStream.toString().let {
215
+ JSONObject (it).let {
208
216
fileInputStream.close()
209
217
return SVGAVideoEntity (it, cacheDir)
210
218
}
@@ -226,20 +234,16 @@ class SVGAParser(private val context: Context) {
226
234
val messageDigest = MessageDigest .getInstance(" MD5" )
227
235
messageDigest.update(str.toByteArray(charset(" UTF-8" )))
228
236
val digest = messageDigest.digest()
229
- val sb = StringBuffer ()
237
+ var sb = " "
230
238
for (b in digest) {
231
- sb.append( String .format(" %02x" , b) )
239
+ sb + = String .format(" %02x" , b)
232
240
}
233
- return sb.toString()
241
+ return sb
234
242
}
235
243
236
- private fun cacheKey (url : URL ): String {
237
- return cacheKey(url.toString())
238
- }
244
+ private fun cacheKey (url : URL ): String = cacheKey(url.toString())
239
245
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 + " /" )
243
247
244
248
private fun readAsBytes (inputStream : InputStream ): ByteArray {
245
249
val byteArrayOutputStream = ByteArrayOutputStream ()
0 commit comments