-
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.
Add a new util to check internet connectivity (#15)
* Add isOnline helper * Fix imports * Add class to manage connection state
- Loading branch information
Showing
5 changed files
with
108 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
86 changes: 86 additions & 0 deletions
86
WalletSdk/src/main/java/com/spruceid/wallet/sdk/ConnectionLiveData.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,86 @@ | ||
package com.spruceid.wallet.sdk | ||
|
||
import android.content.Context | ||
import android.content.Context.CONNECTIVITY_SERVICE | ||
import android.net.ConnectivityManager | ||
import android.net.Network | ||
import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET | ||
import android.net.NetworkCapabilities.TRANSPORT_CELLULAR | ||
import android.net.NetworkCapabilities.TRANSPORT_WIFI | ||
import android.net.NetworkRequest | ||
import androidx.lifecycle.LiveData | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import java.io.IOException | ||
import java.net.InetSocketAddress | ||
import javax.net.SocketFactory | ||
|
||
class ConnectionLiveData(context: Context) : LiveData<Boolean>() { | ||
|
||
private lateinit var networkCallback: ConnectivityManager.NetworkCallback | ||
private val connectivityManager = | ||
context.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager | ||
private val validNetworks: MutableSet<Network> = HashSet() | ||
|
||
private fun checkValidNetworks() { | ||
postValue(validNetworks.size > 0) | ||
} | ||
|
||
override fun onActive() { | ||
networkCallback = createNetworkCallback() | ||
val networkRequest = NetworkRequest.Builder() | ||
.addCapability(NET_CAPABILITY_INTERNET) | ||
.addTransportType(TRANSPORT_WIFI) | ||
.addTransportType(TRANSPORT_CELLULAR) | ||
.build() | ||
connectivityManager.registerNetworkCallback(networkRequest, networkCallback) | ||
} | ||
|
||
override fun onInactive() { | ||
connectivityManager.unregisterNetworkCallback(networkCallback) | ||
} | ||
|
||
private fun createNetworkCallback() = object : ConnectivityManager.NetworkCallback() { | ||
|
||
override fun onAvailable(network: Network) { | ||
val networkCapabilities = connectivityManager.getNetworkCapabilities(network) | ||
val hasInternetCapability = networkCapabilities?.hasCapability(NET_CAPABILITY_INTERNET) | ||
|
||
if (hasInternetCapability == true) { | ||
// Check if this network actually has internet | ||
CoroutineScope(Dispatchers.IO).launch { | ||
val hasInternet = DoesNetworkHaveInternet.execute(network.socketFactory) | ||
if (hasInternet) { | ||
withContext(Dispatchers.Main) { | ||
validNetworks.add(network) | ||
checkValidNetworks() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onLost(network: Network) { | ||
validNetworks.remove(network) | ||
checkValidNetworks() | ||
} | ||
} | ||
|
||
object DoesNetworkHaveInternet { | ||
suspend fun execute(socketFactory: SocketFactory): Boolean { | ||
// Make sure to execute this on a background thread. | ||
delay(1000) | ||
return try { | ||
val socket = socketFactory.createSocket() ?: throw IOException("Socket is null.") | ||
socket.connect(InetSocketAddress("8.8.8.8", 53), 1500) | ||
socket.close() | ||
true | ||
} catch (e: IOException) { | ||
false | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -27,4 +27,4 @@ enum class PresentmentState { | |
|
||
/// App should display a success message and offer to close the page | ||
SUCCESS, | ||
} | ||
} |
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