Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable status bar pull in Oreo

The method for kiosking an application by disabling pull and click of the status bar does not work on android 8. As anserwed on How to disable status bar click and pull down in Android?

  1. You can lay a window over the status bar to disable any touch or pulling down.

As described by this answer, this method of doing it does works on android 7 and below however this method does not work on android 8(oreo).

I have tested it on android 7 and less and it works, but the status bar still pulls down when pulled on android 8.

If you have a solution on this please assist.

Thank you all.

like image 214
Benson Machira Avatar asked Jul 31 '18 06:07

Benson Machira


1 Answers

For and 8 and above you cant realy fully overylay a view over other apps, so what you have to do is, when you pull the drawer down, return the drawer back up so fast that the user wont be able to click anything on it. This is the code that does that. Make sure you are doing this on an activity.

    @Override
public void onWindowFocusChanged(boolean hasFocus) {

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        if (!hasFocus) {

            Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            sendBroadcast(closeDialog);


            // Method that handles loss of window focus
            new BlockStatusBar(this,false).collapseNow();
        }
    }
}

Then the helper class that is doing the job of hiding the status bar is as indicated below.

public class BlockStatusBar {
Context context;

// To keep track of activity's window focus
boolean currentFocus;
// To keep track of activity's foreground/background status
boolean isPaused;

public static Handler collapseNotificationHandler;
Method collapseStatusBar = null;


public BlockStatusBar(Context context,boolean isPaused) {
    this.context=context;
    this.isPaused=isPaused;
    collapseNow();
}

public void collapseNow() {

    // Initialize 'collapseNotificationHandler'
    if (collapseNotificationHandler == null) {
        collapseNotificationHandler = new Handler();
    }

    // If window focus has been lost && activity is not in a paused state
    // Its a valid check because showing of notification panel
    // steals the focus from current activity's window, but does not
    // 'pause' the activity
    if (!currentFocus && !isPaused) {

        // Post a Runnable with some delay - currently set to 300 ms
        collapseNotificationHandler.postDelayed(new Runnable() {

            @Override
            public void run() {

                // Use reflection to trigger a method from 'StatusBarManager'

                Object statusBarService = context.getSystemService("statusbar");
                Class<?> statusBarManager = null;

                try {
                    statusBarManager = Class.forName("android.app.StatusBarManager");
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }



                try {

                    // Prior to API 17, the method to call is 'collapse()'
                    // API 17 onwards, the method to call is `collapsePanels()`

                    if (Build.VERSION.SDK_INT > 16) {
                        collapseStatusBar = statusBarManager .getMethod("collapsePanels");
                    } else {
                        collapseStatusBar = statusBarManager .getMethod("collapse");
                    }
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }

                collapseStatusBar.setAccessible(true);

                try {
                    collapseStatusBar.invoke(statusBarService);
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }

                // Check if the window focus has been returned
                // If it hasn't been returned, post this Runnable again
                // Currently, the delay is 100 ms. You can change this
                // value to suit your needs.
                if (!currentFocus && !isPaused) {
                    collapseNotificationHandler.postDelayed(this, 100L);
                }

                if (!currentFocus && isPaused) {
                    collapseNotificationHandler.removeCallbacksAndMessages(null);
                }

            }
        }, 1L);
    }
}

}

like image 92
Benson Machira Avatar answered Nov 11 '22 16:11

Benson Machira