Skip to content

Commit

Permalink
Replace truetime with networktime
Browse files Browse the repository at this point in the history
  • Loading branch information
stoyicker committed Jul 4, 2024
1 parent 8b0949e commit 372adc0
Show file tree
Hide file tree
Showing 83 changed files with 861 additions and 764 deletions.
2 changes: 1 addition & 1 deletion eventproducer/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies {
implementation(libs.tickaroo.annotation)
implementation(libs.tickaroo.core)
implementation(libs.tickaroo.retrofitConverter)
implementation(libs.truetime)
implementation(libs.tidal.networktime.singletons)

ksp(libs.dagger.compiler)
kapt(libs.room.compiler)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tidal.sdk.eventproducer

import android.content.Context
import com.tidal.networktime.SNTPClient
import com.tidal.sdk.auth.CredentialsProvider
import com.tidal.sdk.eventproducer.di.DaggerEventsComponent
import com.tidal.sdk.eventproducer.model.EventsConfig
Expand Down Expand Up @@ -43,6 +44,9 @@ class EventProducer private constructor(coroutineScope: CoroutineScope) {
@Inject
internal lateinit var monitoringScheduler: MonitoringScheduler

@Inject
internal lateinit var sntpClient: SNTPClient

internal fun startOutage() {
_outageState.value = OutageState.Outage()
}
Expand Down Expand Up @@ -91,6 +95,7 @@ class EventProducer private constructor(coroutineScope: CoroutineScope) {
)
EventProducer(coroutineScope).also {
eventsComponent.inject(it)
it.sntpClient.enableSynchronization()
coroutineScope.launch(Dispatchers.IO) { it.scheduler.scheduleBatchAndSend() }
coroutineScope.launch(Dispatchers.IO) {
it.monitoringScheduler.scheduleSendMonitoringInfo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.tidal.sdk.eventproducer.model.MonitoringEventType
import com.tidal.sdk.eventproducer.repository.EventsRepository
import com.tidal.sdk.eventproducer.utils.EventSizeValidator
import com.tidal.sdk.eventproducer.utils.HeadersUtils
import java.util.*
import java.util.UUID
import javax.inject.Inject
import javax.inject.Singleton

Expand All @@ -20,7 +20,7 @@ internal class Submitter @Inject constructor(
private val headersUtils: HeadersUtils,
) {

fun sendEvent(
suspend fun sendEvent(
eventName: String,
consentCategory: ConsentCategory,
payload: String,
Expand Down Expand Up @@ -50,7 +50,7 @@ internal class Submitter @Inject constructor(
)
}

private fun createEvent(
private suspend fun createEvent(
eventName: String,
consentCategory: ConsentCategory,
payload: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.tidal.sdk.eventproducer.di

import com.tidal.networktime.NTPServer
import com.tidal.networktime.SNTPClient
import com.tidal.networktime.singletons.singleton
import com.tidal.sdk.auth.CredentialsProvider
import com.tidal.sdk.eventproducer.model.EventsConfigProvider
import com.tidal.sdk.eventproducer.utils.HeadersUtils
import com.tidal.sdk.eventproducer.utils.TrueTimeWrapper
import dagger.Module
import dagger.Provides
import dagger.Reusable
Expand All @@ -15,10 +17,10 @@ internal class UtilsModule {
fun provideHeadersUtils(
configProvider: EventsConfigProvider,
credentialsProvider: CredentialsProvider,
trueTimeWrapper: TrueTimeWrapper,
) = HeadersUtils(configProvider.config.appVersion, credentialsProvider, trueTimeWrapper)
sntpClient: SNTPClient,
) = HeadersUtils(configProvider.config.appVersion, credentialsProvider, sntpClient)

@Provides
@Reusable
fun provideTrueTimeWrapper() = TrueTimeWrapper()
fun provideSntpClient() = SNTPClient(NTPServer("time.google.com")).singleton
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal class RepositoryHelper @Inject constructor(
return credentials?.level == Credentials.Level.USER
}

fun getMonitoringEvent(monitoringInfo: MonitoringInfo): Event {
suspend fun getMonitoringEvent(monitoringInfo: MonitoringInfo): Event {
val monitoringEventPayload = getMonitoringEventPayload(monitoringInfo)
val headers = headersUtils.getDefaultHeaders(ConsentCategory.NECESSARY, true)
return Event(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tidal.sdk.eventproducer.utils

import android.os.Build
import com.tidal.networktime.SNTPClient
import com.tidal.sdk.auth.CredentialsProvider
import com.tidal.sdk.eventproducer.model.ConsentCategory
import javax.inject.Inject
Expand All @@ -19,20 +20,20 @@ internal const val DEVICE_VENDOR_KEY = "device-vendor"
internal class HeadersUtils @Inject constructor(
private val appVersion: String,
private val credentialsProvider: CredentialsProvider,
private val trueTimeWrapper: TrueTimeWrapper,
private val sntpClient: SNTPClient,
) {
fun getEventHeaders(
defaultHeaders: Map<String, String>,
suppliedHeaders: Map<String, String>,
): Map<String, String> = defaultHeaders + suppliedHeaders

fun getDefaultHeaders(
suspend fun getDefaultHeaders(
consentCategory: ConsentCategory,
isMonitoringEvent: Boolean,
): Map<String, String> {
val deviceModel = Build.MODEL
val deviceVendor = Build.MANUFACTURER
val sentTimestamp = trueTimeWrapper.currentTimeMillis.toString()
val sentTimestamp = sntpClient.blockingEpochTime().inWholeMilliseconds.toString()
val osName = "Android"
val osVersion = Build.VERSION.SDK_INT.toString()
val headers = mutableMapOf<String, String>()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import io.mockk.mockkObject
import io.mockk.unmockkObject
import io.mockk.verify
import java.util.concurrent.TimeoutException
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -290,7 +291,9 @@ class EventsRepositoryTest {
SendMessageBatchResult(null),
)
every { monitoringDataSource.getMonitoringInfo() } returns monitoringInfo
every { repositoryHelper.getMonitoringEvent(monitoringInfo) } returns monitoringEvent
every {
runBlocking { repositoryHelper.getMonitoringEvent(monitoringInfo) }
} returns monitoringEvent
every { repositoryHelper.isUserLoggedIn() } returns true
every {
sqsParametersConverter.getSendEventsParameters(listOf(monitoringEvent))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package com.tidal.eventproducer.utils
import assertk.assertThat
import assertk.assertions.containsAll
import com.tidal.eventproducer.fakes.FakeCredentialsProvider
import com.tidal.networktime.SNTPClient
import com.tidal.sdk.eventproducer.utils.APP_VERSION_KEY
import com.tidal.sdk.eventproducer.utils.CLIENT_ID_KEY
import com.tidal.sdk.eventproducer.utils.HeadersUtils
import com.tidal.sdk.eventproducer.utils.OS_NAME_KEY
import com.tidal.sdk.eventproducer.utils.TrueTimeWrapper
import io.mockk.mockk
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
Expand All @@ -17,7 +17,7 @@ class HeadersUtilsTest {
private val headerUtils = HeadersUtils(
"",
FakeCredentialsProvider(),
mockk<TrueTimeWrapper>(),
mockk<SNTPClient>(),
)

@Test
Expand Down
5 changes: 4 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ slf4j-api = "2.0.12"
tickaroo = "0.8.13"
tidal-androidx-media = "1.1.1.2"
plugins-tidal = "unspecified"
tidal-networktime = "1.1.0"

[libraries]
plugin-kotlin-android = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" }
Expand Down Expand Up @@ -55,7 +56,6 @@ gson = "com.google.code.gson:gson:2.9.0"
kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }
kotlin-logging = { module = "io.github.oshai:kotlin-logging-jvm", version.ref = "kotlin-logging" }
slf4j-api = { module = "org.slf4j:slf4j-api", version.ref = "slf4j-api" }
truetime = "com.github.instacart.truetime-android:library-extension-rx:3.5"

# Moshi
moshi = { module = "com.squareup.moshi:moshi", version.ref ="moshi"}
Expand Down Expand Up @@ -112,6 +112,9 @@ tidal-exoPlayer-hls = { module = "com.tidal.androidx.media3:media3-exoplayer-hls
tidal-exoPlayer-extension-flac = { module = "com.tidal.androidx.media3:media3-flac", version.ref = "tidal-androidx-media" }
tidal-exoPlayer-extension-okhttp = { module = "com.tidal.androidx.media3:media3-datasource-okhttp", version.ref = "tidal-androidx-media" }

tidal-networktime = { module = "com.tidal.networktime:networktime", version.ref = "tidal-networktime" }
tidal-networktime-singletons = { module = "com.tidal.networktime:networktime-singletons", version.ref = "tidal-networktime" }

# Security
androidx-security-crypto = { module = "androidx.security:security-crypto", version = "1.0.0" }

Expand Down
15 changes: 4 additions & 11 deletions player/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@ The Player module encapsulates the playback functionality of TIDAL media product

### Installation

1. We are using the [TrueTime library](https://github.com/instacart/truetime-android) internally, so you need to add the following to your repositories list:
```kotlin
maven {
url = uri("https://jitpack.io")
}
```

2. Add the dependency to your `build.gradle.kts` file.
1. Add the dependency to your `build.gradle.kts` file.
```kotlin
dependencies {
implementation("com.tidal.sdk:player:<VERSION>")
Expand All @@ -37,7 +30,7 @@ The Player depends on the [Auth](https://github.com/tidal-music/tidal-sdk-androi

Here's how to setup the Player and play a TIDAL track:

1. Initialise the Player which depends on a `CredentialsProvider` from the Auth module and an `EventSender` from the EventProducer module.
2. Initialise the Player which depends on a `CredentialsProvider` from the Auth module and an `EventSender` from the EventProducer module.
```kotlin
val player = Player(
application = application,
Expand All @@ -46,15 +39,15 @@ val player = Player(
)
```

2. Load and play a `MediaProduct` track.
3. Load and play a `MediaProduct` track.
```kotlin
val mediaProduct = MediaProduct(ProductType.TRACK, "PRODUCT_ID")

player.playbackEngine.load(mediaProduct)
player.playbackEngine.play()
```

3. _(Optional)_ Listen to [player events](https://github.com/tidal-music/tidal-sdk-android/blob/main/player/playback-engine/src/main/kotlin/com/tidal/sdk/player/playbackengine/model/Event.kt).
4. _(Optional)_ Listen to [player events](https://github.com/tidal-music/tidal-sdk-android/blob/main/player/playback-engine/src/main/kotlin/com/tidal/sdk/player/playbackengine/model/Event.kt).
```kotlin
player.playbackEngine.events.onEach {
Log.d(TAG, "Event=$it")
Expand Down
2 changes: 1 addition & 1 deletion player/apps/demo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ dependencies {
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.compose.material3)
implementation(libs.truetime)
implementation(libs.tidal.networktime.singletons)
}
1 change: 0 additions & 1 deletion player/apps/demo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package="com.tidal.sdk.player">

<application
android:name=".MyApplication"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="Player"
Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions player/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies {
implementation(libs.okhttp.loggingInterceptor)
implementation(libs.gson)
implementation(project(":player:streaming-privileges"))
implementation(libs.tidal.networktime.singletons)

testImplementation(libs.test.assertk)
testImplementation(libs.test.junit5Api)
Expand Down
1 change: 0 additions & 1 deletion player/common-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ android {

dependencies {
implementation(libs.kotlinx.serialization.json)
implementation(libs.truetime)

testImplementation(libs.test.junit5Api)
testRuntimeOnly(libs.test.junit5Engine)
Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions player/events/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
implementation(libs.tidal.sdk.auth)
implementation(libs.tidal.sdk.eventproducer)
implementation(libs.kotlinxCoroutinesCore)
implementation(libs.tidal.networktime)

testImplementation(libs.test.assertk)
testImplementation(libs.test.junit5Api)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.tidal.sdk.player.events.playlogtest
import android.content.Context
import android.net.ConnectivityManager
import com.google.gson.Gson
import com.tidal.networktime.SNTPClient
import com.tidal.sdk.eventproducer.EventSender
import com.tidal.sdk.player.common.UUIDWrapper
import com.tidal.sdk.player.commonandroid.TrueTimeWrapper
import com.tidal.sdk.player.events.ClientSupplier
import com.tidal.sdk.player.events.UserSupplier
import com.tidal.sdk.player.events.di.DaggerDefaultEventReporterComponent
Expand All @@ -24,7 +24,7 @@ class PlayLogTestDefaultEventReporterComponentFactory(
okHttpClient: OkHttpClient,
gson: Gson,
uuidWrapper: UUIDWrapper,
trueTimeWrapper: TrueTimeWrapper,
sntpClient: SNTPClient,
eventSender: EventSender,
coroutineScope: CoroutineScope,
) = DaggerDefaultEventReporterComponent.factory()
Expand All @@ -36,7 +36,7 @@ class PlayLogTestDefaultEventReporterComponentFactory(
okHttpClient,
gson,
uuidWrapper,
trueTimeWrapper,
sntpClient,
eventSender,
coroutineScopeOverride,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.tidal.sdk.player.events
import android.content.Context
import android.net.ConnectivityManager
import com.google.gson.Gson
import com.tidal.networktime.SNTPClient
import com.tidal.sdk.eventproducer.EventSender
import com.tidal.sdk.player.common.UUIDWrapper
import com.tidal.sdk.player.commonandroid.TrueTimeWrapper
import com.tidal.sdk.player.events.di.DaggerDefaultEventReporterComponent
import kotlinx.coroutines.CoroutineScope
import okhttp3.OkHttpClient
Expand All @@ -19,7 +19,7 @@ class EventReporterModuleRoot(
okHttpClient: OkHttpClient,
gson: Gson,
uuidWrapper: UUIDWrapper,
trueTimeWrapper: TrueTimeWrapper,
sntpClient: SNTPClient,
eventSender: EventSender,
coroutineScope: CoroutineScope,
) {
Expand All @@ -33,7 +33,7 @@ class EventReporterModuleRoot(
okHttpClient,
gson,
uuidWrapper,
trueTimeWrapper,
sntpClient,
eventSender,
coroutineScope,
).eventReporter
Expand Down
Loading

0 comments on commit 372adc0

Please sign in to comment.