Fixed bug 3235 - Make the Android window creation similar to iOS' window creation
authorSam Lantinga <slouken@libsdl.org>
Sun, 13 Aug 2017 20:55:59 -0700
changeset 1127031ce9a8ce2bd
parent 11269 6f981ff3dc5f
child 11271 ab3f2402a777
Fixed bug 3235 - Make the Android window creation similar to iOS' window creation

Sylvain

Here's a patch.
It tries to get the hint first. Resizable will allow any orientation. Otherwise it uses width/height window.

setOrientation method is splitted in static and non-static, so that it can be overloaded in a user subclass.

Some artefact observed :
surfaceChanged() can be called twice at the beginning. When the phone starts in portrait and run a landscape application.
android-project/src/org/libsdl/app/SDLActivity.java
src/core/android/SDL_android.c
src/core/android/SDL_android.h
src/video/android/SDL_androidwindow.c
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Sun Aug 13 20:51:08 2017 -0700
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Sun Aug 13 20:55:59 2017 -0700
     1.3 @@ -515,6 +515,61 @@
     1.4  
     1.5      /**
     1.6       * This method is called by SDL using JNI.
     1.7 +     * This is a static method for JNI convenience, it calls a non-static method
     1.8 +     * so that is can be overridden  
     1.9 +     */
    1.10 +    public static void setOrientation(int w, int h, boolean resizable, String hint)
    1.11 +    {
    1.12 +      mSingleton.setOrientationBis(w, h, resizable, hint);
    1.13 +      return;
    1.14 +    }
    1.15 +   
    1.16 +    /**
    1.17 +     * This can be overridden
    1.18 +     */
    1.19 +    public void setOrientationBis(int w, int h, boolean resizable, String hint) 
    1.20 +    {
    1.21 +      int orientation = -1;
    1.22 +
    1.23 +      if (hint != "") {
    1.24 +         if (hint.contains("LandscapeRight") && hint.contains("LandscapeLeft")) {
    1.25 +            orientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;
    1.26 +         } else if (hint.contains("LandscapeRight")) {
    1.27 +            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    1.28 +         } else if (hint.contains("LandscapeLeft")) {
    1.29 +            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
    1.30 +         } else if (hint.contains("Portrait") && hint.contains("PortraitUpsideDown")) {
    1.31 +            orientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;
    1.32 +         } else if (hint.contains("Portrait")) {
    1.33 +            orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    1.34 +         } else if (hint.contains("PortraitUpsideDown")) {
    1.35 +            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
    1.36 +         }
    1.37 +      }
    1.38 +
    1.39 +      /* no valid hint */
    1.40 +      if (orientation == -1) {
    1.41 +         if (resizable) {
    1.42 +            /* no fixed orientation */
    1.43 +         } else {
    1.44 +            if (w > h) {
    1.45 +               orientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;
    1.46 +            } else {
    1.47 +               orientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;
    1.48 +            }
    1.49 +         }
    1.50 +      }
    1.51 +
    1.52 +      Log.v("SDL", "setOrientation() orientation=" + orientation + " width=" + w +" height="+ h +" resizable=" + resizable + " hint=" + hint);
    1.53 +      if (orientation != -1) {
    1.54 +         mSingleton.setRequestedOrientation(orientation);
    1.55 +      }
    1.56 + 
    1.57 +      return;
    1.58 +    }
    1.59 +
    1.60 +    /**
    1.61 +     * This method is called by SDL using JNI.
    1.62       */
    1.63      public static boolean sendMessage(int command, int param) {
    1.64          return mSingleton.sendCommand(command, Integer.valueOf(param));
     2.1 --- a/src/core/android/SDL_android.c	Sun Aug 13 20:51:08 2017 -0700
     2.2 +++ b/src/core/android/SDL_android.c	Sun Aug 13 20:55:59 2017 -0700
     2.3 @@ -636,6 +636,18 @@
     2.4      }
     2.5  }
     2.6  
     2.7 +void Android_JNI_SetOrientation(int w, int h, int resizable, const char *hint)
     2.8 +{
     2.9 +    jmethodID mid;
    2.10 +    JNIEnv *mEnv = Android_JNI_GetEnv();
    2.11 +    mid = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,"setOrientation","(IIZLjava/lang/String;)V");
    2.12 +    if (mid) {
    2.13 +        jstring jhint = (jstring)((*mEnv)->NewStringUTF(mEnv, (hint ? hint : "")));
    2.14 +        (*mEnv)->CallStaticVoidMethod(mEnv, mActivityClass, mid, w, h, (resizable? 1 : 0), jhint);
    2.15 +        (*mEnv)->DeleteLocalRef(mEnv, jhint);
    2.16 +    }
    2.17 +}
    2.18 +
    2.19  SDL_bool Android_JNI_GetAccelerometerValues(float values[3])
    2.20  {
    2.21      int i;
     3.1 --- a/src/core/android/SDL_android.h	Sun Aug 13 20:51:08 2017 -0700
     3.2 +++ b/src/core/android/SDL_android.h	Sun Aug 13 20:55:59 2017 -0700
     3.3 @@ -34,6 +34,7 @@
     3.4  
     3.5  /* Interface from the SDL library into the Android Java activity */
     3.6  extern void Android_JNI_SetActivityTitle(const char *title);
     3.7 +extern void Android_JNI_SetOrientation(int w, int h, int resizable, const char *hint);
     3.8  extern SDL_bool Android_JNI_GetAccelerometerValues(float values[3]);
     3.9  extern void Android_JNI_ShowTextInput(SDL_Rect *inputRect);
    3.10  extern void Android_JNI_HideTextInput(void);
     4.1 --- a/src/video/android/SDL_androidwindow.c	Sun Aug 13 20:51:08 2017 -0700
     4.2 +++ b/src/video/android/SDL_androidwindow.c	Sun Aug 13 20:55:59 2017 -0700
     4.3 @@ -29,6 +29,7 @@
     4.4  
     4.5  #include "SDL_androidvideo.h"
     4.6  #include "SDL_androidwindow.h"
     4.7 +#include "SDL_hints.h"
     4.8  
     4.9  int
    4.10  Android_CreateWindow(_THIS, SDL_Window * window)
    4.11 @@ -42,6 +43,9 @@
    4.12      Android_PauseSem = SDL_CreateSemaphore(0);
    4.13      Android_ResumeSem = SDL_CreateSemaphore(0);
    4.14  
    4.15 +    /* Set orientation */
    4.16 +    Android_JNI_SetOrientation(window->w, window->h, window->flags & SDL_WINDOW_RESIZABLE, SDL_GetHint(SDL_HINT_ORIENTATIONS));
    4.17 +
    4.18      /* Adjust the window data to match the screen */
    4.19      window->x = 0;
    4.20      window->y = 0;