Skip to content

Commit 5b0cdf4

Browse files
fix(network_info_plus)!: Do not ignore errors on Android (#3080)
1 parent dfa3bc2 commit 5b0cdf4

File tree

1 file changed

+43
-60
lines changed
  • packages/network_info_plus/network_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/network_info

1 file changed

+43
-60
lines changed

packages/network_info_plus/network_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/network_info/NetworkInfo.kt

Lines changed: 43 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import android.os.Build
77
import java.net.*
88

99
/** Reports network info such as wifi name and address. */
10-
internal class NetworkInfo(private val wifiManager: WifiManager,
11-
private val connectivityManager: ConnectivityManager? = null
10+
internal class NetworkInfo(
11+
private val wifiManager: WifiManager,
12+
private val connectivityManager: ConnectivityManager? = null
1213
) {
1314

1415
// Using deprecated `connectionInfo` call here to be able to get info on demand
@@ -25,7 +26,8 @@ internal class NetworkInfo(private val wifiManager: WifiManager,
2526
var ipAddress: String? = null
2627

2728
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
28-
val linkAddresses = connectivityManager?.getLinkProperties(connectivityManager.activeNetwork)?.linkAddresses
29+
val linkAddresses =
30+
connectivityManager?.getLinkProperties(connectivityManager.activeNetwork)?.linkAddresses
2931

3032
val ipV4Address = linkAddresses?.firstOrNull { linkAddress ->
3133
linkAddress.address.hostAddress?.contains('.')
@@ -35,64 +37,52 @@ internal class NetworkInfo(private val wifiManager: WifiManager,
3537
ipAddress = ipV4Address
3638
} else {
3739
@Suppress("DEPRECATION")
38-
val interfaceIp = wifiInfo!!.ipAddress
40+
val interfaceIp = wifiInfo.ipAddress
3941
if (interfaceIp != 0) ipAddress = formatIPAddress(interfaceIp)
4042
}
4143
return ipAddress
4244
}
4345

4446
fun getWifiSubnetMask(): String {
4547
val ip = getWifiIPAddress()
46-
var subnet = ""
47-
try {
48-
val inetAddress = InetAddress.getByName(ip)
49-
subnet = getIPv4Subnet(inetAddress)
50-
} catch (ignored: Exception) {
51-
}
48+
val inetAddress = InetAddress.getByName(ip)
49+
val subnet = getIPv4Subnet(inetAddress)
5250
return subnet
5351
}
5452

5553
fun getBroadcastIP(): String? {
56-
var broadcastIP: String? = null
5754
val currentWifiIpAddress = getWifiIPAddress()
5855
val inetAddress = InetAddress.getByName(currentWifiIpAddress)
59-
try {
60-
val networkInterface = NetworkInterface.getByInetAddress(inetAddress)
61-
networkInterface.interfaceAddresses.forEach { interfaceAddress ->
62-
if (!interfaceAddress.address.isLoopbackAddress) {
63-
if (interfaceAddress.broadcast != null) {
64-
broadcastIP = interfaceAddress.broadcast.hostAddress
65-
}
56+
val networkInterface = NetworkInterface.getByInetAddress(inetAddress)
57+
networkInterface.interfaceAddresses.forEach { interfaceAddress ->
58+
if (!interfaceAddress.address.isLoopbackAddress) {
59+
if (interfaceAddress.broadcast != null) {
60+
return interfaceAddress.broadcast.hostAddress
6661
}
6762
}
68-
} catch (ignored: Exception) {
69-
7063
}
71-
return broadcastIP
64+
return null
7265
}
7366

7467
fun getIpV6(): String? {
75-
try {
76-
val ip = getWifiIPAddress()
77-
val ni = NetworkInterface.getByInetAddress(InetAddress.getByName(ip))
78-
for (interfaceAddress in ni.interfaceAddresses) {
79-
val address = interfaceAddress.address
80-
if (!address.isLoopbackAddress && address is Inet6Address) {
81-
val ipaddress = address.getHostAddress()
82-
if (ipaddress != null) {
83-
return ipaddress.split("%").toTypedArray()[0]
84-
}
68+
val ip = getWifiIPAddress()
69+
val ni = NetworkInterface.getByInetAddress(InetAddress.getByName(ip))
70+
for (interfaceAddress in ni.interfaceAddresses) {
71+
val address = interfaceAddress.address
72+
if (!address.isLoopbackAddress && address is Inet6Address) {
73+
val ipaddress = address.getHostAddress()
74+
if (ipaddress != null) {
75+
return ipaddress.split("%").toTypedArray()[0]
8576
}
8677
}
87-
} catch (ignored: SocketException) {
88-
8978
}
9079
return null
9180
}
9281

9382
fun getGatewayIPAddress(): String? {
9483
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
95-
val linkAddresses = connectivityManager?.getLinkProperties(connectivityManager.activeNetwork)
84+
val linkAddresses =
85+
connectivityManager?.getLinkProperties(connectivityManager.activeNetwork)
9686
val dhcpServer = linkAddresses?.dhcpServerAddress?.hostAddress
9787

9888
dhcpServer
@@ -115,39 +105,32 @@ internal class NetworkInfo(private val wifiManager: WifiManager,
115105
)
116106

117107
private fun getIPv4Subnet(inetAddress: InetAddress): String {
118-
try {
119-
val ni = NetworkInterface.getByInetAddress(inetAddress)
120-
val intAddresses = ni.interfaceAddresses
121-
for (ia in intAddresses) {
122-
if (!ia.address.isLoopbackAddress && ia.address is Inet4Address) {
123-
val networkPrefix =
124-
getIPv4SubnetFromNetPrefixLength(ia.networkPrefixLength.toInt())
125-
if (networkPrefix != null) {
126-
return networkPrefix.hostAddress!!
127-
}
108+
val ni = NetworkInterface.getByInetAddress(inetAddress)
109+
val intAddresses = ni.interfaceAddresses
110+
for (ia in intAddresses) {
111+
if (!ia.address.isLoopbackAddress && ia.address is Inet4Address) {
112+
val networkPrefix =
113+
getIPv4SubnetFromNetPrefixLength(ia.networkPrefixLength.toInt())
114+
if (networkPrefix != null) {
115+
return networkPrefix.hostAddress!!
128116
}
129117
}
130-
} catch (ignored: Exception) {
131118
}
132119
return ""
133120
}
134121

135122
private fun getIPv4SubnetFromNetPrefixLength(netPrefixLength: Int): InetAddress? {
136-
try {
137-
var shift = 1 shl 31
138-
for (i in netPrefixLength - 1 downTo 1) {
139-
shift = shift shr 1
140-
}
141-
val subnet = ((shift shr 24 and 255)
142-
.toString() + "."
143-
+ (shift shr 16 and 255)
144-
+ "."
145-
+ (shift shr 8 and 255)
146-
+ "."
147-
+ (shift and 255))
148-
return InetAddress.getByName(subnet)
149-
} catch (ignored: Exception) {
123+
var shift = 1 shl 31
124+
for (i in netPrefixLength - 1 downTo 1) {
125+
shift = shift shr 1
150126
}
151-
return null
127+
val subnet = ((shift shr 24 and 255)
128+
.toString() + "."
129+
+ (shift shr 16 and 255)
130+
+ "."
131+
+ (shift shr 8 and 255)
132+
+ "."
133+
+ (shift and 255))
134+
return InetAddress.getByName(subnet)
152135
}
153136
}

0 commit comments

Comments
 (0)