Overlay permission error on API 26

Viewed 1958

When my app attempts to draw a System Overlay, it crashes and tells me that permission was denied for window type 2010. However, I require the user to grant the ACTION_MANAGE_OVERLAY_PERMISSION in a tutorial activity on first install. Also, this happens only on API 26. I have tested all other supported SDK's and I am able to draw an overlay with no crashes or errors.

I have used

Settings.canDrawOverlay(context);

and it returns true. So, it seems that the system knows the permission is granted yet the problem persists.

Here is the overlay code:

    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.view_driving, mRelativeLayout);

    WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            PixelFormat.TRANSLUCENT);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
        layoutParams.flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
    } else{
        layoutParams.flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
    }

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        layoutParams.flags = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    }

    int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
            | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

    layoutParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;

    mRelativeLayout.setSystemUiVisibility(uiOptions);

    if(Settings.canDrawOverlays(mContext)){
        WindowManager windowManager = (WindowManager) mContext.getSystemService(WINDOW_SERVICE);
        windowManager.addView(mRelativeLayout, layoutParams);  
    }

The crash happens when addView is called.

permission denied 2010

0 Answers
Related