android: refactor display cutouts

This commit is contained in:
ardocrat
2023-05-31 21:38:27 +03:00
parent 1a7de809c5
commit c3ce297373
2 changed files with 73 additions and 66 deletions
@@ -1,11 +1,12 @@
package mw.gri.android;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Process;
import android.system.ErrnoException;
import android.system.Os;
import android.util.Log;
import android.view.KeyEvent;
import android.view.OrientationEventListener;
import com.google.androidgamesdk.GameActivity;
public class MainActivity extends GameActivity {
@@ -22,31 +23,23 @@ public class MainActivity extends GameActivity {
throw new RuntimeException(e);
}
super.onCreate(savedInstanceState);
OrientationEventListener orientationEventListener = new OrientationEventListener(this,
SensorManager.SENSOR_DELAY_GAME) {
@Override
public void onOrientationChanged(int orientation) {
onDisplayCutoutsChanged(Utils.getDisplayCutouts(MainActivity.this));
}
};
if (orientationEventListener.canDetectOrientation()) {
orientationEventListener.enable();
}
onDisplayCutoutsChanged(Utils.getDisplayCutouts(MainActivity.this));
BackgroundService.start(getApplicationContext());
}
@Override
protected void onDestroy() {
if (!mManualExit) {
BackgroundService.stop(getApplicationContext());
// Temporary fix to prevent app hanging when closed from recent apps
Process.killProcess(Process.myPid());
}
super.onDestroy();
}
public int[] getDisplayCutouts() {
return Utils.getDisplayCutouts(this);
}
private boolean mManualExit = false;
// Called from native code
public void onExit() {
mManualExit = true;
BackgroundService.stop(getApplicationContext());
finish();
}
native void onDisplayCutoutsChanged(int[] cutouts);
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
@@ -58,4 +51,24 @@ public class MainActivity extends GameActivity {
}
public native void onBackButtonPress();
@Override
protected void onDestroy() {
if (!mManualExit) {
BackgroundService.stop(getApplicationContext());
// Temporary fix to prevent app hanging when closed from recent apps
Process.killProcess(Process.myPid());
}
super.onDestroy();
}
private boolean mManualExit = false;
// Called from native code
public void onExit() {
mManualExit = true;
BackgroundService.stop(getApplicationContext());
finish();
}
}