Commit 5f193f0c60c158591fa969d6196121016a90bcfd

Philipp Wiesemann 2014-10-20T22:19:09

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.

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();