Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConnectivityChecker (connectivityManager?.activeNetworkInfo is deprecated) #803 #827

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/src/main/java/io/plaidapp/dagger/HomeModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ abstract class HomeModule {
fun connectivityChecker(activity: Activity): ConnectivityChecker? {
val connectivityManager = activity.getSystemService<ConnectivityManager>()
return if (connectivityManager != null) {
ConnectivityChecker(connectivityManager)
ConnectivityChecker(connectivityManager,activity.applicationContext)
} else {
null
}
Expand Down
39 changes: 36 additions & 3 deletions core/src/main/java/io/plaidapp/core/ui/ConnectivityChecker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

package io.plaidapp.core.ui

import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkInfo
import android.net.NetworkRequest
import android.os.Build
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LiveData
Expand All @@ -39,7 +41,8 @@ import androidx.lifecycle.OnLifecycleEvent
* connected, we stop listening to connectivity.¬
*/
class ConnectivityChecker(
private val connectivityManager: ConnectivityManager
private val connectivityManager: ConnectivityManager,
private val context: Context
) : LifecycleObserver {

private var monitoringConnectivity = false
Expand Down Expand Up @@ -71,8 +74,7 @@ class ConnectivityChecker(

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun startMonitoringConnectivity() {
val activeNetworkInfo: NetworkInfo? = connectivityManager.activeNetworkInfo
val connected = activeNetworkInfo != null && activeNetworkInfo.isConnected
val connected = isInternetAvailable()
_connectedStatus.postValue(connected)
if (!connected) {
// we don't have internet connection, so we listen to notifications in connection status
Expand All @@ -84,4 +86,35 @@ class ConnectivityChecker(
monitoringConnectivity = true
}
}

private fun isInternetAvailable(): Boolean {
var result = false
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val networkCapabilities = connectivityManager.activeNetwork ?: return false
val actNw =
connectivityManager.getNetworkCapabilities(networkCapabilities) ?: return false
result = when {
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
else -> false
}
} else {
connectivityManager.run {
connectivityManager.activeNetworkInfo?.run {
result = when (type) {
ConnectivityManager.TYPE_WIFI -> true
ConnectivityManager.TYPE_MOBILE -> true
ConnectivityManager.TYPE_ETHERNET -> true
else -> false
}

}
}
}

return result
}
}