|
28 | 28 | import java.util.HashMap;
|
29 | 29 | import java.util.Map;
|
30 | 30 | import java.util.Queue;
|
| 31 | +import java.util.concurrent.Callable; |
31 | 32 | import java.util.concurrent.ConcurrentHashMap;
|
32 | 33 | import java.util.concurrent.ConcurrentLinkedQueue;
|
| 34 | +import java.util.concurrent.ExecutionException; |
| 35 | +import java.util.concurrent.FutureTask; |
| 36 | +import java.util.concurrent.RunnableFuture; |
33 | 37 | import java.util.concurrent.atomic.AtomicInteger;
|
34 | 38 |
|
35 | 39 | public class Runtime {
|
@@ -551,7 +555,6 @@ public static Runtime initializeRuntimeWithConfiguration(StaticConfiguration con
|
551 | 555 | WorkThreadScheduler mainThreadScheduler = new WorkThreadScheduler(new MainThreadHandler(Looper.myLooper()));
|
552 | 556 | DynamicConfiguration dynamicConfiguration = new DynamicConfiguration(0, mainThreadScheduler, null);
|
553 | 557 | Runtime runtime = initRuntime(dynamicConfiguration);
|
554 |
| - |
555 | 558 | return runtime;
|
556 | 559 | }
|
557 | 560 |
|
@@ -737,6 +740,37 @@ public void unlock() {
|
737 | 740 | unlock(runtimeId);
|
738 | 741 | }
|
739 | 742 |
|
| 743 | + public static void initInstanceFromPossibleNonMainThread(final Object instance) { |
| 744 | + if (isNotOnMainThread()) { |
| 745 | + Runnable runnable = new Runnable() { |
| 746 | + @Override |
| 747 | + public void run() { |
| 748 | + initInstance(instance); |
| 749 | + } |
| 750 | + }; |
| 751 | + |
| 752 | + RunnableFuture<Void> task = new FutureTask<>(runnable, null); |
| 753 | + getMainThreadHandler().post(task); |
| 754 | + |
| 755 | + try { |
| 756 | + task.get(); // this will block until Runnable completes |
| 757 | + } catch (InterruptedException | ExecutionException e) { |
| 758 | + throw new RuntimeException(e); |
| 759 | + } |
| 760 | + |
| 761 | + } else { |
| 762 | + initInstance(instance); |
| 763 | + } |
| 764 | + } |
| 765 | + |
| 766 | + private static Handler getMainThreadHandler() { |
| 767 | + return new Handler(Looper.getMainLooper()); |
| 768 | + } |
| 769 | + |
| 770 | + private static boolean isNotOnMainThread() { |
| 771 | + return Looper.myLooper() != Looper.getMainLooper(); |
| 772 | + } |
| 773 | + |
740 | 774 | public static void initInstance(Object instance) {
|
741 | 775 | ManualInstrumentation.Frame frame = ManualInstrumentation.start("Runtime.initInstance");
|
742 | 776 | try {
|
@@ -1049,26 +1083,60 @@ private int getOrCreateJavaObjectID(Object obj) {
|
1049 | 1083 | return result;
|
1050 | 1084 | }
|
1051 | 1085 |
|
| 1086 | + public static Object callJSMethodFromPossibleNonMainThread(Object javaObject, String methodName, Class<?> retType, Object... args) throws NativeScriptException { |
| 1087 | + return callJSMethodFromPossibleNonMainThread(javaObject, methodName, retType, false /* isConstructor */, args); |
| 1088 | + } |
| 1089 | + |
| 1090 | + public static Object callJSMethodFromPossibleNonMainThread(Object javaObject, String methodName, Class<?> retType, boolean isConstructor, Object... args) throws NativeScriptException { |
| 1091 | + return callJSMethodFromPossibleNonMainThread(javaObject, methodName, retType, isConstructor, 0, args); |
| 1092 | + } |
| 1093 | + |
| 1094 | + public static Object callJSMethodFromPossibleNonMainThread(Object javaObject, String methodName, boolean isConstructor, Object... args) throws NativeScriptException { |
| 1095 | + return callJSMethodFromPossibleNonMainThread(javaObject, methodName, void.class, isConstructor, 0, args); |
| 1096 | + } |
| 1097 | + |
| 1098 | + public static Object callJSMethodFromPossibleNonMainThread(final Object javaObject, final String methodName, final Class<?> retType, final boolean isConstructor, final long delay, final Object... args) throws NativeScriptException { |
| 1099 | + if (isNotOnMainThread()) { |
| 1100 | + Callable<Object> callable = new Callable<Object>() { |
| 1101 | + @Override |
| 1102 | + public Object call() { |
| 1103 | + return callJSMethod(javaObject, methodName, retType, isConstructor, delay, args); |
| 1104 | + } |
| 1105 | + }; |
| 1106 | + |
| 1107 | + RunnableFuture<Object> task = new FutureTask<>(callable); |
| 1108 | + getMainThreadHandler().post(task); |
| 1109 | + |
| 1110 | + try { |
| 1111 | + return task.get(); // this will block until Runnable completes |
| 1112 | + } catch (InterruptedException | ExecutionException e) { |
| 1113 | + throw new RuntimeException(e); |
| 1114 | + } |
| 1115 | + |
| 1116 | + } else { |
| 1117 | + return callJSMethod(javaObject, methodName, retType, isConstructor, delay, args); |
| 1118 | + } |
| 1119 | + } |
| 1120 | + |
| 1121 | + |
1052 | 1122 | // sends args in pairs (typeID, value, null) except for objects where its
|
1053 | 1123 | // (typeid, javaObjectID, javaJNIClassPath)
|
1054 | 1124 | public static Object callJSMethod(Object javaObject, String methodName, Class<?> retType, Object... args) throws NativeScriptException {
|
1055 | 1125 | return callJSMethod(javaObject, methodName, retType, false /* isConstructor */, args);
|
1056 | 1126 | }
|
1057 | 1127 |
|
1058 |
| - public static Object callJSMethodWithDelay(Object javaObject, String methodName, Class<?> retType, long delay, Object... args) throws NativeScriptException { |
1059 |
| - return callJSMethod(javaObject, methodName, retType, false /* isConstructor */, delay, args); |
1060 |
| - } |
1061 |
| - |
1062 | 1128 | public static Object callJSMethod(Object javaObject, String methodName, Class<?> retType, boolean isConstructor, Object... args) throws NativeScriptException {
|
1063 |
| - Object ret = callJSMethod(javaObject, methodName, retType, isConstructor, 0, args); |
1064 |
| - |
1065 |
| - return ret; |
| 1129 | + return callJSMethod(javaObject, methodName, retType, isConstructor, 0, args); |
1066 | 1130 | }
|
1067 | 1131 |
|
1068 | 1132 | public static Object callJSMethod(Object javaObject, String methodName, boolean isConstructor, Object... args) throws NativeScriptException {
|
1069 | 1133 | return callJSMethod(javaObject, methodName, void.class, isConstructor, 0, args);
|
1070 | 1134 | }
|
1071 | 1135 |
|
| 1136 | + public static Object callJSMethodWithDelay(Object javaObject, String methodName, Class<?> retType, long delay, Object... args) throws NativeScriptException { |
| 1137 | + return callJSMethod(javaObject, methodName, retType, false /* isConstructor */, delay, args); |
| 1138 | + } |
| 1139 | + |
1072 | 1140 | public static Object callJSMethod(Object javaObject, String methodName, Class<?> retType, boolean isConstructor, long delay, Object... args) throws NativeScriptException {
|
1073 | 1141 | Runtime runtime = Runtime.getCurrentRuntime();
|
1074 | 1142 |
|
@@ -1278,7 +1346,7 @@ private static Class<?> getCachedClass(String className) {
|
1278 | 1346 | try {
|
1279 | 1347 | clazz = classStorageService.retrieveClass(className);
|
1280 | 1348 | return clazz;
|
1281 |
| - } catch (RuntimeException e){ |
| 1349 | + } catch (RuntimeException e) { |
1282 | 1350 | return null;
|
1283 | 1351 | }
|
1284 | 1352 | }
|
|
0 commit comments