Skip to content

Commit

Permalink
Android: implementing Safe Area APIs
Browse files Browse the repository at this point in the history
Resolves #127
  • Loading branch information
Shchvova committed Sep 25, 2020
1 parent 4b25033 commit 64d8f70
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
38 changes: 29 additions & 9 deletions platform/android/sdk/src/com/ansca/corona/CoronaActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import com.ansca.corona.permissions.PermissionState;
import com.ansca.corona.permissions.RequestPermissionsResultData;
import com.ansca.corona.storage.ResourceServices;
import com.ansca.corona.graphics.opengl.GLSurfaceView;

import android.view.DisplayCutout;
import android.view.ViewTreeObserver;
/**
* The activity window that hosts the Corona project.
* @see <a href="http://developer.android.com/reference/android/app/Activity.html">Activity</a>
Expand All @@ -50,6 +50,7 @@ public class CoronaActivity extends Activity {
private com.ansca.corona.purchasing.StoreProxy myStore = null;
private CoronaStatusBarSettings myStatusBarMode;
private android.database.ContentObserver fAutoRotateObserver = null;
private DisplayCutout fDisplayCutout = null;

private Controller fController;
private CoronaRuntime fCoronaRuntime;
Expand Down Expand Up @@ -220,7 +221,7 @@ protected void onCreate(Bundle savedInstanceState) {
// We do this because the Android OS does not support ADJUST_PAN when in fullscreen mode.
requestWindowFeature(Window.FEATURE_NO_TITLE);

if (isKeyboardAppPanningEnabled == false) {
if (!isKeyboardAppPanningEnabled) {
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
Expand Down Expand Up @@ -615,7 +616,7 @@ int getLoggedOrientation() {
/**
* Logs the requested orientation.
* This is an internal method that can only be called by Corona.
* @param orintationToLog The "screen orientation" constant in class ActivityInfo we want to log.
* @param orientationToLog The "screen orientation" constant in class ActivityInfo we want to log.
*/
void logOrientation(int orientationToLog) {
fLoggedOrientation = orientationToLog;
Expand Down Expand Up @@ -1114,11 +1115,24 @@ void setStatusBarMode(CoronaStatusBarSettings mode) {
if (mode == myStatusBarMode) {
return;
}

if (android.os.Build.VERSION.SDK_INT >= 28) {
getWindow().getDecorView().setOnApplyWindowInsetsListener(new android.view.View.OnApplyWindowInsetsListener() {
@Override
public android.view.WindowInsets onApplyWindowInsets(android.view.View v, android.view.WindowInsets insets) {
v.onApplyWindowInsets(insets);
fDisplayCutout = insets.consumeStableInsets().getDisplayCutout();
return insets;
}
} );
}
// Show/hide the statusbar.
if (mode == CoronaStatusBarSettings.HIDDEN) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
if (Build.VERSION.SDK_INT >= 28){
getWindow().getAttributes().layoutInDisplayCutoutMode
= WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}

} else if (mode == CoronaStatusBarSettings.DEFAULT
|| mode == CoronaStatusBarSettings.TRANSLUCENT
Expand Down Expand Up @@ -1179,6 +1193,10 @@ void setStatusBarMode(CoronaStatusBarSettings mode) {
CoronaStatusBarSettings getStatusBarMode() {
return myStatusBarMode;
}

public android.view.DisplayCutout getDisplayCutout(){
return fDisplayCutout;
}

int getStatusBarHeight() {

Expand Down Expand Up @@ -3456,7 +3474,7 @@ public void onRequestPermissionsResult(int requestCode, String permissions[], in
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

// Fetch the handler that was assigned the given request code.
CoronaActivity.OnRequestPermissionsResultHandler handler = null;
CoronaActivity.OnRequestPermissionsResultHandler handler;
handler = (CoronaActivity.OnRequestPermissionsResultHandler)fRequestPermissionsResultHandlers.get(Integer.valueOf(requestCode));

// Do not continue if the given request code is unknown.
Expand Down Expand Up @@ -3893,8 +3911,8 @@ private ApiLevel16() {}
* @param listener The listener reference to be removed. Can be null.
*/
public static void removeOnGlobalLayoutListener(
android.view.ViewTreeObserver viewTreeObserver,
android.view.ViewTreeObserver.OnGlobalLayoutListener listener)
ViewTreeObserver viewTreeObserver,
ViewTreeObserver.OnGlobalLayoutListener listener)
{
// Validate.
if ((viewTreeObserver == null) || (listener == null)) {
Expand All @@ -3903,7 +3921,9 @@ public static void removeOnGlobalLayoutListener(

// Remove the listener.
try {
viewTreeObserver.removeOnGlobalLayoutListener(listener);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
viewTreeObserver.removeOnGlobalLayoutListener(listener);
}
}
catch (Exception ex) {}
}
Expand Down
38 changes: 24 additions & 14 deletions platform/android/sdk/src/com/ansca/corona/NativeToJavaBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import android.location.Location;
import android.util.Base64;
import android.util.Log;
import android.view.DisplayCutout;

import dalvik.system.DexClassLoader;

Expand Down Expand Up @@ -1561,21 +1562,30 @@ protected static float[] callGetSafeAreaInsetPixels(CoronaRuntime runtime)
result[ 1 ] = result[ 2 ] = (float)Math.floor(contentWidth * 0.05f);
}
else {
result[ 0 ] = (statusBarMode != CoronaStatusBarSettings.HIDDEN) ? listener.getStatusBarHeight() : 0;
if (hasNavigationBar && runtime.getController().getSystemUiVisibility().contains("immersive"))
{
result[ 1 ] = result[ 2 ] = result[ 3 ] = 0;
} else {
int navBarIndex = 4;
if ((statusBarMode == CoronaStatusBarSettings.LIGHT_TRANSPARENT ||
statusBarMode == CoronaStatusBarSettings.DARK_TRANSPARENT) && hasNavigationBar){
WindowOrientation currentOrientation = WindowOrientation.fromCurrentWindowUsing(runtime.getController().getContext());
navBarIndex = (currentOrientation == WindowOrientation.PORTRAIT_UPRIGHT) ? 3 : 2;
}
for (int i = 1; i < 4; i++) {
result[ i ] = (i == navBarIndex) ? listener.getNavigationBarHeight() : 0;
}
DisplayCutout cutout = CoronaEnvironment.getCoronaActivity().getDisplayCutout();
if ((android.os.Build.VERSION.SDK_INT >= 26) && (cutout != null)){

result[0] = cutout.getSafeInsetTop();
result[1] = cutout.getSafeInsetLeft();
result[2] = cutout.getSafeInsetRight();
result[3] = cutout.getSafeInsetBottom();
}
else {
result[0] = (statusBarMode != CoronaStatusBarSettings.HIDDEN) ? listener.getStatusBarHeight() : 0;
if (hasNavigationBar && runtime.getController().getSystemUiVisibility().contains("immersive")) {
result[1] = result[2] = result[3] = 0;
} else {
int navBarIndex = 4;
if ((statusBarMode == CoronaStatusBarSettings.LIGHT_TRANSPARENT ||
statusBarMode == CoronaStatusBarSettings.DARK_TRANSPARENT) && hasNavigationBar) {
WindowOrientation currentOrientation = WindowOrientation.fromCurrentWindowUsing(runtime.getController().getContext());
navBarIndex = (currentOrientation == WindowOrientation.PORTRAIT_UPRIGHT) ? 3 : 2;
}
for (int i = 1; i < 4; i++) {
result[i] = (i == navBarIndex) ? listener.getNavigationBarHeight() : 0;
}
}
}
}
}
else {
Expand Down

0 comments on commit 64d8f70

Please sign in to comment.