Skip to content

Commit 357fbc8

Browse files
committed
IOIO: android integration
1 parent 5b0deee commit 357fbc8

2 files changed

Lines changed: 59 additions & 12 deletions

File tree

ioio/ioio/src/main/java/net/sourceforge/smallbasic/ioio/IOService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public void start() {
5656
}
5757

5858
public void stop() {
59-
looper.ioio.disconnect();
59+
if (looper.ioio != null) {
60+
looper.ioio.disconnect();
61+
}
6062
connectionController.stop();
6163
}
6264

ioio/main.cpp

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,49 @@ jobject g_activity;
4545
#define CLASS_IOTASK_ID 1
4646
#define ARRAY_SIZE 10
4747

48-
jclass findClass(const char *path) {
4948
#if defined(ANDROID_MODULE)
50-
// calls MainActivity.findClass() to return the loaded jclass for path
49+
//
50+
// calls MainActivity.findClass() to return the loaded jclass for path
51+
//
52+
jclass findClass(const char *path) {
5153
jclass clazz = g_env->GetObjectClass(g_activity);
5254
jmethodID methodId = g_env->GetMethodID(clazz, "findClass", "(Ljava/lang/String;)Ljava/lang/Class;");
5355
jstring className = g_env->NewStringUTF(path);
54-
jclass result = (jclass) g_env->CallObjectMethod(g_activity, methodId, className);
56+
jclass result = (jclass)g_env->CallObjectMethod(g_activity, methodId, className);
5557
g_env->DeleteLocalRef(className);
5658
g_env->DeleteLocalRef(clazz);
59+
return reinterpret_cast<jclass>(g_env->NewGlobalRef(result));
60+
}
61+
62+
jobject createInstance(jclass clazz) {
63+
jmethodID constructor = g_env->GetMethodID(clazz, "<init>", "()V");
64+
jobject result;
65+
if (constructor != nullptr) {
66+
result = g_env->NewObject(clazz, constructor);
67+
result = reinterpret_cast<jclass>(g_env->NewGlobalRef(result));
68+
} else {
69+
result = nullptr;
70+
}
5771
return result;
72+
}
73+
5874
#else
75+
jclass findClass(const char *path) {
5976
return g_env->FindClass(path);
60-
#endif
6177
}
6278

79+
jobject createInstance() {
80+
jmethodID constructor = g_env->GetMethodID(clazz, "<init>", "()V");
81+
jobject result;
82+
if (constructor != nullptr) {
83+
result = g_env->NewObject(clazz, constructor);
84+
} else {
85+
result = nullptr;
86+
}
87+
return result;
88+
}
89+
#endif
90+
6391
struct IOTask {
6492
IOTask():
6593
_clazz(nullptr),
@@ -68,9 +96,15 @@ struct IOTask {
6896
}
6997

7098
virtual ~IOTask() {
99+
attachCurrentThread();
71100
if (_array) {
72101
g_env->DeleteLocalRef(_array);
73102
}
103+
#if defined(ANDROID_MODULE)
104+
g_env->DeleteGlobalRef(_clazz);
105+
g_env->DeleteGlobalRef(_instance);
106+
#endif
107+
detachCurrentThread();
74108
_clazz = nullptr;
75109
_instance = nullptr;
76110
_array = nullptr;
@@ -85,10 +119,7 @@ struct IOTask {
85119
attachCurrentThread();
86120
_clazz = findClass(path);
87121
if (_clazz != nullptr) {
88-
jmethodID constructor = g_env->GetMethodID(_clazz, "<init>", "()V");
89-
if (constructor != nullptr) {
90-
_instance = g_env->NewObject(_clazz, constructor);
91-
}
122+
_instance = createInstance(_clazz);
92123
}
93124
result = _instance != nullptr;
94125
if (!result) {
@@ -457,6 +488,9 @@ SBLIB_API int sblib_func_count() {
457488
return (sizeof(lib_func) / sizeof(lib_func[0]));
458489
}
459490

491+
//
492+
// Program startup
493+
//
460494
int sblib_init(const char *sourceFile) {
461495
#if defined(DESKTOP_MODULE)
462496
JavaVMInitArgs vm_args;
@@ -490,12 +524,15 @@ int sblib_init(const char *sourceFile) {
490524
}
491525

492526
#if defined(ANDROID_MODULE)
493-
extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
527+
//
528+
// Stores the Android JavaVM reference
529+
//
530+
extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void* reserved) {
494531
logEntered();
495532
g_jvm = vm;
496533

497534
jint result;
498-
if (g_jvm->GetEnv((void**)&g_env, JNI_VERSION_1_6) != JNI_OK) {
535+
if (g_jvm->GetEnv((void **)&g_env, JNI_VERSION_1_6) != JNI_OK) {
499536
result = JNI_ERR;
500537
} else {
501538
result = JNI_VERSION_1_6;
@@ -504,10 +541,12 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
504541
return result;
505542
}
506543

544+
//
545+
// Retrieves the _app->activity->clazz value sent from App/JNI to Java to IOIOLoader
546+
//
507547
extern "C" JNIEXPORT void JNICALL Java_net_sourceforge_smallbasic_ioio_IOIOLoader_init
508548
(JNIEnv *env, jclass clazz, jobject activity) {
509549
logEntered();
510-
// get the long value from jobject activity
511550
jclass longClass = env->FindClass("java/lang/Long");
512551
jmethodID longValueMethod = env->GetMethodID(longClass, "longValue", "()J");
513552
g_activity = (jobject)env->CallLongMethod(activity, longValueMethod);
@@ -516,6 +555,9 @@ extern "C" JNIEXPORT void JNICALL Java_net_sourceforge_smallbasic_ioio_IOIOLoade
516555

517556
#endif
518557

558+
//
559+
// Release ioio variables falling out of scope
560+
//
519561
SBLIB_API void sblib_free(int cls_id, int id) {
520562
if (id != -1) {
521563
switch (cls_id) {
@@ -529,6 +571,9 @@ SBLIB_API void sblib_free(int cls_id, int id) {
529571
}
530572
}
531573

574+
//
575+
// Program termination
576+
//
532577
void sblib_close(void) {
533578
if (g_ioioTask) {
534579
g_ioioTask->invokeVoidVoid("close", nullptr);

0 commit comments

Comments
 (0)