Change our fullscreen wait logic to only wait if we need to. (thanks Rachel!)
authorSam Lantinga <slouken@libsdl.org>
Mon, 22 Oct 2018 14:55:47 -0700
changeset 1234809d04d83c3c1
parent 12347 94de324eb7da
child 12353 2cacb7bd6441
Change our fullscreen wait logic to only wait if we need to. (thanks Rachel!)
android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
     1.1 --- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Mon Oct 22 14:55:45 2018 -0700
     1.2 +++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Mon Oct 22 14:55:47 2018 -0700
     1.3 @@ -645,15 +645,54 @@
     1.4          msg.obj = data;
     1.5          boolean result = commandHandler.sendMessage(msg);
     1.6  
     1.7 -        // Ensure we don't return until the resize has actually happened,
     1.8 -        // or 250ms have passed.
     1.9 -        if (command == COMMAND_CHANGE_WINDOW_STYLE) {
    1.10 -            synchronized(SDLActivity.getContext()) {
    1.11 -                try {
    1.12 -                    SDLActivity.getContext().wait(250);
    1.13 +        if ((Build.VERSION.SDK_INT >= 19) && (command == COMMAND_CHANGE_WINDOW_STYLE)) {
    1.14 +            // Ensure we don't return until the resize has actually happened,
    1.15 +            // or 500ms have passed.
    1.16 +
    1.17 +            boolean bShouldWait = false;
    1.18 +            
    1.19 +            if (data instanceof Integer) {
    1.20 +                // Let's figure out if we're already laid out fullscreen or not.
    1.21 +                Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    1.22 +                android.util.DisplayMetrics realMetrics = new android.util.DisplayMetrics();
    1.23 +                display.getRealMetrics( realMetrics );
    1.24 +        
    1.25 +                boolean bFullscreenLayout = ((realMetrics.widthPixels == mSurface.getWidth()) && 
    1.26 +                                             (realMetrics.heightPixels == mSurface.getHeight()));
    1.27 +
    1.28 +                if (((Integer)data).intValue() == 1) {
    1.29 +                    // If we aren't laid out fullscreen or actively in fullscreen mode already, we're going
    1.30 +                    // to change size and should wait for surfaceChanged() before we return, so the size
    1.31 +                    // is right back in native code.  If we're already laid out fullscreen, though, we're
    1.32 +                    // not going to change size even if we change decor modes, so we shouldn't wait for
    1.33 +                    // surfaceChanged() -- which may not even happen -- and should return immediately.
    1.34 +                    bShouldWait = !bFullscreenLayout;
    1.35                  }
    1.36 -                catch (InterruptedException ie) {
    1.37 -                    ie.printStackTrace();
    1.38 +                else {
    1.39 +                    // If we're laid out fullscreen (even if the status bar and nav bar are present),
    1.40 +                    // or are actively in fullscreen, we're going to change size and should wait for
    1.41 +                    // surfaceChanged before we return, so the size is right back in native code.
    1.42 +                    bShouldWait = bFullscreenLayout;
    1.43 +                }
    1.44 +            }
    1.45 +
    1.46 +            if (bShouldWait) {
    1.47 +                // We'll wait for the surfaceChanged() method, which will notify us
    1.48 +                // when called.  That way, we know our current size is really the
    1.49 +                // size we need, instead of grabbing a size that's still got
    1.50 +                // the navigation and/or status bars before they're hidden.
    1.51 +                //
    1.52 +                // We'll wait for up to half a second, because some devices 
    1.53 +                // take a surprisingly long time for the surface resize, but
    1.54 +                // then we'll just give up and return.
    1.55 +                //
    1.56 +                synchronized(SDLActivity.getContext()) {
    1.57 +                    try {
    1.58 +                        SDLActivity.getContext().wait(500);
    1.59 +                    }
    1.60 +                    catch (InterruptedException ie) {
    1.61 +                        ie.printStackTrace();
    1.62 +                    }
    1.63                  }
    1.64              }
    1.65          }
    1.66 @@ -1595,6 +1634,7 @@
    1.67          catch ( java.lang.Throwable throwable ) {}
    1.68  
    1.69          synchronized(SDLActivity.getContext()) {
    1.70 +            // In case we're waiting on a size change after going fullscreen, send a notification.
    1.71              SDLActivity.getContext().notifyAll();
    1.72          }
    1.73