comparison android/dw.cpp @ 2483:9f7af6d8c6a4

Android: Fix signal handlers so they actually work. Create Weak Global References for the returned jobject handles. The jobject references passed from the event handler will be local references so we need to use IsSameObject() to compare the handles. Also create a more simple entrypoint to reduce calls.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Mon, 26 Apr 2021 21:08:02 +0000
parents 4888503c3e3e
children cb5b67154399
comparison
equal deleted inserted replaced
2482:4888503c3e3e 2483:9f7af6d8c6a4
122 static SignalHandler *DWRoot = NULL; 122 static SignalHandler *DWRoot = NULL;
123 123
124 SignalHandler *_dw_get_handler(HWND window, int messageid) 124 SignalHandler *_dw_get_handler(HWND window, int messageid)
125 { 125 {
126 SignalHandler *tmp = DWRoot; 126 SignalHandler *tmp = DWRoot;
127 127 JNIEnv *env;
128 /* Find any callbacks for this function */ 128
129 while(tmp) 129 if((env = (JNIEnv *)pthread_getspecific(_dw_env_key))) {
130 { 130 /* Find any callbacks for this function */
131 if(tmp->message == messageid && window == tmp->window) 131 while (tmp) {
132 { 132 if (tmp->message == messageid && env->IsSameObject(window, tmp->window)) {
133 return tmp; 133 return tmp;
134 }
135 tmp = tmp->next;
134 } 136 }
135 tmp = tmp->next;
136 } 137 }
137 return NULL; 138 return NULL;
138 } 139 }
139 140
140 typedef struct 141 typedef struct
348 void *params[8] = { (void *)obj2, (void *)utf81, (void *)utf82, 349 void *params[8] = { (void *)obj2, (void *)utf81, (void *)utf82,
349 DW_INT_TO_POINTER(int1), DW_INT_TO_POINTER(int2), 350 DW_INT_TO_POINTER(int1), DW_INT_TO_POINTER(int2),
350 DW_INT_TO_POINTER(int3), DW_INT_TO_POINTER(int4), NULL }; 351 DW_INT_TO_POINTER(int3), DW_INT_TO_POINTER(int4), NULL };
351 352
352 return _dw_event_handler(obj1, params, message); 353 return _dw_event_handler(obj1, params, message);
354 }
355
356 /* A more simple method for quicker calls */
357 JNIEXPORT void JNICALL
358 Java_org_dbsoft_dwindows_dwtest_DWindows_eventHandlerSimple(JNIEnv* env, jobject obj, jobject obj1, jint message) {
359 void *params[8] = { NULL };
360
361 _dw_event_handler(obj1, params, message);
353 } 362 }
354 363
355 /* This function adds a signal handler callback into the linked list. 364 /* This function adds a signal handler callback into the linked list.
356 */ 365 */
357 void _dw_new_signal(ULONG message, HWND window, int msgid, void *signalfunction, void *discfunc, void *data) 366 void _dw_new_signal(ULONG message, HWND window, int msgid, void *signalfunction, void *discfunc, void *data)
712 // First get the class that contains the method you need to call 721 // First get the class that contains the method you need to call
713 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 722 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
714 // Get the method that you want to call 723 // Get the method that you want to call
715 jmethodID boxNew = env->GetMethodID(clazz, "boxNew", "(II)Landroid/widget/LinearLayout;"); 724 jmethodID boxNew = env->GetMethodID(clazz, "boxNew", "(II)Landroid/widget/LinearLayout;");
716 // Call the method on the object 725 // Call the method on the object
717 jobject result = env->CallObjectMethod(_dw_obj, boxNew, type, pad); 726 jobject result = env->NewWeakGlobalRef(env->CallObjectMethod(_dw_obj, boxNew, type, pad));
718 return result; 727 return result;
719 } 728 }
720 return 0; 729 return 0;
721 } 730 }
722 731
898 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 907 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
899 // Get the method that you want to call 908 // Get the method that you want to call
900 jmethodID buttonNew = env->GetMethodID(clazz, "buttonNew", 909 jmethodID buttonNew = env->GetMethodID(clazz, "buttonNew",
901 "(Ljava/lang/String;I)Landroid/widget/Button;"); 910 "(Ljava/lang/String;I)Landroid/widget/Button;");
902 // Call the method on the object 911 // Call the method on the object
903 jobject result = env->CallObjectMethod(_dw_obj, buttonNew, jstr, (int)cid); 912 jobject result = env->NewWeakGlobalRef(env->CallObjectMethod(_dw_obj, buttonNew, jstr, (int)cid));
904 return result; 913 return result;
905 } 914 }
906 return 0; 915 return 0;
907 } 916 }
908 917
918 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 927 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
919 // Get the method that you want to call 928 // Get the method that you want to call
920 jmethodID entryfieldNew = env->GetMethodID(clazz, "entryfieldNew", 929 jmethodID entryfieldNew = env->GetMethodID(clazz, "entryfieldNew",
921 "(Ljava/lang/String;II)Landroid/widget/EditText;"); 930 "(Ljava/lang/String;II)Landroid/widget/EditText;");
922 // Call the method on the object 931 // Call the method on the object
923 jobject result = env->CallObjectMethod(_dw_obj, entryfieldNew, jstr, (int)cid, password); 932 jobject result = env->NewWeakGlobalRef(env->CallObjectMethod(_dw_obj, entryfieldNew, jstr, (int)cid, password));
924 return result; 933 return result;
925 } 934 }
926 return 0; 935 return 0;
927 } 936 }
928 /* 937 /*
1072 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 1081 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
1073 // Get the method that you want to call 1082 // Get the method that you want to call
1074 jmethodID radioButtonNew = env->GetMethodID(clazz, "radioButtonNew", 1083 jmethodID radioButtonNew = env->GetMethodID(clazz, "radioButtonNew",
1075 "(Ljava/lang/String;I)Landroid/widget/RadioButton;"); 1084 "(Ljava/lang/String;I)Landroid/widget/RadioButton;");
1076 // Call the method on the object 1085 // Call the method on the object
1077 jobject result = env->CallObjectMethod(_dw_obj, radioButtonNew, jstr, (int)cid); 1086 jobject result = env->NewWeakGlobalRef(env->CallObjectMethod(_dw_obj, radioButtonNew, jstr, (int)cid));
1078 return result; 1087 return result;
1079 } 1088 }
1080 return 0; 1089 return 0;
1081 } 1090 }
1082 1091
1204 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 1213 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
1205 // Get the method that you want to call 1214 // Get the method that you want to call
1206 jmethodID checkboxNew = env->GetMethodID(clazz, "checkboxNew", 1215 jmethodID checkboxNew = env->GetMethodID(clazz, "checkboxNew",
1207 "(Ljava/lang/String;I)Landroid/widget/CheckBox;"); 1216 "(Ljava/lang/String;I)Landroid/widget/CheckBox;");
1208 // Call the method on the object 1217 // Call the method on the object
1209 jobject result = env->CallObjectMethod(_dw_obj, checkboxNew, jstr, (int)cid); 1218 jobject result = env->NewWeakGlobalRef(env->CallObjectMethod(_dw_obj, checkboxNew, jstr, (int)cid));
1210 return result; 1219 return result;
1211 } 1220 }
1212 return 0; 1221 return 0;
1213 } 1222 }
1214 1223
1560 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 1569 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
1561 // Get the method that you want to call 1570 // Get the method that you want to call
1562 jmethodID textNew = env->GetMethodID(clazz, "textNew", 1571 jmethodID textNew = env->GetMethodID(clazz, "textNew",
1563 "(Ljava/lang/String;II)Landroid/widget/TextView;"); 1572 "(Ljava/lang/String;II)Landroid/widget/TextView;");
1564 // Call the method on the object 1573 // Call the method on the object
1565 jobject result = env->CallObjectMethod(_dw_obj, textNew, jstr, (int)cid, status); 1574 jobject result = env->NewWeakGlobalRef(env->CallObjectMethod(_dw_obj, textNew, jstr, (int)cid, status));
1566 return result; 1575 return result;
1567 } 1576 }
1568 return 0; 1577 return 0;
1569 } 1578 }
1570 1579
2858 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 2867 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
2859 // Get the method that you want to call 2868 // Get the method that you want to call
2860 jmethodID windowNew = env->GetMethodID(clazz, "windowNew", 2869 jmethodID windowNew = env->GetMethodID(clazz, "windowNew",
2861 "(Ljava/lang/String;I)Landroid/widget/LinearLayout;"); 2870 "(Ljava/lang/String;I)Landroid/widget/LinearLayout;");
2862 // Call the method on the object 2871 // Call the method on the object
2863 jobject result = env->CallObjectMethod(_dw_obj, windowNew, jstr, (int)flStyle); 2872 jobject result = env->NewWeakGlobalRef(env->CallObjectMethod(_dw_obj, windowNew, jstr, (int)flStyle));
2864 return result; 2873 return result;
2865 } 2874 }
2866 return 0; 2875 return 0;
2867 } 2876 }
2868 2877