-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
290 lines (252 loc) · 7.54 KB
/
main.go
File metadata and controls
290 lines (252 loc) · 7.54 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
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
// Set and read clipboard text via the Android clipboard binder service.
//
// Demonstrates actual clipboard copy-paste through the IClipboard binder
// interface: sets a plain-text clip using the generated ClipData struct,
// reads it back with ClipData.UnmarshalParcel, and verifies the round-trip.
//
// Build:
//
// GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o build/clipboard_monitor ./examples/clipboard_monitor/
// adb push build/clipboard_monitor /data/local/tmp/ && adb shell /data/local/tmp/clipboard_monitor
package main
import (
"context"
"flag"
"fmt"
"os"
"strings"
"github.com/AndroidGoLab/binder/android/content"
"github.com/AndroidGoLab/binder/binder"
"github.com/AndroidGoLab/binder/binder/versionaware"
"github.com/AndroidGoLab/binder/kernelbinder"
"github.com/AndroidGoLab/binder/parcel"
"github.com/AndroidGoLab/binder/servicemanager"
)
// defaultDeviceID is the primary display device.
const defaultDeviceID int32 = 0
func main() {
text := flag.String("text", "Hello from Go binder!", "text to place on the clipboard")
flag.Parse()
if err := run(context.Background(), *text); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func run(
ctx context.Context,
text string,
) error {
driver, err := kernelbinder.Open(ctx, binder.WithMapSize(128*1024))
if err != nil {
return fmt.Errorf("open binder: %w", err)
}
defer driver.Close(ctx)
transport, err := versionaware.NewTransport(ctx, driver, 0)
if err != nil {
return fmt.Errorf("version-aware transport: %w", err)
}
sm := servicemanager.New(transport)
svc, err := sm.GetService(ctx, servicemanager.ClipboardService)
if err != nil {
return fmt.Errorf("get clipboard service: %w", err)
}
cb := content.NewClipboardProxy(svc)
// Set clipboard text.
fmt.Printf("Setting clipboard text: %q\n", text)
if err := setPrimaryClipText(ctx, svc, "go-binder", text); err != nil {
return fmt.Errorf("set clipboard: %w", err)
}
fmt.Println("Clipboard text set successfully.")
// Verify the clipboard has content.
hasPrimary, err := cb.HasPrimaryClip(ctx, defaultDeviceID)
if err != nil {
return fmt.Errorf("HasPrimaryClip: %w", err)
}
fmt.Printf("Has primary clip: %v\n", hasPrimary)
hasText, err := cb.HasClipboardText(ctx, defaultDeviceID)
if err != nil {
return fmt.Errorf("HasClipboardText: %w", err)
}
fmt.Printf("Has clipboard text: %v\n", hasText)
// Read the clipboard text back.
clipText, err := getPrimaryClipText(ctx, svc)
if err != nil {
return fmt.Errorf("get clipboard: %w", err)
}
fmt.Printf("Read back %d item(s):\n", len(clipText.Items))
label := ""
if clipText.ClipDescription.Label != nil {
label = *clipText.ClipDescription.Label
}
fmt.Printf(" Label: %q\n", label)
fmt.Printf(" MIME types: %s\n", strings.Join(clipText.ClipDescription.MimeTypes, ", "))
for i, item := range clipText.Items {
itemText := ""
if item.Text != nil {
itemText = *item.Text
}
fmt.Printf(" Item[%d]: %q\n", i, itemText)
}
// Verify round-trip.
if len(clipText.Items) > 0 && clipText.Items[0].Text != nil && *clipText.Items[0].Text == text {
fmt.Println("Round-trip verified: clipboard text matches.")
} else {
fmt.Println("WARNING: clipboard text does not match what was set.")
}
return nil
}
// setPrimaryClipText places a plain-text clip on the clipboard by building
// the SetPrimaryClip transaction parcel manually. This bypasses the generated
// ClipData.MarshalParcel (which writes an incomplete ClipDescription) and
// instead uses MarshalPlainTextClipData for the correct wire format.
func setPrimaryClipText(
ctx context.Context,
remote binder.IBinder,
label string,
text string,
) error {
identity := remote.Identity()
data := parcel.New()
defer data.Recycle()
data.WriteInterfaceToken(content.DescriptorIClipboard)
// Resolve the method signature to handle version-aware parameter ordering.
sig := binder.ResolveMethodSignature(
remote, ctx,
content.DescriptorIClipboard,
content.MethodIClipboardSetPrimaryClip,
)
compiledDescs := []string{
"Landroid/content/ClipData;",
"Ljava/lang/String;",
"Ljava/lang/String;",
"I",
"I",
}
clipData := &content.ClipData{
ClipDescription: content.ClipDescription{
Label: &label,
MimeTypes: []string{"text/plain"},
},
Items: []content.ClipDataItem{
{Text: &text},
},
}
if sig == nil || binder.SignatureMatches(compiledDescs, sig) {
data.WriteInt32(1) // non-null ClipData
if err := clipData.MarshalParcel(data); err != nil {
return fmt.Errorf("marshaling ClipData: %w", err)
}
data.WriteString16(identity.PackageName)
data.WriteString16(identity.AttributionTag)
data.WriteInt32(identity.UserID)
data.WriteInt32(defaultDeviceID)
} else {
paramMap := binder.MatchParamsToSignature(compiledDescs, sig)
for _, pi := range paramMap {
switch pi {
case 0:
data.WriteInt32(1) // non-null ClipData
if err := clipData.MarshalParcel(data); err != nil {
return fmt.Errorf("marshaling ClipData: %w", err)
}
case 1:
data.WriteString16(identity.PackageName)
case 2:
data.WriteString16(identity.AttributionTag)
case 3:
data.WriteInt32(identity.UserID)
case 4:
data.WriteInt32(defaultDeviceID)
}
}
}
code, err := remote.ResolveCode(
ctx,
content.DescriptorIClipboard,
content.MethodIClipboardSetPrimaryClip,
)
if err != nil {
return fmt.Errorf("resolving %s.%s: %w",
content.DescriptorIClipboard,
content.MethodIClipboardSetPrimaryClip, err)
}
reply, err := remote.Transact(ctx, code, 0, data)
if err != nil {
return err
}
defer reply.Recycle()
return binder.ReadStatus(reply)
}
// getPrimaryClipText reads the primary clip from the clipboard and extracts
// text content using the generated ClipData.UnmarshalParcel.
func getPrimaryClipText(
ctx context.Context,
remote binder.IBinder,
) (content.ClipData, error) {
var empty content.ClipData
identity := remote.Identity()
data := parcel.New()
defer data.Recycle()
data.WriteInterfaceToken(content.DescriptorIClipboard)
sig := binder.ResolveMethodSignature(
remote, ctx,
content.DescriptorIClipboard,
content.MethodIClipboardGetPrimaryClip,
)
compiledDescs := []string{
"Ljava/lang/String;",
"Ljava/lang/String;",
"I",
"I",
}
if sig == nil || binder.SignatureMatches(compiledDescs, sig) {
data.WriteString16(identity.PackageName)
data.WriteString16(identity.AttributionTag)
data.WriteInt32(identity.UserID)
data.WriteInt32(defaultDeviceID)
} else {
paramMap := binder.MatchParamsToSignature(compiledDescs, sig)
for _, pi := range paramMap {
switch pi {
case 0:
data.WriteString16(identity.PackageName)
case 1:
data.WriteString16(identity.AttributionTag)
case 2:
data.WriteInt32(identity.UserID)
case 3:
data.WriteInt32(defaultDeviceID)
}
}
}
code, err := remote.ResolveCode(
ctx,
content.DescriptorIClipboard,
content.MethodIClipboardGetPrimaryClip,
)
if err != nil {
return empty, fmt.Errorf("resolving %s.%s: %w",
content.DescriptorIClipboard,
content.MethodIClipboardGetPrimaryClip, err)
}
reply, err := remote.Transact(ctx, code, 0, data)
if err != nil {
return empty, err
}
defer reply.Recycle()
if err = binder.ReadStatus(reply); err != nil {
return empty, err
}
nullIndicator, err := reply.ReadInt32()
if err != nil {
return empty, fmt.Errorf("reading null indicator: %w", err)
}
if nullIndicator == 0 {
return empty, fmt.Errorf("clipboard returned null ClipData")
}
var clipData content.ClipData
if err := clipData.UnmarshalParcel(reply); err != nil {
return empty, fmt.Errorf("unmarshaling ClipData: %w", err)
}
return clipData, nil
}