-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add mobile implementations of connectivity
- Loading branch information
Showing
21 changed files
with
250 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import dev.jordond.connectivity.convention.Platform | ||
import dev.jordond.connectivity.convention.configureMultiplatform | ||
|
||
plugins { | ||
alias(libs.plugins.android.library) | ||
alias(libs.plugins.multiplatform) | ||
alias(libs.plugins.poko) | ||
alias(libs.plugins.dokka) | ||
alias(libs.plugins.publish) | ||
alias(libs.plugins.convention.multiplatform) | ||
} | ||
|
||
configureMultiplatform(Platform.Android) | ||
|
||
kotlin { | ||
sourceSets { | ||
androidMain.dependencies { | ||
api(projects.connectivityCore) | ||
implementation(projects.connectivityToolsAndroid) | ||
implementation(libs.androidx.core) | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
connectivity-android/src/androidMain/kotlin/AndroidManifest.xml
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 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest | ||
xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||
|
||
</manifest> |
17 changes: 17 additions & 0 deletions
17
...ty-android/src/androidMain/kotlin/dev/jordond/connectivity/android/AndroidConnectivity.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,17 @@ | ||
package dev.jordond.connectivity.android | ||
|
||
import dev.jordond.connectivity.Connectivity | ||
import dev.jordond.connectivity.ConnectivityOptions | ||
import dev.jordond.connectivity.android.internal.AndroidConnectivityProvider | ||
import dev.jordond.connectivity.tools.ContextProvider | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
|
||
public fun Connectivity( | ||
scope: CoroutineScope = CoroutineScope(Dispatchers.Default), | ||
options: ConnectivityOptions.Builder.() -> Unit = {}, | ||
): Connectivity { | ||
val context = ContextProvider.getInstance().context | ||
val provider = AndroidConnectivityProvider(context) | ||
return Connectivity(provider, scope, options) | ||
} |
52 changes: 52 additions & 0 deletions
52
...droidMain/kotlin/dev/jordond/connectivity/android/internal/AndroidConnectivityProvider.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,52 @@ | ||
package dev.jordond.connectivity.android.internal | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import android.net.ConnectivityManager | ||
import android.net.Network | ||
import android.net.NetworkCapabilities.TRANSPORT_CELLULAR | ||
import android.net.NetworkCapabilities.TRANSPORT_WIFI | ||
import android.net.NetworkRequest | ||
import androidx.core.content.getSystemService | ||
import dev.jordond.connectivity.android.Connectivity | ||
import dev.jordond.connectivity.ConnectivityProvider | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.callbackFlow | ||
import kotlinx.coroutines.flow.flowOf | ||
|
||
internal class AndroidConnectivityProvider( | ||
private val context: Context, | ||
) : ConnectivityProvider { | ||
|
||
@SuppressLint("MissingPermission") | ||
override fun monitor(): Flow<Connectivity.Status> { | ||
val connectivityManager = context.getSystemService<ConnectivityManager>() | ||
?: return flowOf(Connectivity.Status.Disconnected) | ||
|
||
return callbackFlow { | ||
val networkRequest = NetworkRequest.Builder().build() | ||
|
||
val networkCallback = object : ConnectivityManager.NetworkCallback() { | ||
override fun onAvailable(network: Network) { | ||
val capability = connectivityManager.getNetworkCapabilities(network) | ||
val isWifi = capability?.hasTransport(TRANSPORT_WIFI) ?: false | ||
val isCellular = capability?.hasTransport(TRANSPORT_CELLULAR) ?: false | ||
val isMetered = | ||
!isWifi || isCellular || connectivityManager.isActiveNetworkMetered | ||
|
||
val status = Connectivity.Status.Connected(isMetered) | ||
trySend(status) | ||
} | ||
|
||
override fun onLost(network: Network) { | ||
trySend(Connectivity.Status.Disconnected) | ||
} | ||
} | ||
|
||
connectivityManager.registerNetworkCallback(networkRequest, networkCallback) | ||
|
||
awaitClose { connectivityManager.unregisterNetworkCallback(networkCallback) } | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,21 @@ | ||
import dev.jordond.connectivity.convention.Platform | ||
import dev.jordond.connectivity.convention.configureMultiplatform | ||
|
||
plugins { | ||
alias(libs.plugins.multiplatform) | ||
alias(libs.plugins.poko) | ||
alias(libs.plugins.dokka) | ||
alias(libs.plugins.publish) | ||
alias(libs.plugins.convention.multiplatform) | ||
} | ||
|
||
configureMultiplatform(Platform.Ios) | ||
|
||
kotlin { | ||
sourceSets { | ||
iosMain.dependencies { | ||
api(projects.connectivityCore) | ||
implementation(libs.kotlinx.coroutines.core) | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
connectivity-ios/src/iosMain/kotlin/dev/jordond/connectivity/ios/IosConnectivity.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,15 @@ | ||
package dev.jordond.connectivity.ios | ||
|
||
import dev.jordond.connectivity.Connectivity | ||
import dev.jordond.connectivity.ConnectivityOptions | ||
import dev.jordond.connectivity.ios.internal.IosConnectivityProvider | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
|
||
public fun Connectivity( | ||
scope: CoroutineScope = CoroutineScope(Dispatchers.Default), | ||
options: ConnectivityOptions.Builder.() -> Unit = {}, | ||
): Connectivity { | ||
val provider = IosConnectivityProvider() | ||
return Connectivity(provider, scope, options) | ||
} |
50 changes: 50 additions & 0 deletions
50
...y-ios/src/iosMain/kotlin/dev/jordond/connectivity/ios/internal/IosConnectivityProvider.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,50 @@ | ||
package dev.jordond.connectivity.ios.internal | ||
|
||
import dev.jordond.connectivity.ios.Connectivity | ||
import dev.jordond.connectivity.ConnectivityProvider | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.callbackFlow | ||
import platform.Network.nw_interface_type_wifi | ||
import platform.Network.nw_path_monitor_cancel | ||
import platform.Network.nw_path_monitor_create | ||
import platform.Network.nw_path_monitor_set_queue | ||
import platform.Network.nw_path_monitor_set_update_handler | ||
import platform.Network.nw_path_monitor_start | ||
import platform.Network.nw_path_status_satisfied | ||
import platform.Network.nw_path_uses_interface_type | ||
import platform.NetworkExtension.NWPath | ||
import platform.NetworkExtension.NWPathStatus | ||
import platform.darwin.DISPATCH_QUEUE_PRIORITY_DEFAULT | ||
import platform.darwin.dispatch_get_global_queue | ||
|
||
internal class IosConnectivityProvider : ConnectivityProvider { | ||
|
||
override fun monitor(): Flow<Connectivity.Status> { | ||
val monitor = nw_path_monitor_create() | ||
val queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT.toLong(), flags = 0uL) | ||
|
||
return callbackFlow { | ||
nw_path_monitor_set_update_handler(monitor) { path -> | ||
val nwPath: NWPath? = path as? NWPath | ||
val status: NWPathStatus? = nwPath?.status() | ||
when { | ||
status != null && status.toLong() == nw_path_status_satisfied.toLong() -> { | ||
val isWifi = nw_path_uses_interface_type(path, nw_interface_type_wifi) | ||
val isMetered = !isWifi && (path.isExpensive() || path.isConstrained()) | ||
|
||
trySend(Connectivity.Status.Connected(isMetered)) | ||
} | ||
else -> Connectivity.Status.Disconnected | ||
} | ||
} | ||
|
||
nw_path_monitor_set_queue(monitor, queue) | ||
nw_path_monitor_start(monitor) | ||
|
||
awaitClose { | ||
nw_path_monitor_cancel(monitor) | ||
} | ||
} | ||
} | ||
} |
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,29 @@ | ||
import dev.jordond.connectivity.convention.Platforms | ||
import dev.jordond.connectivity.convention.configureMultiplatform | ||
|
||
plugins { | ||
alias(libs.plugins.android.library) | ||
alias(libs.plugins.multiplatform) | ||
alias(libs.plugins.poko) | ||
alias(libs.plugins.dokka) | ||
alias(libs.plugins.publish) | ||
alias(libs.plugins.convention.multiplatform) | ||
} | ||
|
||
configureMultiplatform(Platforms.Mobile) | ||
|
||
kotlin { | ||
sourceSets { | ||
commonMain.dependencies { | ||
api(projects.connectivityCore) | ||
} | ||
|
||
androidMain.dependencies { | ||
api(projects.connectivityAndroid) | ||
} | ||
|
||
iosMain.dependencies { | ||
api(projects.connectivityIos) | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...vity-mobile/src/androidMain/kotlin/dev/jordond/connectivity/MobileConnectivity.android.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,9 @@ | ||
package dev.jordond.connectivity | ||
|
||
import dev.jordond.connectivity.android.Connectivity | ||
import kotlinx.coroutines.CoroutineScope | ||
|
||
public actual fun Connectivity( | ||
scope: CoroutineScope, | ||
options: ConnectivityOptions.Builder.() -> Unit, | ||
): Connectivity = Connectivity(scope, options) |
10 changes: 10 additions & 0 deletions
10
connectivity-mobile/src/commonMain/kotlin/dev/jordond/connectivity/MobileConnectivity.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,10 @@ | ||
package dev.jordond.connectivity | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
|
||
public expect fun Connectivity( | ||
scope: CoroutineScope = CoroutineScope(Dispatchers.Default), | ||
options: ConnectivityOptions.Builder.() -> Unit = {}, | ||
): Connectivity | ||
|
9 changes: 9 additions & 0 deletions
9
connectivity-mobile/src/iosMain/kotlin/dev/jordond/connectivity/MobileConnectivity.ios.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,9 @@ | ||
package dev.jordond.connectivity | ||
|
||
import dev.jordond.connectivity.ios.Connectivity | ||
import kotlinx.coroutines.CoroutineScope | ||
|
||
public actual fun Connectivity( | ||
scope: CoroutineScope, | ||
options: ConnectivityOptions.Builder.() -> Unit, | ||
): Connectivity = Connectivity(scope, options) |
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