Skip to content

Commit

Permalink
Detect Android API level if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
bobrofon committed Oct 2, 2024
1 parent 59b8142 commit 4173812
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
7 changes: 7 additions & 0 deletions android_api_level.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !(android && cgo)

package anet

func androidDeviceApiLevel() int {
return -1
}
23 changes: 23 additions & 0 deletions android_api_level_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build android && cgo

package anet

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

import "sync"

var (
apiLevel int
once sync.Once
)

// 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.
func androidDeviceApiLevel() int {
once.Do(func() {
apiLevel = int(C.android_get_device_api_level())
})

return apiLevel
}
32 changes: 25 additions & 7 deletions interface_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
)

const (
android11 = 11
android11ApiLevel = 30
)

var (
androidVersion uint
customAndroidApiLevel = -1
errInvalidInterface = errors.New("invalid network interface")
errInvalidInterfaceIndex = errors.New("invalid network interface index")
errInvalidInterfaceName = errors.New("invalid network interface name")
Expand All @@ -28,7 +28,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 +49,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 +66,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 +112,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 @@ -126,8 +126,26 @@ func InterfaceAddrsByInterface(ifi *net.Interface) ([]net.Addr, error) {
// 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.
// If version is 0 the actual version will be detected automatically if possible.
func SetAndroidVersion(version uint) {
androidVersion = version
switch {
case version == 0:
customAndroidApiLevel = -1
case version >= 11:
customAndroidApiLevel = android11ApiLevel
default:
customAndroidApiLevel = 0
}
}

func androidApiLevel() int {
if customAndroidApiLevel != -1 {
// user-provided api level should be used
return customAndroidApiLevel
}

// try to autodetect api level
return androidDeviceApiLevel()
}

// An ipv6ZoneCache represents a cache holding partial network
Expand Down

0 comments on commit 4173812

Please sign in to comment.