mirror of
https://code.gri.mw/GUI/grim.git
synced 2026-07-17 12:19:09 +00:00
android + ui: display cutouts (insets) refactoring, optimize platform-specific app, reorganize views
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
package mw.gri.android;
|
||||
|
||||
import android.content.*;
|
||||
import android.content.pm.PackageManager;
|
||||
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 android.view.View;
|
||||
import androidx.core.graphics.Insets;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.core.view.WindowInsetsCompat;
|
||||
import com.google.androidgamesdk.GameActivity;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
@@ -45,46 +45,40 @@ public class MainActivity extends GameActivity {
|
||||
}
|
||||
super.onCreate(null);
|
||||
|
||||
// Callback to update display cutouts at native code.
|
||||
OrientationEventListener orientationEventListener = new OrientationEventListener(this,
|
||||
SensorManager.SENSOR_DELAY_NORMAL) {
|
||||
@Override
|
||||
public void onOrientationChanged(int orientation) {
|
||||
onDisplayCutoutsChanged(Utils.getDisplayCutouts(MainActivity.this));
|
||||
}
|
||||
};
|
||||
if (orientationEventListener.canDetectOrientation()) {
|
||||
orientationEventListener.enable();
|
||||
}
|
||||
|
||||
// Register receiver to finish activity from the BackgroundService.
|
||||
registerReceiver(mBroadcastReceiver, new IntentFilter(FINISH_ACTIVITY_ACTION));
|
||||
|
||||
// Start notification service.
|
||||
BackgroundService.start(this);
|
||||
|
||||
// Listener for display cutouts to pass values into native code.
|
||||
View content = getWindow().getDecorView().findViewById(android.R.id.content);
|
||||
ViewCompat.setOnApplyWindowInsetsListener(content, (v, insets) -> {
|
||||
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
|
||||
int[] cutouts = new int[]{0, 0, 0, 0};
|
||||
cutouts[0] = Utils.pxToDp(systemBars.top, this);
|
||||
cutouts[1] = Utils.pxToDp(systemBars.right, this);
|
||||
cutouts[2] = Utils.pxToDp(systemBars.bottom, this);
|
||||
cutouts[3] = Utils.pxToDp(systemBars.left, this);
|
||||
onDisplayCutouts(cutouts);
|
||||
return insets;
|
||||
});
|
||||
}
|
||||
|
||||
// Implemented into native code to handle display cutouts change.
|
||||
native void onDisplayCutoutsChanged(int[] cutouts);
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
// Update display cutouts.
|
||||
onDisplayCutoutsChanged(Utils.getDisplayCutouts(this));
|
||||
}
|
||||
native void onDisplayCutouts(int[] cutouts);
|
||||
|
||||
@Override
|
||||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||||
if (keyCode == KeyEvent.KEYCODE_BACK) {
|
||||
onBackButtonPress();
|
||||
onBack();
|
||||
return true;
|
||||
}
|
||||
return super.onKeyDown(keyCode, event);
|
||||
}
|
||||
|
||||
// Implemented into native code to handle back button press.
|
||||
public native void onBackButtonPress();
|
||||
// Implemented into native code to handle key code BACK event.
|
||||
public native void onBack();
|
||||
|
||||
private boolean mManualExit;
|
||||
private final AtomicBoolean mActivityDestroyed = new AtomicBoolean(false);
|
||||
|
||||
@@ -1,52 +1,10 @@
|
||||
package mw.gri.android;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.view.DisplayCutout;
|
||||
import android.view.WindowInsets;
|
||||
import android.view.WindowManager;
|
||||
import androidx.core.graphics.Insets;
|
||||
import androidx.core.view.DisplayCutoutCompat;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.core.view.WindowInsetsCompat;
|
||||
|
||||
public class Utils {
|
||||
|
||||
public static int[] getDisplayCutouts(Activity context) {
|
||||
int[] cutouts = new int[]{0, 0, 0, 0};
|
||||
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
|
||||
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||
WindowInsets insets = windowManager.getCurrentWindowMetrics().getWindowInsets();
|
||||
android.graphics.Insets barsInsets = insets.getInsets(WindowInsets.Type.systemBars());
|
||||
android.graphics.Insets cutoutsInsets = insets.getInsets(WindowInsets.Type.displayCutout());
|
||||
cutouts[0] = pxToDp(Integer.max(barsInsets.top, cutoutsInsets.top), context);
|
||||
cutouts[1] = pxToDp(Integer.max(barsInsets.right, cutoutsInsets.right), context);
|
||||
cutouts[2] = pxToDp(Integer.max(barsInsets.bottom, cutoutsInsets.bottom), context);
|
||||
cutouts[3] = pxToDp(Integer.max(barsInsets.left, cutoutsInsets.left), context);
|
||||
} else if (Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.Q) {
|
||||
DisplayCutout displayCutout = context.getWindowManager().getDefaultDisplay().getCutout();
|
||||
cutouts[0] = displayCutout.getSafeInsetBottom();
|
||||
cutouts[1] = displayCutout.getSafeInsetRight();
|
||||
cutouts[2] = displayCutout.getSafeInsetBottom();
|
||||
cutouts[3] = displayCutout.getSafeInsetLeft();
|
||||
} else {
|
||||
WindowInsetsCompat insets = ViewCompat.getRootWindowInsets(context.getWindow().getDecorView());
|
||||
if (insets != null) {
|
||||
DisplayCutoutCompat displayCutout = insets.getDisplayCutout();
|
||||
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
|
||||
if (displayCutout != null) {
|
||||
cutouts[0] = pxToDp(Integer.max(displayCutout.getSafeInsetTop(), systemBars.top), context);
|
||||
cutouts[1] = pxToDp(Integer.max(displayCutout.getSafeInsetRight(), systemBars.right), context);
|
||||
cutouts[2] = pxToDp(Integer.max(displayCutout.getSafeInsetBottom(), systemBars.bottom), context);
|
||||
cutouts[3] = pxToDp(Integer.max(displayCutout.getSafeInsetLeft(), systemBars.left), context);
|
||||
}
|
||||
}
|
||||
}
|
||||
return cutouts;
|
||||
}
|
||||
|
||||
private static int pxToDp(int px, Context context) {
|
||||
// Convert Pixels to DensityPixels
|
||||
public static int pxToDp(int px, Context context) {
|
||||
return (int) (px / context.getResources().getDisplayMetrics().density);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user