Search code examples
androidsplit-screensplit-screen-multitasking

Disable split screen mode for all apps in Android


What I Want: Disable user to use split screen mode for any application in his phone.

What I've already done: To disable split screen mode, I need to detect which method is called and in that method I can further add a functionality to draw a custom view over it or quickly pull down split screen window. I'm looking into AccessibilityEvents as well, might be I need to parse and filter some keywords to get to split screen detection.

So what can be that method in which Android will tell that user has just started to use split screen mode. And how can I then quickly pull down split screen window?


Solution

  • You can detect when any application goes to split screen mode if you have asked AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED event when registering for accessibility service.

    Possible way to detect Split screen mode:

    In the onAccessibilityEvent(AccessibilityEvent event) function we need to write event.getSource().getContentDescription(); and search for "Split" or "Dismiss" or other keywords in the string, depends upon various custom roms. Whenever application comes in split screen mode, its content description is set as 'Split Whatsapp' etc. That's how we can detect when any particular application comes in split screen mode.

    Possible way to block usage of split screen mode for any app:

    After detecting you need to add this line in order to make it impossible for the user to utilize split screen mode. It will just dock the current application window.

    performGlobalAction(AccessibilityService.GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN)
    

    There are other global events as well to perform an action like:

    1. GLOBAL_ACTION_BACK
    2. GLOBAL_ACTION_HOME
    3. GLOBAL_ACTION_LOCK_SCREEN
    4. GLOBAL_ACTION_NOTIFICATIONS
    5. GLOBAL_ACTION_POWER_DIALOG
    6. GLOBAL_ACTION_QUICK_SETTINGS
    7. GLOBAL_ACTION_RECENTS
    8. GLOBAL_ACTION_TAKE_SCREENSHOT

    GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN

    But most suitable for this scenario is: GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN

    public class AppAccessibility extends AccessibilityService {
    
        @Override
        protected void onServiceConnected() {
            super.onServiceConnected();
            AccessibilityServiceInfo config = new AccessibilityServiceInfo();
            config.eventTypes = AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED;
            config.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
            if (Build.VERSION.SDK_INT >= 16) {
                config.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
            }
            setServiceInfo(config);
        }
        @Override
        public void onAccessibilityEvent(AccessibilityEvent event) {
            if (event != null && event.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
                if (event.getSource() != null && event.getSource().getContentDescription() != null) {
                    if (event.getSource().getContentDescription().toString().contains("Split")) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            performGlobalAction(AccessibilityService.GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN));
                    }
                }
            }
        }
    }