Skip to content

Commit e536eec

Browse files
authored
Create an enum for IL location (#791)
1 parent 1c7df31 commit e536eec

File tree

9 files changed

+96
-32
lines changed

9 files changed

+96
-32
lines changed

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import androidx.media3.common.MediaItem
99
import androidx.media3.common.MediaMetadata
1010
import ch.srgssr.pillarbox.core.business.integrationlayer.data.isValidMediaUrn
1111
import ch.srgssr.pillarbox.core.business.integrationlayer.service.IlHost
12+
import ch.srgssr.pillarbox.core.business.integrationlayer.service.IlLocation
1213
import ch.srgssr.pillarbox.core.business.integrationlayer.service.Vector
1314
import ch.srgssr.pillarbox.core.business.source.MimeTypeSrg
1415
import ch.srgssr.pillarbox.player.PillarboxDsl
@@ -79,7 +80,7 @@ class SRGMediaItemBuilder internal constructor(mediaItem: MediaItem) {
7980
private var urn: String = mediaItem.mediaId
8081
private var host: URL = IlHost.DEFAULT
8182
private var forceSAM: Boolean = false
82-
private var forceLocation: String? = null
83+
private var ilLocation: IlLocation? = null
8384
private var vector: String = Vector.MOBILE
8485

8586
init {
@@ -91,7 +92,7 @@ class SRGMediaItemBuilder internal constructor(mediaItem: MediaItem) {
9192
uri.host?.let { hostname -> host = URL(Uri.Builder().scheme(host.protocol).authority(hostname).build().toString()) }
9293
this.urn = urn!!
9394
this.forceSAM = uri.getQueryParameter(PARAM_FORCE_SAM)?.toBooleanStrictOrNull() ?: false
94-
this.forceLocation = uri.getQueryParameter(PARAM_FORCE_LOCATION)
95+
this.ilLocation = uri.getQueryParameter(PARAM_FORCE_LOCATION)?.let { IlLocation.fromName(it) }
9596
uri.getQueryParameter(PARAM_VECTOR)?.let { vector = it }
9697
}
9798
}
@@ -143,15 +144,12 @@ class SRGMediaItemBuilder internal constructor(mediaItem: MediaItem) {
143144
}
144145

145146
/**
146-
* Forces the location for IL/SAM backend calls.
147+
* Sets the location for IL backend calls.
147148
*
148-
* @param forceLocation The location to force. Valid values are:
149-
* - `null`: disables forced location and uses automatic detection.
150-
* - `"CH"`: forces the location to Switzerland.
151-
* - `"WW"`: forces the location to Worldwide.
149+
* @param ilLocation The location to set. Passing `null` defaults to automatic detection.
152150
*/
153-
fun forceLocation(forceLocation: String?) {
154-
this.forceLocation = forceLocation
151+
fun ilLocation(ilLocation: IlLocation?) {
152+
this.ilLocation = ilLocation
155153
}
156154

157155
/**
@@ -186,8 +184,8 @@ class SRGMediaItemBuilder internal constructor(mediaItem: MediaItem) {
186184
if (forceSAM) {
187185
appendQueryParameter(PARAM_FORCE_SAM, true.toString())
188186
}
189-
if (!forceLocation.isNullOrBlank()) {
190-
appendQueryParameter(PARAM_FORCE_LOCATION, forceLocation)
187+
ilLocation?.let {
188+
appendQueryParameter(PARAM_FORCE_LOCATION, it.toString())
191189
}
192190
if (vector.isNotBlank()) {
193191
appendQueryParameter(PARAM_VECTOR, vector)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) SRG SSR. All rights reserved.
3+
* License information is available from the LICENSE file.
4+
*/
5+
package ch.srgssr.pillarbox.core.business.integrationlayer.service
6+
7+
/**
8+
* Represents the location from which requests to the integration layer are made.
9+
*/
10+
enum class IlLocation {
11+
/**
12+
* Represents Switzerland.
13+
*/
14+
CH,
15+
16+
/**
17+
* Represents the rest of the world.
18+
*/
19+
WW;
20+
21+
@Suppress("UndocumentedPublicClass")
22+
companion object {
23+
/**
24+
* Retrieves an [IlLocation] associated with the given [name].
25+
*
26+
* @param name The name to search for.
27+
* @return The [IlLocation] associated with the name, or `null` if not found.
28+
*/
29+
fun fromName(name: String): IlLocation? {
30+
return entries.find { it.name.equals(name, ignoreCase = true) }
31+
}
32+
}
33+
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.media3.common.MediaItem
1010
import androidx.media3.common.MediaMetadata
1111
import androidx.test.ext.junit.runners.AndroidJUnit4
1212
import ch.srgssr.pillarbox.core.business.integrationlayer.service.IlHost
13+
import ch.srgssr.pillarbox.core.business.integrationlayer.service.IlLocation
1314
import ch.srgssr.pillarbox.core.business.integrationlayer.service.Vector
1415
import ch.srgssr.pillarbox.core.business.source.MimeTypeSrg
1516
import org.junit.runner.RunWith
@@ -195,18 +196,18 @@ class SRGMediaItemBuilderTest {
195196
}
196197

197198
@Test
198-
fun `Check set forceLocation`() {
199+
fun `Check set ilLocation`() {
199200
val urn = "urn:rts:audio:3262363"
200201
val ilHost = IlHost.STAGE
201-
val forceLocation = "CH"
202+
val ilLocation = IlLocation.CH
202203
val mediaItem = SRGMediaItem(urn) {
203204
host(ilHost)
204-
forceLocation(forceLocation)
205+
ilLocation(ilLocation)
205206
}
206207
val localConfiguration = mediaItem.localConfiguration
207208

208209
assertNotNull(localConfiguration)
209-
assertEquals(urn.toIlUri(ilHost, forceLocation = forceLocation), localConfiguration.uri)
210+
assertEquals(urn.toIlUri(ilHost, ilLocation = ilLocation), localConfiguration.uri)
210211
assertEquals(MimeTypeSrg, localConfiguration.mimeType)
211212
assertEquals(urn, mediaItem.mediaId)
212213
assertEquals(MediaMetadata.EMPTY, mediaItem.mediaMetadata)
@@ -217,12 +218,12 @@ class SRGMediaItemBuilderTest {
217218
host: URL = IlHost.DEFAULT,
218219
vector: String = Vector.MOBILE,
219220
forceSAM: Boolean = false,
220-
forceLocation: String? = null,
221+
ilLocation: IlLocation? = null,
221222
): Uri {
222223
val samPath = if (forceSAM) "sam/" else ""
223224
val queryParameters = listOfNotNull(
224225
if (forceSAM) "forceSAM" to true else null,
225-
if (forceLocation != null) "forceLocation" to forceLocation else null,
226+
if (ilLocation != null) "forceLocation" to ilLocation else null,
226227
if (vector.isNotBlank()) "vector" to vector else null,
227228
"onlyChapters" to true,
228229
).joinToString(separator = "&") { (name, value) ->
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) SRG SSR. All rights reserved.
3+
* License information is available from the LICENSE file.
4+
*/
5+
package ch.srgssr.pillarbox.core.business.integrationlayer.service
6+
7+
import kotlin.test.Test
8+
import kotlin.test.assertEquals
9+
import kotlin.test.assertNull
10+
11+
class IlLocationTest {
12+
@Test
13+
fun `fromName CH`() {
14+
assertEquals(IlLocation.CH, IlLocation.fromName("ch"))
15+
assertEquals(IlLocation.CH, IlLocation.fromName("CH"))
16+
}
17+
18+
@Test
19+
fun `fromName WW`() {
20+
assertEquals(IlLocation.WW, IlLocation.fromName("ww"))
21+
assertEquals(IlLocation.WW, IlLocation.fromName("WW"))
22+
}
23+
24+
@Test
25+
fun `fromName invalid name`() {
26+
assertNull(IlLocation.fromName("INVALID"))
27+
}
28+
}

pillarbox-demo-shared/src/main/java/ch/srgssr/pillarbox/demo/shared/data/DemoItem.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import androidx.media3.common.MediaItem.DrmConfiguration
1111
import androidx.media3.common.MediaMetadata
1212
import ch.srgssr.pillarbox.core.business.SRGMediaItem
1313
import ch.srgssr.pillarbox.core.business.integrationlayer.service.IlHost
14+
import ch.srgssr.pillarbox.core.business.integrationlayer.service.IlLocation
1415
import java.io.Serializable
1516

1617
/**
@@ -75,7 +76,7 @@ sealed class DemoItem(
7576
* @property imageUri The optional image URI of the media.
7677
* @property host The host from which to load the media.
7778
* @property forceSAM Whether to use SAM instead of the IL.
78-
* @property forceLocation The optional location from which to load the media (either `CH`, `WW`, or `null`).
79+
* @property ilLocation The optional location from which to load the media.
7980
*/
8081
data class URN(
8182
val urn: String,
@@ -84,13 +85,13 @@ sealed class DemoItem(
8485
override val imageUri: String? = null,
8586
val host: java.net.URL = IlHost.PROD,
8687
val forceSAM: Boolean = false,
87-
val forceLocation: String? = null,
88+
val ilLocation: IlLocation? = null,
8889
) : DemoItem(urn, title, description, imageUri) {
8990
override fun toMediaItem(): MediaItem {
9091
return SRGMediaItem(urn) {
9192
host(host)
9293
forceSAM(forceSAM)
93-
forceLocation(forceLocation)
94+
ilLocation(ilLocation)
9495
mediaMetadata {
9596
setTitle(title)
9697
setDescription(description)

pillarbox-demo-shared/src/main/java/ch/srgssr/pillarbox/demo/shared/di/PlayerModule.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import ch.srg.dataProvider.integrationlayer.dependencies.modules.OkHttpModule
1010
import ch.srgssr.dataprovider.paging.DataProviderPaging
1111
import ch.srgssr.pillarbox.core.business.PillarboxExoPlayer
1212
import ch.srgssr.pillarbox.core.business.integrationlayer.service.IlHost
13+
import ch.srgssr.pillarbox.core.business.integrationlayer.service.IlLocation
1314
import ch.srgssr.pillarbox.demo.shared.source.BlockedTimeRangeAssetLoader
1415
import ch.srgssr.pillarbox.demo.shared.source.CustomAssetLoader
1516
import ch.srgssr.pillarbox.demo.shared.ui.integrationLayer.data.ILRepository
@@ -40,7 +41,7 @@ object PlayerModule {
4041
context: Context,
4142
ilHost: URL = IlHost.DEFAULT,
4243
forceSAM: Boolean = false,
43-
ilLocation: String? = null,
44+
ilLocation: IlLocation? = null,
4445
): ILRepository {
4546
val okHttp = OkHttpModule.createOkHttpClient(context)
4647
.newBuilder()
@@ -79,16 +80,16 @@ object PlayerModule {
7980
}
8081
}
8182

82-
private class LocationInterceptor(private val location: String?) : Interceptor {
83+
private class LocationInterceptor(private val location: IlLocation?) : Interceptor {
8384
override fun intercept(chain: Interceptor.Chain): Response {
8485
val request = chain.request()
85-
if (location.isNullOrBlank()) {
86+
if (location == null) {
8687
return chain.proceed(request)
8788
}
8889

8990
val newUrl = request.url
9091
.newBuilder()
91-
.addQueryParameter("forceLocation", location)
92+
.addQueryParameter("forceLocation", location.toString())
9293
.build()
9394
val newRequest = request.newBuilder()
9495
.url(newUrl)

pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/MainNavigation.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import ch.srg.dataProvider.integrationlayer.request.image.ImageWidth
5353
import ch.srg.dataProvider.integrationlayer.request.image.decorated
5454
import ch.srgssr.pillarbox.analytics.SRGAnalytics
5555
import ch.srgssr.pillarbox.core.business.integrationlayer.service.IlHost
56+
import ch.srgssr.pillarbox.core.business.integrationlayer.service.IlLocation
5657
import ch.srgssr.pillarbox.demo.shared.data.DemoItem
5758
import ch.srgssr.pillarbox.demo.shared.di.PlayerModule
5859
import ch.srgssr.pillarbox.demo.shared.ui.HomeDestination
@@ -94,7 +95,7 @@ fun MainNavigation() {
9495

9596
var ilHost by remember { mutableStateOf(IlHost.DEFAULT) }
9697
var forceSAM by remember { mutableStateOf(false) }
97-
var ilLocation by remember { mutableStateOf<String?>(null) }
98+
var ilLocation by remember { mutableStateOf<IlLocation?>(null) }
9899

99100
Scaffold(
100101
topBar = {
@@ -187,8 +188,8 @@ fun MainNavigation() {
187188
private fun ListsMenu(
188189
currentServer: URL,
189190
currentForceSAM: Boolean,
190-
currentLocation: String?,
191-
onServerSelected: (server: URL, forceSAM: Boolean, location: String?) -> Unit
191+
currentLocation: IlLocation?,
192+
onServerSelected: (server: URL, forceSAM: Boolean, location: IlLocation?) -> Unit
192193
) {
193194
var isMenuVisible by remember { mutableStateOf(false) }
194195

@@ -247,7 +248,7 @@ private fun ListsMenu(
247248
}
248249

249250
internal fun getServers(context: Context): List<EnvironmentConfig> {
250-
val ilServers = listOf(null, "CH", "WW").flatMap { location ->
251+
val ilServers = listOf(null, IlLocation.CH, IlLocation.WW).flatMap { location ->
251252
val name = location?.let { "IL ($location)" } ?: "IL"
252253

253254
listOf(
@@ -300,7 +301,7 @@ internal data class EnvironmentConfig(
300301
val serverName: String,
301302
val host: URL,
302303
val forceSAM: Boolean = false,
303-
val location: String? = null,
304+
val location: IlLocation? = null,
304305
) {
305306
val displayName: String
306307
get() = "$serverName - $name"

pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/examples/InsertContentView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private data class InsertContentData(
8686
urn = uri,
8787
host = checkNotNull(environmentConfig).host,
8888
forceSAM = environmentConfig.forceSAM,
89-
forceLocation = environmentConfig.location,
89+
ilLocation = environmentConfig.location,
9090
)
9191

9292
else -> DemoItem.URL(

pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/lists/ListsHome.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import androidx.navigation.NavController
2020
import androidx.navigation.NavGraphBuilder
2121
import androidx.navigation.toRoute
2222
import androidx.paging.compose.collectAsLazyPagingItems
23+
import ch.srgssr.pillarbox.core.business.integrationlayer.service.IlLocation
2324
import ch.srgssr.pillarbox.demo.DemoPageView
2425
import ch.srgssr.pillarbox.demo.composable
2526
import ch.srgssr.pillarbox.demo.shared.data.DemoItem
@@ -47,7 +48,7 @@ fun NavGraphBuilder.listsNavGraph(
4748
ilRepository: ILRepository,
4849
ilHost: URL,
4950
forceSAM: Boolean,
50-
ilLocation: String?,
51+
ilLocation: IlLocation?,
5152
) {
5253
val contentClick = { contentList: ContentList, content: Content ->
5354
when (content) {
@@ -75,7 +76,7 @@ fun NavGraphBuilder.listsNavGraph(
7576
urn = content.urn,
7677
host = ilHost,
7778
forceSAM = forceSAM,
78-
forceLocation = ilLocation,
79+
ilLocation = ilLocation,
7980
)
8081

8182
SimplePlayerActivity.startActivity(navController.context, item)

0 commit comments

Comments
 (0)