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.
This commit is contained in:
Sam Lantinga 2018-02-11 18:23:37 -08:00
parent 66f9492b69
commit 6e01fbb763
9 changed files with 66 additions and 29 deletions

View File

@ -8,14 +8,14 @@ else {
} }
android { android {
compileSdkVersion 16 compileSdkVersion 19
buildToolsVersion "26.0.1" buildToolsVersion "26.0.1"
defaultConfig { defaultConfig {
if (buildAsApplication) { if (buildAsApplication) {
applicationId "org.libsdl.app" applicationId "org.libsdl.app"
} }
minSdkVersion 14 minSdkVersion 14
targetSdkVersion 16 targetSdkVersion 19
versionCode 1 versionCode 1
versionName "1.0" versionName "1.0"
externalNativeBuild { externalNativeBuild {

View File

@ -5,5 +5,5 @@
APP_ABI := armeabi-v7a arm64-v8a x86 x86_64 APP_ABI := armeabi-v7a arm64-v8a x86 x86_64
# Min SDK level # Min runtime API level
APP_PLATFORM=android-14 APP_PLATFORM=android-14

View File

@ -213,19 +213,7 @@ public class SDLActivity extends Activity {
setContentView(mLayout); setContentView(mLayout);
/* setWindowStyle(false);
* Per SDL_androidwindow.c, Android will only ever have one window, and that window
* is always flagged SDL_WINDOW_FULLSCREEN. Let's treat it as an immersive fullscreen
* window for Android UI purposes, as a result.
*/
int iFlags =
//View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | // Only available since API 19
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_FULLSCREEN;
getWindow().getDecorView().setSystemUiVisibility(iFlags);
// Get filename from "Open with" of another application // Get filename from "Open with" of another application
Intent intent = getIntent(); Intent intent = getIntent();
@ -408,7 +396,7 @@ public class SDLActivity extends Activity {
// Messages from the SDLMain thread // Messages from the SDLMain thread
static final int COMMAND_CHANGE_TITLE = 1; static final int COMMAND_CHANGE_TITLE = 1;
static final int COMMAND_UNUSED = 2; static final int COMMAND_CHANGE_WINDOW_STYLE = 2;
static final int COMMAND_TEXTEDIT_HIDE = 3; static final int COMMAND_TEXTEDIT_HIDE = 3;
static final int COMMAND_SET_KEEP_SCREEN_ON = 5; static final int COMMAND_SET_KEEP_SCREEN_ON = 5;
@ -447,6 +435,29 @@ public class SDLActivity extends Activity {
Log.e(TAG, "error handling message, getContext() returned no Activity"); Log.e(TAG, "error handling message, getContext() returned no Activity");
} }
break; break;
case COMMAND_CHANGE_WINDOW_STYLE:
if (context instanceof Activity) {
Window window = ((Activity) context).getWindow();
if (window != null) {
if ((msg.obj instanceof Integer) && (((Integer) msg.obj).intValue() != 0)) {
int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
window.getDecorView().setSystemUiVisibility(flags);
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
window.getDecorView().setSystemUiVisibility(flags);
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
} else {
Log.e(TAG, "error handling message, getContext() returned no Activity");
}
break;
case COMMAND_TEXTEDIT_HIDE: case COMMAND_TEXTEDIT_HIDE:
if (mTextEdit != null) { if (mTextEdit != null) {
// Note: On some devices setting view to GONE creates a flicker in landscape. // Note: On some devices setting view to GONE creates a flicker in landscape.
@ -524,6 +535,14 @@ public class SDLActivity extends Activity {
return mSingleton.sendCommand(COMMAND_CHANGE_TITLE, title); return mSingleton.sendCommand(COMMAND_CHANGE_TITLE, title);
} }
/**
* This method is called by SDL using JNI.
*/
public static void setWindowStyle(boolean fullscreen) {
// Called from SDLMain() thread and can't directly affect the view
mSingleton.sendCommand(COMMAND_CHANGE_WINDOW_STYLE, fullscreen ? 1 : 0);
}
/** /**
* This method is called by SDL using JNI. * This method is called by SDL using JNI.
* This is a static method for JNI convenience, it calls a non-static method * This is a static method for JNI convenience, it calls a non-static method

View File

@ -14,7 +14,7 @@ supported, but you can use the "android-project-ant" directory as a template.
Requirements Requirements
================================================================================ ================================================================================
Android SDK (version 16 or later) Android SDK (version 19 or later)
https://developer.android.com/sdk/index.html https://developer.android.com/sdk/index.html
Android NDK r10e or later Android NDK r10e or later

View File

@ -209,6 +209,7 @@ static jclass mActivityClass;
/* method signatures */ /* method signatures */
static jmethodID midGetNativeSurface; static jmethodID midGetNativeSurface;
static jmethodID midSetActivityTitle; static jmethodID midSetActivityTitle;
static jmethodID midSetWindowStyle;
static jmethodID midSetOrientation; static jmethodID midSetOrientation;
static jmethodID midGetContext; static jmethodID midGetContext;
static jmethodID midIsAndroidTV; static jmethodID midIsAndroidTV;
@ -302,6 +303,8 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv* mEnv, jclass c
"getNativeSurface","()Landroid/view/Surface;"); "getNativeSurface","()Landroid/view/Surface;");
midSetActivityTitle = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, midSetActivityTitle = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
"setActivityTitle","(Ljava/lang/String;)Z"); "setActivityTitle","(Ljava/lang/String;)Z");
midSetWindowStyle = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
"setWindowStyle","(Z)V");
midSetOrientation = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, midSetOrientation = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
"setOrientation","(IIZLjava/lang/String;)V"); "setOrientation","(IIZLjava/lang/String;)V");
midGetContext = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, midGetContext = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
@ -332,7 +335,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv* mEnv, jclass c
midGetDisplayDPI = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "getDisplayDPI", "()Landroid/util/DisplayMetrics;"); midGetDisplayDPI = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "getDisplayDPI", "()Landroid/util/DisplayMetrics;");
if (!midGetNativeSurface || if (!midGetNativeSurface ||
!midSetActivityTitle || !midSetOrientation || !midGetContext || !midIsAndroidTV || !midInputGetInputDeviceIds || !midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsAndroidTV || !midInputGetInputDeviceIds ||
!midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown || !midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown ||
!midClipboardSetText || !midClipboardGetText || !midClipboardHasText || !midClipboardSetText || !midClipboardGetText || !midClipboardHasText ||
!midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariables|| !midGetDisplayDPI) { !midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariables|| !midGetDisplayDPI) {
@ -907,6 +910,12 @@ void Android_JNI_SetActivityTitle(const char *title)
(*mEnv)->DeleteLocalRef(mEnv, jtitle); (*mEnv)->DeleteLocalRef(mEnv, jtitle);
} }
void Android_JNI_SetWindowStyle(SDL_bool fullscreen)
{
JNIEnv *mEnv = Android_JNI_GetEnv();
(*mEnv)->CallStaticVoidMethod(mEnv, mActivityClass, midSetWindowStyle, fullscreen ? 1 : 0);
}
void Android_JNI_SetOrientation(int w, int h, int resizable, const char *hint) void Android_JNI_SetOrientation(int w, int h, int resizable, const char *hint)
{ {
JNIEnv *mEnv = Android_JNI_GetEnv(); JNIEnv *mEnv = Android_JNI_GetEnv();

View File

@ -34,6 +34,7 @@ extern "C" {
/* Interface from the SDL library into the Android Java activity */ /* Interface from the SDL library into the Android Java activity */
extern void Android_JNI_SetActivityTitle(const char *title); extern void Android_JNI_SetActivityTitle(const char *title);
extern void Android_JNI_SetWindowStyle(SDL_bool fullscreen);
extern void Android_JNI_SetOrientation(int w, int h, int resizable, const char *hint); extern void Android_JNI_SetOrientation(int w, int h, int resizable, const char *hint);
extern SDL_bool Android_JNI_GetAccelerometerValues(float values[3]); extern SDL_bool Android_JNI_GetAccelerometerValues(float values[3]);

View File

@ -120,6 +120,7 @@ Android_CreateDevice(int devindex)
device->CreateSDLWindow = Android_CreateWindow; device->CreateSDLWindow = Android_CreateWindow;
device->SetWindowTitle = Android_SetWindowTitle; device->SetWindowTitle = Android_SetWindowTitle;
device->SetWindowFullscreen = Android_SetWindowFullscreen;
device->DestroyWindow = Android_DestroyWindow; device->DestroyWindow = Android_DestroyWindow;
device->GetWindowWMInfo = Android_GetWindowWMInfo; device->GetWindowWMInfo = Android_GetWindowWMInfo;
@ -220,7 +221,7 @@ Android_SetScreenResolution(int width, int height, Uint32 format, float rate)
/* /*
Update the resolution of the desktop mode, so that the window Update the resolution of the desktop mode, so that the window
can be properly resized. The screen resolution change can for can be properly resized. The screen resolution change can for
example happen when the Activity enters or exists immersive mode, example happen when the Activity enters or exits immersive mode,
which can happen after VideoInit(). which can happen after VideoInit().
*/ */
device = SDL_GetVideoDevice(); device = SDL_GetVideoDevice();
@ -234,16 +235,17 @@ Android_SetScreenResolution(int width, int height, Uint32 format, float rate)
} }
if (Android_Window) { if (Android_Window) {
SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height);
/* Force the current mode to match the resize otherwise the SDL_WINDOWEVENT_RESTORED event /* Force the current mode to match the resize otherwise the SDL_WINDOWEVENT_RESTORED event
* will fall back to the old mode */ * will fall back to the old mode */
display = SDL_GetDisplayForWindow(Android_Window); display = SDL_GetDisplayForWindow(Android_Window);
display->current_mode.format = format; display->display_modes[0].format = format;
display->current_mode.w = width; display->display_modes[0].w = width;
display->current_mode.h = height; display->display_modes[0].h = height;
display->current_mode.refresh_rate = rate; display->display_modes[0].refresh_rate = rate;
display->current_mode = display->display_modes[0];
SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height);
} }
} }

