Search code examples
androidxmltext-to-speechaccessibilityservice

Detect "shut-down/reboot menu" using accessibility service in android?


I've created my accessibility service and I would like to call OnAccessibilityEvent() only when the "shut down" menu appears, my goal is to call the TTS engine in order to make the phone talk only when this menu is on the screen.

Everything I need to understand is how to detect this only event.

Here's my xml:

<?xml version="1.0" encoding="utf-8" ?> <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:accessibilityEventTypes="typeAllMask" android:accessibilityFlags="flagDefault" android:accessibilityFeedbackType="feedbackAllMask" android:canRetrieveWindowContent="true" />

Thank you so much!


Solution

  • NO, there is no reliable way to do this. The only ways to do this are hacky. The hacky way to do this is the following:

    You could listen for window content change events, I believe the constant is TYPE_WINDOW_CONTENT_CHANGED.

    So the full code would be:

    public void onAccessibilityEvent(AccessibilityEvent e) {
    
        switch (e.getEventType()) {
            case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED: {
    
                if (isPowerMenu(getRootInActiveWindow()) {
                   doYourStuff();
                }
            }
        }
    }
    
    private boolean isPowerMenu(AccessibilityNodeInfo nodeInfo) {
        //In here assume you've been given the root accessibility 
        //node of the power menu.  It should be easy to detect specific
        //power menu views.  However, this will be very fragile, and 
        //depend on which version of the Android Launcher, OS, etc you
        //have.  Hence the comments, and no code :)
    }
    

    The problem is that each power on/off menu may be different. So this approach will only work for a particular launcher app. However, even then, someone could potentially create an identical modal in another application. So you could have a false positive detection, even within a given launcher/device/OS version set up.