Fixed setting of screen saver state crashing on some version of Android. Setting Window flags seems to affect Views and must be handled on UI thread.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
diff --git a/android-project/src/org/libsdl/app/SDLActivity.java b/android-project/src/org/libsdl/app/SDLActivity.java
index 20920c9..f850bf5 100644
--- a/android-project/src/org/libsdl/app/SDLActivity.java
+++ b/android-project/src/org/libsdl/app/SDLActivity.java
@@ -187,13 +187,6 @@ public class SDLActivity extends Activity {
return super.dispatchKeyEvent(event);
}
- public static void suspendScreenSaver(boolean suspend) {
- if (suspend)
- mSingleton.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
- else
- mSingleton.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
- }
-
/** Called by onPause or surfaceDestroyed. Even if surfaceDestroyed
* is the first to be called, mIsSurfaceReady should still be set
* to 'true' during the call to onPause (in a usual scenario).
@@ -229,6 +222,7 @@ public class SDLActivity extends Activity {
static final int COMMAND_CHANGE_TITLE = 1;
static final int COMMAND_UNUSED = 2;
static final int COMMAND_TEXTEDIT_HIDE = 3;
+ static final int COMMAND_SET_KEEP_SCREEN_ON = 5;
protected static final int COMMAND_USER = 0x8000;
@@ -273,7 +267,18 @@ public class SDLActivity extends Activity {
imm.hideSoftInputFromWindow(mTextEdit.getWindowToken(), 0);
}
break;
-
+ case COMMAND_SET_KEEP_SCREEN_ON:
+ {
+ Window window = ((Activity) context).getWindow();
+ if (window != null) {
+ if ((msg.obj instanceof Integer) && (((Integer) msg.obj).intValue() != 0)) {
+ window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
+ } else {
+ window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
+ }
+ }
+ break;
+ }
default:
if ((context instanceof SDLActivity) && !((SDLActivity) context).onUnhandledMessage(msg.arg1, msg.obj)) {
Log.e(TAG, "error handling message, command is " + msg.arg1);
diff --git a/src/core/android/SDL_android.c b/src/core/android/SDL_android.c
index a998499..24442fd 100644
--- a/src/core/android/SDL_android.c
+++ b/src/core/android/SDL_android.c
@@ -77,7 +77,6 @@ static jmethodID midAudioWriteShortBuffer;
static jmethodID midAudioWriteByteBuffer;
static jmethodID midAudioQuit;
static jmethodID midPollInputDevices;
-static jmethodID midSuspendScreenSaver;
/* Accelerometer data storage */
static float fLastAccelerometer[3];
@@ -132,8 +131,6 @@ JNIEXPORT void JNICALL SDL_Android_Init(JNIEnv* mEnv, jclass cls)
"audioQuit", "()V");
midPollInputDevices = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
"pollInputDevices", "()V");
- midSuspendScreenSaver = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
- "suspendScreenSaver", "(Z)V");
bHasNewData = false;
@@ -450,12 +447,6 @@ static SDL_bool LocalReferenceHolder_IsActive()
return s_active > 0;
}
-void Android_JNI_SuspendScreenSaver(SDL_bool suspend)
-{
- JNIEnv *env = Android_JNI_GetEnv();
- (*env)->CallStaticObjectMethod(env, mActivityClass, midSuspendScreenSaver, suspend);
-}
-
ANativeWindow* Android_JNI_GetNativeWindow(void)
{
ANativeWindow* anw;
@@ -1311,6 +1302,9 @@ void Android_JNI_PollInputDevices()
(*env)->CallStaticVoidMethod(env, mActivityClass, midPollInputDevices);
}
+/* See SDLActivity.java for constants. */
+#define COMMAND_SET_KEEP_SCREEN_ON 5
+
/* sends message to be handled on the UI event dispatch thread */
int Android_JNI_SendMessage(int command, int param)
{
@@ -1326,6 +1320,11 @@ int Android_JNI_SendMessage(int command, int param)
return success ? 0 : -1;
}
+void Android_JNI_SuspendScreenSaver(SDL_bool suspend)
+{
+ Android_JNI_SendMessage(COMMAND_SET_KEEP_SCREEN_ON, (suspend == SDL_FALSE) ? 0 : 1);
+}
+
void Android_JNI_ShowTextInput(SDL_Rect *inputRect)
{
JNIEnv *env = Android_JNI_GetEnv();