Skip to content

Commit df04e00

Browse files
committed
messy stuff
1 parent 7cb66fe commit df04e00

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

MauiApp1/Platforms/Android/Services/MyAccessibilityService.cs

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public class MyAccessibilityService : AccessibilityService
1616
{
1717
private Dictionary<string, Match> dict;
1818
private List<Var> globals;
19+
private readonly Bundle CursorArgs = new();
20+
private readonly Bundle TextArgs = new();
21+
private const string CursorStr = "$|$";
22+
1923
private static readonly char[] separator = [' ', '\n', ','];
2024
//private static readonly char[] wordSeparator = [' ', /*'\n',*/ ','];
2125

@@ -81,18 +85,8 @@ public override async void OnAccessibilityEvent(AccessibilityEvent e)
8185
if (Text != null)
8286
{
8387
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-
}
9588
//quick brown fox
89+
CheckAndUpdateCursorArgs(og, sendIfCursorFound: true, e);
9690
var arr = og.Split(separator, StringSplitOptions.RemoveEmptyEntries);
9791
bool send = false;
9892

@@ -152,21 +146,14 @@ public override async void OnAccessibilityEvent(AccessibilityEvent e)
152146
}
153147
if (send)
154148
{
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);
159153
if (e.Source.Refresh())
160154
{
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);
170157
}
171158
}
172159
}
@@ -178,6 +165,28 @@ public override async void OnAccessibilityEvent(AccessibilityEvent e)
178165

179166
}
180167
}
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+
181190
private async Task<string> ParseItemAsync(Var item, string replace)
182191
{
183192
try

0 commit comments

Comments
 (0)