3131import android .content .Intent ;
3232import android .content .res .AssetManager ;
3333import android .content .res .Configuration ;
34+ import android .graphics .Insets ;
3435import android .net .Uri ;
3536import android .os .Build ;
3637import android .os .Bundle ;
3940import android .text .method .LinkMovementMethod ;
4041import android .text .util .Linkify ;
4142import android .util .Log ;
43+ import android .view .KeyEvent ;
4244import android .view .MenuItem ;
4345import android .view .MotionEvent ;
44- import android .view .SurfaceView ;
4546import android .view .View ;
4647import android .view .ViewGroup ;
4748import android .view .WindowInsets ;
4849import android .widget .RelativeLayout ;
4950import android .widget .RelativeLayout .LayoutParams ;
5051import android .widget .TextView ;
52+ import android .window .OnBackInvokedDispatcher ;
5153
52- import androidx .activity .OnBackPressedCallback ;
5354import androidx .annotation .NonNull ;
5455import androidx .core .content .FileProvider ;
5556import androidx .core .view .GravityCompat ;
@@ -90,7 +91,8 @@ public class EasyRpgPlayerActivity extends SDLActivity implements NavigationView
9091 DrawerLayout drawer ;
9192 InputLayout inputLayout ;
9293 private boolean uiVisible = true ;
93- SurfaceView surface ;
94+
95+ private int topInset = 0 ;
9496
9597 @ Override
9698 protected String [] getLibraries () {
@@ -114,6 +116,9 @@ protected void onCreate(Bundle savedInstanceState) {
114116
115117 SettingsManager .init (getApplicationContext ());
116118
119+ // Load our custom activity layout
120+ setContentView (R .layout .activity_player );
121+
117122 // Menu configuration
118123 this .drawer = findViewById (R .id .drawer_layout );
119124 drawer .setDrawerLockMode (DrawerLayout .LOCK_MODE_LOCKED_CLOSED );
@@ -150,26 +155,24 @@ public void onDrawerStateChanged(int newState) {
150155 this .setOrientationBis (0 , 0 , false , "LandscapeRight|LandscapeLeft" );
151156 }
152157
153- // Hardware acceleration
154- try {
155- // Api > 11: FLAG_HARDWARE_ACCELERATED
156- // TODO : Is it still useful?
157- getWindow ().setFlags (0x01000000 , 0x01000000 );
158- } catch (Exception e ) {
159- e .printStackTrace ();
160- }
161-
162158 // Put the gameScreen
163- surface = mSurface ;
164159 mLayout = findViewById (R .id .main_layout );
165- mLayout .addView (surface );
160+
161+ ViewGroup parent = (ViewGroup ) mSurface .getParent ();
162+ if (parent != null ) {
163+ parent .removeView (mSurface );
164+ }
165+ mLayout .addView (mSurface );
166+
166167 updateScreenPosition ();
167168
168169 if (Build .VERSION .SDK_INT >= 35 ) {
169170 getWindow ().getDecorView ().setOnApplyWindowInsetsListener (new View .OnApplyWindowInsetsListener () {
170171 @ NonNull
171172 @ Override
172173 public WindowInsets onApplyWindowInsets (@ NonNull View view , @ NonNull WindowInsets insets ) {
174+ Insets systemInsets = insets .getInsetsIgnoringVisibility (WindowInsets .Type .systemBars ());
175+ topInset = systemInsets .top ;
173176 showInputLayout ();
174177 return insets ;
175178 }
@@ -178,13 +181,14 @@ public WindowInsets onApplyWindowInsets(@NonNull View view, @NonNull WindowInset
178181 showInputLayout ();
179182 }
180183
181- OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback (true ) {
182- @ Override
183- public void handleOnBackPressed () {
184- backPressed ();
185- }
186- };
187- getOnBackPressedDispatcher ().addCallback (this , onBackPressedCallback );
184+ if (Build .VERSION .SDK_INT >= 33 ) {
185+ getOnBackInvokedDispatcher ().registerOnBackInvokedCallback (
186+ OnBackInvokedDispatcher .PRIORITY_DEFAULT ,
187+ () -> {
188+ backPressed ();
189+ }
190+ );
191+ }
188192 }
189193
190194 @ Override
@@ -333,6 +337,22 @@ public void backPressed() {
333337 openOrCloseMenu ();
334338 }
335339
340+ @ Override
341+ public boolean dispatchKeyEvent (KeyEvent event ) {
342+ if (Build .VERSION .SDK_INT >= 33 ) {
343+ // Uses OnBackInvokedDispatcher
344+ return super .dispatchKeyEvent (event );
345+ }
346+
347+ // Handle back button on older Androids
348+ if (event .getKeyCode () == KeyEvent .KEYCODE_BACK ) {
349+ backPressed ();
350+ return false ;
351+ }
352+
353+ return super .dispatchKeyEvent (event );
354+ }
355+
336356 @ Override
337357 public void setOrientationBis (int w , int h , boolean resizable , String hint ) {
338358 // Filter orientation events generated by SDL (hint is empty)
@@ -468,11 +488,11 @@ public void updateScreenPosition() {
468488
469489 // Determine the multiplier
470490 int screenWidth = getWindowManager ().getDefaultDisplay ().getWidth ();
471- params .topMargin = 0 ;
491+ params .topMargin = topInset ;
472492 params .leftMargin = 0 ;
473493 params .width = screenWidth ;
474494 params .height = (int )(screenWidth * 0.75 );
475- surface .setLayoutParams (params );
495+ mSurface .setLayoutParams (params );
476496 }
477497
478498 /**
0 commit comments