Skip to content

Commit

Permalink
Migrate Vector to be an enum (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 authored Nov 19, 2024
1 parent e536eec commit 3f6e6e1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class SRGMediaItemBuilder internal constructor(mediaItem: MediaItem) {
private var host: URL = IlHost.DEFAULT
private var forceSAM: Boolean = false
private var ilLocation: IlLocation? = null
private var vector: String = Vector.MOBILE
private var vector: Vector = Vector.MOBILE

init {
urn = mediaItem.mediaId
Expand All @@ -91,9 +91,11 @@ class SRGMediaItemBuilder internal constructor(mediaItem: MediaItem) {
if (uri.toString().contains(PATH) && urn.isValidMediaUrn()) {
uri.host?.let { hostname -> host = URL(Uri.Builder().scheme(host.protocol).authority(hostname).build().toString()) }
this.urn = urn!!
this.forceSAM = uri.getQueryParameter(PARAM_FORCE_SAM)?.toBooleanStrictOrNull() ?: false
this.forceSAM = uri.getQueryParameter(PARAM_FORCE_SAM)?.toBooleanStrictOrNull() == true
this.ilLocation = uri.getQueryParameter(PARAM_FORCE_LOCATION)?.let { IlLocation.fromName(it) }
uri.getQueryParameter(PARAM_VECTOR)?.let { vector = it }
uri.getQueryParameter(PARAM_VECTOR)
?.let { Vector.fromLabel(it) }
?.let { vector = it }
}
}
}
Expand Down Expand Up @@ -155,9 +157,9 @@ class SRGMediaItemBuilder internal constructor(mediaItem: MediaItem) {
/**
* Sets the vector.
*
* @param vector The vector to forward to the integration layer. Should be either [Vector.MOBILE] or [Vector.TV].
* @param vector The vector to forward to the integration layer.
*/
fun vector(vector: String) {
fun vector(vector: Vector) {
this.vector = vector
}

Expand Down Expand Up @@ -187,9 +189,7 @@ class SRGMediaItemBuilder internal constructor(mediaItem: MediaItem) {
ilLocation?.let {
appendQueryParameter(PARAM_FORCE_LOCATION, it.toString())
}
if (vector.isNotBlank()) {
appendQueryParameter(PARAM_VECTOR, vector)
}
appendQueryParameter(PARAM_VECTOR, vector.toString())
appendQueryParameter(PARAM_ONLY_CHAPTERS, true.toString())
}.build()
mediaItemBuilder.setUri(uri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,53 @@ package ch.srgssr.pillarbox.core.business.integrationlayer.service

import android.content.Context
import android.content.res.Configuration
import ch.srgssr.pillarbox.core.business.integrationlayer.service.Vector.MOBILE
import ch.srgssr.pillarbox.core.business.integrationlayer.service.Vector.TV

/**
* Provides constants and utilities to determine the device vector ([MOBILE] or [TV]).
* Represents a vector used to distinguish between different device types.
*
* @param label The label of this vector.
*/
object Vector {
enum class Vector(private val label: String) {
/**
* Constant for the TV vector.
* Represents the mobile vector.
*/
const val TV = "TVPLAY"
MOBILE("APPPLAY"),

/**
* Constant for the mobile vector.
* Represents the TV vector.
*/
const val MOBILE = "APPPLAY"
TV("TVPLAY");

/**
* Retrieves the vector based on the device type.
*
* @return The vector for the current device type.
*
* @receiver The [Context] used to access system resources.
*/
fun Context.getVector(): String {
val uiMode = resources.configuration.uiMode
return if (uiMode and Configuration.UI_MODE_TYPE_MASK == Configuration.UI_MODE_TYPE_TELEVISION) {
TV
} else {
MOBILE
override fun toString(): String {
return label
}

@Suppress("UndocumentedPublicClass")
companion object {
/**
* Retrieves a [Vector] associated with the given [label].
*
* @param label The label to search for.
* @return The [Vector] associated with the label, or `null` if not found.
*/
fun fromLabel(label: String): Vector? {
return entries.find { it.label.equals(label, ignoreCase = true) }
}

/**
* Retrieves the vector based on the device type.
*
* @return The vector for the current device type.
*
* @receiver The [Context] used to access system resources.
*/
fun Context.getVector(): Vector {
val uiMode = resources.configuration.uiMode
return if (uiMode and Configuration.UI_MODE_TYPE_MASK == Configuration.UI_MODE_TYPE_TELEVISION) {
TV
} else {
MOBILE
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,6 @@ class SRGMediaItemBuilderTest {
assertEquals(MediaMetadata.EMPTY, mediaItem.mediaMetadata)
}

@Test
fun `Check no vector`() {
val urn = "urn:rts:audio:3262363"
val vector = ""
val mediaItem = SRGMediaItem(urn) {
vector(vector)
}
val localConfiguration = mediaItem.localConfiguration

assertNotNull(localConfiguration)
assertEquals(urn.toIlUri(vector = vector), localConfiguration.uri)
assertEquals(MimeTypeSrg, localConfiguration.mimeType)
assertEquals(urn, mediaItem.mediaId)
assertEquals(MediaMetadata.EMPTY, mediaItem.mediaMetadata)
}

@Test
fun `Check uri from existing MediaItem`() {
val urn = "urn:rts:audio:3262363"
Expand All @@ -142,7 +126,7 @@ class SRGMediaItemBuilderTest {
.setUri("https://il-stage.srgssr.ch/integrationlayer/2.1/mediaComposition/byUrn/$urn?vector=${Vector.TV}")
.build()
val urn2 = "urn:rts:audio:123456"
val mediaItem = SRGMediaItem(urn) {
val mediaItem = inputMediaItem.buildUpon {
host(IlHost.PROD)
vector(Vector.MOBILE)
urn(urn2)
Expand Down Expand Up @@ -216,15 +200,15 @@ class SRGMediaItemBuilderTest {
companion object {
fun String.toIlUri(
host: URL = IlHost.DEFAULT,
vector: String = Vector.MOBILE,
vector: Vector = Vector.MOBILE,
forceSAM: Boolean = false,
ilLocation: IlLocation? = null,
): Uri {
val samPath = if (forceSAM) "sam/" else ""
val queryParameters = listOfNotNull(
if (forceSAM) "forceSAM" to true else null,
if (ilLocation != null) "forceLocation" to ilLocation else null,
if (vector.isNotBlank()) "vector" to vector else null,
"vector" to vector,
"onlyChapters" to true,
).joinToString(separator = "&") { (name, value) ->
"$name=$value"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ package ch.srgssr.pillarbox.core.business.integrationlayer.service
import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.srgssr.pillarbox.core.business.integrationlayer.service.Vector.getVector
import ch.srgssr.pillarbox.core.business.integrationlayer.service.Vector.Companion.getVector
import org.junit.runner.RunWith
import org.robolectric.annotation.Config
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull

@RunWith(AndroidJUnit4::class)
class VectorTest {
Expand All @@ -23,6 +24,23 @@ class VectorTest {
context = ApplicationProvider.getApplicationContext()
}

@Test
fun `fromLabel MOBILE`() {
assertEquals(Vector.MOBILE, Vector.fromLabel("appplay"))
assertEquals(Vector.MOBILE, Vector.fromLabel("APPPLAY"))
}

@Test
fun `fromLabel TV`() {
assertEquals(Vector.TV, Vector.fromLabel("tvplay"))
assertEquals(Vector.TV, Vector.fromLabel("TVPLAY"))
}

@Test
fun `fromLabel invalid label`() {
assertNull(Vector.fromLabel("INVALID"))
}

@Test
fun getVector() {
assertEquals(Vector.MOBILE, context.getVector())
Expand Down

0 comments on commit 3f6e6e1

Please sign in to comment.