Skip to content

Add Android P cutout area support #19286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions cocos/platform/android/CCGLViewImpl-android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ Rect GLViewImpl::getSafeAreaRect() const {

bool isScreenRound = JniHelper::callStaticBooleanMethod("org/cocos2dx/lib/Cocos2dxHelper", "isScreenRound");
bool hasSoftKeys = JniHelper::callStaticBooleanMethod("org/cocos2dx/lib/Cocos2dxHelper", "hasSoftKeys");
bool isCutoutEnabled = JniHelper::callStaticBooleanMethod("org/cocos2dx/lib/Cocos2dxHelper", "isCutoutEnabled");

if(isScreenRound) {
// edge screen (ex. Samsung Galaxy s7, s9, s9+, Note 9, Nokia 8 Sirocco, Sony Xperia XZ3, Oppo Find X...)
if(safeAreaRect.size.width < safeAreaRect.size.height) {
Expand Down Expand Up @@ -184,6 +186,33 @@ Rect GLViewImpl::getSafeAreaRect() const {
}
}

if (isCutoutEnabled) {
// screen with enabled cutout area (ex. Google Pixel 3 XL, Huawei P20, Asus ZenFone 5, etc)
static int* safeInsets = JniHelper::callStaticIntArrayMethod("org/cocos2dx/lib/Cocos2dxHelper", "getSafeInsets");
if (safeInsets != nullptr) {
float safeInsetBottom = safeInsets[0] / _scaleY;
float safeInsetLeft = safeInsets[1] / _scaleX;
float safeInsetRight = safeInsets[2] / _scaleX;
float safeInsetTop = safeInsets[3] / _scaleY;

// fit safe area rect with safe insets
if (safeInsetBottom > 0) {
safeAreaRect.origin.y += safeInsetBottom;
safeAreaRect.size.height -= safeInsetBottom;
}
if (safeInsetLeft > 0) {
safeAreaRect.origin.x += safeInsetLeft;
safeAreaRect.size.width -= safeInsetLeft;
}
if (safeInsetRight > 0) {
safeAreaRect.size.width -= safeInsetRight;
}
if (safeInsetTop > 0) {
safeAreaRect.size.height -= safeInsetTop;
}
}
}

return safeAreaRect;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ of this software and associated documentation files (the "Software"), to deal
****************************************************************************/
package org.cocos2dx.lib;

import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.graphics.Rect;
import android.media.AudioManager;
import android.app.Activity;
import android.content.ComponentName;
Expand All @@ -47,9 +49,11 @@ of this software and associated documentation files (the "Software"), to deal
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.DisplayCutout;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.ViewConfiguration;
import android.view.Window;
import android.view.WindowManager;

import com.android.vending.expansion.zipfile.APKExpansionSupport;
Expand All @@ -64,6 +68,7 @@ of this software and associated documentation files (the "Software"), to deal
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -771,6 +776,48 @@ public static boolean isScreenRound() {
return false;
}

/**
* Returns whether the window is always allowed to extend into the DisplayCutout areas on the short edges of the screen.
*
* @return true if the window in display cutout mode on the short edges of the screen, false otherwise
*/
@SuppressLint("InlinedApi")
public static boolean isCutoutEnabled() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowManager.LayoutParams lp = sActivity.getWindow().getAttributes();
return lp.layoutInDisplayCutoutMode == WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}

return false;
}

/**
* Returns safe insets array.
*
* @return array of int with safe insets values
*/
@SuppressLint("NewApi")
public static int[] getSafeInsets() {
final int[] safeInsets = new int[]{0, 0, 0, 0};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Window cocosWindow = sActivity.getWindow();
DisplayCutout displayCutout = cocosWindow.getDecorView().getRootWindowInsets().getDisplayCutout();
// Judge whether it is cutouts (aka notch) screen phone by judge cutout equle to null
if (displayCutout != null) {
List<Rect> rects = displayCutout.getBoundingRects();
// Judge whether it is cutouts (aka notch) screen phone by judge cutout rects is null or zero size
if (rects != null && rects.size() != 0) {
safeInsets[0] = displayCutout.getSafeInsetBottom();
safeInsets[1] = displayCutout.getSafeInsetLeft();
safeInsets[2] = displayCutout.getSafeInsetRight();
safeInsets[3] = displayCutout.getSafeInsetTop();
}
}
}

return safeInsets;
}

/**
* Queries about whether any physical keys exist on the
* any keyboard attached to the device and returns <code>true</code>
Expand Down
31 changes: 31 additions & 0 deletions cocos/platform/android/jni/JniHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,37 @@ class CC_DLL JniHelper
return nullptr;
}

/**
@brief Call of Java static int* method
@return address of JniMethodInfo if there are proper JniMethodInfo; otherwise nullptr.
*/
template <typename... Ts>
static int* callStaticIntArrayMethod(const std::string& className,
const std::string& methodName,
Ts... xs) {
static int ret[32];
cocos2d::JniMethodInfo t;
std::string signature = "(" + std::string(getJNISignature(xs...)) + ")[I";
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
LocalRefMapType localRefs;
jintArray array = (jintArray) t.env->CallStaticObjectMethod(t.classID, t.methodID, convert(localRefs, t, xs)...);
jsize len = t.env->GetArrayLength(array);
if (len <= 32) {
jint* elems = t.env->GetIntArrayElements(array, 0);
if (elems) {
memcpy(ret, elems, sizeof(int) * len);
t.env->ReleaseIntArrayElements(array, elems, 0);
};
}
t.env->DeleteLocalRef(t.classID);
deleteLocalRefs(t.env, localRefs);
return &ret[0];
} else {
reportError(className, methodName, signature);
}
return nullptr;
}

/**
@brief Call of Java static Vec3 method
@return JniMethodInfo of Vec3 type if there are proper JniMethodInfo; otherwise Vec3(0, 0, 0).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ of this software and associated documentation files (the "Software"), to deal

import android.os.Bundle;
import org.cocos2dx.lib.Cocos2dxActivity;
import android.os.Build;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

public class AppActivity extends Cocos2dxActivity {

Expand All @@ -41,6 +44,13 @@ protected void onCreate(Bundle savedInstanceState) {
// Don't need to finish it again since it's finished in super.onCreate .
return;
}
// Make sure we're running on Pie or higher to change cutout mode
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// Enable rendering into the cutout area
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
getWindow().setAttributes(lp);
}
// DO OTHER INITIALIZATION BELOW

}
Expand Down