Fixed bug 2258 - Crash when using Android clipboard
authorPhilipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 23 Nov 2013 23:38:16 +0100
changeset 8029cf81e6709b3d
parent 8028 e3e00f8e6b91
child 8030 7b0d28c202d2
Fixed bug 2258 - Crash when using Android clipboard

chw

The Android clipboard manager methods must be called from the UI thread,
otherwise crashes of the dalvikvm happen.
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	Sat Nov 23 15:52:49 2013 -0500
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Sat Nov 23 23:38:16 2013 +0100
     1.3 @@ -277,6 +277,34 @@
     1.4          return mSingleton;
     1.5      }
     1.6  
     1.7 +    /**
     1.8 +     * @return result of getSystemService(name) but executed on UI thread.
     1.9 +     */
    1.10 +    public Object getSystemServiceFromUiThread(final String name) {
    1.11 +        final Object lock = new Object();
    1.12 +        final Object[] results = new Object[2]; // array for writable variables
    1.13 +        synchronized (lock) {
    1.14 +            runOnUiThread(new Runnable() {
    1.15 +                @Override
    1.16 +                public void run() {
    1.17 +                    synchronized (lock) {
    1.18 +                        results[0] = getSystemService(name);
    1.19 +                        results[1] = Boolean.TRUE;
    1.20 +                        lock.notify();
    1.21 +                    }
    1.22 +                }
    1.23 +            });
    1.24 +            if (results[1] == null) {
    1.25 +                try {
    1.26 +                    lock.wait();
    1.27 +                } catch (InterruptedException ex) {
    1.28 +                    ex.printStackTrace();
    1.29 +                }
    1.30 +            }
    1.31 +        }
    1.32 +        return results[0];
    1.33 +    }
    1.34 +
    1.35      static class ShowTextInputTask implements Runnable {
    1.36          /*
    1.37           * This is used to regulate the pan&scan method to have some offset from
     2.1 --- a/src/core/android/SDL_android.c	Sat Nov 23 15:52:49 2013 -0500
     2.2 +++ b/src/core/android/SDL_android.c	Sat Nov 23 23:38:16 2013 +0100
     2.3 @@ -1041,7 +1041,7 @@
     2.4      mid = (*env)->GetStaticMethodID(env, mActivityClass, "getContext", "()Landroid/content/Context;");
     2.5      jobject context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
     2.6  
     2.7 -    mid = (*env)->GetMethodID(env, mActivityClass, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
     2.8 +    mid = (*env)->GetMethodID(env, mActivityClass, "getSystemServiceFromUiThread", "(Ljava/lang/String;)Ljava/lang/Object;");
     2.9      jobject manager = (*env)->CallObjectMethod(env, context, mid, service);
    2.10  
    2.11      (*env)->DeleteLocalRef(env, service);