0

I am using Accessibility service in my application and getting ANR in onEvent method while calling event.getSource();

When i put this code in another thread, to resolve this above ANR it creates an exception:

java.lang.IllegalStateException: Cannot perform this action on a not sealed instance.

Please guide me how can i resolve it? Code is below:

  @Override
public void onAccessibilityEvent(final AccessibilityEvent event) {
    int eventType = event.getEventType();
    if (eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
        OnEvent(this, event);
    }
}

private void OnEvent(final Context context, AccessibilityEvent event) {
    String eventText = "", eventPackageName = "", eventClassName = "";
    try {

        if (event.getText() != null && event.getText().size() > 0)
            eventText = event.getText().toString();
        if (event.getPackageName() != null)
            eventPackageName = event.getPackageName().toString();
        if (event.getClassName() != null)
            eventClassName = event.getClassName().toString();

        AccessibilityNodeInfo eventSource = event.getSource();
        final AccessibilityEventModel accessibilityEventModel = new AccessibilityEventModel();
        accessibilityEventModel.setPackageName(eventPackageName);
        accessibilityEventModel.setEventText(eventText);
        accessibilityEventModel.setEventClassName(eventClassName);
        accessibilityEventModel.setEventSource(eventSource);
        accessibilityEventModel.setParcelable(event.getParcelableData());
       executorService.execute(() -> {
            AccessibilityManager accessibilityManager = new AccessibilityManager(context, localDatabaseSource);
            accessibilityManager.goThroughAccessibilityEvent(eventType,
                    accessibilityEventModel);
        });
        
    } catch (Exception e) {
        Log.d(TAG, " Error: " + e.getMessage());
    }
}






fun goThroughAccessibilityEvent(
    eventType: AccessibilityEventType,
    accessibilityEventModel: AccessibilityEventModel
) {
    when (eventType) {
        AccessibilityEventType.TYPE_WINDOW_CONTENT_CHANGED -> WindowContentChangeEventData(
            localDatabaseSource
        ).onAccessibilityEvent(context, accessibilityEventModel)
    }
}


 
Ahmad Ali
  • 23
  • 6

1 Answers1

0

I can't tell what you're actually doing with the accessibilityEventModel after it's generated, but you should try placing the whole try/catch (and any other logic that needs uses that accessibilityEventModel) block inside a background thread:

new Thread(new Runnable() {
   @Override
   public void run() {
        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND)
        //your code
   }
}).start()
user496854
  • 6,461
  • 10
  • 47
  • 84
  • Ok, i will try this solution and will let you know that it work on not Thanks – Ahmad Ali Jun 16 '23 at 05:32
  • After moving try/catch (business logic) to thread. I'm getting this exception in catch block "Cannot perform this action on a not sealed instance." – Ahmad Ali Jun 16 '23 at 05:46
  • I have added some more code. Kindly can please check it again?. I am just looping through whole Window content – Ahmad Ali Jun 16 '23 at 05:56
  • what's the actual line that triggers that error? – user496854 Jun 16 '23 at 18:14
  • AccessibilityNodeInfo eventSource = event.getSource(); this line triggering error – Ahmad Ali Jun 19 '23 at 05:17
  • just to clarify, you originally said that `event.getSource()` was causing the ANR. Are you saying that the exact same line is causing the exception inside the thread? – user496854 Jun 19 '23 at 11:08
  • yes, it is causing issue – Ahmad Ali Jun 19 '23 at 11:48
  • Does it crash every time `OnEvent()` is called, or only sometimes? If it doesn't happen all the time, then this is probably normal -- it's supposed to crash if the node was recycled – user496854 Jun 19 '23 at 13:05
  • Its crashing all the time – Ahmad Ali Jun 20 '23 at 05:36
  • In that case, I would have to say that there's probably something goin on with he rest of your code that's possibly causing problems. For troubleshooting's sake, just remove all of your code from inside `OnEvent()`, and leave only the line `AccessibilityNodeInfo eventSource = event.getSource()` (inside the `Thread`), and see if you still get the errors – user496854 Jun 20 '23 at 18:15
  • ok, i will try it and will let you know whether it works or not – Ahmad Ali Jun 21 '23 at 06:16