|
| 1 | +package com.theoplayer.android.ui.util |
| 2 | + |
| 3 | +import androidx.annotation.CheckResult |
| 4 | +import com.theoplayer.android.api.player.track.Track |
| 5 | +import com.theoplayer.android.api.player.track.mediatrack.MediaTrack |
| 6 | +import com.theoplayer.android.api.player.track.texttrack.TextTrack |
| 7 | +import com.theoplayer.android.api.player.track.texttrack.TextTrackType |
| 8 | +import java.util.Locale |
| 9 | + |
| 10 | +private const val LANGUAGE_UNDEFINED = "und" |
| 11 | + |
| 12 | +/** |
| 13 | + * Returns a name for the [Track.language] in the |
| 14 | + * [Locale.Category.DISPLAY] locale that is appropriate |
| 15 | + * for display to the user. |
| 16 | + * If such conversion is not possible, for instance |
| 17 | + * when [Track.language] is `null`, blank, or `"und"`, |
| 18 | + * returns `null`. |
| 19 | + */ |
| 20 | +@get:CheckResult |
| 21 | +internal val Track.localizedLanguageName: String? |
| 22 | + get() { |
| 23 | + val languageCode = this.language |
| 24 | + ?.takeUnless { it.isBlank() || it == LANGUAGE_UNDEFINED } |
| 25 | + ?: return null |
| 26 | + val locale = Locale.forLanguageTag(languageCode) |
| 27 | + val localisedLanguage = locale.getDisplayName(locale).orEmpty() |
| 28 | + return localisedLanguage.takeUnless { it.isBlank() } |
| 29 | + } |
| 30 | + |
| 31 | +/** |
| 32 | + * Constructs a label for the given [MediaTrack] instance. |
| 33 | + * |
| 34 | + * This returns the first non-empty entry from the list: |
| 35 | + * 1. Track label |
| 36 | + * 2. Track language display name |
| 37 | + */ |
| 38 | +internal fun constructLabel(track: MediaTrack<*>): String? { |
| 39 | + return track.label?.takeUnless { it.isBlank() } |
| 40 | + ?: track.localizedLanguageName |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Constructs a label for the given [TextTrack] instance. |
| 45 | + * The method works slightly different for different player version. |
| 46 | + * |
| 47 | + * On version 10 and below the logic checks the following and condition |
| 48 | + * and the first not `null` entry from the list: |
| 49 | + * 1. Track label if is not a language code |
| 50 | + * or a CEA-prefixed string. |
| 51 | + * 2. Track language display name |
| 52 | + * 3. Track caption channel if a text CEA-608 track |
| 53 | + * 4. Track label if was either a language code or a CEA-prefixed string |
| 54 | + * |
| 55 | + * If none of the above is satisfied, returns `null`. |
| 56 | + * |
| 57 | + * On version 11 and later the logic has slightly changed as |
| 58 | + * the player no longer constructs the [Track.getLabel] internally: |
| 59 | + * 1. Track label |
| 60 | + * 2. Track language display name |
| 61 | + * 3. Track caption channel |
| 62 | + */ |
| 63 | +internal fun constructLabel(track: TextTrack): String? { |
| 64 | + val label: String? = if ( |
| 65 | + THEOplayerGlobalExt.version.major < 11 && |
| 66 | + (isLabelCeaFormatted(track.label) || (track.label != null && track.language == track.label)) |
| 67 | + ) { |
| 68 | + // If we are below 11th major release |
| 69 | + // and the label is CEA-formatted we |
| 70 | + // can safely assume it was the last resort |
| 71 | + // option to produce a meaningful label, given |
| 72 | + // we cannot localize the language code in the player. |
| 73 | + null |
| 74 | + } else { |
| 75 | + // With 11 release, the player will no longer |
| 76 | + // prefix text tracks with "CC" for CEA-608 and CEA-708, |
| 77 | + // if [Track.label] is `null`. |
| 78 | + track.label |
| 79 | + } |
| 80 | + |
| 81 | + if (!label.isNullOrBlank()) { |
| 82 | + return label |
| 83 | + } |
| 84 | + |
| 85 | + track.localizedLanguageName?.let { return it } |
| 86 | + |
| 87 | + if (track.type == TextTrackType.CEA608) { |
| 88 | + track.captionChannelCompat |
| 89 | + ?.let { getLabelForChannelNumber(it) } |
| 90 | + ?.let { return it } |
| 91 | + |
| 92 | + track.label |
| 93 | + ?.takeUnless { it.isBlank() } |
| 94 | + ?.let { return it } |
| 95 | + } |
| 96 | + |
| 97 | + return null |
| 98 | +} |
0 commit comments