Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off overlay permission on android

https://facebook.github.io/react-native/docs/integration-with-existing-apps.html

https://medium.com/react-native-development/fixing-problems-in-react-native-caused-by-new-permission-model-on-android-1e547f754b8

talks about overlay permission.

They add the permission check code to the activity which hosts react native view.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (!Settings.canDrawOverlays(this)) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                                   Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
    }
}

It seems the permission is required because react native needs to show the debug window in an overlay.

How to turn it off in production build?

like image 398
eugene Avatar asked Jul 27 '17 10:07

eugene


1 Answers

I turned it off in release with two changes, one in the manifest, and one in the code.

First: In your main Manifest (src/main/AndroidManifest.xml), remove the line:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

Add a new manifest in src/debug/AndroidManifest.xml that looks like this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.mycompany.myapp">

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
</manifest>

This will merge the permission only on debug variants.

Second, to the code you mentioned, I added a check for BuildConfig.DEBUG:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (!Settings.canDrawOverlays(this) && BuildConfig.DEBUG) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
    }
}
like image 161
Jeremy Avatar answered Sep 30 '22 18:09

Jeremy