Skip to content

Commit

Permalink
Fix no connection with gnirehtet reverse tethering
Browse files Browse the repository at this point in the history
  • Loading branch information
Linus789 committed Feb 18, 2023
1 parent 9d23388 commit e10b1ae
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions app/src/main/java/com/github/libretube/helpers/NetworkHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,32 @@ package com.github.libretube.helpers

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import androidx.core.content.getSystemService

object NetworkHelper {
/**
* Detect whether network is available
*/
fun isNetworkAvailable(context: Context): Boolean {
val connectivityManager = context.getSystemService<ConnectivityManager>()
// In case we are using a VPN, we return true since we might be using reverse tethering
val connectivityManager = context.getSystemService<ConnectivityManager>() ?: return false

// this seems to not recognize vpn connections
/*
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val nw = connectivityManager.activeNetwork ?: return false
val actNw = connectivityManager.getNetworkCapabilities(nw) ?: return false
return when {
// WiFi
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
// Mobile
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
// Ethernet
actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
// Bluetooth
actNw.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH) -> true
// VPN
actNw.hasTransport(NetworkCapabilities.TRANSPORT_VPN) -> true
else -> false
}
if (Build.VERSION.SDK_INT >= 23) {
val activeNetwork = connectivityManager.activeNetwork
val caps = connectivityManager.getNetworkCapabilities(activeNetwork) ?: return false
val hasConnection = caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
val isVpn = caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN)
return hasConnection || isVpn
} else {
return connectivityManager.activeNetworkInfo?.isConnected ?: false
}
*/
if (connectivityManager.activeNetworkInfo?.isConnected == true) {
return true
}

@Suppress("DEPRECATION")
return connectivityManager?.activeNetworkInfo?.isConnected ?: false
val vpnInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_VPN)
return vpnInfo?.isConnected == true
}
}

/**
Expand Down

0 comments on commit e10b1ae

Please sign in to comment.