Skip to content

Commit

Permalink
messy stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
lochidev committed Jun 3, 2024
1 parent 7cb66fe commit df04e00
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions MauiApp1/Platforms/Android/Services/MyAccessibilityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public class MyAccessibilityService : AccessibilityService
{
private Dictionary<string, Match> dict;
private List<Var> globals;
private readonly Bundle CursorArgs = new();
private readonly Bundle TextArgs = new();
private const string CursorStr = "$|$";

private static readonly char[] separator = [' ', '\n', ','];
//private static readonly char[] wordSeparator = [' ', /*'\n',*/ ','];

Expand Down Expand Up @@ -81,18 +85,8 @@ public override async void OnAccessibilityEvent(AccessibilityEvent e)
if (Text != null)
{
string og = Text[0].ToString();
const string cursorStr = "$|$";
int startIndex = og.IndexOf(cursorStr);
if(startIndex != -1)
{
Bundle cursorArgs = null;
cursorArgs = new Bundle();
cursorArgs.PutInt(AccessibilityNodeInfo.ActionArgumentSelectionStartInt, startIndex);
cursorArgs.PutInt(AccessibilityNodeInfo.ActionArgumentSelectionEndInt, startIndex + cursorStr.Length);

e.Source.PerformAction(Android.Views.Accessibility.Action.SetSelection, cursorArgs);
}
//quick brown fox
CheckAndUpdateCursorArgs(og, sendIfCursorFound: true, e);
var arr = og.Split(separator, StringSplitOptions.RemoveEmptyEntries);
bool send = false;

Expand Down Expand Up @@ -152,21 +146,14 @@ public override async void OnAccessibilityEvent(AccessibilityEvent e)
}
if (send)
{
Bundle args = new();
args.PutCharSequence(AccessibilityNodeInfo.ActionArgumentSetTextCharsequence, og);
e.Source.PerformAction(Android.Views.Accessibility.Action.SetText, args);

//og has been modified with our new expansion
TextArgs.Remove(AccessibilityNodeInfo.ActionArgumentSetTextCharsequence);
TextArgs.PutCharSequence(AccessibilityNodeInfo.ActionArgumentSetTextCharsequence, og);
e.Source.PerformAction(Android.Views.Accessibility.Action.SetText, TextArgs);
if (e.Source.Refresh())
{

//og has been modified with our new expansion
Bundle cursorArgs = null;
cursorArgs = new Bundle();
cursorArgs.PutInt(AccessibilityNodeInfo.ActionArgumentSelectionStartInt, og.Length);
cursorArgs.PutInt(AccessibilityNodeInfo.ActionArgumentSelectionEndInt, og.Length);

e.Source.PerformAction(Android.Views.Accessibility.Action.SetSelection, cursorArgs);

CheckAndUpdateCursorArgs(og, sendIfCursorFound: false, e);
e.Source.PerformAction(Android.Views.Accessibility.Action.SetSelection, CursorArgs);
}
}
}
Expand All @@ -178,6 +165,28 @@ public override async void OnAccessibilityEvent(AccessibilityEvent e)

}
}

private void CheckAndUpdateCursorArgs(string og, bool sendIfCursorFound, AccessibilityEvent e)
{
int startIndex = og.IndexOf(CursorStr);
CursorArgs.Remove(AccessibilityNodeInfo.ActionArgumentSelectionStartInt);
CursorArgs.Remove(AccessibilityNodeInfo.ActionArgumentSelectionEndInt);
if (startIndex != -1)
{
CursorArgs.PutInt(AccessibilityNodeInfo.ActionArgumentSelectionStartInt, startIndex);
CursorArgs.PutInt(AccessibilityNodeInfo.ActionArgumentSelectionEndInt, startIndex + CursorStr.Length);
if(sendIfCursorFound)
{
e.Source.PerformAction(Android.Views.Accessibility.Action.SetSelection, CursorArgs);
}
}
else
{
CursorArgs.PutInt(AccessibilityNodeInfo.ActionArgumentSelectionStartInt, og.Length);
CursorArgs.PutInt(AccessibilityNodeInfo.ActionArgumentSelectionEndInt, og.Length);
}
}

private async Task<string> ParseItemAsync(Var item, string replace)
{
try
Expand Down

0 comments on commit df04e00

Please sign in to comment.