Use android HttpResponseCache instead of an ugly implementation.
This commit is contained in:
parent
4cb330d522
commit
f70045ac67
|
@ -274,20 +274,19 @@ class ApngAnimator(private val context: Context?) {
|
|||
this@ApngAnimator.speed = speed
|
||||
// Download PNG
|
||||
try {
|
||||
Loader.load(context!!, url).apply {
|
||||
Loader.load(url).apply {
|
||||
try {
|
||||
this@ApngAnimator.load(this, speed, apngAnimatorOptions)
|
||||
} catch (e: NotPngException) {
|
||||
if (loadNotApng) {
|
||||
val bytes = this.readBytes()
|
||||
GlobalScope.launch(Dispatchers.Main) {
|
||||
imageView?.scaleType =
|
||||
this@ApngAnimator.scaleType ?: ImageView.ScaleType.FIT_CENTER
|
||||
imageView?.setImageBitmap(
|
||||
BitmapFactory.decodeByteArray(
|
||||
bytes,
|
||||
this@apply,
|
||||
0,
|
||||
bytes.size
|
||||
this@apply.size
|
||||
)
|
||||
)
|
||||
}
|
||||
|
|
|
@ -1,36 +1,30 @@
|
|||
package oupson.apng
|
||||
|
||||
import android.content.Context
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.BufferedInputStream
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.IOException
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
|
||||
class Loader {
|
||||
companion object {
|
||||
/**
|
||||
* Download file from given url.
|
||||
* @param context Context of app.
|
||||
* @param url Url of the file to download.
|
||||
* @return [ByteArray] of the file.
|
||||
*/
|
||||
@Throws(IOException::class, java.io.FileNotFoundException::class)
|
||||
suspend fun load(context: Context, url: URL) =
|
||||
@Throws(IOException::class)
|
||||
suspend fun load(url: URL): ByteArray =
|
||||
withContext(Dispatchers.IO) {
|
||||
val currentDir = context.filesDir
|
||||
val fileTXT = File(currentDir, "apngLoader.txt")
|
||||
val filePNG = File(currentDir, "apngLoader.png")
|
||||
if (fileTXT.exists() && url.toString() == fileTXT.readText()) {
|
||||
filePNG
|
||||
} else {
|
||||
val connection = url.openConnection()
|
||||
connection.connect()
|
||||
val input = BufferedInputStream(connection.getInputStream())
|
||||
val output = FileOutputStream(filePNG)
|
||||
var bytesRead : Int
|
||||
val connection = url.openConnection() as HttpURLConnection
|
||||
connection.useCaches = true
|
||||
connection.connect()
|
||||
if (connection.responseCode == 200) {
|
||||
val input = BufferedInputStream(connection.inputStream)
|
||||
val output = ByteArrayOutputStream()
|
||||
var bytesRead: Int
|
||||
val buffer = ByteArray(4096)
|
||||
do {
|
||||
bytesRead = input.read(buffer)
|
||||
|
@ -39,8 +33,11 @@ class Loader {
|
|||
} while (bytesRead != -1)
|
||||
input.close()
|
||||
output.close()
|
||||
fileTXT.writeText(url.toString())
|
||||
filePNG
|
||||
connection.disconnect()
|
||||
output.toByteArray()
|
||||
} else {
|
||||
connection.disconnect()
|
||||
throw Exception("Error when downloading file : ${connection.responseCode}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,10 +24,7 @@ import oupson.apng.exceptions.BadApng
|
|||
import oupson.apng.exceptions.BadCRC
|
||||
import oupson.apng.utils.Utils
|
||||
import oupson.apng.utils.Utils.Companion.isPng
|
||||
import java.io.BufferedInputStream
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.InputStream
|
||||
import java.io.*
|
||||
import java.net.URL
|
||||
import java.nio.ByteBuffer
|
||||
import java.util.zip.CRC32
|
||||
|
@ -536,9 +533,7 @@ class ApngDecoder {
|
|||
withContext(Dispatchers.IO) {
|
||||
decodeApng(
|
||||
context,
|
||||
FileInputStream(
|
||||
Loader.load(context, url)
|
||||
),
|
||||
ByteArrayInputStream(Loader.load(url)),
|
||||
speed,
|
||||
config
|
||||
)
|
||||
|
@ -706,9 +701,8 @@ class ApngDecoder {
|
|||
val drawable =
|
||||
decodeApng(
|
||||
context,
|
||||
FileInputStream(
|
||||
ByteArrayInputStream(
|
||||
Loader.load(
|
||||
context,
|
||||
url
|
||||
)
|
||||
),
|
||||
|
|
|
@ -2,6 +2,7 @@ package oupson.apngcreator.activities
|
|||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Intent
|
||||
import android.net.http.HttpResponseCache
|
||||
import android.os.Bundle
|
||||
import android.view.MenuItem
|
||||
import androidx.appcompat.app.ActionBarDrawerToggle
|
||||
|
@ -17,6 +18,7 @@ import oupson.apngcreator.R
|
|||
import oupson.apngcreator.fragments.ApngDecoderFragment
|
||||
import oupson.apngcreator.fragments.JavaFragment
|
||||
import oupson.apngcreator.fragments.KotlinFragment
|
||||
import java.io.File
|
||||
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
@ -30,11 +32,15 @@ class MainActivity : AppCompatActivity() {
|
|||
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
|
||||
setSupportActionBar(bottomAppBar)
|
||||
|
||||
setUpBottomAppBarShapeAppearance()
|
||||
|
||||
val httpCacheSize = 10 * 1024 * 1024.toLong() // 10 MiB
|
||||
|
||||
val httpCacheDir = File(cacheDir, "http")
|
||||
HttpResponseCache.install(httpCacheDir, httpCacheSize)
|
||||
|
||||
fabCreate.setOnClickListener {
|
||||
startActivity(Intent(this, CreatorActivity::class.java))
|
||||
}
|
||||
|
@ -135,6 +141,11 @@ class MainActivity : AppCompatActivity() {
|
|||
}
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
HttpResponseCache.getInstalled()?.flush()
|
||||
}
|
||||
|
||||
private fun setUpBottomAppBarShapeAppearance() {
|
||||
val fabShapeAppearanceModel: ShapeAppearanceModel = fabCreate.shapeAppearanceModel
|
||||
val cutCornersFab =
|
||||
|
|
Loading…
Reference in New Issue