Skip to content

Commit 3f6e6e1

Browse files
authored
Migrate Vector to be an enum (#790)
1 parent e536eec commit 3f6e6e1

File tree

4 files changed

+68
-49
lines changed

4 files changed

+68
-49
lines changed

pillarbox-core-business/src/main/java/ch/srgssr/pillarbox/core/business/SRGMediaItem.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class SRGMediaItemBuilder internal constructor(mediaItem: MediaItem) {
8181
private var host: URL = IlHost.DEFAULT
8282
private var forceSAM: Boolean = false
8383
private var ilLocation: IlLocation? = null
84-
private var vector: String = Vector.MOBILE
84+
private var vector: Vector = Vector.MOBILE
8585

8686
init {
8787
urn = mediaItem.mediaId
@@ -91,9 +91,11 @@ class SRGMediaItemBuilder internal constructor(mediaItem: MediaItem) {
9191
if (uri.toString().contains(PATH) && urn.isValidMediaUrn()) {
9292
uri.host?.let { hostname -> host = URL(Uri.Builder().scheme(host.protocol).authority(hostname).build().toString()) }
9393
this.urn = urn!!
94-
this.forceSAM = uri.getQueryParameter(PARAM_FORCE_SAM)?.toBooleanStrictOrNull() ?: false
94+
this.forceSAM = uri.getQueryParameter(PARAM_FORCE_SAM)?.toBooleanStrictOrNull() == true
9595
this.ilLocation = uri.getQueryParameter(PARAM_FORCE_LOCATION)?.let { IlLocation.fromName(it) }
96-
uri.getQueryParameter(PARAM_VECTOR)?.let { vector = it }
96+
uri.getQueryParameter(PARAM_VECTOR)
97+
?.let { Vector.fromLabel(it) }
98+
?.let { vector = it }
9799
}
98100
}
99101
}
@@ -155,9 +157,9 @@ class SRGMediaItemBuilder internal constructor(mediaItem: MediaItem) {
155157
/**
156158
* Sets the vector.
157159
*
158-
* @param vector The vector to forward to the integration layer. Should be either [Vector.MOBILE] or [Vector.TV].
160+
* @param vector The vector to forward to the integration layer.
159161
*/
160-
fun vector(vector: String) {
162+
fun vector(vector: Vector) {
161163
this.vector = vector
162164
}
163165

@@ -187,9 +189,7 @@ class SRGMediaItemBuilder internal constructor(mediaItem: MediaItem) {
187189
ilLocation?.let {
188190
appendQueryParameter(PARAM_FORCE_LOCATION, it.toString())
189191
}
190-
if (vector.isNotBlank()) {
191-
appendQueryParameter(PARAM_VECTOR, vector)
192-
}
192+
appendQueryParameter(PARAM_VECTOR, vector.toString())
193193
appendQueryParameter(PARAM_ONLY_CHAPTERS, true.toString())
194194
}.build()
195195
mediaItemBuilder.setUri(uri)

pillarbox-core-business/src/main/java/ch/srgssr/pillarbox/core/business/integrationlayer/service/Vector.kt

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,53 @@ package ch.srgssr.pillarbox.core.business.integrationlayer.service
66

77
import android.content.Context
88
import android.content.res.Configuration
9-
import ch.srgssr.pillarbox.core.business.integrationlayer.service.Vector.MOBILE
10-
import ch.srgssr.pillarbox.core.business.integrationlayer.service.Vector.TV
119

1210
/**
13-
* Provides constants and utilities to determine the device vector ([MOBILE] or [TV]).
11+
* Represents a vector used to distinguish between different device types.
12+
*
13+
* @param label The label of this vector.
1414
*/
15-
object Vector {
15+
enum class Vector(private val label: String) {
1616
/**
17-
* Constant for the TV vector.
17+
* Represents the mobile vector.
1818
*/
19-
const val TV = "TVPLAY"
19+
MOBILE("APPPLAY"),
2020

2121
/**
22-
* Constant for the mobile vector.
22+
* Represents the TV vector.
2323
*/
24-
const val MOBILE = "APPPLAY"
24+
TV("TVPLAY");
2525

26-
/**
27-
* Retrieves the vector based on the device type.
28-
*
29-
* @return The vector for the current device type.
30-
*
31-
* @receiver The [Context] used to access system resources.
32-
*/
33-
fun Context.getVector(): String {
34-
val uiMode = resources.configuration.uiMode
35-
return if (uiMode and Configuration.UI_MODE_TYPE_MASK == Configuration.UI_MODE_TYPE_TELEVISION) {
36-
TV
37-
} else {
38-
MOBILE
26+
override fun toString(): String {
27+
return label
28+
}
29+
30+
@Suppress("UndocumentedPublicClass")
31+
companion object {
32+
/**
33+
* Retrieves a [Vector] associated with the given [label].
34+
*
35+
* @param label The label to search for.
36+
* @return The [Vector] associated with the label, or `null` if not found.
37+
*/
38+
fun fromLabel(label: String): Vector? {
39+
return entries.find { it.label.equals(label, ignoreCase = true) }
40+
}
41+
42+
/**
43+
* Retrieves the vector based on the device type.
44+
*
45+
* @return The vector for the current device type.
46+
*
47+
* @receiver The [Context] used to access system resources.
48+
*/
49+
fun Context.getVector(): Vector {
50+
val uiMode = resources.configuration.uiMode
51+
return if (uiMode and Configuration.UI_MODE_TYPE_MASK == Configuration.UI_MODE_TYPE_TELEVISION) {
52+
TV
53+
} else {
54+
MOBILE
55+
}
3956
}
4057
}
4158
}

pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/SRGMediaItemBuilderTest.kt

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,6 @@ class SRGMediaItemBuilderTest {
102102
assertEquals(MediaMetadata.EMPTY, mediaItem.mediaMetadata)
103103
}
104104

105-
@Test
106-
fun `Check no vector`() {
107-
val urn = "urn:rts:audio:3262363"
108-
val vector = ""
109-
val mediaItem = SRGMediaItem(urn) {
110-
vector(vector)
111-
}
112-
val localConfiguration = mediaItem.localConfiguration
113-
114-
assertNotNull(localConfiguration)
115-
assertEquals(urn.toIlUri(vector = vector), localConfiguration.uri)
116-
assertEquals(MimeTypeSrg, localConfiguration.mimeType)
117-
assertEquals(urn, mediaItem.mediaId)
118-
assertEquals(MediaMetadata.EMPTY, mediaItem.mediaMetadata)
119-
}
120-
121105
@Test
122106
fun `Check uri from existing MediaItem`() {
123107
val urn = "urn:rts:audio:3262363"
@@ -142,7 +126,7 @@ class SRGMediaItemBuilderTest {
142126
.setUri("https://il-stage.srgssr.ch/integrationlayer/2.1/mediaComposition/byUrn/$urn?vector=${Vector.TV}")
143127
.build()
144128
val urn2 = "urn:rts:audio:123456"
145-
val mediaItem = SRGMediaItem(urn) {
129+
val mediaItem = inputMediaItem.buildUpon {
146130
host(IlHost.PROD)
147131
vector(Vector.MOBILE)
148132
urn(urn2)
@@ -216,15 +200,15 @@ class SRGMediaItemBuilderTest {
216200
companion object {
217201
fun String.toIlUri(
218202
host: URL = IlHost.DEFAULT,
219-
vector: String = Vector.MOBILE,
203+
vector: Vector = Vector.MOBILE,
220204
forceSAM: Boolean = false,
221205
ilLocation: IlLocation? = null,
222206
): Uri {
223207
val samPath = if (forceSAM) "sam/" else ""
224208
val queryParameters = listOfNotNull(
225209
if (forceSAM) "forceSAM" to true else null,
226210
if (ilLocation != null) "forceLocation" to ilLocation else null,
227-
if (vector.isNotBlank()) "vector" to vector else null,
211+
"vector" to vector,
228212
"onlyChapters" to true,
229213
).joinToString(separator = "&") { (name, value) ->
230214
"$name=$value"

pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/integrationlayer/service/VectorTest.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ package ch.srgssr.pillarbox.core.business.integrationlayer.service
77
import android.content.Context
88
import androidx.test.core.app.ApplicationProvider
99
import androidx.test.ext.junit.runners.AndroidJUnit4
10-
import ch.srgssr.pillarbox.core.business.integrationlayer.service.Vector.getVector
10+
import ch.srgssr.pillarbox.core.business.integrationlayer.service.Vector.Companion.getVector
1111
import org.junit.runner.RunWith
1212
import org.robolectric.annotation.Config
1313
import kotlin.test.BeforeTest
1414
import kotlin.test.Test
1515
import kotlin.test.assertEquals
16+
import kotlin.test.assertNull
1617

1718
@RunWith(AndroidJUnit4::class)
1819
class VectorTest {
@@ -23,6 +24,23 @@ class VectorTest {
2324
context = ApplicationProvider.getApplicationContext()
2425
}
2526

27+
@Test
28+
fun `fromLabel MOBILE`() {
29+
assertEquals(Vector.MOBILE, Vector.fromLabel("appplay"))
30+
assertEquals(Vector.MOBILE, Vector.fromLabel("APPPLAY"))
31+
}
32+
33+
@Test
34+
fun `fromLabel TV`() {
35+
assertEquals(Vector.TV, Vector.fromLabel("tvplay"))
36+
assertEquals(Vector.TV, Vector.fromLabel("TVPLAY"))
37+
}
38+
39+
@Test
40+
fun `fromLabel invalid label`() {
41+
assertNull(Vector.fromLabel("INVALID"))
42+
}
43+
2644
@Test
2745
fun getVector() {
2846
assertEquals(Vector.MOBILE, context.getVector())

0 commit comments

Comments
 (0)