Skip to content

Commit

Permalink
- Fixed issue #8
Browse files Browse the repository at this point in the history
  • Loading branch information
rommansabbir committed Jun 9, 2021
1 parent bb5039c commit 186135f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Step 2. Add the dependency.

| Releases
| ------------- |
| 2.1.0 |
| 2.2.0 |

### What's new in this version?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class NetworkXImpl(

private var isInternetConnected: Boolean = false
private var isInternetConnectedLiveData: MutableLiveData<Boolean> =
MutableLiveData()
MutableLiveData(false)

override fun isInternetConnected(): Boolean = isInternetConnected

Expand Down Expand Up @@ -96,7 +96,7 @@ class NetworkXImpl(
* Developer can provide custom delay to this observation
* Use recursive strategy to perform the same procedure again and again
*/
suspend fun startObserving(strategy: NetworkXObservingStrategy) {
private suspend fun startObserving(strategy: NetworkXObservingStrategy) {
if (ioScope != null && mainScope != null) {
val temp = isNetworkAvailable(application)
mainScope?.launch {
Expand Down Expand Up @@ -128,28 +128,32 @@ class NetworkXImpl(
private fun isNetworkAvailable(
context: Context
): Boolean {
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val capabilities =
connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
if (capabilities != null) {
when {
capabilities.hasTransport(
NetworkCapabilities.TRANSPORT_CELLULAR
) -> return true
capabilities.hasTransport(
NetworkCapabilities.TRANSPORT_WIFI
) -> return true
capabilities.hasTransport(
NetworkCapabilities.TRANSPORT_ETHERNET
) -> return true
try {
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val capabilities =
connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
if (capabilities != null) {
when {
capabilities.hasTransport(
NetworkCapabilities.TRANSPORT_CELLULAR
) -> return true
capabilities.hasTransport(
NetworkCapabilities.TRANSPORT_WIFI
) -> return true
capabilities.hasTransport(
NetworkCapabilities.TRANSPORT_ETHERNET
) -> return true
}
}
} else {
return connectivityManager.activeNetworkInfo != null &&
connectivityManager.activeNetworkInfo!!.isConnected
}
} else {
return connectivityManager.activeNetworkInfo != null &&
connectivityManager.activeNetworkInfo!!.isConnected
return false
} catch (e: Exception) {
return false
}
return false
}
}

0 comments on commit 186135f

Please sign in to comment.