-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
45 changed files
with
1,115 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
app/src/main/java/tech/relaycorp/gateway/background/CourierConnectionObserver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package tech.relaycorp.gateway.background | ||
|
||
import android.net.ConnectivityManager | ||
import android.net.Network | ||
import android.net.NetworkCapabilities | ||
import android.net.NetworkRequest | ||
import android.net.wifi.WifiManager | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class CourierConnectionObserver | ||
@Inject constructor( | ||
connectivityManager: ConnectivityManager, | ||
wifiManager: WifiManager | ||
) { | ||
|
||
private val state = | ||
MutableStateFlow<CourierConnectionState>(CourierConnectionState.Disconnected) | ||
|
||
private val networkRequest = | ||
NetworkRequest.Builder() | ||
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI) | ||
.build() | ||
|
||
private val networkCallback = object : ConnectivityManager.NetworkCallback() { | ||
override fun onAvailable(network: Network) { | ||
// TODO: check if there's an actual courier server listening at the right port | ||
val hotspotSourceIpAddress = | ||
wifiManager.dhcpInfo?.serverAddress?.toIpAddressString() | ||
|
||
state.value = | ||
hotspotSourceIpAddress | ||
?.let { CourierConnectionState.ConnectedWithCourier("https://$it:21473") } | ||
?: CourierConnectionState.ConnectedWithUnknown | ||
} | ||
|
||
override fun onUnavailable() { | ||
state.value = CourierConnectionState.Disconnected | ||
} | ||
|
||
override fun onLost(network: Network) { | ||
state.value = CourierConnectionState.Disconnected | ||
} | ||
} | ||
|
||
init { | ||
connectivityManager.registerNetworkCallback(networkRequest, networkCallback) | ||
} | ||
|
||
fun observe(): Flow<CourierConnectionState> = state | ||
|
||
private fun Int.toIpAddressString() = | ||
String.format( | ||
"%d.%d.%d.%d", | ||
this and 0xff, | ||
this shr 8 and 0xff, | ||
this shr 16 and 0xff, | ||
this shr 24 and 0xff | ||
) | ||
} |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/tech/relaycorp/gateway/background/CourierConnectionState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package tech.relaycorp.gateway.background | ||
|
||
sealed class CourierConnectionState { | ||
data class ConnectedWithCourier(val address: String) : CourierConnectionState() | ||
object ConnectedWithUnknown : CourierConnectionState() | ||
object Disconnected : CourierConnectionState() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
app/src/main/java/tech/relaycorp/gateway/data/database/DateConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package tech.relaycorp.gateway.data.database | ||
|
||
import androidx.room.TypeConverter | ||
import java.util.Date | ||
|
||
class DateConverter { | ||
@TypeConverter | ||
fun toDate(dateLong: Long) = Date(dateLong) | ||
|
||
@TypeConverter | ||
fun fromDate(date: Date) = date.time | ||
} |
33 changes: 33 additions & 0 deletions
33
app/src/main/java/tech/relaycorp/gateway/data/database/MessageConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package tech.relaycorp.gateway.data.database | ||
|
||
import androidx.room.TypeConverter | ||
import tech.relaycorp.gateway.data.model.MessageAddress | ||
import tech.relaycorp.gateway.data.model.MessageId | ||
import tech.relaycorp.gateway.data.model.PrivateMessageAddress | ||
import tech.relaycorp.gateway.data.model.PublicMessageAddress | ||
|
||
class MessageConverter { | ||
@TypeConverter | ||
fun toAddress(value: String) = MessageAddress.of(value) | ||
|
||
@TypeConverter | ||
fun fromAddress(address: MessageAddress) = address.value | ||
|
||
@TypeConverter | ||
fun toPrivateAddress(value: String) = PrivateMessageAddress(value) | ||
|
||
@TypeConverter | ||
fun fromPrivateAddress(address: PrivateMessageAddress) = address.value | ||
|
||
@TypeConverter | ||
fun toPublicAddress(value: String) = PublicMessageAddress(value) | ||
|
||
@TypeConverter | ||
fun fromPublicAddress(address: PublicMessageAddress) = address.value | ||
|
||
@TypeConverter | ||
fun toId(value: String) = MessageId(value) | ||
|
||
@TypeConverter | ||
fun fromId(id: MessageId) = id.value | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/tech/relaycorp/gateway/data/database/StorageSizeConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package tech.relaycorp.gateway.data.database | ||
|
||
import androidx.room.TypeConverter | ||
import tech.relaycorp.gateway.data.model.StorageSize | ||
|
||
class StorageSizeConverter { | ||
@TypeConverter | ||
fun toStorageSize(value: Long) = StorageSize(value) | ||
|
||
@TypeConverter | ||
fun fromStorageSize(size: StorageSize) = size.bytes | ||
} |
Oops, something went wrong.