|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2025 Esri |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | +package com.arcgismaps.toolkit.offline.internal.utils |
| 19 | + |
| 20 | +import android.content.Context |
| 21 | +import android.net.ConnectivityManager |
| 22 | +import android.net.Network |
| 23 | +import android.net.NetworkCapabilities |
| 24 | +import android.net.NetworkRequest |
| 25 | +import androidx.compose.runtime.Composable |
| 26 | +import androidx.compose.runtime.produceState |
| 27 | +import androidx.compose.ui.platform.LocalContext |
| 28 | +import kotlinx.coroutines.channels.awaitClose |
| 29 | +import kotlinx.coroutines.flow.callbackFlow |
| 30 | +import androidx.compose.runtime.State |
| 31 | + |
| 32 | +@Composable |
| 33 | +internal fun networkConnectivityState(): State<NetworkConnectionState> { |
| 34 | + val context = LocalContext.current |
| 35 | + |
| 36 | + // Creates a State<NetworkConnectionState> with current connectivity state as initial value |
| 37 | + return produceState(initialValue = context.currentConnectivityState) { |
| 38 | + // In a coroutine, can make suspend calls |
| 39 | + context.observeConnectivityAsFlow().collect { value = it } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +internal fun Context.observeConnectivityAsFlow() = callbackFlow { |
| 44 | + val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager |
| 45 | + |
| 46 | + val callback = NetworkCallback(connectivityManager) { networkConnectionState -> trySend(networkConnectionState) } |
| 47 | + |
| 48 | + val networkRequest = NetworkRequest.Builder() |
| 49 | + .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) |
| 50 | + .build() |
| 51 | + |
| 52 | + connectivityManager.registerNetworkCallback(networkRequest, callback) |
| 53 | + |
| 54 | + // Set current state |
| 55 | + val currentState = getCurrentConnectivityState(connectivityManager) |
| 56 | + trySend(currentState) |
| 57 | + |
| 58 | + // Remove callback when not used |
| 59 | + awaitClose { |
| 60 | + // Remove listeners |
| 61 | + connectivityManager.unregisterNetworkCallback(callback) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +internal fun NetworkCallback( |
| 66 | + connectivityManager: ConnectivityManager, |
| 67 | + callback: (NetworkConnectionState) -> Unit |
| 68 | +): ConnectivityManager.NetworkCallback { |
| 69 | + return object : ConnectivityManager.NetworkCallback() { |
| 70 | + /** |
| 71 | + * This callback is triggered when a network is available. |
| 72 | + * It indicates that the device has internet connectivity. |
| 73 | + */ |
| 74 | + override fun onAvailable(network: Network) { |
| 75 | + callback(NetworkConnectionState.Available) |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * This callback is triggered when a network is temporarily unavailable. |
| 80 | + * It does not necessarily mean that there is no internet connectivity, |
| 81 | + * but rather that the network is in a transient state (e.g., switching networks). |
| 82 | + */ |
| 83 | + override fun onLost(network: Network) { |
| 84 | + // This callback is triggered when any previously available network is lost, |
| 85 | + // not just when all internet connectivity is lost. If the device switches |
| 86 | + // between networks (e.g., from Wi-Fi to mobile data), onLost is called for |
| 87 | + // the old network, even if another network is still available. |
| 88 | + val state = getCurrentConnectivityState(connectivityManager) |
| 89 | + callback(state) |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Network utility to get current state of internet connection |
| 96 | + */ |
| 97 | +internal val Context.currentConnectivityState: NetworkConnectionState |
| 98 | + get() { |
| 99 | + val connectivityManager = |
| 100 | + getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager |
| 101 | + return getCurrentConnectivityState(connectivityManager) |
| 102 | + } |
| 103 | + |
| 104 | +/** |
| 105 | + * Helper function to determine the current connectivity state. |
| 106 | + */ |
| 107 | +private fun getCurrentConnectivityState( |
| 108 | + connectivityManager: ConnectivityManager |
| 109 | +): NetworkConnectionState { |
| 110 | + val connected = connectivityManager.allNetworks.any { network -> |
| 111 | + connectivityManager.getNetworkCapabilities(network) |
| 112 | + ?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) == true |
| 113 | + } |
| 114 | + |
| 115 | + return if (connected) NetworkConnectionState.Available else NetworkConnectionState.Unavailable |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Represents the state of network connectivity. |
| 120 | + * |
| 121 | + * - [Available]: Indicates that the device has internet connectivity. |
| 122 | + * - [Unavailable]: Indicates that the device does not have internet connectivity. |
| 123 | + */ |
| 124 | +internal sealed class NetworkConnectionState { |
| 125 | + object Available : NetworkConnectionState() |
| 126 | + object Unavailable : NetworkConnectionState() |
| 127 | +} |
0 commit comments