-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
accessibility_control.go
268 lines (235 loc) · 8.85 KB
/
accessibility_control.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
package accessibility
import (
"fmt"
dtx "github.com/danielpaulus/go-ios/ios/dtx_codec"
"github.com/danielpaulus/go-ios/ios/nskeyedarchiver"
log "github.com/sirupsen/logrus"
)
//ControlInterface provides a simple interface to controlling the AX service on the device
//It only needs the global dtx channel as all AX methods are invoked on it.
type ControlInterface struct {
channel *dtx.Channel
}
func (a ControlInterface) readhostAppStateChanged() {
for {
msg := a.channel.ReceiveMethodCall("hostAppStateChanged:")
stateChange, err := nskeyedarchiver.Unarchive(msg.Auxiliary.GetArguments()[0].([]byte))
if err != nil {
panic(err)
}
value := stateChange[0]
log.Infof("hostAppStateChanged:%s", value)
}
}
func (a ControlInterface) readhostInspectorNotificationReceived() {
for {
msg := a.channel.ReceiveMethodCall("hostInspectorNotificationReceived:")
notification, err := nskeyedarchiver.Unarchive(msg.Auxiliary.GetArguments()[0].([]byte))
if err != nil {
panic(err)
}
value := notification[0].(map[string]interface{})["Value"]
log.Infof("hostInspectorNotificationReceived:%s", value)
}
}
//Init wires up event receivers and gets Info from the device
func (a ControlInterface) init() error {
a.channel.RegisterMethodForRemote("hostInspectorCurrentElementChanged:")
a.channel.RegisterMethodForRemote("hostInspectorMonitoredEventTypeChanged:")
a.channel.RegisterMethodForRemote("hostAppStateChanged:")
a.channel.RegisterMethodForRemote("hostInspectorNotificationReceived:")
go a.readhostAppStateChanged()
go a.readhostInspectorNotificationReceived()
err := a.notifyPublishedCapabilities()
if err != nil {
return err
}
//a list of methods we are allowed to call on the device
deviceCapabilities, err := a.deviceCapabilities()
if err != nil {
return err
}
log.Info("Device Capabilities:", deviceCapabilities)
apiVersion, err := a.deviceAPIVersion()
if err != nil {
return err
}
log.Info("Api version:", apiVersion)
auditCaseIds, err := a.deviceAllAuditCaseIDs()
if err != nil {
return err
}
log.Info("AuditCaseIDs", auditCaseIds)
deviceInspectorSupportedEventTypes, err := a.deviceInspectorSupportedEventTypes()
if err != nil {
return err
}
log.Info("deviceInspectorSupportedEventTypes:", deviceInspectorSupportedEventTypes)
canNav, err := a.deviceInspectorCanNavWhileMonitoringEvents()
if err != nil {
return err
}
log.Info("deviceInspectorCanNavWhileMonitoringEvents:", canNav)
err = a.deviceSetAppMonitoringEnabled(true)
if err != nil {
return err
}
for _, v := range auditCaseIds {
name, err := a.deviceHumanReadableDescriptionForAuditCaseID(v)
if err != nil {
return err
}
log.Infof("%s -- %s", v, name)
}
return nil
}
//EnableSelectionMode enables the UI element selection mode on the device,
//it is the same as clicking the little crosshair in AX Inspector
func (a ControlInterface) EnableSelectionMode() {
a.deviceInspectorSetMonitoredEventType(2)
a.deviceInspectorShowVisuals(true)
a.awaitHostInspectorMonitoredEventTypeChanged()
}
//SwitchToDevice is the same as switching to the Device in AX inspector.
//After running this, notifications and events should be received.
func (a ControlInterface) SwitchToDevice() {
a.TurnOff()
resp, _ := a.deviceAccessibilitySettings()
log.Info("AX Settings received:", resp)
a.deviceInspectorShowIgnoredElements(false)
a.deviceSetAuditTargetPid(0)
a.deviceInspectorFocusOnElement()
a.awaitHostInspectorCurrentElementChanged()
a.deviceInspectorPreviewOnElement()
a.deviceHighlightIssue()
}
//TurnOff disable AX
func (a ControlInterface) TurnOff() {
a.deviceInspectorSetMonitoredEventType(0)
a.awaitHostInspectorMonitoredEventTypeChanged()
a.deviceInspectorFocusOnElement()
a.awaitHostInspectorCurrentElementChanged()
a.deviceInspectorPreviewOnElement()
a.deviceHighlightIssue()
a.deviceInspectorShowVisuals(false)
}
//GetElement moves the green selection rectangle one element further
func (a ControlInterface) GetElement() {
log.Info("changing")
a.deviceInspectorMoveWithOptions()
//a.deviceInspectorMoveWithOptions()
resp := a.awaitHostInspectorCurrentElementChanged()
log.Info("item changed", resp)
}
func (a ControlInterface) awaitHostInspectorCurrentElementChanged() map[string]interface{} {
msg := a.channel.ReceiveMethodCall("hostInspectorCurrentElementChanged:")
log.Info("received hostInspectorCurrentElementChanged")
result, err := nskeyedarchiver.Unarchive(msg.Auxiliary.GetArguments()[0].([]byte))
if err != nil {
panic(fmt.Sprintf("Failed unarchiving: %s this is a bug and should not happen", err))
}
return result[0].(map[string]interface{})
}
func (a ControlInterface) awaitHostInspectorMonitoredEventTypeChanged() {
msg := a.channel.ReceiveMethodCall("hostInspectorMonitoredEventTypeChanged:")
n, _ := nskeyedarchiver.Unarchive(msg.Auxiliary.GetArguments()[0].([]byte))
log.Infof("hostInspectorMonitoredEventTypeChanged: was set to %d by the device", n[0])
}
func (a ControlInterface) deviceInspectorMoveWithOptions() {
method := "deviceInspectorMoveWithOptions:"
options := nskeyedarchiver.NewNSMutableDictionary(map[string]interface{}{
"ObjectType": "passthrough",
"Value": nskeyedarchiver.NewNSMutableDictionary(map[string]interface{}{
"allowNonAX": nskeyedarchiver.NewNSMutableDictionary(map[string]interface{}{"ObjectType": "passthrough", "Value": false}),
"direction": nskeyedarchiver.NewNSMutableDictionary(map[string]interface{}{"ObjectType": "passthrough", "Value": int32(4)}),
"includeContainers": nskeyedarchiver.NewNSMutableDictionary(map[string]interface{}{"ObjectType": "passthrough", "Value": true}),
}),
})
//str, _ := nskeyedarchiver.ArchiveXML(options)
//println(str)
a.channel.MethodCallAsync(method, options)
}
func (a ControlInterface) notifyPublishedCapabilities() error {
capabs := map[string]interface{}{
"com.apple.private.DTXBlockCompression": uint64(2),
"com.apple.private.DTXConnection": uint64(1),
}
return a.channel.MethodCallAsync("_notifyOfPublishedCapabilities:", capabs)
}
func (a ControlInterface) deviceCapabilities() ([]string, error) {
response, err := a.channel.MethodCall("deviceCapabilities")
if err != nil {
return nil, err
}
return convertToStringList(response.Payload), nil
}
func (a ControlInterface) deviceAllAuditCaseIDs() ([]string, error) {
response, err := a.channel.MethodCall("deviceAllAuditCaseIDs")
if err != nil {
return nil, err
}
return convertToStringList(response.Payload), nil
}
func (a ControlInterface) deviceAccessibilitySettings() (map[string]interface{}, error) {
response, err := a.channel.MethodCall("deviceAccessibilitySettings")
if err != nil {
return nil, err
}
return response.Payload[0].(map[string]interface{}), nil
}
func (a ControlInterface) deviceInspectorSupportedEventTypes() (uint64, error) {
response, err := a.channel.MethodCall("deviceInspectorSupportedEventTypes")
if err != nil {
return 0, err
}
return response.Payload[0].(uint64), nil
}
func (a ControlInterface) deviceAPIVersion() (uint64, error) {
response, err := a.channel.MethodCall("deviceApiVersion")
if err != nil {
return 0, err
}
return response.Payload[0].(uint64), nil
}
func (a ControlInterface) deviceInspectorCanNavWhileMonitoringEvents() (bool, error) {
response, err := a.channel.MethodCall("deviceInspectorCanNavWhileMonitoringEvents")
if err != nil {
return false, err
}
return response.Payload[0].(bool), nil
}
func (a ControlInterface) deviceSetAppMonitoringEnabled(val bool) error {
err := a.channel.MethodCallAsync("deviceSetAppMonitoringEnabled:", val)
if err != nil {
return err
}
return nil
}
func (a ControlInterface) deviceHumanReadableDescriptionForAuditCaseID(auditCaseID string) (string, error) {
response, err := a.channel.MethodCall("deviceHumanReadableDescriptionForAuditCaseID:", auditCaseID)
if err != nil {
return "", err
}
return response.Payload[0].(string), nil
}
func (a ControlInterface) deviceInspectorShowIgnoredElements(val bool) error {
return a.channel.MethodCallAsync("deviceInspectorShowIgnoredElements:", val)
}
func (a ControlInterface) deviceSetAuditTargetPid(pid uint64) error {
return a.channel.MethodCallAsync("deviceSetAuditTargetPid:", pid)
}
func (a ControlInterface) deviceInspectorFocusOnElement() error {
return a.channel.MethodCallAsync("deviceInspectorFocusOnElement:", nskeyedarchiver.NewNSNull())
}
func (a ControlInterface) deviceInspectorPreviewOnElement() error {
return a.channel.MethodCallAsync("deviceInspectorPreviewOnElement:", nskeyedarchiver.NewNSNull())
}
func (a ControlInterface) deviceHighlightIssue() error {
return a.channel.MethodCallAsync("deviceHighlightIssue:", map[string]interface{}{})
}
func (a ControlInterface) deviceInspectorSetMonitoredEventType(eventtype uint64) error {
return a.channel.MethodCallAsync("deviceInspectorSetMonitoredEventType:", eventtype)
}
func (a ControlInterface) deviceInspectorShowVisuals(val bool) error {
return a.channel.MethodCallAsync("deviceInspectorShowVisuals:", val)
}