diff --git a/app/src/main/java/com/github/libretube/obj/DownloadedFile.kt b/app/src/main/java/com/github/libretube/obj/DownloadedFile.kt index 7b5dc691a..ffa9c9d55 100644 --- a/app/src/main/java/com/github/libretube/obj/DownloadedFile.kt +++ b/app/src/main/java/com/github/libretube/obj/DownloadedFile.kt @@ -1,10 +1,12 @@ package com.github.libretube.obj +import android.graphics.Bitmap import com.github.libretube.api.obj.Streams data class DownloadedFile( val name: String, val size: Long, val type: Int, - var metadata: Streams? = null + var metadata: Streams? = null, + var thumbnail: Bitmap? = null ) diff --git a/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt index 8f29b53d7..b1f35725b 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt @@ -42,6 +42,8 @@ class DownloadsAdapter( " • " + it.uploadDate } + thumbnailImage.setImageBitmap(file.thumbnail) + root.setOnClickListener { val intent = Intent(root.context, OfflinePlayerActivity::class.java).also { it.putExtra(IntentData.fileName, file.name) diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/DownloadDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/DownloadDialog.kt index 32c5754e3..f721cbb3f 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/DownloadDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/DownloadDialog.kt @@ -17,6 +17,7 @@ import com.github.libretube.databinding.DialogDownloadBinding import com.github.libretube.extensions.TAG import com.github.libretube.extensions.sanitize import com.github.libretube.services.DownloadService +import com.github.libretube.util.ImageHelper import com.github.libretube.util.MetadataHelper import com.github.libretube.util.ThemeHelper import com.google.android.material.dialog.MaterialAlertDialogBuilder @@ -137,8 +138,13 @@ class DownloadDialog( val metadataHelper = MetadataHelper(requireContext()) metadataHelper.createMetadata(fileName, streams) - - Log.e("meta", metadataHelper.getMetadata(fileName).toString()) + streams.thumbnailUrl?.let { thumbnailUrl -> + ImageHelper.downloadImage( + requireContext(), + thumbnailUrl, + fileName + ) + } val intent = Intent(context, DownloadService::class.java) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt index b2242e5b6..fb7812ca9 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt @@ -11,6 +11,7 @@ import com.github.libretube.databinding.FragmentDownloadsBinding import com.github.libretube.ui.adapters.DownloadsAdapter import com.github.libretube.ui.base.BaseFragment import com.github.libretube.util.DownloadHelper +import com.github.libretube.util.ImageHelper import com.github.libretube.util.MetadataHelper class DownloadsFragment : BaseFragment() { @@ -37,6 +38,9 @@ class DownloadsFragment : BaseFragment() { metadataHelper.getMetadata(it.name)?.let { streams -> it.metadata = streams } + ImageHelper.getDownloadedImage(requireContext(), it.name)?.let { bitmap -> + it.thumbnail = bitmap + } } binding.downloadsEmpty.visibility = View.GONE diff --git a/app/src/main/java/com/github/libretube/util/DownloadHelper.kt b/app/src/main/java/com/github/libretube/util/DownloadHelper.kt index 1d8af58c3..30d52d0ca 100644 --- a/app/src/main/java/com/github/libretube/util/DownloadHelper.kt +++ b/app/src/main/java/com/github/libretube/util/DownloadHelper.kt @@ -39,6 +39,13 @@ object DownloadHelper { ).createDir() } + fun getThumbnailDir(context: Context): File { + return File( + getOfflineStorageDir(context), + "thumbnail" + ).createDir() + } + fun getDownloadedFiles(context: Context): MutableList { val videoFiles = getVideoDir(context).listFiles() val audioFiles = getAudioDir(context).listFiles()?.toMutableList() diff --git a/app/src/main/java/com/github/libretube/util/ImageHelper.kt b/app/src/main/java/com/github/libretube/util/ImageHelper.kt index 8b1bcc2be..f3fcad18d 100644 --- a/app/src/main/java/com/github/libretube/util/ImageHelper.kt +++ b/app/src/main/java/com/github/libretube/util/ImageHelper.kt @@ -1,12 +1,20 @@ package com.github.libretube.util import android.content.Context +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.graphics.drawable.BitmapDrawable +import android.net.Uri import android.widget.ImageView import coil.ImageLoader import coil.disk.DiskCache import coil.load +import coil.request.ImageRequest import com.github.libretube.api.CronetHelper import com.github.libretube.constants.PreferenceKeys +import okio.use +import java.io.File +import java.io.FileOutputStream object ImageHelper { lateinit var imageLoader: ImageLoader @@ -42,4 +50,51 @@ object ImageHelper { ) if (!dataSaverModeEnabled) target.load(url, imageLoader) } + + fun downloadImage(context: Context, url: String, fileName: String) { + val request = ImageRequest.Builder(context) + .data(url) + .target { result -> + val bitmap = (result as BitmapDrawable).bitmap + saveImage( + context, + bitmap, + Uri.fromFile( + File( + DownloadHelper.getThumbnailDir(context), + fileName + ) + ) + ) + } + .build() + + imageLoader.enqueue(request) + } + + fun getDownloadedImage(context: Context, fileName: String): Bitmap? { + return getImage( + context, + Uri.fromFile( + File( + DownloadHelper.getThumbnailDir(context), + fileName + ) + ) + ) + } + 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 + } } diff --git a/app/src/main/res/layout/downloaded_media_row.xml b/app/src/main/res/layout/downloaded_media_row.xml index 339b374e9..f04296262 100644 --- a/app/src/main/res/layout/downloaded_media_row.xml +++ b/app/src/main/res/layout/downloaded_media_row.xml @@ -1,5 +1,7 @@ + + + + + + + android:orientation="vertical">