-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
android: offline api structure (#1918)
* add connectivity monitoring
- Loading branch information
1 parent
d213e59
commit 7be9b10
Showing
6 changed files
with
142 additions
and
21 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
49 changes: 49 additions & 0 deletions
49
...-android/core/src/main/java/net/nymtech/vpn/service/network/NetworkConnectivityService.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,49 @@ | ||
package net.nymtech.vpn.service.network | ||
|
||
import android.content.Context | ||
import android.net.ConnectivityManager | ||
import android.net.ConnectivityManager.NetworkCallback | ||
import android.net.Network | ||
import android.net.NetworkCapabilities | ||
import android.net.NetworkRequest | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.callbackFlow | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.flowOn | ||
|
||
class NetworkConnectivityService(context: Context) : NetworkService { | ||
|
||
private val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
|
||
override val networkStatus: Flow<NetworkStatus> = callbackFlow { | ||
val connectivityCallback = object : NetworkCallback() { | ||
override fun onAvailable(network: Network) { | ||
trySend(NetworkStatus.Connected) | ||
} | ||
|
||
override fun onUnavailable() { | ||
trySend(NetworkStatus.Disconnected) | ||
} | ||
|
||
override fun onLost(network: Network) { | ||
trySend(NetworkStatus.Disconnected) | ||
} | ||
|
||
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) { | ||
trySend(NetworkStatus.Connected) | ||
} | ||
} | ||
|
||
val request = NetworkRequest.Builder() | ||
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) | ||
.build() | ||
|
||
connectivityManager.registerNetworkCallback(request, connectivityCallback) | ||
|
||
awaitClose { | ||
connectivityManager.unregisterNetworkCallback(connectivityCallback) | ||
} | ||
}.distinctUntilChanged().flowOn(Dispatchers.IO) | ||
} |
7 changes: 7 additions & 0 deletions
7
nym-vpn-android/core/src/main/java/net/nymtech/vpn/service/network/NetworkService.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 net.nymtech.vpn.service.network | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
|
||
internal interface NetworkService { | ||
val networkStatus: Flow<NetworkStatus> | ||
} |
7 changes: 7 additions & 0 deletions
7
nym-vpn-android/core/src/main/java/net/nymtech/vpn/service/network/NetworkStatus.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 net.nymtech.vpn.service.network | ||
|
||
sealed class NetworkStatus { | ||
object Unknown : NetworkStatus() | ||
object Connected : NetworkStatus() | ||
object Disconnected : NetworkStatus() | ||
} |
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: 10 additions & 2 deletions
12
nym-vpn-core/crates/nym-vpn-lib/src/tunnel_provider/android.rs
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 |
---|---|---|
@@ -1,13 +1,21 @@ | ||
// Copyright 2024 - Nym Technologies SA <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
use std::{fmt::Debug, os::fd::RawFd}; | ||
|
||
use super::tunnel_settings::TunnelNetworkSettings; | ||
use crate::platform::error::VpnError; | ||
use std::sync::Arc; | ||
use std::{fmt::Debug, os::fd::RawFd}; | ||
|
||
#[uniffi::export(with_foreign)] | ||
pub trait ConnectivityObserver: Send + Sync + std::fmt::Debug { | ||
fn on_network_change(&self, is_online: bool); | ||
} | ||
|
||
#[uniffi::export(with_foreign)] | ||
pub trait AndroidTunProvider: Send + Sync + Debug { | ||
fn bypass(&self, socket: i32); | ||
fn configure_tunnel(&self, config: TunnelNetworkSettings) -> Result<RawFd, VpnError>; | ||
|
||
fn add_connectivity_observer(&self, observer: Arc<dyn ConnectivityObserver>); | ||
fn remove_connectivity_observer(&self, observer: Arc<dyn ConnectivityObserver>); | ||
} |