Repository navigation
Expand file tree
/
Copy pathandroid.go
More file actions
122 lines (114 loc) · 4.06 KB
/
Copy pathandroid.go
File metadata and controls
122 lines (114 loc) · 4.06 KB
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
package daemon
import (
"fmt"
"net/http"
"os"
"github.com/coder/websocket"
"agentbox/internal/agent"
"agentbox/internal/android"
"agentbox/internal/api"
"agentbox/internal/devicestream"
"agentbox/internal/hostos"
"agentbox/internal/state"
)
// findAndroidSDK looks for the Android SDK every time, so installing one needs
// no restart. In AgentBox's Cloud Hypervisor VM, the SDK is the host's: the
// front end finds it with the host's settings and says where in
// AGENTBOX_ANDROID_SDK (hostvm's vmEnv), and the usual place is in the host's
// home, which the VM shares at the same path, not in the VM user's own.
// In WSL it is found as on a Linux host, in the distro: Android Studio's SDK
// on Windows has Windows' emulator, which can't run there, so the error says so.
func findAndroidSDK() (android.SDK, error) {
if share := hostos.Home(); hostos.OS() == hostos.Linux && share != "" {
return android.FindSharedSDK(android.Candidates(os.Getenv, share), share)
}
home, _ := os.UserHomeDir()
sdk, err := android.FindSDK(android.Candidates(os.Getenv, home))
if err != nil && hostos.OS() == hostos.Windows {
return sdk, fmt.Errorf("%w. In WSL that is the Linux SDK, installed in the distro (the command-line tools for Linux, in ~/Android/Sdk), not Android Studio's SDK on Windows", err)
}
return sdk, err
}
func toAPIAndroid(st agent.AndroidStatus) api.AndroidStatus {
return api.AndroidStatus{
Available: st.Available, Problem: st.Problem, SDK: st.SDK, Images: st.Images,
Running: st.Running, Booted: st.Booted, Image: st.Image, Device: st.Device,
GPU: st.GPU, GPUProblem: st.GPUProblem,
}
}
// android serves an agent's emulator endpoints. source is "agent" on the
// agent's own socket, which doesn't see host paths.
func (s *Server) android(action string, agentOf func(*http.Request) (state.Agent, error), source string) func(http.ResponseWriter, *http.Request) error {
return func(w http.ResponseWriter, r *http.Request) error {
a, err := agentOf(r)
if err != nil {
return err
}
ctx, m := r.Context(), s.manager(s.cfg.Log)
switch action {
case "start":
var req api.AndroidStartRequest
if err := readJSON(r, &req); err != nil {
return err
}
if _, err := m.StartAndroid(ctx, a, agent.AndroidOptions{Image: req.Image, MemoryMB: req.MemoryMB, Cores: req.Cores}); err != nil {
return err
}
case "stop":
if err := m.StopAndroid(ctx, a); err != nil {
return err
}
case "install":
var req api.AndroidInstallRequest
if err := readJSON(r, &req); err != nil {
return err
}
out, err := m.InstallAPK(ctx, a, req.Path)
if err != nil {
return err
}
return writeJSON(w, http.StatusOK, api.AndroidInstallResult{Output: out})
}
status, err := m.AndroidStatus(ctx, a)
if err != nil {
return err
}
out := toAPIAndroid(status)
if source == "agent" {
out.SDK = ""
}
return writeJSON(w, http.StatusOK, out)
}
}
// androidStream relays an agent's emulator's stream (devicestream: H.264
// video out, input in) between the app and the stream server in the agent,
// message for message. Like the VNC views, it is reached only through the
// daemon's socket, and the agent's server only through its proxy device.
func (s *Server) androidStream(w http.ResponseWriter, r *http.Request) error {
a, err := s.agentFromPath(r)
if err != nil {
return err
}
defer s.viewIdle(a.Ref())()
conn, err := s.manager(nil).DialAndroidStream(r.Context(), a)
if err != nil {
return err
}
device, err := devicestream.Dial(r.Context(), conn)
if err != nil {
return err
}
defer func() { _ = device.CloseNow() }()
// The socket is only reachable by this user, so any origin (the desktop app's) is fine.
viewer, err := websocket.Accept(w, r, &websocket.AcceptOptions{InsecureSkipVerify: true})
if err != nil {
return nil // Accept has already written the response
}
defer func() { _ = viewer.CloseNow() }()
viewer.SetReadLimit(1 << 20)
if err := devicestream.Relay(r.Context(), viewer, device); err != nil {
s.logf("%s's Android stream: %v", a.Ref(), err)
}
_ = viewer.Close(websocket.StatusNormalClosure, "")
return nil
}