On Android show the system UI when an SDL window is windowed, hide the system UI when it's fullscreen, like we do on iOS.
authorSam Lantinga <slouken@libsdl.org>
Sun, 11 Feb 2018 18:23:37 -0800
changeset 11865af91d564aa40
parent 11864 2eff89e82b60
child 11866 609bef491213
On Android show the system UI when an SDL window is windowed, hide the system UI when it's fullscreen, like we do on iOS.
We're increasing the Android SDK minimum version to API 19, this doesn't increase the minimum target API, which is API 14.
android-project/app/build.gradle
android-project/app/jni/Application.mk
android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
docs/README-android.md
src/core/android/SDL_android.c
src/core/android/SDL_android.h
src/video/android/SDL_androidvideo.c
src/video/android/SDL_androidwindow.c
src/video/android/SDL_androidwindow.h
     1.1 --- a/android-project/app/build.gradle	Sun Feb 11 17:25:51 2018 -0800
     1.2 +++ b/android-project/app/build.gradle	Sun Feb 11 18:23:37 2018 -0800
     1.3 @@ -8,14 +8,14 @@
     1.4  }
     1.5  
     1.6  android {
     1.7 -    compileSdkVersion 16
     1.8 +    compileSdkVersion 19
     1.9      buildToolsVersion "26.0.1"
    1.10      defaultConfig {
    1.11          if (buildAsApplication) {
    1.12              applicationId "org.libsdl.app"
    1.13          }
    1.14          minSdkVersion 14
    1.15 -        targetSdkVersion 16
    1.16 +        targetSdkVersion 19
    1.17          versionCode 1
    1.18          versionName "1.0"
    1.19          externalNativeBuild {
    1.20 @@ -65,4 +65,4 @@
    1.21          exclude group: 'com.android.support', module: 'support-annotations'
    1.22      })
    1.23      testCompile 'junit:junit:4.12'
    1.24 -}
    1.25 \ No newline at end of file
    1.26 +}
     2.1 --- a/android-project/app/jni/Application.mk	Sun Feb 11 17:25:51 2018 -0800
     2.2 +++ b/android-project/app/jni/Application.mk	Sun Feb 11 18:23:37 2018 -0800
     2.3 @@ -5,5 +5,5 @@
     2.4  
     2.5  APP_ABI := armeabi-v7a arm64-v8a x86 x86_64
     2.6  
     2.7 -# Min SDK level
     2.8 +# Min runtime API level
     2.9  APP_PLATFORM=android-14
     3.1 --- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Sun Feb 11 17:25:51 2018 -0800
     3.2 +++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Sun Feb 11 18:23:37 2018 -0800
     3.3 @@ -213,19 +213,7 @@
     3.4  
     3.5          setContentView(mLayout);
     3.6  
     3.7 -        /* 
     3.8 -         * Per SDL_androidwindow.c, Android will only ever have one window, and that window 
     3.9 -         * is always flagged SDL_WINDOW_FULLSCREEN.  Let's treat it as an immersive fullscreen 
    3.10 -         * window for Android UI purposes, as a result.
    3.11 -         */
    3.12 -        int iFlags = 
    3.13 -            //View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | // Only available since API 19
    3.14 -            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
    3.15 -            View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
    3.16 -            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
    3.17 -            View.SYSTEM_UI_FLAG_FULLSCREEN;
    3.18 -
    3.19 -        getWindow().getDecorView().setSystemUiVisibility(iFlags);        
    3.20 +        setWindowStyle(false);
    3.21  
    3.22          // Get filename from "Open with" of another application
    3.23          Intent intent = getIntent();
    3.24 @@ -408,7 +396,7 @@
    3.25  
    3.26      // Messages from the SDLMain thread
    3.27      static final int COMMAND_CHANGE_TITLE = 1;
    3.28 -    static final int COMMAND_UNUSED = 2;
    3.29 +    static final int COMMAND_CHANGE_WINDOW_STYLE = 2;
    3.30      static final int COMMAND_TEXTEDIT_HIDE = 3;
    3.31      static final int COMMAND_SET_KEEP_SCREEN_ON = 5;
    3.32  
    3.33 @@ -447,6 +435,29 @@
    3.34                      Log.e(TAG, "error handling message, getContext() returned no Activity");
    3.35                  }
    3.36                  break;
    3.37 +            case COMMAND_CHANGE_WINDOW_STYLE:
    3.38 +                if (context instanceof Activity) {
    3.39 +                    Window window = ((Activity) context).getWindow();
    3.40 +                    if (window != null) {
    3.41 +                        if ((msg.obj instanceof Integer) && (((Integer) msg.obj).intValue() != 0)) {
    3.42 +                            int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
    3.43 +                                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
    3.44 +                                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
    3.45 +                                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
    3.46 +                                        View.SYSTEM_UI_FLAG_FULLSCREEN |
    3.47 +                                        View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
    3.48 +                            window.getDecorView().setSystemUiVisibility(flags);        
    3.49 +                            window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    3.50 +                        } else {
    3.51 +                            int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
    3.52 +                            window.getDecorView().setSystemUiVisibility(flags);        
    3.53 +                            window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    3.54 +                        }
    3.55 +                    }
    3.56 +                } else {
    3.57 +                    Log.e(TAG, "error handling message, getContext() returned no Activity");
    3.58 +                }
    3.59 +                break;
    3.60              case COMMAND_TEXTEDIT_HIDE:
    3.61                  if (mTextEdit != null) {
    3.62                      // Note: On some devices setting view to GONE creates a flicker in landscape.
    3.63 @@ -526,6 +537,14 @@
    3.64  
    3.65      /**
    3.66       * This method is called by SDL using JNI.
    3.67 +     */
    3.68 +    public static void setWindowStyle(boolean fullscreen) {
    3.69 +        // Called from SDLMain() thread and can't directly affect the view
    3.70 +        mSingleton.sendCommand(COMMAND_CHANGE_WINDOW_STYLE, fullscreen ? 1 : 0);
    3.71 +    }
    3.72 +
    3.73 +    /**
    3.74 +     * This method is called by SDL using JNI.
    3.75       * This is a static method for JNI convenience, it calls a non-static method
    3.76       * so that is can be overridden  
    3.77       */
    3.78 @@ -637,7 +656,7 @@
    3.79              if (bundle == null) {
    3.80                  return false;
    3.81              }
    3.82 -			String prefix = "SDL_ENV.";
    3.83 +            String prefix = "SDL_ENV.";
    3.84              final int trimLength = prefix.length();
    3.85              for (String key : bundle.keySet()) {
    3.86                  if (key.startsWith(prefix)) {
     4.1 --- a/docs/README-android.md	Sun Feb 11 17:25:51 2018 -0800
     4.2 +++ b/docs/README-android.md	Sun Feb 11 18:23:37 2018 -0800
     4.3 @@ -14,7 +14,7 @@
     4.4   Requirements
     4.5  ================================================================================
     4.6  
     4.7 -Android SDK (version 16 or later)
     4.8 +Android SDK (version 19 or later)
     4.9  https://developer.android.com/sdk/index.html
    4.10  
    4.11  Android NDK r10e or later
     5.1 --- a/src/core/android/SDL_android.c	Sun Feb 11 17:25:51 2018 -0800
     5.2 +++ b/src/core/android/SDL_android.c	Sun Feb 11 18:23:37 2018 -0800
     5.3 @@ -209,6 +209,7 @@
     5.4  /* method signatures */
     5.5  static jmethodID midGetNativeSurface;
     5.6  static jmethodID midSetActivityTitle;
     5.7 +static jmethodID midSetWindowStyle;
     5.8  static jmethodID midSetOrientation;
     5.9  static jmethodID midGetContext;
    5.10  static jmethodID midIsAndroidTV;
    5.11 @@ -302,6 +303,8 @@
    5.12                                  "getNativeSurface","()Landroid/view/Surface;");
    5.13      midSetActivityTitle = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    5.14                                  "setActivityTitle","(Ljava/lang/String;)Z");
    5.15 +    midSetWindowStyle = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    5.16 +                                "setWindowStyle","(Z)V");
    5.17      midSetOrientation = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    5.18                                  "setOrientation","(IIZLjava/lang/String;)V");
    5.19      midGetContext = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    5.20 @@ -332,7 +335,7 @@
    5.21      midGetDisplayDPI = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "getDisplayDPI", "()Landroid/util/DisplayMetrics;");
    5.22  
    5.23      if (!midGetNativeSurface ||
    5.24 -       !midSetActivityTitle || !midSetOrientation || !midGetContext || !midIsAndroidTV || !midInputGetInputDeviceIds ||
    5.25 +       !midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsAndroidTV || !midInputGetInputDeviceIds ||
    5.26         !midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown ||
    5.27         !midClipboardSetText || !midClipboardGetText || !midClipboardHasText ||
    5.28         !midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariables|| !midGetDisplayDPI) {
    5.29 @@ -907,6 +910,12 @@
    5.30      (*mEnv)->DeleteLocalRef(mEnv, jtitle);
    5.31  }
    5.32  
    5.33 +void Android_JNI_SetWindowStyle(SDL_bool fullscreen)
    5.34 +{
    5.35 +    JNIEnv *mEnv = Android_JNI_GetEnv();
    5.36 +    (*mEnv)->CallStaticVoidMethod(mEnv, mActivityClass, midSetWindowStyle, fullscreen ? 1 : 0);
    5.37 +}
    5.38 +
    5.39  void Android_JNI_SetOrientation(int w, int h, int resizable, const char *hint)
    5.40  {
    5.41      JNIEnv *mEnv = Android_JNI_GetEnv();
     6.1 --- a/src/core/android/SDL_android.h	Sun Feb 11 17:25:51 2018 -0800
     6.2 +++ b/src/core/android/SDL_android.h	Sun Feb 11 18:23:37 2018 -0800
     6.3 @@ -34,6 +34,7 @@
     6.4  
     6.5  /* Interface from the SDL library into the Android Java activity */
     6.6  extern void Android_JNI_SetActivityTitle(const char *title);
     6.7 +extern void Android_JNI_SetWindowStyle(SDL_bool fullscreen);
     6.8  extern void Android_JNI_SetOrientation(int w, int h, int resizable, const char *hint);
     6.9  
    6.10  extern SDL_bool Android_JNI_GetAccelerometerValues(float values[3]);
     7.1 --- a/src/video/android/SDL_androidvideo.c	Sun Feb 11 17:25:51 2018 -0800
     7.2 +++ b/src/video/android/SDL_androidvideo.c	Sun Feb 11 18:23:37 2018 -0800
     7.3 @@ -120,6 +120,7 @@
     7.4  
     7.5      device->CreateSDLWindow = Android_CreateWindow;
     7.6      device->SetWindowTitle = Android_SetWindowTitle;
     7.7 +    device->SetWindowFullscreen = Android_SetWindowFullscreen;
     7.8      device->DestroyWindow = Android_DestroyWindow;
     7.9      device->GetWindowWMInfo = Android_GetWindowWMInfo;
    7.10  
    7.11 @@ -220,7 +221,7 @@
    7.12      /*
    7.13        Update the resolution of the desktop mode, so that the window
    7.14        can be properly resized. The screen resolution change can for
    7.15 -      example happen when the Activity enters or exists immersive mode,
    7.16 +      example happen when the Activity enters or exits immersive mode,
    7.17        which can happen after VideoInit().
    7.18      */
    7.19      device = SDL_GetVideoDevice();
    7.20 @@ -234,16 +235,17 @@
    7.21      }
    7.22  
    7.23      if (Android_Window) {
    7.24 -        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height);
    7.25 -
    7.26          /* Force the current mode to match the resize otherwise the SDL_WINDOWEVENT_RESTORED event
    7.27           * will fall back to the old mode */
    7.28          display = SDL_GetDisplayForWindow(Android_Window);
    7.29  
    7.30 -        display->current_mode.format = format;
    7.31 -        display->current_mode.w = width;
    7.32 -        display->current_mode.h = height;
    7.33 -        display->current_mode.refresh_rate = rate;
    7.34 +        display->display_modes[0].format = format;
    7.35 +        display->display_modes[0].w = width;
    7.36 +        display->display_modes[0].h = height;
    7.37 +        display->display_modes[0].refresh_rate = rate;
    7.38 +        display->current_mode = display->display_modes[0];
    7.39 +
    7.40 +        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height);
    7.41      }
    7.42  }
    7.43  
     8.1 --- a/src/video/android/SDL_androidwindow.c	Sun Feb 11 17:25:51 2018 -0800
     8.2 +++ b/src/video/android/SDL_androidwindow.c	Sun Feb 11 18:23:37 2018 -0800
     8.3 @@ -53,7 +53,6 @@
     8.4      window->h = Android_ScreenHeight;
     8.5  
     8.6      window->flags &= ~SDL_WINDOW_RESIZABLE;     /* window is NEVER resizeable */
     8.7 -    window->flags |= SDL_WINDOW_FULLSCREEN;     /* window is always fullscreen */
     8.8      window->flags &= ~SDL_WINDOW_HIDDEN;
     8.9      window->flags |= SDL_WINDOW_SHOWN;          /* only one window on Android */
    8.10      window->flags |= SDL_WINDOW_INPUT_FOCUS;    /* always has input focus */
    8.11 @@ -99,6 +98,12 @@
    8.12  }
    8.13  
    8.14  void
    8.15 +Android_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
    8.16 +{
    8.17 +    Android_JNI_SetWindowStyle(fullscreen);
    8.18 +}
    8.19 +
    8.20 +void
    8.21  Android_DestroyWindow(_THIS, SDL_Window * window)
    8.22  {
    8.23      SDL_WindowData *data;
     9.1 --- a/src/video/android/SDL_androidwindow.h	Sun Feb 11 17:25:51 2018 -0800
     9.2 +++ b/src/video/android/SDL_androidwindow.h	Sun Feb 11 18:23:37 2018 -0800
     9.3 @@ -28,6 +28,7 @@
     9.4  
     9.5  extern int Android_CreateWindow(_THIS, SDL_Window * window);
     9.6  extern void Android_SetWindowTitle(_THIS, SDL_Window * window);
     9.7 +extern void Android_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen);
     9.8  extern void Android_DestroyWindow(_THIS, SDL_Window * window);
     9.9  extern SDL_bool Android_GetWindowWMInfo(_THIS, SDL_Window * window, struct SDL_SysWMinfo * info);
    9.10