11package anet
22
3+ // #include <android/api-level.h>
4+ import "C"
5+
36import (
47 "bytes"
58 "errors"
@@ -12,11 +15,10 @@ import (
1215)
1316
1417const (
15- android11 = 11
18+ android11ApiLevel = 30
1619)
1720
1821var (
19- androidVersion uint
2022 errInvalidInterface = errors .New ("invalid network interface" )
2123 errInvalidInterfaceIndex = errors .New ("invalid network interface index" )
2224 errInvalidInterfaceName = errors .New ("invalid network interface name" )
@@ -28,7 +30,7 @@ type ifReq [40]byte
2830
2931// Interfaces returns a list of the system's network interfaces.
3032func Interfaces () ([]net.Interface , error ) {
31- if androidVersion < android11 {
33+ if androidApiLevel () < android11ApiLevel {
3234 return net .Interfaces ()
3335 }
3436
@@ -49,7 +51,7 @@ func Interfaces() ([]net.Interface, error) {
4951// The returned list does not identify the associated interface; use
5052// Interfaces and Interface.Addrs for more detail.
5153func InterfaceAddrs () ([]net.Addr , error ) {
52- if androidVersion < android11 {
54+ if androidApiLevel () < android11ApiLevel {
5355 return net .InterfaceAddrs ()
5456 }
5557
@@ -66,7 +68,7 @@ func InterfaceAddrs() ([]net.Addr, error) {
6668// sharing the logical data link; for more precision use
6769// InterfaceByName.
6870func InterfaceByIndex (index int ) (* net.Interface , error ) {
69- if androidVersion < android11 {
71+ if androidApiLevel () < android11ApiLevel {
7072 return net .InterfaceByIndex (index )
7173 }
7274
@@ -112,7 +114,7 @@ func InterfaceAddrsByInterface(ifi *net.Interface) ([]net.Addr, error) {
112114 return nil , & net.OpError {Op : "route" , Net : "ip+net" , Source : nil , Addr : nil , Err : errInvalidInterface }
113115 }
114116
115- if androidVersion < android11 {
117+ if androidApiLevel () < android11ApiLevel {
116118 return ifi .Addrs ()
117119 }
118120
@@ -123,13 +125,6 @@ func InterfaceAddrsByInterface(ifi *net.Interface) ([]net.Addr, error) {
123125 return ifat , err
124126}
125127
126- // SetAndroidVersion set the Android environment in which the program runs.
127- // The Android system version number can be obtained through
128- // `android.os.Build.VERSION.RELEASE` of the Android framework.
129- func SetAndroidVersion (version uint ) {
130- androidVersion = version
131- }
132-
133128// An ipv6ZoneCache represents a cache holding partial network
134129// interface information. It is used for reducing the cost of IPv6
135130// addressing scope zone resolution.
@@ -422,3 +417,18 @@ func nameToFlags(name string) (net.Flags, error) {
422417
423418 return linkFlags (* (* uint32 )(unsafe .Pointer (& ifr [syscall .IFNAMSIZ ]))), nil
424419}
420+
421+ // Returns the API level of the device we're actually running on, or -1 on failure.
422+ // The returned value is equivalent to the Java Build.VERSION.SDK_INT API.
423+ var androidApiLevel = func () func () int {
424+ var apiLevel int
425+ var once sync.Once
426+
427+ return func () int {
428+ once .Do (func () {
429+ apiLevel = int (C .android_get_device_api_level ())
430+ })
431+
432+ return apiLevel
433+ }
434+ }()
0 commit comments