Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ mobilecli io button --device <device-id> POWER

# Send text
mobilecli io text --device <device-id> 'hello world'

# Read device clipboard
mobilecli io clipboard get --device <device-id>

# Write device clipboard
mobilecli io clipboard set --device <device-id> 'hello world'
```

### Supported Hardware Buttons
Expand Down
57 changes: 57 additions & 0 deletions cli/clipboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cli

import (
"fmt"

"github.com/mobile-next/mobilecli/commands"
"github.com/spf13/cobra"
)

var clipboardCmd = &cobra.Command{
Use: "clipboard",
Short: "Clipboard commands",
Long: `Commands for reading and writing the device clipboard.`,
}

var clipboardGetCmd = &cobra.Command{
Use: "get",
Short: "Read the clipboard of a device",
RunE: func(cmd *cobra.Command, args []string) error {
response := commands.ClipboardGetCommand(commands.ClipboardGetRequest{
DeviceID: deviceId,
})

printJson(response)
if response.Status == "error" {
return fmt.Errorf("%s", response.Error)
}
return nil
},
}

var clipboardSetCmd = &cobra.Command{
Use: "set [text]",
Short: "Replace the clipboard of a device",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
response := commands.ClipboardSetCommand(commands.ClipboardSetRequest{
DeviceID: deviceId,
Text: args[0],
})

printJson(response)
if response.Status == "error" {
return fmt.Errorf("%s", response.Error)
}
return nil
},
}

func init() {
ioCmd.AddCommand(clipboardCmd)
clipboardCmd.AddCommand(clipboardGetCmd)
clipboardCmd.AddCommand(clipboardSetCmd)

clipboardGetCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to read the clipboard from")
clipboardSetCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to write the clipboard on")
}
2 changes: 1 addition & 1 deletion cli/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
var ioCmd = &cobra.Command{
Use: "io",
Short: "Input/output operations with devices",
Long: `Perform input/output operations like tapping, pressing buttons, and sending text to devices.`,
Long: `Perform input/output operations like tapping, pressing buttons, sending text, and reading or writing the device clipboard.`,
}

var ioTapCmd = &cobra.Command{
Expand Down
6 changes: 6 additions & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ INPUT/OUTPUT:
# Send text input
mobilecli io text --device <device-id> "Hello World"

# Read device clipboard
mobilecli io clipboard get --device <device-id>

# Write device clipboard
mobilecli io clipboard set --device <device-id> "Hello World"

WEBVIEW:
# List embedded webviews in the foreground app
mobilecli webview list --device <device-id>
Expand Down
64 changes: 64 additions & 0 deletions commands/clipboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package commands

import (
"fmt"

"github.com/mobile-next/mobilecli/devices"
)

type ClipboardGetRequest struct {
DeviceID string `json:"deviceId"`
}

type ClipboardSetRequest struct {
DeviceID string `json:"deviceId"`
Text string `json:"text"`
}

type ClipboardResult struct {
Text string `json:"text"`
}

func ClipboardGetCommand(req ClipboardGetRequest) *CommandResponse {
targetDevice, err := FindDeviceOrAutoSelect(req.DeviceID)
if err != nil {
return NewErrorResponse(fmt.Errorf("error finding device: %v", err))
}

err = targetDevice.StartAgent(devices.StartAgentConfig{
Hook: GetShutdownHook(),
})
if err != nil {
return NewErrorResponse(fmt.Errorf("failed to start agent on device %s: %v", targetDevice.ID(), err))
}

text, err := targetDevice.GetClipboard()
if err != nil {
return NewErrorResponse(fmt.Errorf("failed to read clipboard on device %s: %v", targetDevice.ID(), err))
}

return NewSuccessResponse(ClipboardResult{Text: text})
}

func ClipboardSetCommand(req ClipboardSetRequest) *CommandResponse {
targetDevice, err := FindDeviceOrAutoSelect(req.DeviceID)
if err != nil {
return NewErrorResponse(fmt.Errorf("error finding device: %v", err))
}

err = targetDevice.StartAgent(devices.StartAgentConfig{
Hook: GetShutdownHook(),
})
if err != nil {
return NewErrorResponse(fmt.Errorf("failed to start agent on device %s: %v", targetDevice.ID(), err))
}

err = targetDevice.SetClipboard(req.Text)
if err != nil {
return NewErrorResponse(fmt.Errorf("failed to write clipboard on device %s: %v", targetDevice.ID(), err))
}

return NewSuccessResponse(MessageResult{
Message: fmt.Sprintf("Clipboard set on device %s", targetDevice.ID()),
})
}
37 changes: 37 additions & 0 deletions devices/android.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,43 @@ func (d *AndroidDevice) Swipe(x1, y1, x2, y2 int) error {
return nil
}

const devicekitClipboardClass = "com.mobilenext.devicekit.Clipboard"

func (d *AndroidDevice) clipboardCommand(args ...string) (string, error) {
appPath, err := d.GetAppPath("com.mobilenext.devicekit")
if err != nil || appPath == "" {
return "", fmt.Errorf("DeviceKit is not installed on device %s, it is required for clipboard access", d.ID())
}

cmdArgs := append([]string{"exec-out", fmt.Sprintf("CLASSPATH=%s", appPath), "app_process", "/system/bin", devicekitClipboardClass}, args...)
out, err := d.runAdbCommand(cmdArgs...)
if err != nil {
return "", err
}

return string(out), nil
}

func (d *AndroidDevice) GetClipboard() (string, error) {
out, err := d.clipboardCommand("get")
if err != nil {
return "", err
}

return strings.TrimSuffix(out, "\n"), nil
}

func (d *AndroidDevice) SetClipboard(text string) error {
if text == "" {
_, err := d.clipboardCommand("clear")
return err
}

// base64 keeps spaces, emoji and other UTF-8 intact across the shell.
_, err := d.clipboardCommand("set", "--base64", base64.StdEncoding.EncodeToString([]byte(text)))
return err
}

// Gesture performs a sequence of touch actions on the Android device
func (d *AndroidDevice) Gesture(actions []devicekit.TapAction) error {

Expand Down
2 changes: 2 additions & 0 deletions devices/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ type ControllableDevice interface {
Gesture(actions []devicekit.TapAction) error
StartAgent(config StartAgentConfig) error
SendKeys(text string) error
GetClipboard() (string, error)
SetClipboard(text string) error
PressKeys(combos []KeyCombo) error
PressButton(key string) error
LaunchApp(bundleID string, opts LaunchOptions) error
Expand Down
33 changes: 33 additions & 0 deletions devices/devicekit/clipboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package devicekit

import (
"encoding/json"
"fmt"
)

type clipboardText struct {
Text string `json:"text"`
}

func (c *DeviceKitClient) GetClipboard() (string, error) {
result, err := c.CallRPC("device.clipboard.get", nil)
if err != nil {
return "", fmt.Errorf("failed to get clipboard: %w", err)
}

var clipboard clipboardText
if err := json.Unmarshal(result, &clipboard); err != nil {
return "", fmt.Errorf("failed to parse clipboard: %w", err)
}

return clipboard.Text, nil
}

func (c *DeviceKitClient) SetClipboard(text string) error {
_, err := c.CallRPC("device.clipboard.set", map[string]any{"text": text})
if err != nil {
return fmt.Errorf("failed to set clipboard: %w", err)
}

return nil
}
8 changes: 8 additions & 0 deletions devices/ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ func (d IOSDevice) Swipe(x1, y1, x2, y2 int) error {
return d.deviceKitClient.Swipe(x1, y1, x2, y2)
}

func (d IOSDevice) GetClipboard() (string, error) {
return d.deviceKitClient.GetClipboard()
}

func (d IOSDevice) SetClipboard(text string) error {
return d.deviceKitClient.SetClipboard(text)
}

func (d IOSDevice) Gesture(actions []devicekit.TapAction) error {
return d.deviceKitClient.Gesture(actions)
}
Expand Down
15 changes: 15 additions & 0 deletions devices/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ func (r *RemoteDevice) Swipe(x1, y1, x2, y2 int) error {
return r.fireRPC("device.io.swipe", params{"x1": x1, "y1": y1, "x2": x2, "y2": y2})
}

func (r *RemoteDevice) GetClipboard() (string, error) {
resp, err := rpcCall[struct {
Text string `json:"text"`
}](r, "device.clipboard.get", params{})
if err != nil {
return "", err
}

return resp.Text, nil
}

func (r *RemoteDevice) SetClipboard(text string) error {
return r.fireRPC("device.clipboard.set", params{"text": text})
}

func (r *RemoteDevice) Gesture(actions []devicekit.TapAction) error {
return r.fireRPC("device.io.gesture", params{"actions": actions})
}
Expand Down
8 changes: 8 additions & 0 deletions devices/simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,14 @@ func (s SimulatorDevice) Swipe(x1, y1, x2, y2 int) error {
return s.deviceKitClient.Swipe(x1, y1, x2, y2)
}

func (s SimulatorDevice) GetClipboard() (string, error) {
return s.deviceKitClient.GetClipboard()
}

func (s SimulatorDevice) SetClipboard(text string) error {
return s.deviceKitClient.SetClipboard(text)
}

func (s SimulatorDevice) Gesture(actions []devicekit.TapAction) error {
return s.deviceKitClient.Gesture(actions)
}
Expand Down
58 changes: 58 additions & 0 deletions docs/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,64 @@
}
}
},
{
"name": "device.clipboard.get",
"summary": "Read clipboard",
"description": "Reads the text currently on the device clipboard",
"params": [
{
"name": "deviceId",
"description": "ID of the target device",
"required": true,
"schema": {
"type": "string"
}
}
],
"result": {
"name": "clipboard",
"description": "Clipboard contents",
"schema": {
"type": "object",
"properties": {
"text": {
"type": "string",
"description": "Text on the clipboard, empty when it holds no text"
}
}
}
}
},
{
"name": "device.clipboard.set",
"summary": "Write clipboard",
"description": "Replaces the text on the device clipboard, an empty string clears it",
"params": [
{
"name": "deviceId",
"description": "ID of the target device",
"required": true,
"schema": {
"type": "string"
}
},
{
"name": "text",
"description": "Text to place on the clipboard",
"required": true,
"schema": {
"type": "string"
}
}
],
"result": {
"name": "success",
"description": "Operation result",
"schema": {
"$ref": "#/components/schemas/SuccessResult"
}
}
},
{
"name": "device.io.keys",
"summary": "Press keyboard keys",
Expand Down
Loading
Loading