Skip to content

Commit

Permalink
Merge pull request wlynxg#4 from bobrofon/android-version-resolve
Browse files Browse the repository at this point in the history
Detect Android version automatically
  • Loading branch information
wlynxg authored Aug 6, 2024
2 parents 4d6caa2 + 1c971b2 commit e684438
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ func InterfaceAddrsByInterface(ifi *net.Interface) ([]net.Addr, error) {
return ifi.Addrs()
}

// Deprecated: Android version is detected automatically.
func SetAndroidVersion(version uint) {}
36 changes: 23 additions & 13 deletions interface_android.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package anet

// #include <android/api-level.h>
import "C"

import (
"bytes"
"errors"
Expand All @@ -12,11 +15,10 @@ import (
)

const (
android11 = 11
android11ApiLevel = 30
)

var (
androidVersion uint
errInvalidInterface = errors.New("invalid network interface")
errInvalidInterfaceIndex = errors.New("invalid network interface index")
errInvalidInterfaceName = errors.New("invalid network interface name")
Expand All @@ -28,7 +30,7 @@ type ifReq [40]byte

// Interfaces returns a list of the system's network interfaces.
func Interfaces() ([]net.Interface, error) {
if androidVersion < android11 {
if androidApiLevel() < android11ApiLevel {
return net.Interfaces()
}

Expand All @@ -49,7 +51,7 @@ func Interfaces() ([]net.Interface, error) {
// The returned list does not identify the associated interface; use
// Interfaces and Interface.Addrs for more detail.
func InterfaceAddrs() ([]net.Addr, error) {
if androidVersion < android11 {
if androidApiLevel() < android11ApiLevel {
return net.InterfaceAddrs()
}

Expand All @@ -66,7 +68,7 @@ func InterfaceAddrs() ([]net.Addr, error) {
// sharing the logical data link; for more precision use
// InterfaceByName.
func InterfaceByIndex(index int) (*net.Interface, error) {
if androidVersion < android11 {
if androidApiLevel() < android11ApiLevel {
return net.InterfaceByIndex(index)
}

Expand Down Expand Up @@ -112,7 +114,7 @@ func InterfaceAddrsByInterface(ifi *net.Interface) ([]net.Addr, error) {
return nil, &net.OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterface}
}

if androidVersion < android11 {
if androidApiLevel() < android11ApiLevel {
return ifi.Addrs()
}

Expand All @@ -123,13 +125,6 @@ func InterfaceAddrsByInterface(ifi *net.Interface) ([]net.Addr, error) {
return ifat, err
}

// SetAndroidVersion set the Android environment in which the program runs.
// The Android system version number can be obtained through
// `android.os.Build.VERSION.RELEASE` of the Android framework.
func SetAndroidVersion(version uint) {
androidVersion = version
}

// An ipv6ZoneCache represents a cache holding partial network
// interface information. It is used for reducing the cost of IPv6
// addressing scope zone resolution.
Expand Down Expand Up @@ -422,3 +417,18 @@ func nameToFlags(name string) (net.Flags, error) {

return linkFlags(*(*uint32)(unsafe.Pointer(&ifr[syscall.IFNAMSIZ]))), nil
}

// Returns the API level of the device we're actually running on, or -1 on failure.
// The returned value is equivalent to the Java Build.VERSION.SDK_INT API.
var androidApiLevel = func() func() int {
var apiLevel int
var once sync.Once

return func() int {
once.Do(func() {
apiLevel = int(C.android_get_device_api_level())
})

return apiLevel
}
}()

0 comments on commit e684438

Please sign in to comment.