From 7491bc3293dc1eb3902f85c2c2d8c418eecf56db Mon Sep 17 00:00:00 2001 From: Andrew Jack Date: Mon, 11 Sep 2017 12:41:39 -0700 Subject: [PATCH] Android Oreo (8.0) Support Summary: Apps targeting Android 8.0 (API level 26) cannot use `TYPE_SYSTEM_OVERLAY ` and `TYPE_SYSTEM_OVERLAY `. Targeting 26 will cause the app to crash when in `DEV_MODE`. https://developer.android.com/about/versions/oreo/android-8.0-changes.html#cwt This PR replaces uses of these overlay flags with the new `TYPE_APPLICATION_OVERLAY` when running on a device with Android 8.0 or later. When using `TYPE_APPLICATION_OVERLAY` it still requires the `SYSTEM_ALERT_WINDOW` permission, just like previous android versions. https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#TYPE_APPLICATION_OVERLAY https://github.com/AndrewJack/react-native-android-oreo tested here Closes https://github.com/facebook/react-native/pull/15601 Reviewed By: achen1 Differential Revision: D5801619 Pulled By: shergin fbshipit-source-id: 27d1b9bb64018e7f12f9c3d3d222f1fda468b124 --- .../devsupport/DebugOverlayController.java | 2 +- .../devsupport/DevLoadingViewController.java | 2 +- .../devsupport/DevSupportManagerImpl.java | 5 ++--- .../react/devsupport/WindowOverlayCompat.java | 18 ++++++++++++++++++ 4 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 ReactAndroid/src/main/java/com/facebook/react/devsupport/WindowOverlayCompat.java diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DebugOverlayController.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DebugOverlayController.java index 00e075c509fdb7..bb485af8ba63f5 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DebugOverlayController.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DebugOverlayController.java @@ -40,7 +40,7 @@ public void setFpsDebugViewVisible(boolean fpsDebugViewVisible) { WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, - WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, + WindowOverlayCompat.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT); diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevLoadingViewController.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevLoadingViewController.java index 44e9a816c62ac5..f9573acdce5f40 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevLoadingViewController.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevLoadingViewController.java @@ -143,7 +143,7 @@ private void setVisible(boolean visible) { WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, - WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, + WindowOverlayCompat.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); params.gravity = Gravity.TOP; diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerImpl.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerImpl.java index ce8a4938a643b1..d7c9e23e4e0901 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerImpl.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerImpl.java @@ -34,7 +34,6 @@ import android.hardware.SensorManager; import android.net.Uri; import android.os.AsyncTask; -import android.view.WindowManager; import android.widget.Toast; import com.facebook.common.logging.FLog; @@ -336,7 +335,7 @@ private void showNewError( public void run() { if (mRedBoxDialog == null) { mRedBoxDialog = new RedBoxDialog(mApplicationContext, DevSupportManagerImpl.this, mRedBoxHandler); - mRedBoxDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); + mRedBoxDialog.getWindow().setType(WindowOverlayCompat.TYPE_SYSTEM_ALERT); } if (mRedBoxDialog.isShowing()) { // Sometimes errors cause multiple errors to be thrown in JS in quick succession. Only @@ -478,7 +477,7 @@ public void onCancel(DialogInterface dialog) { } }) .create(); - mDevOptionsDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); + mDevOptionsDialog.getWindow().setType(WindowOverlayCompat.TYPE_SYSTEM_ALERT); mDevOptionsDialog.show(); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/WindowOverlayCompat.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/WindowOverlayCompat.java new file mode 100644 index 00000000000000..b4bfa91eeba915 --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/WindowOverlayCompat.java @@ -0,0 +1,18 @@ +package com.facebook.react.devsupport; + +import android.os.Build; +import android.view.WindowManager; + +/** + * Compatibility wrapper for apps targeting API level 26 or later. + * See https://developer.android.com/about/versions/oreo/android-8.0-changes.html#cwt + */ +/* package */ class WindowOverlayCompat { + + private static final int ANDROID_OREO = 26; + private static final int TYPE_APPLICATION_OVERLAY = 2038; + + static final int TYPE_SYSTEM_ALERT = Build.VERSION.SDK_INT < ANDROID_OREO ? WindowManager.LayoutParams.TYPE_SYSTEM_ALERT : TYPE_APPLICATION_OVERLAY; + static final int TYPE_SYSTEM_OVERLAY = Build.VERSION.SDK_INT < ANDROID_OREO ? WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY : TYPE_APPLICATION_OVERLAY; + +}