From edc85f1f4c61ba45f3ec4b08b2af3d480a1e1322 Mon Sep 17 00:00:00 2001 From: Knox Date: Wed, 25 Nov 2020 16:58:46 +0800 Subject: [PATCH] add getSafeArea for android and iOS (#3067) --- .../cocos/platform/android/Device-android.cpp | 4 +- .../java/src/com/cocos/lib/CocosHelper.java | 46 +++++++++++++++++-- native/cocos/platform/ios/Device-ios.mm | 33 ++++--------- 3 files changed, 53 insertions(+), 30 deletions(-) diff --git a/native/cocos/platform/android/Device-android.cpp b/native/cocos/platform/android/Device-android.cpp index e87aff62ed..90d29844e6 100644 --- a/native/cocos/platform/android/Device-android.cpp +++ b/native/cocos/platform/android/Device-android.cpp @@ -130,8 +130,8 @@ Device::NetworkType Device::getNetworkType() cc::Vec4 Device::getSafeAreaEdge() { - // no SafeArea concept on android, return ZERO Vec4. - return cc::Vec4(); + float *data = JniHelper::callStaticFloatArrayMethod(JCLS_HELPER, "getSafeArea"); + return cc::Vec4(data[0], data[1], data[2],data[3]); } float Device::getDevicePixelRatio() diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java index b5f3c0d29e..e57f1691c9 100644 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java @@ -46,6 +46,7 @@ import android.os.Vibrator; import android.util.Log; import android.view.Display; import android.view.Surface; +import android.view.WindowInsets; import android.view.WindowManager; import com.android.vending.expansion.zipfile.APKExpansionSupport; @@ -81,7 +82,7 @@ public class CocosHelper { // The absolute path to the OBB if it exists. private static String sObbFilePath = ""; - + // The OBB file private static ZipResourceFile sOBBFile = null; @@ -215,7 +216,7 @@ public class CocosHelper { } } - public static boolean openURL(String url) { + public static boolean openURL(String url) { boolean ret = false; try { Intent i = new Intent(Intent.ACTION_VIEW); @@ -237,7 +238,7 @@ public class CocosHelper { } }); } - + public static long[] getObbAssetFileDescriptor(final String path) { long[] array = new long[3]; if (CocosHelper.sOBBFile != null) { @@ -306,4 +307,43 @@ public class CocosHelper { e.printStackTrace(); } } + + public static float[] getSafeArea() { + if (android.os.Build.VERSION.SDK_INT >= 28) { + do { + Object windowInsectObj = GlobalObject.getActivity().getWindow().getDecorView().getRootWindowInsets(); + + if(windowInsectObj == null) break; + + Class windowInsets = WindowInsets.class; + try { + Method wiGetDisplayCutout = windowInsets.getMethod("getDisplayCutout"); + Object cutout = wiGetDisplayCutout.invoke(windowInsectObj); + + if(cutout == null) break; + + Class displayCutout = cutout.getClass(); + Method dcGetLeft = displayCutout.getMethod("getSafeInsetLeft"); + Method dcGetRight = displayCutout.getMethod("getSafeInsetRight"); + Method dcGetBottom = displayCutout.getMethod("getSafeInsetBottom"); + Method dcGetTop = displayCutout.getMethod("getSafeInsetTop"); + + if (dcGetLeft != null && dcGetRight != null && dcGetBottom != null && dcGetTop != null) { + int left = (Integer) dcGetLeft.invoke(cutout); + int right = (Integer) dcGetRight.invoke(cutout); + int top = (Integer) dcGetTop.invoke(cutout); + int bottom = (Integer) dcGetBottom.invoke(cutout); + return new float[]{top, left, bottom, right}; + } + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + }while(false); + } + return new float[]{0,0,0,0}; + } } diff --git a/native/cocos/platform/ios/Device-ios.mm b/native/cocos/platform/ios/Device-ios.mm index 5f5e73c324..9feaf7c5a2 100644 --- a/native/cocos/platform/ios/Device-ios.mm +++ b/native/cocos/platform/ios/Device-ios.mm @@ -299,31 +299,14 @@ Device::NetworkType Device::getNetworkType() cc::Vec4 Device::getSafeAreaEdge() { - //TODO:minggo -// UIView* screenView = (UIView*)Application::getInstance()->getView(); -// -//#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 -// float version = [[UIDevice currentDevice].systemVersion floatValue]; -// if (version >= 11.0f) -// { -//#pragma clang diagnostic push -//#pragma clang diagnostic ignored "-Wpartial-availability" -// UIEdgeInsets safeAreaEdge = screenView.safeAreaInsets; -//#pragma clang diagnostic pop -// -// // Multiply contentScaleFactor since safeAreaInsets return points. -// uint8_t scale = screenView.contentScaleFactor; -// safeAreaEdge.left *= scale; -// safeAreaEdge.right *= scale; -// safeAreaEdge.top *= scale; -// safeAreaEdge.bottom *= scale; -// -// return cc::Vec4(safeAreaEdge.top, safeAreaEdge.left, safeAreaEdge.bottom, safeAreaEdge.right); -// } -//#endif -// -// // If running on iOS devices lower than 11.0, return ZERO Vec4. -// return cc::Vec4(); + UIView* screenView = UIApplication.sharedApplication.delegate.window.rootViewController.view; + + if (@available(iOS 11.0, *)) + { + UIEdgeInsets safeAreaEdge = screenView.safeAreaInsets; + return cc::Vec4(safeAreaEdge.top, safeAreaEdge.left, safeAreaEdge.bottom, safeAreaEdge.right); + } + // If running on iOS devices lower than 11.0, return ZERO Vec4. return cc::Vec4(); } }