2022-08-15 14:17:31 +05:30
|
|
|
package com.github.libretube.util
|
|
|
|
|
|
|
|
import android.content.Context
|
2022-10-15 19:59:12 +05:30
|
|
|
import android.graphics.Bitmap
|
|
|
|
import android.graphics.BitmapFactory
|
|
|
|
import android.graphics.drawable.BitmapDrawable
|
|
|
|
import android.net.Uri
|
2022-08-15 14:17:31 +05:30
|
|
|
import android.widget.ImageView
|
|
|
|
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
|
2022-10-15 19:59:12 +05:30
|
|
|
import java.io.File
|
|
|
|
import java.io.FileOutputStream
|
2022-12-19 21:28:34 +05:30
|
|
|
import okio.use
|
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
|
2022-12-25 20:38:52 +05:30
|
|
|
if (!DataSaverMode.isEnabled(target.context)) target.load(url, imageLoader)
|
2022-08-15 14:17:31 +05:30
|
|
|
}
|
2022-10-15 19:59:12 +05:30
|
|
|
|
2022-12-21 21:10:48 +05:30
|
|
|
fun downloadImage(context: Context, url: String, path: String) {
|
2022-10-15 19:59:12 +05:30
|
|
|
val request = ImageRequest.Builder(context)
|
|
|
|
.data(url)
|
|
|
|
.target { result ->
|
|
|
|
val bitmap = (result as BitmapDrawable).bitmap
|
2022-12-21 21:10:48 +05:30
|
|
|
val file = File(path)
|
2022-11-11 23:09:56 +05:30
|
|
|
saveImage(context, bitmap, Uri.fromFile(file))
|
2022-10-15 19:59:12 +05:30
|
|
|
}
|
|
|
|
.build()
|
|
|
|
|
|
|
|
imageLoader.enqueue(request)
|
|
|
|
}
|
|
|
|
|
2022-12-21 21:10:48 +05:30
|
|
|
fun getDownloadedImage(context: Context, path: String): Bitmap? {
|
|
|
|
val file = File(path)
|
2022-11-11 23:13:01 +05:30
|
|
|
if (!file.exists()) return null
|
2022-11-11 23:09:56 +05:30
|
|
|
return getImage(context, Uri.fromFile(file))
|
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) {
|
|
|
|
context.contentResolver.openFileDescriptor(imagePath, "w")?.use {
|
|
|
|
FileOutputStream(it.fileDescriptor).use { fos ->
|
|
|
|
bitmapImage.compress(Bitmap.CompressFormat.PNG, 25, fos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getImage(context: Context, imagePath: Uri): Bitmap? {
|
|
|
|
context.contentResolver.openInputStream(imagePath)?.use {
|
|
|
|
return BitmapFactory.decodeStream(it)
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
2022-08-15 14:17:31 +05:30
|
|
|
}
|