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

Fix no connection with gnirehtet reverse tethering #3119

Merged
merged 2 commits into from
Feb 20, 2023
Merged
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
57 changes: 32 additions & 25 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,33 @@ 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
// activeNetworkInfo might return null instead of the VPN, so better check it explicitly
val vpnInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_VPN)
return vpnInfo?.isConnected == true
}
}

/**
Expand All @@ -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
}
}