mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 16:30:31 +05:30
Merge pull request #3119 from Linus789/fix-rt-connection
Fix no connection with gnirehtet reverse tethering
This commit is contained in:
commit
091a855e4b
@ -2,6 +2,8 @@ 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 {
|
||||
@ -9,33 +11,24 @@ 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
|
||||
// activeNetworkInfo might return null instead of the VPN, so better check it explicitly
|
||||
val vpnInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_VPN)
|
||||
return vpnInfo?.isConnected == true
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -44,6 +37,20 @@ object NetworkHelper {
|
||||
* @return whether the network is metered or not
|
||||
*/
|
||||
fun isNetworkMetered(context: Context): Boolean {
|
||||
return context.getSystemService<ConnectivityManager>()!!.isActiveNetworkMetered
|
||||
val connectivityManager = context.getSystemService<ConnectivityManager>()!!
|
||||
val activeNetworkInfo = connectivityManager.activeNetworkInfo
|
||||
|
||||
// In case we are using nothing but a VPN, it should default to not metered
|
||||
if (activeNetworkInfo == null) {
|
||||
// activeNetworkInfo might return null instead of the VPN, so better check it explicitly
|
||||
val vpnInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_VPN)
|
||||
if (vpnInfo?.isConnected == true) {
|
||||
return false
|
||||
}
|
||||
} else if (activeNetworkInfo.type == ConnectivityManager.TYPE_VPN) {
|
||||
return false
|
||||
}
|
||||
|
||||
return connectivityManager.isActiveNetworkMetered
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user