Fixed bug 2258 - Crash when using Android clipboard
authorPhilipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 23 Nov 2013 23:38:16 +0100
changeset 8029 cf81e6709b3d
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
--- a/android-project/src/org/libsdl/app/SDLActivity.java	Sat Nov 23 15:52:49 2013 -0500
+++ b/android-project/src/org/libsdl/app/SDLActivity.java	Sat Nov 23 23:38:16 2013 +0100
@@ -277,6 +277,34 @@
         return mSingleton;
     }
 
+    /**
+     * @return result of getSystemService(name) but executed on UI thread.
+     */
+    public Object getSystemServiceFromUiThread(final String name) {
+        final Object lock = new Object();
+        final Object[] results = new Object[2]; // array for writable variables
+        synchronized (lock) {
+            runOnUiThread(new Runnable() {
+                @Override
+                public void run() {
+                    synchronized (lock) {
+                        results[0] = getSystemService(name);
+                        results[1] = Boolean.TRUE;
+                        lock.notify();
+                    }
+                }
+            });
+            if (results[1] == null) {
+                try {
+                    lock.wait();
+                } catch (InterruptedException ex) {
+                    ex.printStackTrace();
+                }
+            }
+        }
+        return results[0];
+    }
+
     static class ShowTextInputTask implements Runnable {
         /*
          * This is used to regulate the pan&scan method to have some offset from
--- a/src/core/android/SDL_android.c	Sat Nov 23 15:52:49 2013 -0500
+++ b/src/core/android/SDL_android.c	Sat Nov 23 23:38:16 2013 +0100
@@ -1041,7 +1041,7 @@
     mid = (*env)->GetStaticMethodID(env, mActivityClass, "getContext", "()Landroid/content/Context;");
     jobject context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
 
-    mid = (*env)->GetMethodID(env, mActivityClass, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
+    mid = (*env)->GetMethodID(env, mActivityClass, "getSystemServiceFromUiThread", "(Ljava/lang/String;)Ljava/lang/Object;");
     jobject manager = (*env)->CallObjectMethod(env, context, mid, service);
 
     (*env)->DeleteLocalRef(env, service);