mirror of
https://github.com/rebelonion/Dantotsu.git
synced 2026-01-21 21:33:57 +00:00
feat: add cache to bitmap download
dfgdfg
This commit is contained in:
@@ -1,12 +1,18 @@
|
||||
package ani.dantotsu.util
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.BitmapShader
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Paint
|
||||
import android.graphics.RectF
|
||||
import android.graphics.Shader
|
||||
import android.graphics.drawable.Drawable
|
||||
import androidx.collection.LruCache
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import java.io.InputStream
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
|
||||
class BitmapUtil {
|
||||
companion object {
|
||||
@@ -22,12 +28,39 @@ class BitmapUtil {
|
||||
return output
|
||||
}
|
||||
|
||||
fun convertDrawableToBitmap(drawable: Drawable, width: Int, height: Int): Bitmap {
|
||||
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
|
||||
val canvas = Canvas(bitmap)
|
||||
drawable.setBounds(0, 0, canvas.width, canvas.height)
|
||||
drawable.draw(canvas)
|
||||
return bitmap
|
||||
private val cacheSize = (Runtime.getRuntime().maxMemory() / 1024 / 16).toInt()
|
||||
private val bitmapCache = LruCache<String, Bitmap>(cacheSize)
|
||||
|
||||
fun downloadImageAsBitmap(imageUrl: String): Bitmap? {
|
||||
var bitmap: Bitmap? = null
|
||||
|
||||
runBlocking(Dispatchers.IO) {
|
||||
|
||||
bitmapCache[imageUrl]?.let {
|
||||
bitmap = it
|
||||
} ?: {
|
||||
var inputStream: InputStream? = null
|
||||
var urlConnection: HttpURLConnection? = null
|
||||
try {
|
||||
val url = URL(imageUrl)
|
||||
urlConnection = url.openConnection() as HttpURLConnection
|
||||
urlConnection.requestMethod = "GET"
|
||||
urlConnection.connect()
|
||||
|
||||
if (urlConnection.responseCode == HttpURLConnection.HTTP_OK) {
|
||||
inputStream = urlConnection.inputStream
|
||||
bitmap = BitmapFactory.decodeStream(inputStream)
|
||||
bitmap?.let { bitmapCache.put(imageUrl, it) }
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
} finally {
|
||||
inputStream?.close()
|
||||
urlConnection?.disconnect()
|
||||
}
|
||||
}
|
||||
}
|
||||
return bitmap?.let { roundCorners(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ import ani.dantotsu.connections.anilist.Anilist
|
||||
import ani.dantotsu.profile.ProfileActivity
|
||||
import ani.dantotsu.settings.saving.PrefManager
|
||||
import ani.dantotsu.settings.saving.PrefName
|
||||
import ani.dantotsu.util.BitmapUtil
|
||||
import ani.dantotsu.util.BitmapUtil.Companion.downloadImageAsBitmap
|
||||
import ani.dantotsu.widgets.WidgetSizeProvider
|
||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -52,32 +52,6 @@ class ProfileStatsWidget : AppWidgetProvider() {
|
||||
}
|
||||
|
||||
companion object {
|
||||
private fun downloadImageAsBitmap(imageUrl: String): Bitmap? {
|
||||
var bitmap: Bitmap? = null
|
||||
|
||||
runBlocking(Dispatchers.IO) {
|
||||
var inputStream: InputStream? = null
|
||||
var urlConnection: HttpURLConnection? = null
|
||||
try {
|
||||
val url = URL(imageUrl)
|
||||
urlConnection = url.openConnection() as HttpURLConnection
|
||||
urlConnection.requestMethod = "GET"
|
||||
urlConnection.connect()
|
||||
|
||||
if (urlConnection.responseCode == HttpURLConnection.HTTP_OK) {
|
||||
inputStream = urlConnection.inputStream
|
||||
bitmap = BitmapFactory.decodeStream(inputStream)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
} finally {
|
||||
inputStream?.close()
|
||||
urlConnection?.disconnect()
|
||||
}
|
||||
}
|
||||
return bitmap?.let { BitmapUtil.roundCorners(it) }
|
||||
}
|
||||
|
||||
@OptIn(DelicateCoroutinesApi::class)
|
||||
fun updateAppWidget(
|
||||
context: Context,
|
||||
|
||||
@@ -12,6 +12,7 @@ import ani.dantotsu.connections.anilist.Anilist
|
||||
import ani.dantotsu.media.Media
|
||||
import ani.dantotsu.settings.saving.PrefManager
|
||||
import ani.dantotsu.settings.saving.PrefName
|
||||
import ani.dantotsu.util.BitmapUtil.Companion.downloadImageAsBitmap
|
||||
import ani.dantotsu.util.BitmapUtil.Companion.roundCorners
|
||||
import ani.dantotsu.util.Logger
|
||||
import com.google.gson.GsonBuilder
|
||||
@@ -183,33 +184,6 @@ class UpcomingRemoteViewsFactory(private val context: Context) :
|
||||
return rv
|
||||
}
|
||||
|
||||
private fun downloadImageAsBitmap(imageUrl: String): Bitmap? {
|
||||
var bitmap: Bitmap? = null
|
||||
var inputStream: InputStream? = null
|
||||
var urlConnection: HttpURLConnection? = null
|
||||
|
||||
try {
|
||||
val url = URL(imageUrl)
|
||||
urlConnection = url.openConnection() as HttpURLConnection
|
||||
urlConnection.requestMethod = "GET"
|
||||
urlConnection.connect()
|
||||
|
||||
if (urlConnection.responseCode == HttpURLConnection.HTTP_OK) {
|
||||
inputStream = urlConnection.inputStream
|
||||
bitmap = BitmapFactory.decodeStream(inputStream)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
} finally {
|
||||
inputStream?.close()
|
||||
urlConnection?.disconnect()
|
||||
}
|
||||
return bitmap?.let { roundCorners(it) }
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
override fun getLoadingView(): RemoteViews {
|
||||
return RemoteViews(context.packageName, R.layout.item_upcoming_widget)
|
||||
}
|
||||
|
||||
@@ -11,9 +11,9 @@ import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.widget.RemoteViews
|
||||
import androidx.core.content.res.ResourcesCompat
|
||||
import androidx.core.graphics.drawable.toBitmap
|
||||
import ani.dantotsu.MainActivity
|
||||
import ani.dantotsu.R
|
||||
import ani.dantotsu.util.BitmapUtil.Companion.convertDrawableToBitmap
|
||||
import ani.dantotsu.widgets.WidgetSizeProvider
|
||||
|
||||
/**
|
||||
@@ -97,7 +97,7 @@ class UpcomingWidget : AppWidgetProvider() {
|
||||
intentTemplate.putExtra("fromWidget", true)
|
||||
|
||||
val views = RemoteViews(context.packageName, R.layout.upcoming_widget).apply {
|
||||
setImageViewBitmap(R.id.backgroundView, convertDrawableToBitmap(gradientDrawable, width, height))
|
||||
setImageViewBitmap(R.id.backgroundView, gradientDrawable.toBitmap(width, height))
|
||||
setTextColor(R.id.text_show_title, titleTextColor)
|
||||
setTextColor(R.id.text_show_countdown, countdownTextColor)
|
||||
setTextColor(R.id.widgetTitle, titleTextColor)
|
||||
|
||||
Reference in New Issue
Block a user