-
Notifications
You must be signed in to change notification settings - Fork 12
/
other.go
74 lines (67 loc) · 1.8 KB
/
other.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package androidutils
import (
"errors"
"io/ioutil"
"regexp"
"strconv"
"strings"
)
func HWAddrWLAN() (string, error) {
macaddr, err := getHWAddrWLAN()
return strings.ToLower(macaddr), err
}
// WLAN Hardware Address, also known as wlan0 mac address
// Thanks to this article https://android.stackexchange.com/questions/142606/how-can-i-find-my-mac-address/142630#142630
func getHWAddrWLAN() (string, error) {
// method 1
if macaddr := CachedProperty("ro.boot.wifimacaddr"); macaddr != "" {
return macaddr, nil
}
// method 2
data, err := ioutil.ReadFile("/sys/class/net/wlan0/address")
if err == nil {
return strings.TrimSpace(string(data)), nil
}
// method 3
output, err := runShell("ip", "address", "show", "wlan0")
if err != nil {
return "", err
}
matches := regexp.MustCompile(`link/ether ([\w\d:]{17})`).FindStringSubmatch(output)
if matches == nil {
return "", errors.New("no mac address founded")
}
return matches[1], nil
}
type Display struct {
Width int `json:"width"`
Height int `json:"height"`
}
// WindowSize parse command "wm size" output
// command output example:
// Physical size: 1440x2560
// Override size: 1080x1920
func WindowSize() (display Display, err error) {
output, err := runShell("wm", "size")
if err != nil {
return
}
w, h, err := parseWmSize(output)
return Display{w, h}, err
}
var wmSizePattern = regexp.MustCompile(`(\w+)\s+size:\s+(\d+)x(\d+)`)
func parseWmSize(output string) (width, height int, err error) {
ms := wmSizePattern.FindAllStringSubmatch(output, -1)
if len(ms) == 0 {
err = errors.New("wm size return unrecognize output: " + output)
return
}
if len(ms) == 2 {
width, _ = strconv.Atoi(ms[1][2])
height, _ = strconv.Atoi(ms[1][3])
return width, height, nil
}
width, _ = strconv.Atoi(ms[0][2])
height, _ = strconv.Atoi(ms[0][3])
return
}