3939import android .window .OnBackInvokedCallback ;
4040import android .window .OnBackInvokedDispatcher ;
4141
42+ import androidx .annotation .NonNull ;
4243import androidx .annotation .RequiresPermission ;
4344import androidx .core .app .ActivityCompat ;
4445import androidx .core .content .ContextCompat ;
4546import androidx .core .content .FileProvider ;
47+ import androidx .core .graphics .Insets ;
48+ import androidx .core .view .OnApplyWindowInsetsListener ;
49+ import androidx .core .view .ViewCompat ;
50+ import androidx .core .view .WindowInsetsCompat ;
4651
4752import java .io .BufferedReader ;
4853import java .io .BufferedWriter ;
@@ -121,7 +126,7 @@ public class MainActivity extends NativeActivity {
121126 public static native boolean libraryMode ();
122127 public static native void onActivityPaused (boolean paused );
123128 public static native void onBack ();
124- public static native void onResize (int width , int height );
129+ public static native void onResize (int width , int height , int imeState );
125130 public static native void onUnicodeChar (int ch );
126131 public static native boolean optionSelected (int index );
127132 public static native void runFile (String fileName );
@@ -429,6 +434,10 @@ public int getWindowHeight() {
429434 return rect .height ();
430435 }
431436
437+ public boolean isInsetBasedOnResize () {
438+ return Build .VERSION .SDK_INT >= Build .VERSION_CODES .BAKLAVA ;
439+ }
440+
432441 public boolean isPredictiveBack () {
433442 return Build .VERSION .SDK_INT >= Build .VERSION_CODES .BAKLAVA ;
434443 }
@@ -452,10 +461,11 @@ public boolean loadModules() {
452461 @ Override
453462 public void onGlobalLayout () {
454463 super .onGlobalLayout ();
455- // find the visible coordinates of our view
456- Rect rect = new Rect ();
457- findViewById (android .R .id .content ).getWindowVisibleDisplayFrame (rect );
458- onResize (rect .width (), rect .height ());
464+ if (!isInsetBasedOnResize ()) {
465+ Rect rect = new Rect ();
466+ findViewById (android .R .id .content ).getWindowVisibleDisplayFrame (rect );
467+ onResize (rect .width (), rect .height (), 0 );
468+ }
459469 }
460470
461471 @ Override
@@ -774,6 +784,7 @@ protected void onCreate(Bundle savedInstanceState) {
774784 setImmersiveMode ();
775785 setupStorageEnvironment ();
776786 setupPredictiveBack ();
787+ setupInsetBaseOnResize ();
777788 if (!libraryMode ()) {
778789 processIntent ();
779790 processSettings ();
@@ -1063,14 +1074,42 @@ private void setImmersiveMode() {
10631074 }
10641075 }
10651076
1077+ /**
1078+ * onResize() handler for android 16+
1079+ */
1080+ private void setupInsetBaseOnResize () {
1081+ if (isInsetBasedOnResize ()) {
1082+ View view = getWindow ().getDecorView ();
1083+ ViewCompat .setOnApplyWindowInsetsListener (view , new OnApplyWindowInsetsListener () {
1084+ @ NonNull
1085+ @ Override
1086+ public WindowInsetsCompat onApplyWindowInsets (@ NonNull View view , @ NonNull WindowInsetsCompat insets ) {
1087+ Insets systemBars = insets .getInsets (WindowInsetsCompat .Type .systemBars ());
1088+ Insets navInsets = insets .getInsets (WindowInsetsCompat .Type .navigationBars ());
1089+ Insets ime = insets .getInsets (WindowInsetsCompat .Type .ime ());
1090+ boolean imeVisible = insets .isVisible (WindowInsetsCompat .Type .ime ());
1091+ int bottomInset = Math .max (systemBars .bottom , ime .bottom );
1092+ int width = view .getWidth () - navInsets .right ;
1093+ int height = view .getHeight () - (systemBars .top + bottomInset );
1094+ view .setPadding (0 , 0 , navInsets .right , navInsets .bottom );
1095+ if (width > 0 && height > 0 ) {
1096+ // ignore spurious transitional values
1097+ onResize (width , height , imeVisible ? 1 : -1 );
1098+ }
1099+ return insets ;
1100+ }
1101+ });
1102+ view .post (() -> ViewCompat .requestApplyInsets (view ));
1103+ }
1104+ }
1105+
10661106 //
10671107 // Hook into Predictive Back (Android 13+)
10681108 //
10691109 private void setupPredictiveBack () {
10701110 if (isPredictiveBack ()) {
10711111 getOnBackInvokedDispatcher ().registerOnBackInvokedCallback (
1072- OnBackInvokedDispatcher .PRIORITY_DEFAULT ,
1073- new OnBackInvokedCallback () {
1112+ OnBackInvokedDispatcher .PRIORITY_OVERLAY , new OnBackInvokedCallback () {
10741113 @ Override
10751114 public void onBackInvoked () {
10761115 Log .d (TAG , "onBackInvoked" );
@@ -1226,7 +1265,8 @@ protected Response getFile(String remoteHost, String path, boolean asset) throws
12261265 String name = "webui/" + path ;
12271266 long length = getFileLength (name );
12281267 log ("Opened " + name + " " + length + " bytes" );
1229- String contentType = path .endsWith ("js" ) ? "text/javascript" : "text/html" ;
1268+ String contentType = path .endsWith ("js" ) ? "text/javascript" :
1269+ path .endsWith ("css" ) ? "text/css" : "text/html" ;
12301270 result = new Response (getAssets ().open (name ), length , contentType );
12311271 if ("index.html" .equals (path ) && isHostNotPermitted (remoteHost )) {
12321272 requestHostPermission (remoteHost );
0 commit comments