From 1edcd5d16195cb6e8a224accf08d9da017059d6d Mon Sep 17 00:00:00 2001 From: Naveen Singh Date: Fri, 27 Sep 2024 23:15:25 +0530 Subject: [PATCH] Show user friendly error messages See https://github.com/FossifyOrg/Gallery/issues/309 --- .../gallery/activities/VideoPlayerActivity.kt | 2 +- .../org/fossify/gallery/extensions/Player.kt | 23 +++++++++++++++++++ .../gallery/fragments/VideoFragment.kt | 2 +- app/src/main/res/values/strings.xml | 7 +++++- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/gallery/activities/VideoPlayerActivity.kt b/app/src/main/kotlin/org/fossify/gallery/activities/VideoPlayerActivity.kt index ac6fe8b58..b4e58c0b6 100644 --- a/app/src/main/kotlin/org/fossify/gallery/activities/VideoPlayerActivity.kt +++ b/app/src/main/kotlin/org/fossify/gallery/activities/VideoPlayerActivity.kt @@ -311,7 +311,7 @@ open class VideoPlayerActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListen override fun onPlayerErrorChanged(error: PlaybackException?) { binding.errorMessageHolder.errorMessage.apply { if (error != null) { - text = error.localizedMessage ?: getString(R.string.failed_to_load_media) + text = error.getFriendlyMessage(context) setTextColor(if (context.config.blackBackground) Color.WHITE else context.getProperTextColor()) fadeIn() } else { diff --git a/app/src/main/kotlin/org/fossify/gallery/extensions/Player.kt b/app/src/main/kotlin/org/fossify/gallery/extensions/Player.kt index 05ea56c63..39d0a67da 100644 --- a/app/src/main/kotlin/org/fossify/gallery/extensions/Player.kt +++ b/app/src/main/kotlin/org/fossify/gallery/extensions/Player.kt @@ -1,6 +1,10 @@ package org.fossify.gallery.extensions +import android.content.Context +import androidx.media3.common.PlaybackException +import androidx.media3.common.PlaybackException.* import androidx.media3.common.Player +import org.fossify.gallery.R fun Player.mute() { volume = 0f @@ -9,3 +13,22 @@ fun Player.mute() { fun Player.unmute() { volume = 1f } + +fun PlaybackException.getFriendlyMessage(context: Context): String? { + val resource = when (errorCode) { + ERROR_CODE_PARSING_CONTAINER_MALFORMED, + ERROR_CODE_PARSING_MANIFEST_MALFORMED -> R.string.file_is_malformed_or_corrupted + + ERROR_CODE_DECODER_INIT_FAILED, + ERROR_CODE_DECODING_FAILED, + ERROR_CODE_DECODING_FORMAT_EXCEEDS_CAPABILITIES -> R.string.media_exceeds_device_capabilities + + ERROR_CODE_PARSING_CONTAINER_UNSUPPORTED, + ERROR_CODE_DECODING_FORMAT_UNSUPPORTED, + ERROR_CODE_PARSING_MANIFEST_UNSUPPORTED -> R.string.unsupported_format + + else -> return localizedMessage ?: context.getString(R.string.failed_to_load_media) + } + + return context.getString(resource) +} diff --git a/app/src/main/kotlin/org/fossify/gallery/fragments/VideoFragment.kt b/app/src/main/kotlin/org/fossify/gallery/fragments/VideoFragment.kt index fe5f2ba0d..89ef889d6 100644 --- a/app/src/main/kotlin/org/fossify/gallery/fragments/VideoFragment.kt +++ b/app/src/main/kotlin/org/fossify/gallery/fragments/VideoFragment.kt @@ -452,7 +452,7 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S if (error != null) { binding.videoPreview.beGone() binding.videoPlayOutline.beGone() - text = error.localizedMessage ?: getString(R.string.failed_to_load_media) + text = error.getFriendlyMessage(context) setTextColor(if (context.config.blackBackground) Color.WHITE else context.getProperTextColor()) fadeIn() } else { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 79b037a63..56e1173af 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -34,7 +34,6 @@ Reorder folders by dragging (Pro) Restoring to \'%s\' Fossify Gallery needs full access to display all your photos and videos. Go to Settings > Permissions > Photos and videos > Allow all. - Failed to load media. Filter media @@ -242,6 +241,12 @@ Toggle favorite Toggle file visibility + + Failed to load media. + File is malformed or corrupted. + Media exceeds device capabilities. + Unsupported format. + How can I make Fossify Gallery the default device gallery? First you have to find the currently default gallery in the Apps section of your device settings, look for a button that says something like \"Open by default\", click on it, then select \"Clear defaults\".