Skip to content

Commit

Permalink
Add a new util to check internet connectivity (#15)
Browse files Browse the repository at this point in the history
* Add isOnline helper

* Fix imports

* Add class to manage connection state
  • Loading branch information
w4ll3 authored Jul 26, 2024
1 parent 970cf58 commit 5a3ad87
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 3 deletions.
3 changes: 2 additions & 1 deletion WalletSdk/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.CAMERA" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
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
}
}
}
}
2 changes: 1 addition & 1 deletion WalletSdk/src/main/java/com/spruceid/wallet/sdk/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ enum class PresentmentState {

/// App should display a success message and offer to close the page
SUCCESS,
}
}
2 changes: 2 additions & 0 deletions example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>


<application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import com.spruceid.wallet.sdk.ConnectionLiveData
import com.spruceid.wallet.sdk.CredentialsViewModel
import com.spruceid.wallet.sdk.MDoc
import com.spruceid.wallet.sdk.PresentmentState
Expand All @@ -47,10 +51,22 @@ import java.security.spec.PKCS8EncodedKeySpec
import java.util.Base64

class MainActivity : ComponentActivity() {
private lateinit var connectionLiveData: ConnectionLiveData

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContent {
val viewModel by viewModels<CredentialsViewModel>()
var isConnected by remember { mutableStateOf(false) }

connectionLiveData = ConnectionLiveData(this)

connectionLiveData.observe(this) { isNetworkAvailable ->
isNetworkAvailable?.let {
isConnected = it
}
}

viewModel.storeCredential(generateMDoc())

Expand Down Expand Up @@ -96,7 +112,7 @@ class MainActivity : ComponentActivity() {
viewModel.present(getBluetoothManager(baseContext)!!)
}
) {
Text(text = "Share via QR Code")
Text(text = "Share via QR Code " + isConnected)
}
}
}
Expand Down

0 comments on commit 5a3ad87

Please sign in to comment.