Skip to content

Commit

Permalink
Fix: Cache (thumbnails) cleared after watching a video (#762)
Browse files Browse the repository at this point in the history
* fix: thumbnails cleared after playing a video

* lint: run ktlintFormat
  • Loading branch information
anilbeesetti authored Nov 26, 2023
1 parent 6c61570 commit b7df714
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -310,16 +310,6 @@ fun Context.convertToUTF8(inputUri: Uri, inputStreamReader: InputStreamReader):
return inputUri
}

fun Context.clearCache() {
try {
cacheDir.listFiles()?.onEach {
if (it.isFile) it.delete()
}
} catch (e: Exception) {
e.printStackTrace()
}
}

/**
* For this to work set android:requestLegacyExternalStorage=true in AndroidManifest.xml
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class LocalMediaInfoSynchronizer @Inject constructor(
media.collect { mediumUri ->
val path = context.getPath(mediumUri) ?: return@collect
val medium = mediumDao.getWithInfo(path) ?: return@collect
Log.d(TAG, "sync: $mediumUri - ${medium.mediumEntity.thumbnailPath}")
if (medium.mediumEntity.thumbnailPath?.let { File(it) }?.exists() == true) return@collect

Log.d(TAG, "sync: $mediumUri")
Expand All @@ -56,7 +57,7 @@ class LocalMediaInfoSynchronizer @Inject constructor(
val videoStreamInfo = mediaInfo.videoStream?.toVideoStreamInfoEntity(medium.mediumEntity.path)
val audioStreamsInfo = mediaInfo.audioStreams.map { it.toAudioStreamInfoEntity(medium.mediumEntity.path) }
val subtitleStreamsInfo = mediaInfo.subtitleStreams.map { it.toSubtitleStreamInfoEntity(medium.mediumEntity.path) }
val thumbnailPath = thumbnail?.saveTo(storageDir = context.cacheDir, quality = 30)
val thumbnailPath = thumbnail?.saveTo(storageDir = File(context.cacheDir, "thumbnails"), quality = 30)

mediumDao.upsert(medium.mediumEntity.copy(format = mediaInfo.format, thumbnailPath = thumbnailPath))
videoStreamInfo?.let { mediumDao.upsertVideoStreamInfo(it) }
Expand Down Expand Up @@ -111,6 +112,7 @@ fun SubtitleStream.toSubtitleStreamInfoEntity(mediumPath: String) = SubtitleStre
)

suspend fun Bitmap.saveTo(storageDir: File, quality: Int = 100): String? = withContext(Dispatchers.IO) {
if (!storageDir.exists()) storageDir.mkdir()
val thumbnailFileName = "thumbnail-${System.currentTimeMillis()}"
val thumbFile = File(storageDir, thumbnailFileName)
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import com.google.android.material.color.DynamicColors
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import dagger.hilt.android.AndroidEntryPoint
import dev.anilbeesetti.nextplayer.core.common.Utils
import dev.anilbeesetti.nextplayer.core.common.extensions.clearCache
import dev.anilbeesetti.nextplayer.core.common.extensions.convertToUTF8
import dev.anilbeesetti.nextplayer.core.common.extensions.getFilenameFromUri
import dev.anilbeesetti.nextplayer.core.common.extensions.getMediaContentUri
Expand Down Expand Up @@ -632,7 +631,6 @@ class PlayerActivity : AppCompatActivity() {
}

override fun finish() {
clearCache()
if (playerApi.shouldReturnResult) {
val result = playerApi.getResult(
isPlaybackFinished = isPlaybackFinished,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ fun ScreenOrientation.toActivityOrientation(videoOrientation: Int? = null): Int
ScreenOrientation.LANDSCAPE_REVERSE -> ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
ScreenOrientation.LANDSCAPE_AUTO -> ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
ScreenOrientation.PORTRAIT -> ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT
ScreenOrientation.VIDEO_ORIENTATION ->
videoOrientation ?: ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
ScreenOrientation.VIDEO_ORIENTATION -> videoOrientation ?: ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
}
}

0 comments on commit b7df714

Please sign in to comment.