X-SLAYER / flutter_accessibility_service

Flutter plugin for interacting with Accessibility Service in Android.
https://pub.dev/packages/flutter_accessibility_service
MIT License
36 stars 26 forks source link

[Feature-Request] Offload Accessibility Service onListen processing to a separate thread and timeout #31

Open saurabhkumar8112 opened 4 months ago

saurabhkumar8112 commented 4 months ago

Its a common problem that accessibility service often leads to ANRs because of timeouts in onListen/locks(ANR triggered by main thread waiting for too long).

Do you think it would be better to offload the processing inside onListen to a separate thread with a timeout? If the Thread doesn't finish processing within a set time, timeout it and exit the onListen.

What does the author think?

Something like this

 @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {

        Callable<Void> task = () -> {
            handler()
        };

        Future<Void> future = executor.submit(task);
        try {
            future.get(5, TimeUnit.SECONDS); 
        } catch (TimeoutException e) {
            future.cancel(true);
            handleTimeout(event);
        } catch (Exception e) {

            e.printStackTrace();
        }
    }
X-SLAYER commented 4 months ago

Thank you for the suggestion and for highlighting the common issue . Yes offloading this to a separate thread with a timeout mechanism is a thoughtful approach to enhance the robustness of the application. i will try this otherwise if u open a PL if you can