mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
add getSafeArea for android and iOS (#3067)
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user