@@ -16,6 +16,10 @@ public class MyAccessibilityService : AccessibilityService
16
16
{
17
17
private Dictionary < string , Match > dict ;
18
18
private List < Var > globals ;
19
+ private readonly Bundle CursorArgs = new ( ) ;
20
+ private readonly Bundle TextArgs = new ( ) ;
21
+ private const string CursorStr = "$|$" ;
22
+
19
23
private static readonly char [ ] separator = [ ' ' , '\n ' , ',' ] ;
20
24
//private static readonly char[] wordSeparator = [' ', /*'\n',*/ ','];
21
25
@@ -81,18 +85,8 @@ public override async void OnAccessibilityEvent(AccessibilityEvent e)
81
85
if ( Text != null )
82
86
{
83
87
string og = Text [ 0 ] . ToString ( ) ;
84
- const string cursorStr = "$|$" ;
85
- int startIndex = og . IndexOf ( cursorStr ) ;
86
- if ( startIndex != - 1 )
87
- {
88
- Bundle cursorArgs = null ;
89
- cursorArgs = new Bundle ( ) ;
90
- cursorArgs . PutInt ( AccessibilityNodeInfo . ActionArgumentSelectionStartInt , startIndex ) ;
91
- cursorArgs . PutInt ( AccessibilityNodeInfo . ActionArgumentSelectionEndInt , startIndex + cursorStr . Length ) ;
92
-
93
- e . Source . PerformAction ( Android . Views . Accessibility . Action . SetSelection , cursorArgs ) ;
94
- }
95
88
//quick brown fox
89
+ CheckAndUpdateCursorArgs ( og , sendIfCursorFound : true , e ) ;
96
90
var arr = og . Split ( separator , StringSplitOptions . RemoveEmptyEntries ) ;
97
91
bool send = false ;
98
92
@@ -152,21 +146,14 @@ public override async void OnAccessibilityEvent(AccessibilityEvent e)
152
146
}
153
147
if ( send )
154
148
{
155
- Bundle args = new ( ) ;
156
- args . PutCharSequence ( AccessibilityNodeInfo . ActionArgumentSetTextCharsequence , og ) ;
157
- e . Source . PerformAction ( Android . Views . Accessibility . Action . SetText , args ) ;
158
-
149
+ //og has been modified with our new expansion
150
+ TextArgs . Remove ( AccessibilityNodeInfo . ActionArgumentSetTextCharsequence ) ;
151
+ TextArgs . PutCharSequence ( AccessibilityNodeInfo . ActionArgumentSetTextCharsequence , og ) ;
152
+ e . Source . PerformAction ( Android . Views . Accessibility . Action . SetText , TextArgs ) ;
159
153
if ( e . Source . Refresh ( ) )
160
154
{
161
-
162
- //og has been modified with our new expansion
163
- Bundle cursorArgs = null ;
164
- cursorArgs = new Bundle ( ) ;
165
- cursorArgs . PutInt ( AccessibilityNodeInfo . ActionArgumentSelectionStartInt , og . Length ) ;
166
- cursorArgs . PutInt ( AccessibilityNodeInfo . ActionArgumentSelectionEndInt , og . Length ) ;
167
-
168
- e . Source . PerformAction ( Android . Views . Accessibility . Action . SetSelection , cursorArgs ) ;
169
-
155
+ CheckAndUpdateCursorArgs ( og , sendIfCursorFound : false , e ) ;
156
+ e . Source . PerformAction ( Android . Views . Accessibility . Action . SetSelection , CursorArgs ) ;
170
157
}
171
158
}
172
159
}
@@ -178,6 +165,28 @@ public override async void OnAccessibilityEvent(AccessibilityEvent e)
178
165
179
166
}
180
167
}
168
+
169
+ private void CheckAndUpdateCursorArgs ( string og , bool sendIfCursorFound , AccessibilityEvent e )
170
+ {
171
+ int startIndex = og . IndexOf ( CursorStr ) ;
172
+ CursorArgs . Remove ( AccessibilityNodeInfo . ActionArgumentSelectionStartInt ) ;
173
+ CursorArgs . Remove ( AccessibilityNodeInfo . ActionArgumentSelectionEndInt ) ;
174
+ if ( startIndex != - 1 )
175
+ {
176
+ CursorArgs . PutInt ( AccessibilityNodeInfo . ActionArgumentSelectionStartInt , startIndex ) ;
177
+ CursorArgs . PutInt ( AccessibilityNodeInfo . ActionArgumentSelectionEndInt , startIndex + CursorStr . Length ) ;
178
+ if ( sendIfCursorFound )
179
+ {
180
+ e . Source . PerformAction ( Android . Views . Accessibility . Action . SetSelection , CursorArgs ) ;
181
+ }
182
+ }
183
+ else
184
+ {
185
+ CursorArgs . PutInt ( AccessibilityNodeInfo . ActionArgumentSelectionStartInt , og . Length ) ;
186
+ CursorArgs . PutInt ( AccessibilityNodeInfo . ActionArgumentSelectionEndInt , og . Length ) ;
187
+ }
188
+ }
189
+
181
190
private async Task < string > ParseItemAsync ( Var item , string replace )
182
191
{
183
192
try
0 commit comments