mirror of
https://github.com/wgtunnel/android.git
synced 2026-06-02 08:33:40 +02:00
feat: display Wi-Fi security type for Android 12 and greater
refactor: deprecated clipboard manager
This commit is contained in:
+19
-6
@@ -46,13 +46,18 @@ class AndroidNetworkMonitor(
|
||||
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO
|
||||
|
||||
@get:Synchronized @set:Synchronized var currentSsid: String? = null
|
||||
@get:Synchronized @set:Synchronized var securityType: WifiSecurityType? = null
|
||||
|
||||
@get:Synchronized @set:Synchronized var wifiConnected = false
|
||||
|
||||
// Track active Wi-Fi networks and last active network ID
|
||||
private val activeNetworks = Collections.synchronizedSet(mutableSetOf<Network>())
|
||||
|
||||
data class WifiState(val connected: Boolean = false, val ssid: String? = null)
|
||||
data class WifiState(
|
||||
val connected: Boolean = false,
|
||||
val ssid: String? = null,
|
||||
val securityType: WifiSecurityType? = null,
|
||||
)
|
||||
|
||||
data class TransportState(val connected: Boolean = false)
|
||||
|
||||
@@ -76,15 +81,15 @@ class AndroidNetworkMonitor(
|
||||
|
||||
suspend fun handleUnknownWifi() {
|
||||
val newSsid = getWifiSsid()
|
||||
val securityType = wifiManager?.getCurrentSecurityType()
|
||||
// Only update if new SSID is valid; preserve existing valid SSID otherwise
|
||||
if (newSsid != null && newSsid != WifiManager.UNKNOWN_SSID) {
|
||||
currentSsid = newSsid
|
||||
trySend(WifiState(connected = wifiConnected, ssid = currentSsid))
|
||||
trySend(WifiState(wifiConnected, currentSsid, securityType))
|
||||
} else if (currentSsid == null || currentSsid == WifiManager.UNKNOWN_SSID) {
|
||||
currentSsid = newSsid
|
||||
trySend(WifiState(connected = wifiConnected, ssid = currentSsid))
|
||||
trySend(WifiState(wifiConnected, currentSsid, securityType))
|
||||
}
|
||||
Timber.d("handleUnknownWifi: currentSsid=$currentSsid")
|
||||
}
|
||||
|
||||
val locationPermissionReceiver =
|
||||
@@ -146,8 +151,15 @@ class AndroidNetworkMonitor(
|
||||
activeNetworks.add(network)
|
||||
launch {
|
||||
currentSsid = getWifiSsid()
|
||||
securityType = wifiManager?.getCurrentSecurityType()
|
||||
wifiConnected = true
|
||||
trySend(WifiState(connected = true, ssid = currentSsid))
|
||||
trySend(
|
||||
WifiState(
|
||||
connected = true,
|
||||
ssid = currentSsid,
|
||||
securityType = securityType,
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,7 +172,7 @@ class AndroidNetworkMonitor(
|
||||
)
|
||||
currentSsid = null
|
||||
wifiConnected = false
|
||||
trySend(WifiState(connected = false, ssid = null))
|
||||
trySend(WifiState(connected = false, ssid = null, securityType = null))
|
||||
} else {
|
||||
Timber.d("Wi-Fi onLost, but still connected to other networks, ignoring")
|
||||
}
|
||||
@@ -241,6 +253,7 @@ class AndroidNetworkMonitor(
|
||||
if (hasAnyConnection) {
|
||||
NetworkStatus.Connected(
|
||||
wifiSsid = wifi.ssid,
|
||||
securityType = wifi.securityType,
|
||||
wifiConnected = wifi.connected,
|
||||
cellularConnected = cellular.connected,
|
||||
ethernetConnected = ethernet.connected,
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.zaneschepke.networkmonitor
|
||||
|
||||
import android.net.wifi.WifiManager
|
||||
import android.os.Build
|
||||
import com.wireguard.android.util.RootShell
|
||||
|
||||
fun RootShell.getCurrentWifiName(): String? {
|
||||
@@ -10,3 +12,12 @@ fun RootShell.getCurrentWifiName(): String? {
|
||||
)
|
||||
return response.firstOrNull()
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
fun WifiManager.getCurrentSecurityType(): WifiSecurityType? {
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
WifiSecurityType.from(connectionInfo.currentSecurityType)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ sealed class NetworkStatus {
|
||||
|
||||
data class Connected(
|
||||
val wifiSsid: String? = null,
|
||||
val securityType: WifiSecurityType? = null,
|
||||
override val wifiConnected: Boolean = false,
|
||||
override val ethernetConnected: Boolean = false,
|
||||
override val cellularConnected: Boolean = false,
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.zaneschepke.networkmonitor
|
||||
|
||||
import android.net.wifi.WifiInfo
|
||||
|
||||
enum class WifiSecurityType {
|
||||
UNKNOWN,
|
||||
OPEN,
|
||||
WEP,
|
||||
WPA2, // WPA and WPA2
|
||||
WPA3, // WPA3-Personal (SAE)
|
||||
OWE,
|
||||
WAPI, // All WAPI_PSK and WAPI_CERT
|
||||
EAP, // All EAP (covers both WPA3 and others)
|
||||
PASSPOINT, // All Passpoint versions
|
||||
DPP;
|
||||
|
||||
companion object {
|
||||
fun from(securityType: Int): WifiSecurityType {
|
||||
return when (securityType) {
|
||||
WifiInfo.SECURITY_TYPE_OPEN -> OPEN
|
||||
WifiInfo.SECURITY_TYPE_WEP -> WEP
|
||||
WifiInfo.SECURITY_TYPE_PSK -> WPA2
|
||||
WifiInfo.SECURITY_TYPE_EAP -> EAP
|
||||
WifiInfo.SECURITY_TYPE_SAE -> WPA3
|
||||
WifiInfo.SECURITY_TYPE_OWE -> OWE
|
||||
WifiInfo.SECURITY_TYPE_WAPI_PSK,
|
||||
WifiInfo.SECURITY_TYPE_WAPI_CERT -> WAPI
|
||||
WifiInfo.SECURITY_TYPE_EAP_WPA3_ENTERPRISE -> EAP
|
||||
WifiInfo.SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT -> EAP
|
||||
WifiInfo.SECURITY_TYPE_PASSPOINT_R1_R2,
|
||||
WifiInfo.SECURITY_TYPE_PASSPOINT_R3 -> PASSPOINT
|
||||
WifiInfo.SECURITY_TYPE_DPP -> DPP
|
||||
WifiInfo.SECURITY_TYPE_UNKNOWN -> UNKNOWN
|
||||
else -> UNKNOWN
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user