# Settings.canDrawOverlays() always returns false on Android O

*2017-12-28*

> Work around Settings.canDrawOverlays() returning false after permission is granted on Android O by testing a temporary invisible overlay.

I was updating an app for one of my clients and testing its behavior on Android O (API 26). The app needs [`android.permission.SYSTEM_ALERT_WINDOW`](https://developer.android.com/reference/android/Manifest.permission#SYSTEM_ALERT_WINDOW) to draw over other apps.

On Android 6 (API 23) and later, apps targeting API 23 or higher must request this [special permission](https://developer.android.com/training/permissions/requesting-special) through system settings.

First, call [`Settings.canDrawOverlays()`](https://developer.android.com/reference/android/provider/Settings.html#canDrawOverlays(android.content.Context)) to check whether permission has already been granted. If it returns `false`, open the overlay permission settings so the user can choose whether to allow the app to draw over other apps.

```java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
    // On API 23 and later, ask the user to grant overlay permission if needed.
    if (!Settings.canDrawOverlays(this))
    {
        Intent intent = new Intent(
            Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
            Uri.parse("package:" + getPackageName()));

        startActivityForResult(intent, REQUEST_PERMISSION_SYSTEM_OVERLAY_RESULT);
    }
}
```

On Android O (API 26), I encountered a [bug](https://issuetracker.google.com/issues/66072795) that caused `Settings.canDrawOverlays()` to keep returning `false`, even after the user had granted overlay permission. The workaround below is a little awkward, but it avoids making the user restart the app after granting permission.

The code below first checks `Settings.canDrawOverlays()`. If it returns `true`, the app continues its normal flow. If it returns `false` on Android O (API 26), the code calls our `canDrawOverlays()` helper as a fallback.

The helper tries to add an invisible overlay window and then remove it. If both operations succeed, it returns `true`. If the window manager is unavailable or an exception occurs, it returns `false`.

```java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return;

    if (requestCode == REQUEST_PERMISSION_SYSTEM_OVERLAY_RESULT)
    {
        if (Settings.canDrawOverlays(this))
        {
            m_permissionSystemOverlayWindowGranted = true;

            if (m_permissionReadPhoneStateGranted && m_permissionProcessOutgoingCallsGranted)
            {
                startService(new Intent(this, EstatePlusService.class));
                m_layoutNoPermissions.setVisibility(View.INVISIBLE);
                m_progressBar.setVisibility(View.INVISIBLE);
                m_layoutLogin.setVisibility(View.VISIBLE);
            }
        }
        else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O)
        {
            // Work around the Android O bug that can report false after permission is granted.
            if (canDrawOverlays(this))
            {
                m_permissionSystemOverlayWindowGranted = true;

                if (m_permissionReadPhoneStateGranted && m_permissionProcessOutgoingCallsGranted)
                {
                    startService(new Intent(this, EstatePlusService.class));
                    m_layoutNoPermissions.setVisibility(View.INVISIBLE);
                    m_progressBar.setVisibility(View.INVISIBLE);
                    m_layoutLogin.setVisibility(View.VISIBLE);
                }
            }
        }
    }
}

/**
 * Workaround for Android O.
 */
public static boolean canDrawOverlays(Context context)
{
    try
    {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (windowManager == null)
        {
            return false;
        }

        final View viewToAdd = new View(context);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            0,
            0,
            android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O
                ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
                : WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSPARENT);
        viewToAdd.setLayoutParams(params);
        windowManager.addView(viewToAdd, params);
        windowManager.removeView(viewToAdd);
        return true;
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return false;
}
```

