-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoftWingAccessibility.cs
244 lines (212 loc) · 9.09 KB
/
SoftWingAccessibility.cs
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
using System;
using Android.App;
using Android.Content;
using Android.Util;
using Android.AccessibilityServices;
using Android.Views.Accessibility;
using Android.Graphics;
using static Android.AccessibilityServices.AccessibilityService;
using SoftWing.SwSystem;
using SoftWing.SwSystem.Messages;
using System.Collections.Generic;
namespace SoftWing
{
class SwGestureCallback : GestureResultCallback
{
private const String TAG = "SwGestureCallback";
public SwGestureCallback()
{
Log.Info(TAG, "SwGestureCallback()");
}
public override void OnCancelled(GestureDescription gestureDescription)
{
Log.Info(TAG, "OnCancelled");
base.OnCancelled(gestureDescription);
}
public override void OnCompleted(GestureDescription gestureDescription)
{
Log.Info(TAG, "OnCompleted");
base.OnCompleted(gestureDescription);
}
}
[Service(Label = "SoftWingAccessibility", Permission = "android.permission.BIND_ACCESSIBILITY_SERVICE", Exported = true)]
[IntentFilter(new[] { "android.accessibilityservice.AccessibilityService" })]
[MetaData("android.accessibilityservice", Resource = "@xml/accessibility_service_config")]
class SoftWingAccessibility : AccessibilityService, MessageSubscriber
{
private const String TAG = "SoftWingAccessibility";
private const long GESTURE_START_DELAY_MS = 0;
private const long FIRST_STROKE_DURATION_MS = 10;
private const long CONTINUOUS_STROKE_DURATION_MS = 100;
private long HOLD_STROKE_DURATION_MS = GestureDescription.MaxGestureDuration - FIRST_STROKE_DURATION_MS;
private int MAX_GESTURE_RETRIES = 10;
private MessageDispatcher dispatcher;
private Dictionary<int, MotionDescription> activeMotions = new Dictionary<int, MotionDescription>();
private Android.OS.Handler extendedMotionHandler = null;
private System.Action extendedMotionCallback = null;
public override void OnCreate()
{
Log.Info(TAG, "OnCreate");
base.OnCreate();
dispatcher = MessageDispatcher.GetInstance();
dispatcher.Subscribe(MessageType.MotionUpdate, this);
extendedMotionHandler = new Android.OS.Handler(Android.OS.Looper.MainLooper);
extendedMotionCallback = delegate
{
RunActiveGestures();
};
}
protected override void OnServiceConnected()
{
Log.Info(TAG, "OnServiceConnected");
base.OnServiceConnected();
}
public override bool OnUnbind(Intent intent)
{
Log.Info(TAG, "OnUnbind");
return base.OnUnbind(intent);
}
private List<GestureDescription.StrokeDescription> GenerateTap(MotionDescription motion)
{
Log.Info(TAG, "GenerateTap(" + motion.beginX.ToString() + ", " + motion.beginY.ToString() + ")");
Path firstPath = new Path();
firstPath.MoveTo(motion.beginX, motion.beginY);
firstPath.LineTo(motion.endX, motion.endY);
Path holdPath = new Path();
holdPath.MoveTo(motion.endX, motion.endY);
holdPath.LineTo(motion.endX, motion.endY);
var stroke = new GestureDescription.StrokeDescription(holdPath, GESTURE_START_DELAY_MS, HOLD_STROKE_DURATION_MS, false);
return new List<GestureDescription.StrokeDescription> { stroke };
}
private List<GestureDescription.StrokeDescription> GenerateSwipe(MotionDescription motion)
{
Log.Info(TAG, "GenerateSwipe(" + motion.beginX.ToString() + ", " + motion.beginY.ToString() + ")");
Path firstPath = new Path();
firstPath.MoveTo(motion.beginX, motion.beginY);
firstPath.LineTo(motion.endX, motion.endY);
Path holdPath = new Path();
holdPath.MoveTo(motion.endX, motion.endY);
holdPath.LineTo(motion.endX, motion.endY);
var stroke = new GestureDescription.StrokeDescription(firstPath, GESTURE_START_DELAY_MS, FIRST_STROKE_DURATION_MS, true);
stroke.ContinueStroke(holdPath, GESTURE_START_DELAY_MS + FIRST_STROKE_DURATION_MS, HOLD_STROKE_DURATION_MS, false);
return new List<GestureDescription.StrokeDescription> { stroke };
}
private List<GestureDescription.StrokeDescription> GenerateContinuous(MotionDescription motion)
{
Log.Info(TAG, "GenerateContinuous(" + motion.beginX.ToString() + ", " + motion.beginY.ToString() + ")");
var output = new List<GestureDescription.StrokeDescription>();
Path swipePath = new Path();
swipePath.MoveTo(motion.beginX, motion.beginY);
swipePath.LineTo(motion.endX, motion.endY);
var stroke = new GestureDescription.StrokeDescription(swipePath, GESTURE_START_DELAY_MS, CONTINUOUS_STROKE_DURATION_MS, false);
output.Add(stroke);
// Add a callback to add more strokes for "continuous" behavior
extendedMotionHandler.PostDelayed(extendedMotionCallback, CONTINUOUS_STROKE_DURATION_MS);
return output;
}
private List<GestureDescription.StrokeDescription> GenerateStroke(MotionDescription motion)
{
switch (motion.type)
{
case MotionType.Swipe:
return GenerateSwipe(motion);
case MotionType.Continuous:
return GenerateContinuous(motion);
default:
return GenerateTap(motion);
}
}
private List<GestureDescription.StrokeDescription> GenerateActiveStrokeList()
{
var output = new List<GestureDescription.StrokeDescription>();
foreach (var motion in activeMotions.Values)
{
output.AddRange(GenerateStroke(motion));
}
return output;
}
private void TryDispatchGesture(GestureDescription gesture)
{
var retry_count = 1;
while (!DispatchGesture(gesture, new SwGestureCallback(), null))
{
if (retry_count++ == MAX_GESTURE_RETRIES)
{
throw new AndroidRuntimeException("Failed to execute gesture");
}
}
}
private void RunActiveGestures()
{
var strokes = GenerateActiveStrokeList();
if (strokes.Count == 0)
{
Log.Info(TAG, "Warning: Tried to run empty gesture set");
return;
}
// Add a callback in case the gesture lasts more than the max duration
//extendedMotionHandler.RemoveCallbacks(extendedMotionCallback);
//extendedMotionHandler.PostDelayed(extendedMotionCallback, HOLD_STROKE_DURATION_MS);
GestureDescription.Builder gestureBuilder = new GestureDescription.Builder();
foreach (var stroke in strokes)
{
gestureBuilder.AddStroke(stroke);
}
TryDispatchGesture(gestureBuilder.Build());
}
private void CancelGesture(int id)
{
Log.Info(TAG, "CancelGesture");
//extendedMotionHandler.RemoveCallbacks(extendedMotionCallback);
var motion = activeMotions[id];
activeMotions.Remove(id);
if (activeMotions.Count > 0)
{
RunActiveGestures();
}
else if (motion.type != MotionType.Continuous)
{
// Force-cancel the gesture with a quick motion
Path path = new Path();
path.MoveTo(motion.endX, motion.endY);
path.LineTo(motion.endX, motion.endY);
var stroke = new GestureDescription.StrokeDescription(path, 0, 1, false);
GestureDescription.Builder gestureBuilder = new GestureDescription.Builder();
gestureBuilder.AddStroke(stroke);
TryDispatchGesture(gestureBuilder.Build());
}
}
private void PerformGesture(int id, MotionDescription motion)
{
Log.Info(TAG, "PerformGesture");
activeMotions.Remove(id);
activeMotions.Add(id, motion);
RunActiveGestures();
}
public override void OnAccessibilityEvent(AccessibilityEvent e)
{
Log.Info(TAG, "OnAccessibilityEvent " + e.ToString());
}
public override void OnInterrupt()
{
Log.Info(TAG, "OnInterrupt");
}
public void Accept(SystemMessage message)
{
Log.Info(TAG, "Accept");
if (message.getMessageType() != MessageType.MotionUpdate)
{
return;
}
var motionUpdate = (MotionUpdateMessage)message;
if (motionUpdate.cancel_requested)
{
CancelGesture(motionUpdate.id);
}
else
{
PerformGesture(motionUpdate.id, motionUpdate.motion);
}
}
}
}