Skip to content

Commit

Permalink
Add localized error message.
Browse files Browse the repository at this point in the history
Http error are currently not localized.
  • Loading branch information
StaehliJ committed Sep 22, 2023
1 parent f12be0d commit fbd7f26
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@
*/
package ch.srgssr.pillarbox.core.business

import android.content.Context
import android.os.RemoteException
import android.util.Pair
import androidx.media3.common.ErrorMessageProvider
import androidx.media3.common.PlaybackException
import ch.srgssr.pillarbox.core.business.integrationlayer.data.BlockReasonException
import ch.srgssr.pillarbox.core.business.integrationlayer.data.ResourceNotFoundException
import io.ktor.client.plugins.ClientRequestException
import kotlinx.serialization.SerializationException

/**
* Process error message from [PlaybackException]
*/
class SRGErrorMessageProvider : ErrorMessageProvider<PlaybackException> {
class SRGErrorMessageProvider(private val context: Context) : ErrorMessageProvider<PlaybackException> {

override fun getErrorMessage(throwable: PlaybackException): Pair<Int, String> {
return when (val cause = throwable.cause) {
is BlockReasonException -> {
Pair.create(0, cause.blockReason.name)
val message = context.resources.getStringArray(R.array.blockReasonArray)[cause.blockReason.ordinal]
Pair.create(0, message)
}
// When using MediaController, RemoteException is send instead of HttpException.
is RemoteException ->
Expand All @@ -31,11 +34,15 @@ class SRGErrorMessageProvider : ErrorMessageProvider<PlaybackException> {
}

is ResourceNotFoundException -> {
Pair.create(0, "Can't find Resource to play")
Pair.create(0, context.getString(R.string.noPlayableResourceFound))
}

is SerializationException -> {
Pair.create(0, context.getString(R.string.invalidDataError))
}

else -> {
Pair.create(throwable.errorCode, "${throwable.localizedMessage} (${throwable.errorCodeName})")
Pair.create(throwable.errorCode, context.getString(R.string.unkownError))
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions pillarbox-core-business/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (c) 2023. SRG SSR. All rights reserved.
~ License information is available from the LICENSE file.
-->
<resources>
<string name="invalidDataError">The data is invalid</string>
<string name="blockReason_geoBlock">Dieser Inhalt ist ausserhalb der Schweiz nicht verfügbar.</string>
<string name="blockReason_legal">Dieser Inhalt ist aus rechtlichen Gründen nicht verfügbar.</string>
<string name="blockReason_commercial">Dieser Werbe-Inhalt ist nicht verfügbar.</string>
<string name="blockReason_ageRating18">Dieser Inhalt ist aus Gründen des Jugendschutzes nur zwischen 22:00 und 5:00 Uhr verfügbar.</string>
<string name="blockReason_ageRating12">Dieser Inhalt ist aus Gründen des Jugendschutzes nur zwischen 20:00 und 6:00 Uhr verfügbar.</string>
<string name="blockReason_startDate">Dieser Inhalt ist noch nicht verfügbar.</string>
<string name="blockReason_endDate">Dieser Inhalt ist nicht mehr verfügbar.</string>
<string name="blockReason_unknown">Dieser Inhalt ist nicht verfügbar.</string>
</resources>
17 changes: 17 additions & 0 deletions pillarbox-core-business/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (c) 2023. SRG SSR. All rights reserved.
~ License information is available from the LICENSE file.
-->
<resources>
<string name="invalidDataError">Les données sont invalides.</string>
<string name="noPlayableResourceFound">Aucune ressource jouable n\'a pu être trouvée.</string>
<string name="blockReason_geoBlock">Ce contenu n\'est pas disponible hors de Suisse.</string>
<string name="blockReason_legal">Ce contenu a été retiré par décision de justice.</string>
<string name="blockReason_commercial">Ce contenu n’est actuellement pas disponible.</string>
<string name="blockReason_ageRating18">Ce contenu n\'est disponible qu\'entre 22h et 5h afin de protéger le jeune public.</string>
<string name="blockReason_ageRating12">Ce contenu n\'est disponible qu\'entre 20h et 6h afin de protéger le jeune public.</string>
<string name="blockReason_startDate">Ce contenu n’est pas encore disponible.</string>
<string name="blockReason_endDate">Ce contenu n’est plus disponible.</string>
<string name="blockReason_unknown">Ce contenu n’est pas disponible.</string>
<string name="unkownError">Erreur inconnue</string>
</resources>
13 changes: 13 additions & 0 deletions pillarbox-core-business/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (c) 2023. SRG SSR. All rights reserved.
~ License information is available from the LICENSE file.
-->
<resources>
<string name="invalidDataError">The data is invalid</string>
<string name="blockReason_geoBlock">Questo media non è disponibile fuori dalla Svizzera.</string>
<string name="blockReason_legal">Questo media non è disponibile a causa di restrizioni legali.</string>
<string name="blockReason_commercial">Questo contenuto commerciale non è disponibile.</string>
<string name="blockReason_startDate">Questo media non è ancora disponibile.</string>
<string name="blockReason_endDate">Questo media non è più disponibile.</string>
<string name="blockReason_unknown">Questo media non è disponibile.</string>
</resources>
13 changes: 13 additions & 0 deletions pillarbox-core-business/src/main/res/values-rm/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (c) 2023. SRG SSR. All rights reserved.
~ License information is available from the LICENSE file.
-->
<resources>
<string name="invalidDataError">The data is invalid</string>
<string name="blockReason_geoBlock">Quest medium n\'è betg disponibel ordaifer la Svizra.</string>
<string name="blockReason_legal">Quest medium n\'è betg disponibel perquei ch\'el è scadì.</string>
<string name="blockReason_commercial">Quest medium commerzial n\'è betg disponibel.</string>
<string name="blockReason_startDate">Quest medium n\'è betg anc disponibel.</string>
<string name="blockReason_endDate">Quest medium n\'è betg pli disponibel.</string>
<string name="blockReason_unknown">Quest medium n\'è betg disponibel.</string>
</resources>
29 changes: 29 additions & 0 deletions pillarbox-core-business/src/main/res/values/string.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (c) 2023. SRG SSR. All rights reserved.
~ License information is available from the LICENSE file.
-->
<resources>
<string name="unkownError">Unknown error</string>
<string name="invalidDataError">The data is invalid</string>
<string name="noPlayableResourceFound">No playable resources could be found.</string>
<string name="blockReason_geoBlock">This content is not available outside Switzerland.</string>
<string name="blockReason_legal">This content is not available due to legal restrictions.</string>
<string name="blockReason_commercial">This commercial content is not available.</string>
<string name="blockReason_ageRating18">To protect children this content is only available between 10PM and 5AM.</string>
<string name="blockReason_ageRating12">To protect children this content is only available between 8PM and 6AM.</string>
<string name="blockReason_startDate">This content is not available yet.</string>
<string name="blockReason_endDate">This content is not available anymore.</string>
<string name="blockReason_unknown">This content is not available.</string>

<!-- Same order that the ordinal of BlockReason enum -->
<string-array name="blockReasonArray" translatable="false">
<item>@string/blockReason_geoBlock</item>
<item>@string/blockReason_legal</item>
<item>@string/blockReason_commercial</item>
<item>@string/blockReason_ageRating18</item>
<item>@string/blockReason_ageRating12</item>
<item>@string/blockReason_startDate</item>
<item>@string/blockReason_endDate</item>
<item>@string/blockReason_unknown</item>
</string-array>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontStyle
import androidx.media3.common.PlaybackException
import ch.srgssr.pillarbox.core.business.SRGErrorMessageProvider
Expand All @@ -29,8 +30,9 @@ import ch.srgssr.pillarbox.core.business.SRGErrorMessageProvider
*/
@Composable
fun PlayerError(playerError: PlaybackException, modifier: Modifier = Modifier, onRetry: () -> Unit) {
val errorMessageProvider = remember {
SRGErrorMessageProvider()
val context = LocalContext.current
val errorMessageProvider = remember(context) {
SRGErrorMessageProvider(context)
}
Surface(modifier, color = Color.Black) {
Box(
Expand Down

0 comments on commit fbd7f26

Please sign in to comment.