Skip to content

Commit

Permalink
[FIX] Hopefully fixed the high memory bug and ANR caused by Thumbnail…
Browse files Browse the repository at this point in the history
…Fetcher.
  • Loading branch information
iZakirSheikh committed Nov 19, 2024
1 parent 76e31d0 commit f2614c9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 14 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ android {
applicationId = "com.prime.player"
minSdk = 21
targetSdk = 35
versionCode = 163
versionName = "3.1.0-alpha02"
versionCode = 169
versionName = "3.1.0-alpha03"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables { useSupportLibrary = true }
// init different config fields.
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/prime/media/impl/Initializers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import com.zs.core.db.Playlists2
import com.zs.core.db.Playlists2.Companion.invoke
import com.zs.core.store.MediaProvider
import com.zs.core_ui.coil.MediaMetaDataArtFetcher
import com.zs.core_ui.coil.VideoThumbnailFetcher
import com.zs.core_ui.toast.ToastHostState
import org.koin.android.ext.koin.androidContext
import org.koin.core.KoinApplication
Expand All @@ -66,6 +65,7 @@ import com.prime.media.R
import com.zs.core.playback.PlaybackController
import com.zs.core.playback.PlaybackController.Companion.invoke as PlaybackController
import com.zs.core_ui.Anim
import com.zs.core_ui.coil.ThumbnailFetcher

private const val TAG = "Initializers"

Expand Down Expand Up @@ -159,7 +159,7 @@ class CoilInitializer : Initializer<Unit> {
.components {
if (!preference(Settings.USE_LEGACY_ARTWORK_METHOD))
add(MediaMetaDataArtFetcher.Factory())
add(VideoThumbnailFetcher.Factory())
add(ThumbnailFetcher.Factory())
}
// Set the created ImageLoader as the default for Coil.
Coil.setImageLoader(imageLoader.build())
Expand Down
Binary file removed app/src/main/res/drawable/noise.webp
Binary file not shown.
2 changes: 2 additions & 0 deletions app/src/main/res/values/what_s_new.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<b>What\'s new</b>
<font color='grey'>
\n <b>Celebrating </b> Our Exciting 3.0.0 Release!💐
\n 🐞 <b>Fixed high memory usage bug:</b> Fixed a bug that caused high memory usage
after adding video browsing support.
\n 🚀 <b>Introduced a Brand-New In-App Video Browser Screen</b>: Seamlessly browse and play
videos directly within the app.
\n 🛠 <b>Squashed the Video Surface Delay Bug</b>: Fixed an old issue that caused the video
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright 2024 Zakir Sheikh
*
* Created by Zakir Sheikh on 16-11-2024.
* Created by Zakir Sheikh on 19-11-2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,9 +45,31 @@ import coil.size.pxOrElse
import kotlin.math.roundToInt
import android.util.Size as ThumbnailSize

private const val TAG = "VideoThumbnailFetcher"
private const val TAG = "ThumbnailFetcher"

class VideoThumbnailFetcher(
/**
* Enables or disables the use of android's cached thumbnails for this request.
*
* If enabled and a cached thumbnail is available, Coil will use it instead of
* generating a thumbnail from the image's URI. This can improve performance,
* especially for large images.
*
* By default, this is enabled.
*
* @param value True to use cached thumbnails if available, false otherwise.
*/
fun ImageRequest.Builder.preferCachedThumbnail(value: Boolean)
= setParameter("coil#preferCachedThumbnail", value)

/**
* Whether to use cached thumbnails for this request.
*
* @see preferCachedThumbnail
*/
private val Options.preferCachedThumbnail: Boolean
get() = parameters.value("coil#preferCachedThumbnail") ?: true

class ThumbnailFetcher(
private val data: Uri,
private val options: Options
) : Fetcher {
Expand All @@ -66,17 +88,25 @@ class VideoThumbnailFetcher(
// Early exit if the URI is not a content URI
if (data.scheme != ContentResolver.SCHEME_CONTENT) return null

// Early exit if the request is
// because we only support loading thumbnails from this that too of images and videos
// that android automatically generated.
if (!options.preferCachedThumbnail) {
Log.d(TAG, "preferCachedThumbnail: ${options.preferCachedThumbnail}")
return null
}

val context = options.context
// Retrieve the MIME type of the content
val mimeType = context.contentResolver.getType(data) ?: return null

// Check if the MIME type is supported (image or video)
if (!mimeType.startsWith("video/")) {
if (/*!mimeType.startsWith("image/") && */!mimeType.startsWith("video/")) {
return null
}

// Create and return a ThumbnailFetcher instance
return VideoThumbnailFetcher(data, options)
return ThumbnailFetcher(data, options)
}
}

Expand Down Expand Up @@ -201,4 +231,4 @@ class VideoThumbnailFetcher(
dataSource = DataSource.DISK
)
}
}
}
8 changes: 4 additions & 4 deletions core-ui/src/main/java/com/zs/core_ui/toast/Toast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -349,18 +349,18 @@ internal fun Toast(
drawRect(color = actionColor, size = size.copy(width = 3.dp.toPx()))
}
}
.thenIf(!colors.isLight) { border(
.border(
1.dp,
Brush.linearGradient(
listOf(
Color.Gray.copy(0.24f),
Color.Gray.copy(if(!colors.isLight) 0.24f else 0.48f),
Color.Transparent,
Color.Transparent,
Color.Gray.copy(0.24f)
Color.Gray.copy(if(!colors.isLight) 0.24f else 0.48f),
)
),
shape
) }
)
.visualEffect(ImageBrush.NoiseBrush, 0.60f, overlay = true)
.background(backgroundColor)
//.clip(shape)
Expand Down

0 comments on commit f2614c9

Please sign in to comment.