View File

@ -53,7 +53,6 @@ Android_CreateWindow(_THIS, SDL_Window * window)
window->h = Android_ScreenHeight; window->h = Android_ScreenHeight;
window->flags &= ~SDL_WINDOW_RESIZABLE; /* window is NEVER resizeable */ window->flags &= ~SDL_WINDOW_RESIZABLE; /* window is NEVER resizeable */
window->flags |= SDL_WINDOW_FULLSCREEN; /* window is always fullscreen */
window->flags &= ~SDL_WINDOW_HIDDEN; window->flags &= ~SDL_WINDOW_HIDDEN;
window->flags |= SDL_WINDOW_SHOWN; /* only one window on Android */ window->flags |= SDL_WINDOW_SHOWN; /* only one window on Android */
window->flags |= SDL_WINDOW_INPUT_FOCUS; /* always has input focus */ window->flags |= SDL_WINDOW_INPUT_FOCUS; /* always has input focus */
@ -98,6 +97,12 @@ Android_SetWindowTitle(_THIS, SDL_Window * window)
Android_JNI_SetActivityTitle(window->title); Android_JNI_SetActivityTitle(window->title);
} }
void
Android_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
{
Android_JNI_SetWindowStyle(fullscreen);
}
void void
Android_DestroyWindow(_THIS, SDL_Window * window) Android_DestroyWindow(_THIS, SDL_Window * window)
{ {

View File

@ -28,6 +28,7 @@
extern int Android_CreateWindow(_THIS, SDL_Window * window); extern int Android_CreateWindow(_THIS, SDL_Window * window);
extern void Android_SetWindowTitle(_THIS, SDL_Window * window); extern void Android_SetWindowTitle(_THIS, SDL_Window * window);
extern void Android_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen);
extern void Android_DestroyWindow(_THIS, SDL_Window * window); extern void Android_DestroyWindow(_THIS, SDL_Window * window);
extern SDL_bool Android_GetWindowWMInfo(_THIS, SDL_Window * window, struct SDL_SysWMinfo * info); extern SDL_bool Android_GetWindowWMInfo(_THIS, SDL_Window * window, struct SDL_SysWMinfo * info);