Documentation
¶
Overview ¶
Package android implements the application half of the go-widgets Android host protocol, so a go-widgets application runs inside a real Android app exactly as it runs on X11, Wayland, Cocoa or Win32.
Android hands out no drawable surface to a process that is not the app: the whole graphics API is behind JNI, and JNI needs cgo. So the app is split in two. A thin Java host owns the Activity, the SurfaceView and the input stream; the go-widgets application is an ordinary CGO-free executable the host spawns, which paints into a shared mapping and tells the host which rectangle changed. The split is the same one the Linux back-ends already live with — a socket protocol plus a shared pixel buffer — with the Java host standing where the X server or the Wayland compositor stands.
This file is the SOVEREIGN, transport-agnostic codec: the wire messages, the framing, and the input→toolkit.Event mapping, over plain Go values. It carries no syscall and no net dependency, so it builds — and is unit-tested to 100% — on every GOOS. The transport that dials the host socket, maps the buffer and drives a widget tree lives in client.go.
Index ¶
- Constants
- Variables
- func DecodeReady(b []byte) (w, h int, err error)
- func EncodeConfig(c Config) []byte
- func EncodeFrame(r Rect) []byte
- func EncodeKey(k Key) []byte
- func EncodeReady(w, h int) []byte
- func EncodeTouch(t Touch) []byte
- func MapKey(k Key) []toolkit.Event
- func MapTouch(t Touch, held bool) []toolkit.Event
- func ReadMessage(r io.Reader) (typ uint8, body []byte, err error)
- func WriteMessage(w io.Writer, typ uint8, body []byte) error
- type Client
- type Config
- type Key
- type Rect
- type Touch
Constants ¶
const ( // MsgConfig carries the surface geometry and the shared buffer path. The // host sends it once at start-up and again on every resize or rotation. MsgConfig uint8 = 0x01 // MsgTouch carries one pointer sample in surface pixels. MsgTouch uint8 = 0x02 // MsgKey carries one key event: an Android key code plus the unicode rune // the host's key-character map produced (0 when the key produces none). MsgKey uint8 = 0x03 // MsgLifecycle carries an Activity transition: the app keeps its widget // tree across a pause, but stops painting until it resumes. MsgLifecycle uint8 = 0x04 // MsgClose asks the application to end its Run loop. MsgClose uint8 = 0x05 // MsgReady tells the host the shared buffer is mapped at the announced // size, so the host may map it in turn. Every MsgFrame that follows // refers to this mapping, until the next MsgReady replaces it. MsgReady uint8 = 0x81 // MsgFrame tells the host which surface-local rectangle changed. MsgFrame uint8 = 0x82 // MsgTitle updates the host's window title. MsgTitle uint8 = 0x83 // MsgBye tells the host the application ended. MsgBye uint8 = 0x84 )
Message types. Host→app messages are below 0x80, app→host at or above it, so a misrouted message is a decode error rather than a plausible other message.
const ( TouchDown uint8 = 0 TouchUp uint8 = 1 TouchMove uint8 = 2 )
Touch actions, matching the three MotionEvent actions the host forwards.
const ( KeyDown uint8 = 0 KeyUp uint8 = 1 )
Key actions.
const ( LifecyclePause uint8 = 0 LifecycleResume uint8 = 1 )
Lifecycle states.
const MaxPayload = 1 << 16
MaxPayload bounds one decoded message body. The largest message a host legitimately sends is a Config carrying a filesystem path, so a frame beyond this is a desynchronised stream — refused rather than allocated.
Variables ¶
var ErrShortPayload = errors.New("android: truncated message payload")
ErrShortPayload reports a message whose body is too short for its type.
var ErrUnsupported = errors.New("android: no Android host on this platform")
ErrUnsupported reports an environment with no Android host: every GOOS but Linux, where the abstract socket and the shared mapping the host protocol needs do not exist. A cross-built application gets this from Dial and can report it and exit cleanly, exactly as go-widgets/window does off its supported back-ends.
Functions ¶
func DecodeReady ¶
DecodeReady parses a MsgReady body.
func EncodeFrame ¶
EncodeFrame builds a MsgFrame body naming the damaged rectangle.
func EncodeReady ¶
EncodeReady builds a MsgReady body: the size the application actually mapped.
func MapKey ¶
MapKey maps one Android key event to toolkit events, mirroring the wasmbox and X11 mappings: a named key is one EventKeyDown/EventKeyUp; a key that committed a character is an EventKeyDown followed by an EventChar on press, and an EventKeyUp on release. A key that is neither named nor printable reaches the tree as nothing.
func MapTouch ¶
MapTouch maps one pointer sample to toolkit events. It mirrors the wasmbox and X11 mappings: a press is a click, a move with the finger down is a drag. A touch screen has no hover, so a move with no finger down cannot occur and is mapped to a plain move rather than dropped, keeping a synthetic host (a test, a replay) honest.
func ReadMessage ¶
ReadMessage reads one framed message. It returns io.EOF when the stream ends cleanly between messages, so a caller can tell a closed host from a truncated one.
Types ¶
type Client ¶
type Client struct{}
Client is the unavailable-here shape of the Android host surface. The host protocol needs a Linux abstract socket and a shared mapping, so there is nothing to connect to anywhere else — but the type and its method set exist on every GOOS so an application using this back-end still cross-builds and still vets, exactly as go-widgets/window's open_other.go keeps the native back-ends' entry point compiling everywhere.
type Config ¶
type Config struct {
// W and H are the surface size in physical pixels.
W, H int
// Density is the display density in hundredths (Android's
// DisplayMetrics.density × 100, so a 3.0x panel arrives as 300). It is the
// Android spelling of the backing-scale factor the Cocoa back-end reads
// from the screen.
Density int
// BufPath is the file the application maps as its framebuffer. The host
// picks it inside the app's own storage, which both processes share.
BufPath string
}
Config is the host's geometry announcement.
func DecodeConfig ¶
DecodeConfig parses a MsgConfig body.
type Key ¶
type Key struct {
Action uint8
// Code is the Android KeyEvent key code.
Code int
// Rune is the character the key produced, or 0 for a key that produces
// none (an arrow, a modifier, the back key).
Rune rune
}
Key is one key event.
type Rect ¶
type Rect struct{ X, Y, W, H int }
Rect is a surface-local rectangle in pixels. It mirrors toolkit.Rect but is kept local so the codec stays a leaf with one toolkit dependency (the event model).
