Use android HttpResponseCache instead of an ugly implementation.

This commit is contained in:
oupson 2020-05-06 17:32:19 +02:00
parent 4cb330d522
commit f70045ac67
4 changed files with 34 additions and 33 deletions

View File

@ -274,20 +274,19 @@ class ApngAnimator(private val context: Context?) {
this@ApngAnimator.speed = speed this@ApngAnimator.speed = speed
// Download PNG // Download PNG
try { try {
Loader.load(context!!, url).apply { Loader.load(url).apply {
try { try {
this@ApngAnimator.load(this, speed, apngAnimatorOptions) this@ApngAnimator.load(this, speed, apngAnimatorOptions)
} catch (e: NotPngException) { } catch (e: NotPngException) {
if (loadNotApng) { if (loadNotApng) {
val bytes = this.readBytes()
GlobalScope.launch(Dispatchers.Main) { GlobalScope.launch(Dispatchers.Main) {
imageView?.scaleType = imageView?.scaleType =
this@ApngAnimator.scaleType ?: ImageView.ScaleType.FIT_CENTER this@ApngAnimator.scaleType ?: ImageView.ScaleType.FIT_CENTER
imageView?.setImageBitmap( imageView?.setImageBitmap(
BitmapFactory.decodeByteArray( BitmapFactory.decodeByteArray(
bytes, this@apply,
0, 0,
bytes.size this@apply.size
) )
) )
} }

View File

@ -1,36 +1,30 @@
package oupson.apng package oupson.apng
import android.content.Context
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.io.BufferedInputStream import java.io.BufferedInputStream
import java.io.File import java.io.ByteArrayOutputStream
import java.io.FileOutputStream
import java.io.IOException import java.io.IOException
import java.net.HttpURLConnection
import java.net.URL import java.net.URL
class Loader { class Loader {
companion object { companion object {
/** /**
* Download file from given url. * Download file from given url.
* @param context Context of app.
* @param url Url of the file to download. * @param url Url of the file to download.
* @return [ByteArray] of the file. * @return [ByteArray] of the file.
*/ */
@Throws(IOException::class, java.io.FileNotFoundException::class) @Throws(IOException::class)
suspend fun load(context: Context, url: URL) = suspend fun load(url: URL): ByteArray =
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val currentDir = context.filesDir val connection = url.openConnection() as HttpURLConnection
val fileTXT = File(currentDir, "apngLoader.txt") connection.useCaches = true
val filePNG = File(currentDir, "apngLoader.png") connection.connect()
if (fileTXT.exists() && url.toString() == fileTXT.readText()) { if (connection.responseCode == 200) {
filePNG val input = BufferedInputStream(connection.inputStream)
} else { val output = ByteArrayOutputStream()
val connection = url.openConnection() var bytesRead: Int
connection.connect()
val input = BufferedInputStream(connection.getInputStream())
val output = FileOutputStream(filePNG)
var bytesRead : Int
val buffer = ByteArray(4096) val buffer = ByteArray(4096)
do { do {
bytesRead = input.read(buffer) bytesRead = input.read(buffer)
@ -39,8 +33,11 @@ class Loader {
} while (bytesRead != -1) } while (bytesRead != -1)
input.close() input.close()
output.close() output.close()
fileTXT.writeText(url.toString()) connection.disconnect()
filePNG output.toByteArray()
} else {
connection.disconnect()
throw Exception("Error when downloading file : ${connection.responseCode}")
} }
} }
} }

View File

@ -24,10 +24,7 @@ import oupson.apng.exceptions.BadApng
import oupson.apng.exceptions.BadCRC import oupson.apng.exceptions.BadCRC
import oupson.apng.utils.Utils import oupson.apng.utils.Utils
import oupson.apng.utils.Utils.Companion.isPng import oupson.apng.utils.Utils.Companion.isPng
import java.io.BufferedInputStream import java.io.*
import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import java.net.URL import java.net.URL
import java.nio.ByteBuffer import java.nio.ByteBuffer
import java.util.zip.CRC32 import java.util.zip.CRC32
@ -536,9 +533,7 @@ class ApngDecoder {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
decodeApng( decodeApng(
context, context,
FileInputStream( ByteArrayInputStream(Loader.load(url)),
Loader.load(context, url)
),
speed, speed,
config config
) )
@ -706,9 +701,8 @@ class ApngDecoder {
val drawable = val drawable =
decodeApng( decodeApng(
context, context,
FileInputStream( ByteArrayInputStream(
Loader.load( Loader.load(
context,
url url
) )
), ),

View File

@ -2,6 +2,7 @@ package oupson.apngcreator.activities
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Intent import android.content.Intent
import android.net.http.HttpResponseCache
import android.os.Bundle import android.os.Bundle
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.ActionBarDrawerToggle
@ -17,6 +18,7 @@ import oupson.apngcreator.R
import oupson.apngcreator.fragments.ApngDecoderFragment import oupson.apngcreator.fragments.ApngDecoderFragment
import oupson.apngcreator.fragments.JavaFragment import oupson.apngcreator.fragments.JavaFragment
import oupson.apngcreator.fragments.KotlinFragment import oupson.apngcreator.fragments.KotlinFragment
import java.io.File
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
@ -30,11 +32,15 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
setSupportActionBar(bottomAppBar) setSupportActionBar(bottomAppBar)
setUpBottomAppBarShapeAppearance() setUpBottomAppBarShapeAppearance()
val httpCacheSize = 10 * 1024 * 1024.toLong() // 10 MiB
val httpCacheDir = File(cacheDir, "http")
HttpResponseCache.install(httpCacheDir, httpCacheSize)
fabCreate.setOnClickListener { fabCreate.setOnClickListener {
startActivity(Intent(this, CreatorActivity::class.java)) startActivity(Intent(this, CreatorActivity::class.java))
} }
@ -135,6 +141,11 @@ class MainActivity : AppCompatActivity() {
} }
} }
override fun onStop() {
super.onStop()
HttpResponseCache.getInstalled()?.flush()
}
private fun setUpBottomAppBarShapeAppearance() { private fun setUpBottomAppBarShapeAppearance() {
val fabShapeAppearanceModel: ShapeAppearanceModel = fabCreate.shapeAppearanceModel val fabShapeAppearanceModel: ShapeAppearanceModel = fabCreate.shapeAppearanceModel
val cutCornersFab = val cutCornersFab =