-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemulator.go
291 lines (246 loc) · 6.45 KB
/
emulator.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Package emulator provides common functionality to manage Android emulators.
package emulator
import (
"bytes"
"fmt"
"os/exec"
"slices"
"strconv"
"strings"
)
// PrintInvocations controls whether to print invocations of subprocesses.
var PrintInvocations bool
// Serial is the serial port of the running AVD, for example "emulator-5554".
var Serial string
// AVD represents an Android Virtual Device.
//
// It assumes that no 2 instances of the same AVD run at the same time.
type AVD struct {
Name string
Running bool
// PID of the emulator process. Equals 0 if Running is false.
Pid int
}
func (a AVD) Describe() string {
suffix := ""
if a.Running {
suffix = " RUNNING"
}
return fmt.Sprintf("%s%s", a.Name, suffix)
}
// List returns a list of available AVDs and whether they're running or not.
func List() ([]AVD, error) {
cmd := exec.Command("emulator", "-list-avds")
data, err := cmd.Output()
if err != nil {
return nil, err
}
avdsStr := strings.Split(string(data), "\n")
// remove empty strings
for i := len(avdsStr) - 1; i >= 0; i-- {
if avdsStr[i] == "" {
avdsStr = append(avdsStr[:i], avdsStr[i+1:]...)
}
}
// Workaround for a bug in emulator v34
for i, avd := range avdsStr {
if strings.Contains(avd, "Storing crashdata") {
avdsStr = slices.Delete(avdsStr, i, i+1)
}
}
// map avds to AVD struct
avds := make([]AVD, len(avdsStr))
for i, avd := range avdsStr {
avds[i] = AVD{Name: avd}
}
cmd = exec.Command(
"ps",
"-e",
"-ww", // don't truncate output
"-o", "pid=,comm=",
)
data, err = cmd.Output()
if err != nil {
return nil, fmt.Errorf("failed to get processes: %v", err)
}
// parse output of ps
lines := strings.Split(string(data), "\n")
for _, line := range lines {
if !strings.Contains(line, "qemu-system") {
continue
}
// remove leading and trailing spaces
line = strings.Trim(line, " ")
fields := strings.Split(line, " ")
pid, err := strconv.Atoi(fields[0])
if err != nil {
return nil, fmt.Errorf("failed to parse pid: %v", err)
}
avdName := emuInPID(pid)
for i, avd := range avds {
if avd.Name == avdName {
avds[i].Running = true
avds[i].Pid = pid
}
}
}
return avds, nil
}
// Start starts the AVD with the given name.
func Start(name string) error {
avds, err := List()
if err != nil {
return fmt.Errorf("list avds: %v", err)
}
for _, avd := range avds {
if avd.Name == name {
if avd.Running {
return fmt.Errorf("avd %s is already running", name)
}
args := []string{fmt.Sprintf("@%s", name), "-no-boot-anim", "-no-audio"}
cmd := exec.Command("emulator", args...)
printInvocation(cmd)
err = cmd.Start()
if err != nil {
return fmt.Errorf("start avd %s: %v", name, err)
}
return nil
}
}
return fmt.Errorf("avd %s not found", name)
}
func EnableDarkTheme() error {
return adbShell("cmd", "uimode", "night", "yes")
}
func DisableDarkTheme() error {
return adbShell("cmd", "uimode", "night", "no")
}
func ToggleDarkTheme() error {
cmd := exec.Command("adb", "shell", "cmd", "uimode", "night")
printInvocation(cmd)
out, err := cmd.Output()
if err != nil {
return fmt.Errorf("failed to run and read stdout: %v", err)
}
output := string(out)
targetMode := "yes"
if output == "Night mode: yes\n" {
targetMode = "no"
}
return adbShell("cmd", "uimode", "night", targetMode)
}
func SetFontSize(value string) error {
return adbShell("settings", "put", "system", "font_scale", value)
}
func SetDisplaySize(value float32) error {
density, err := getDensity()
if err != nil {
return fmt.Errorf("failed to get density: %v", err)
}
return adbShell("wm", "density", fmt.Sprintf("%d", int(float32(density)*value)))
}
func DisableAnimations() error {
var err error
err = adbShell("settings", "put", "global", "window_animation_scale", "0")
if err != nil {
return err
}
err = adbShell("settings", "put", "global", "transition_animation_scale", "0")
if err != nil {
return err
}
err = adbShell("settings", "put", "global", "animator_duration_scale", "0")
return err
}
func EnableAnimations() error {
var err error
err = adbShell("settings", "put", "global", "window_animation_scale", "1")
if err != nil {
return err
}
err = adbShell("settings", "put", "global", "transition_animation_scale", "1")
if err != nil {
return err
}
err = adbShell("settings", "put", "global", "animator_duration_scale", "1")
return err
}
func ToggleAnimations() error {
// the 3 values are always in sync, so I think it's enough to get just a single one
cmd := exec.Command("adb", "shell", "settings", "get", "global", "window_animation_scale")
printInvocation(cmd)
out, err := cmd.Output()
if err != nil {
return fmt.Errorf("failed to run and read stdout: %v", err)
}
output := string(out)
// basic validation
if output != "0\n" && output != "1\n" {
return fmt.Errorf("unexpected output: %s", output)
}
targetScale := "1"
if output == "1\n" {
targetScale = "0"
}
err = adbShell("settings", "put", "global", "window_animation_scale", targetScale)
if err != nil {
return err
}
err = adbShell("settings", "put", "global", "transition_animation_scale", targetScale)
if err != nil {
return err
}
err = adbShell("settings", "put", "global", "animator_duration_scale", targetScale)
return err
}
func getDensity() (int, error) {
cmd := exec.Command("adb", "shell", "wm", "density")
printInvocation(cmd)
out, err := cmd.Output()
if err != nil {
return 0, fmt.Errorf("failed to run: %v", err)
}
output := string(out)
var density int
_, err = fmt.Sscanf(output, "Physical density: %d", &density)
if err != nil {
return 0, fmt.Errorf("failed to parse density: %v", err)
}
return density, nil
}
// emuInPID returns the name of the AVD that is running in process.
//
// Returns an empty string if the process isn't an emulator process.
func emuInPID(pid int) string {
cmd := exec.Command(
"ps",
"-e",
"-ww", // don't truncate output
"-o", "args=",
"-p", strconv.Itoa(pid),
)
data, err := cmd.Output()
if err != nil {
return ""
}
args := strings.Split(string(data), " ")
for _, arg := range args {
if strings.HasPrefix(arg, "@") {
return strings.TrimPrefix(arg, "@")
}
}
return ""
}
func adbShell(cmd ...string) error {
args := []string{"shell"}
args = append(args, cmd...)
var stderr bytes.Buffer
adbCmd := exec.Command("adb", args...)
printInvocation(adbCmd)
adbCmd.Stderr = &stderr
err := adbCmd.Run()
if err != nil {
return fmt.Errorf("failed to run %s: %v, %v", cmd, err, stderr.String())
}
return nil
}