aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-android/app/src/main/kotlin/org
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-android/app/src/main/kotlin/org')
-rw-r--r--packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastChannels.kt27
1 files changed, 23 insertions, 4 deletions
diff --git a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastChannels.kt b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastChannels.kt
index edaec67a..ced0ed3b 100644
--- a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastChannels.kt
+++ b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastChannels.kt
@@ -134,19 +134,38 @@ class CastChannels(
* The Wi-Fi (or Ethernet) address, never the mobile network's: a TV cannot
* be reached there. Nor a VPN's: a VPN network carries the transports of
* the network under it, Wi-Fi included, and its address is a tunnel the
- * television cannot route to.
+ * television cannot route to. Nor the phone's own hotspot when it is
+ * joined to a Wi-Fi: Android 17 lists the hotspot to applications as a
+ * Wi-Fi network of its own, local only, and taken first it handed the
+ * television an address on a network it is not on (`pickLan`).
*/
private fun lanAddress(): InetAddress? {
val cm = context.getSystemService(ConnectivityManager::class.java)
+ val active = cm.activeNetwork
+ val found = ArrayList<Lan>()
for (network in cm.allNetworks) {
val caps = cm.getNetworkCapabilities(network) ?: continue
if (caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) continue
if (!caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) &&
!caps.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) continue
val addr = cm.getLinkProperties(network)?.linkAddresses?.map { it.address }
- ?.firstOrNull { it is Inet4Address && !it.isLoopbackAddress }
- if (addr != null) return addr
+ ?.firstOrNull { it is Inet4Address && !it.isLoopbackAddress } ?: continue
+ found.add(Lan(addr, network == active,
+ caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)))
}
- return null
+ return pickLan(found)
+ }
+
+ /** A candidate network for the relay: its address, and what says it is the one the phone is on. */
+ class Lan(val address: InetAddress, val active: Boolean, val internet: Boolean)
+
+ companion object {
+ /**
+ * The network the phone uses, then one with Internet, then any: a
+ * home Wi-Fi without Internet still casts, a hotspot is the last resort.
+ */
+ fun pickLan(candidates: List<Lan>): InetAddress? =
+ (candidates.firstOrNull { it.active } ?: candidates.firstOrNull { it.internet }
+ ?: candidates.firstOrNull())?.address
}
}