2023-01-31 21:13:39 +05:30
|
|
|
package com.github.libretube.helpers
|
2022-08-15 14:17:31 +05:30
|
|
|
|
|
|
|
import android.content.Context
|
2022-10-15 19:59:12 +05:30
|
|
|
import android.graphics.Bitmap
|
|
|
|
import android.graphics.BitmapFactory
|
|
|
|
import android.net.Uri
|
2022-08-15 14:17:31 +05:30
|
|
|
import android.widget.ImageView
|
2023-03-01 05:02:51 +05:30
|
|
|
import androidx.core.graphics.drawable.toBitmap
|
2022-08-15 14:17:31 +05:30
|
|
|
import coil.ImageLoader
|
|
|
|
import coil.disk.DiskCache
|
|
|
|
import coil.load
|
2022-11-17 20:29:12 +05:30
|
|
|
import coil.request.CachePolicy
|
2022-10-15 19:59:12 +05:30
|
|
|
import coil.request.ImageRequest
|
2022-08-15 14:17:31 +05:30
|
|
|
import com.github.libretube.api.CronetHelper
|
2022-09-08 21:59:00 +05:30
|
|
|
import com.github.libretube.constants.PreferenceKeys
|
2023-03-07 07:14:09 +05:30
|
|
|
import com.github.libretube.extensions.toAndroidUri
|
2023-03-17 05:47:45 +05:30
|
|
|
import com.github.libretube.extensions.toAndroidUriOrNull
|
2023-01-31 21:13:39 +05:30
|
|
|
import com.github.libretube.util.DataSaverMode
|
2023-03-07 07:14:09 +05:30
|
|
|
import java.nio.file.Path
|
2022-08-15 14:17:31 +05:30
|
|
|
|
|
|
|
object ImageHelper {
|
|
|
|
lateinit var imageLoader: ImageLoader
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the image loader
|
|
|
|
*/
|
|
|
|
fun initializeImageLoader(context: Context) {
|
2022-08-25 14:59:12 +05:30
|
|
|
val maxImageCacheSize = PreferenceHelper.getString(
|
2022-08-15 14:17:31 +05:30
|
|
|
PreferenceKeys.MAX_IMAGE_CACHE,
|
2022-11-17 20:29:12 +05:30
|
|
|
""
|
|
|
|
)
|
2022-08-15 14:17:31 +05:30
|
|
|
|
|
|
|
imageLoader = ImageLoader.Builder(context)
|
|
|
|
.callFactory(CronetHelper.callFactory)
|
2022-11-17 20:29:12 +05:30
|
|
|
.apply {
|
|
|
|
when (maxImageCacheSize) {
|
|
|
|
"" -> {
|
2022-11-17 21:58:07 +05:30
|
|
|
diskCachePolicy(CachePolicy.DISABLED)
|
2022-11-17 20:29:12 +05:30
|
|
|
}
|
|
|
|
else -> diskCache(
|
|
|
|
DiskCache.Builder()
|
2023-01-09 20:19:09 +05:30
|
|
|
.directory(context.cacheDir.resolve("coil"))
|
2022-11-17 20:29:12 +05:30
|
|
|
.maxSizeBytes(maxImageCacheSize.toInt() * 1024 * 1024L)
|
|
|
|
.build()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2022-08-15 14:17:31 +05:30
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* load an image from a url into an imageView
|
|
|
|
*/
|
|
|
|
fun loadImage(url: String?, target: ImageView) {
|
|
|
|
// only load the image if the data saver mode is disabled
|
2023-03-11 20:41:43 +05:30
|
|
|
if (DataSaverMode.isEnabled(target.context) || url == null) return
|
|
|
|
val urlToLoad = ProxyHelper.unwrapIfEnabled(url)
|
|
|
|
target.load(urlToLoad, imageLoader)
|
2022-08-15 14:17:31 +05:30
|
|
|
}
|
2022-10-15 19:59:12 +05:30
|
|
|
|
2023-03-07 07:14:09 +05:30
|
|
|
fun downloadImage(context: Context, url: String, path: Path) {
|
2023-01-21 22:50:45 +05:30
|
|
|
getAsync(context, url) { bitmap ->
|
2023-03-17 05:47:45 +05:30
|
|
|
saveImage(context, bitmap, path.toAndroidUri())
|
2023-01-21 22:50:45 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getAsync(context: Context, url: String?, onSuccess: (Bitmap) -> Unit) {
|
2022-10-15 19:59:12 +05:30
|
|
|
val request = ImageRequest.Builder(context)
|
|
|
|
.data(url)
|
2023-03-01 05:02:51 +05:30
|
|
|
.target { onSuccess(it.toBitmap()) }
|
2022-10-15 19:59:12 +05:30
|
|
|
.build()
|
|
|
|
|
|
|
|
imageLoader.enqueue(request)
|
|
|
|
}
|
|
|
|
|
2023-03-07 07:14:09 +05:30
|
|
|
fun getDownloadedImage(context: Context, path: Path): Bitmap? {
|
2023-03-17 05:47:45 +05:30
|
|
|
return path.toAndroidUriOrNull()?.let { getImage(context, it) }
|
2022-10-15 19:59:12 +05:30
|
|
|
}
|
2022-10-19 23:23:18 +05:30
|
|
|
|
2022-10-15 19:59:12 +05:30
|
|
|
private fun saveImage(context: Context, bitmapImage: Bitmap, imagePath: Uri) {
|
2023-03-05 14:17:53 +05:30
|
|
|
context.contentResolver.openOutputStream(imagePath)?.use {
|
|
|
|
bitmapImage.compress(Bitmap.CompressFormat.PNG, 25, it)
|
2022-10-15 19:59:12 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getImage(context: Context, imagePath: Uri): Bitmap? {
|
2023-03-05 14:17:53 +05:30
|
|
|
return context.contentResolver.openInputStream(imagePath)?.use {
|
|
|
|
BitmapFactory.decodeStream(it)
|
2022-10-15 19:59:12 +05:30
|
|
|
}
|
|
|
|
}
|
2023-01-15 17:50:33 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a squared bitmap with the same width and height from a bitmap
|
|
|
|
* @param bitmap The bitmap to resize
|
|
|
|
*/
|
|
|
|
fun getSquareBitmap(bitmap: Bitmap?): Bitmap? {
|
|
|
|
bitmap ?: return null
|
|
|
|
val newSize = minOf(bitmap.width, bitmap.height)
|
|
|
|
return Bitmap.createBitmap(
|
|
|
|
bitmap,
|
|
|
|
(bitmap.width - newSize) / 2,
|
|
|
|
(bitmap.height - newSize) / 2,
|
|
|
|
newSize,
|
|
|
|
newSize
|
|
|
|
)
|
|
|
|
}
|
2022-08-15 14:17:31 +05:30
|
|
|
}
|