-
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.
Created .util.network.NetworkConnectionUtility for Issue #3
- Loading branch information
1 parent
5368397
commit 1f13872
Showing
6 changed files
with
80 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<?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> |
71 changes: 71 additions & 0 deletions
71
csci448/src/main/java/edu/mines/csci448/util/network/NetworkConnectionUtility.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,71 @@ | ||
package edu.mines.csci448.util.network | ||
|
||
import android.content.Context | ||
import android.net.ConnectivityManager | ||
import android.net.Network | ||
import android.net.NetworkCapabilities | ||
import android.net.NetworkRequest | ||
import android.util.Log | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.update | ||
|
||
object NetworkConnectionUtility { | ||
private const val LOG_TAG = "448.NetworkConnectionUtility" | ||
|
||
fun isNetworkAvailableAndConnected(context: Context): Boolean { | ||
val connectivityManager = context.getSystemService(ConnectivityManager::class.java) as ConnectivityManager | ||
val activeNetwork = connectivityManager.activeNetwork | ||
return activeNetwork != null && (connectivityManager.getNetworkCapabilities(activeNetwork)?.hasCapability( | ||
NetworkCapabilities.NET_CAPABILITY_VALIDATED) ?: false) | ||
} | ||
|
||
private val mNetworkAvailableStateFlow: MutableStateFlow<Boolean> = MutableStateFlow(false) | ||
val networkAvailableStateFlow: StateFlow<Boolean> | ||
get() = mNetworkAvailableStateFlow.asStateFlow() | ||
|
||
private val networkRequest = NetworkRequest.Builder() | ||
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) | ||
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI) | ||
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR) | ||
.build() | ||
|
||
private val networkCallback = object : ConnectivityManager.NetworkCallback() { | ||
// network is available for use | ||
override fun onAvailable(network: Network) { | ||
super.onAvailable(network) | ||
Log.d(LOG_TAG, "onAvailable() called: network is available for use") | ||
mNetworkAvailableStateFlow.update { true } | ||
} | ||
|
||
// Network capabilities have changed for the network | ||
override fun onCapabilitiesChanged( | ||
network: Network, | ||
networkCapabilities: NetworkCapabilities | ||
) { | ||
super.onCapabilitiesChanged(network, networkCapabilities) | ||
val unmetered = networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED) | ||
val validated = networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) | ||
Log.d(LOG_TAG, "onCapabilitiesChanged() called - validated/unmetered = $validated/$unmetered") | ||
mNetworkAvailableStateFlow.update { validated } | ||
} | ||
|
||
// lost network connection | ||
override fun onLost(network: Network) { | ||
super.onLost(network) | ||
Log.d(LOG_TAG, "onLost() called: lost network connection") | ||
mNetworkAvailableStateFlow.update { false } | ||
} | ||
} | ||
|
||
fun monitorNetworkStatus(context: Context) { | ||
val connectivityManager = context.getSystemService(ConnectivityManager::class.java) as ConnectivityManager | ||
connectivityManager.requestNetwork(networkRequest, networkCallback) | ||
} | ||
|
||
fun stopMonitoringNetworkStatus(context: Context) { | ||
val connectivityManager = context.getSystemService(ConnectivityManager::class.java) as ConnectivityManager | ||
connectivityManager.unregisterNetworkCallback(networkCallback) | ||
} | ||
} |