Fixed setting of screen saver state crashing on some version of Android.
authorPhilipp Wiesemann <philipp.wiesemann@arcor.de>
Mon, 20 Oct 2014 22:19:09 +0200
changeset 9174f9244b2a1511
parent 9173 c677ab1148c5
child 9175 ea006adc8372
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.
android-project/src/org/libsdl/app/SDLActivity.java
src/core/android/SDL_android.c
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Mon Oct 20 10:10:39 2014 -0300
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Mon Oct 20 22:19:09 2014 +0200
     1.3 @@ -187,13 +187,6 @@
     1.4          return super.dispatchKeyEvent(event);
     1.5      }
     1.6  
     1.7 -    public static void suspendScreenSaver(boolean suspend) {
     1.8 -        if (suspend)
     1.9 -            mSingleton.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    1.10 -        else
    1.11 -            mSingleton.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    1.12 -    }
    1.13 -
    1.14      /** Called by onPause or surfaceDestroyed. Even if surfaceDestroyed
    1.15       *  is the first to be called, mIsSurfaceReady should still be set
    1.16       *  to 'true' during the call to onPause (in a usual scenario).
    1.17 @@ -229,6 +222,7 @@
    1.18      static final int COMMAND_CHANGE_TITLE = 1;
    1.19      static final int COMMAND_UNUSED = 2;
    1.20      static final int COMMAND_TEXTEDIT_HIDE = 3;
    1.21 +    static final int COMMAND_SET_KEEP_SCREEN_ON = 5;
    1.22  
    1.23      protected static final int COMMAND_USER = 0x8000;
    1.24  
    1.25 @@ -273,7 +267,18 @@
    1.26                      imm.hideSoftInputFromWindow(mTextEdit.getWindowToken(), 0);
    1.27                  }
    1.28                  break;
    1.29 -
    1.30 +            case COMMAND_SET_KEEP_SCREEN_ON:
    1.31 +            {
    1.32 +                Window window = ((Activity) context).getWindow();
    1.33 +                if (window != null) {
    1.34 +                    if ((msg.obj instanceof Integer) && (((Integer) msg.obj).intValue() != 0)) {
    1.35 +                        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    1.36 +                    } else {
    1.37 +                        window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    1.38 +                    }
    1.39 +                }
    1.40 +                break;
    1.41 +            }
    1.42              default:
    1.43                  if ((context instanceof SDLActivity) && !((SDLActivity) context).onUnhandledMessage(msg.arg1, msg.obj)) {
    1.44                      Log.e(TAG, "error handling message, command is " + msg.arg1);
     2.1 --- a/src/core/android/SDL_android.c	Mon Oct 20 10:10:39 2014 -0300
     2.2 +++ b/src/core/android/SDL_android.c	Mon Oct 20 22:19:09 2014 +0200
     2.3 @@ -77,7 +77,6 @@
     2.4  static jmethodID midAudioWriteByteBuffer;
     2.5  static jmethodID midAudioQuit;
     2.6  static jmethodID midPollInputDevices;
     2.7 -static jmethodID midSuspendScreenSaver;
     2.8  
     2.9  /* Accelerometer data storage */
    2.10  static float fLastAccelerometer[3];
    2.11 @@ -132,8 +131,6 @@
    2.12                                  "audioQuit", "()V");
    2.13      midPollInputDevices = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    2.14                                  "pollInputDevices", "()V");
    2.15 -    midSuspendScreenSaver = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    2.16 -                                "suspendScreenSaver", "(Z)V");
    2.17  
    2.18      bHasNewData = false;
    2.19  
    2.20 @@ -450,12 +447,6 @@
    2.21      return s_active > 0;
    2.22  }
    2.23  
    2.24 -void Android_JNI_SuspendScreenSaver(SDL_bool suspend)
    2.25 -{
    2.26 -    JNIEnv *env = Android_JNI_GetEnv();
    2.27 -    (*env)->CallStaticObjectMethod(env, mActivityClass, midSuspendScreenSaver, suspend);
    2.28 -}
    2.29 -
    2.30  ANativeWindow* Android_JNI_GetNativeWindow(void)
    2.31  {
    2.32      ANativeWindow* anw;
    2.33 @@ -1311,6 +1302,9 @@
    2.34      (*env)->CallStaticVoidMethod(env, mActivityClass, midPollInputDevices);    
    2.35  }
    2.36  
    2.37 +/* See SDLActivity.java for constants. */
    2.38 +#define COMMAND_SET_KEEP_SCREEN_ON    5
    2.39 +
    2.40  /* sends message to be handled on the UI event dispatch thread */
    2.41  int Android_JNI_SendMessage(int command, int param)
    2.42  {
    2.43 @@ -1326,6 +1320,11 @@
    2.44      return success ? 0 : -1;
    2.45  }
    2.46  
    2.47 +void Android_JNI_SuspendScreenSaver(SDL_bool suspend)
    2.48 +{
    2.49 +    Android_JNI_SendMessage(COMMAND_SET_KEEP_SCREEN_ON, (suspend == SDL_FALSE) ? 0 : 1);
    2.50 +}
    2.51 +
    2.52  void Android_JNI_ShowTextInput(SDL_Rect *inputRect)
    2.53  {
    2.54      JNIEnv *env = Android_JNI_GetEnv();