Skip to content

Commit

Permalink
fix(Awala): Migrate to bindAutomatically() (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea authored Apr 19, 2024
1 parent c349879 commit 494aa11
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 25 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ dependencies {
implementation 'androidx.sharetarget:sharetarget:1.2.0'

// Awala-powered messaging
implementation 'com.github.relaycorp:awala-endpoint-android:1.15.4'
implementation 'com.github.relaycorp:awala-endpoint-android:1.16.2'
def bouncy_castle_version = "1.70"
implementation "org.bouncycastle:bcprov-jdk15on:$bouncy_castle_version"
implementation "org.bouncycastle:bcpkix-jdk15on:$bouncy_castle_version"
Expand Down
4 changes: 2 additions & 2 deletions app/lint.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
<issue id="InvalidPackage">
<!-- Ignore errors about BC importing javax.naming because we don't use those modules -->
<ignore path="**/bcpkix-*.jar" />
<!-- It reports that the Awala SDK is using IntellijIdeaDebugDetector indirectly, which isn't the case -->
<ignore path="**/ktor-utils-jvm-*.jar" />
</issue>
<issue id="IconDuplicates">
<ignore path="**/awala_initialization_error.webp" />
</issue>
<!-- TODO: remove after uploading https://letro.app/.well-known/assetlinks.json -->
<issue id="AppLinksAutoVerify" severity="warning" />
</lint>
37 changes: 21 additions & 16 deletions app/src/main/java/tech/relaycorp/letro/awala/AwalaManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableSharedFlow
Expand All @@ -17,7 +18,6 @@ import kotlinx.coroutines.newSingleThreadContext
import kotlinx.coroutines.withContext
import tech.relaycorp.awaladroid.AwaladroidException
import tech.relaycorp.awaladroid.EncryptionInitializationException
import tech.relaycorp.awaladroid.GatewayBindingException
import tech.relaycorp.awaladroid.endpoint.FirstPartyEndpoint
import tech.relaycorp.awaladroid.endpoint.PrivateThirdPartyEndpoint
import tech.relaycorp.awaladroid.endpoint.PublicThirdPartyEndpoint
Expand Down Expand Up @@ -141,7 +141,7 @@ class AwalaManagerImpl @Inject constructor(
return@withContext
}
_awalaInitializationState.emit(AWALA_SET_UP)
initializeGateway()
initializeGatewayAsync()
awalaSetupJob = null
}
}
Expand Down Expand Up @@ -330,24 +330,29 @@ class AwalaManagerImpl @Inject constructor(
}

override fun initializeGatewayAsync() {
awalaScope.launch(awalaThreadContext) {
awalaScope.launch(Dispatchers.Main) {
initializeGateway()
}
}

private suspend fun initializeGateway() {
withContext(awalaThreadContext) {
try {
logger.i(TAG, "GatewayClient binding...")
awala.bindGateway()
startReceivingMessages()
_awalaInitializationState.emit(INITIALIZED)
} catch (exp: GatewayBindingException) {
logger.i(TAG, "GatewayClient cannot be bound: $exp")
_awalaUnsuccessfulBindings.emit(Unit)
_awalaInitializationState.emit(AWALA_NOT_INSTALLED)
}
}
private fun initializeGateway() {
logger.i(TAG, "Binding to Awala gateway...")
awala.bindGateway(
onBindSuccessful = {
awalaScope.launch(awalaThreadContext) {
startReceivingMessages()
_awalaInitializationState.emit(INITIALIZED)
logger.i(TAG, "Binding complete")
}
},
onBindFailure = {
awalaScope.launch(awalaThreadContext) {
logger.i(TAG, "GatewayClient cannot be bound: $it")
_awalaUnsuccessfulBindings.emit(Unit)
_awalaInitializationState.emit(AWALA_NOT_INSTALLED)
}
},
)
}

@Throws(AwaladroidException::class)
Expand Down
13 changes: 10 additions & 3 deletions app/src/main/java/tech/relaycorp/letro/awala/AwalaWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.Flow
import tech.relaycorp.awaladroid.Awala
import tech.relaycorp.awaladroid.AwaladroidException
import tech.relaycorp.awaladroid.GatewayBindingException
import tech.relaycorp.awaladroid.GatewayClient
import tech.relaycorp.awaladroid.endpoint.FirstPartyEndpoint
import tech.relaycorp.awaladroid.endpoint.InvalidThirdPartyEndpoint
Expand All @@ -24,7 +25,10 @@ import kotlin.Exception

interface AwalaWrapper {
suspend fun setUp()
suspend fun bindGateway()
fun bindGateway(
onBindSuccessful: () -> Unit,
onBindFailure: (GatewayBindingException) -> Unit,
)
suspend fun registerFirstPartyEndpoint(): FirstPartyEndpoint
suspend fun importServerThirdPartyEndpoint(
connectionParams: ByteArray,
Expand Down Expand Up @@ -64,8 +68,11 @@ class AwalaWrapperImpl @Inject constructor(
Awala.setUp(context)
}

override suspend fun bindGateway() {
GatewayClient.bind()
override fun bindGateway(
onBindSuccessful: () -> Unit,
onBindFailure: (GatewayBindingException) -> Unit,
) {
GatewayClient.bindAutomatically(onBindSuccessful, onBindFailure = onBindFailure)
}

override suspend fun registerFirstPartyEndpoint(): FirstPartyEndpoint {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.mockk.ConstantAnswer
import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk
import io.mockk.slot
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.test.TestDispatcher
Expand Down Expand Up @@ -52,12 +53,17 @@ private fun createAwalaWrapper(awalaInitializationResult: AwalaInitializationRes
ConstantAnswer(Unit)
}
}
coEvery { bindGateway() } answers {

val onSuccessBindCallable = slot<() -> Unit>()
val onFailureBindCallable = slot<(GatewayBindingException) -> Unit>()
every { bindGateway(capture(onSuccessBindCallable), capture(onFailureBindCallable)) } answers {
if (awalaInitializationResult == AwalaInitializationResult.CRASH_ON_GATEAWAY_BINDING) {
throw GatewayBindingException("Gateway binding exception")
val exc = GatewayBindingException("Gateway binding exception")
onFailureBindCallable.captured(exc)
} else {
ConstantAnswer(Unit)
onSuccessBindCallable.captured()
}
ConstantAnswer(Unit)
}
coEvery { registerFirstPartyEndpoint() } answers {
if (awalaInitializationResult == AwalaInitializationResult.CRASH_ON_FIRST_PARTY_ENDPOINT_REGISTRATION) {
Expand Down

0 comments on commit 494aa11

Please sign in to comment.