Appropriate naming for JniHandler and associated usages

This commit is contained in:
Shuvo
2025-01-08 21:58:52 +06:00
committed by Dan Albert
parent 03e7823f9e
commit 69fe5a569b

View File

@@ -33,8 +33,8 @@ static const char *kTAG = "hello-jniCallback";
// processing callback to handler class // processing callback to handler class
typedef struct tick_context { typedef struct tick_context {
JavaVM *javaVM; JavaVM *javaVM;
jclass jniHelperClz; jclass jniHandlerClz;
jobject jniHelperObj; jobject jniHandlerObj;
jclass mainActivityClz; jclass mainActivityClz;
jobject mainActivityObj; jobject mainActivityObj;
pthread_mutex_t lock; pthread_mutex_t lock;
@@ -88,26 +88,26 @@ Java_com_example_hellojnicallback_MainActivity_stringFromJNI(JNIEnv *env,
/* /*
* A helper function to show how to call * A helper function to show how to call
* java static functions JniHelper::getBuildVersion() * java static functions JniHandler::getBuildVersion()
* java non-static function JniHelper::getRuntimeMemorySize() * java non-static function JniHandler::getRuntimeMemorySize()
* The trivial implementation for these functions are inside file * The trivial implementation for these functions are inside file
* JniHelper.java * JniHandler.java
*/ */
void queryRuntimeInfo(JNIEnv *env, jobject instance) { void queryRuntimeInfo(JNIEnv *env, jobject instance) {
// Find out which OS we are running on. It does not matter for this app // Find out which OS we are running on. It does not matter for this app
// just to demo how to call static functions. // just to demo how to call static functions.
// Our java JniHelper class id and instance are initialized when this // Our java JniHandler class id and instance are initialized when this
// shared lib got loaded, we just directly use them // shared lib got loaded, we just directly use them
// static function does not need instance, so we just need to feed // static function does not need instance, so we just need to feed
// class and method id to JNI // class and method id to JNI
jmethodID versionFunc = (*env)->GetStaticMethodID( jmethodID versionFunc = (*env)->GetStaticMethodID(
env, g_ctx.jniHelperClz, "getBuildVersion", "()Ljava/lang/String;"); env, g_ctx.jniHandlerClz, "getBuildVersion", "()Ljava/lang/String;");
if (!versionFunc) { if (!versionFunc) {
LOGE("Failed to retrieve getBuildVersion() methodID @ line %d", __LINE__); LOGE("Failed to retrieve getBuildVersion() methodID @ line %d", __LINE__);
return; return;
} }
jstring buildVersion = jstring buildVersion =
(*env)->CallStaticObjectMethod(env, g_ctx.jniHelperClz, versionFunc); (*env)->CallStaticObjectMethod(env, g_ctx.jniHandlerClz, versionFunc);
const char *version = (*env)->GetStringUTFChars(env, buildVersion, NULL); const char *version = (*env)->GetStringUTFChars(env, buildVersion, NULL);
if (!version) { if (!version) {
LOGE("Unable to get version string @ line %d", __LINE__); LOGE("Unable to get version string @ line %d", __LINE__);
@@ -120,8 +120,8 @@ void queryRuntimeInfo(JNIEnv *env, jobject instance) {
(*env)->DeleteLocalRef(env, buildVersion); (*env)->DeleteLocalRef(env, buildVersion);
// Query available memory size from a non-static public function // Query available memory size from a non-static public function
// we need use an instance of JniHelper class to call JNI // we need use an instance of JniHandler class to call JNI
jmethodID memFunc = (*env)->GetMethodID(env, g_ctx.jniHelperClz, jmethodID memFunc = (*env)->GetMethodID(env, g_ctx.jniHandlerClz,
"getRuntimeMemorySize", "()J"); "getRuntimeMemorySize", "()J");
if (!memFunc) { if (!memFunc) {
LOGE("Failed to retrieve getRuntimeMemorySize() methodID @ line %d", LOGE("Failed to retrieve getRuntimeMemorySize() methodID @ line %d",
@@ -136,8 +136,8 @@ void queryRuntimeInfo(JNIEnv *env, jobject instance) {
/* /*
* processing one time initialization: * processing one time initialization:
* Cache the javaVM into our context * Cache the javaVM into our context
* Find class ID for JniHelper * Find class ID for JniHandler
* Create an instance of JniHelper * Create an instance of JniHandler
* Make global reference since we are using them from a native thread * Make global reference since we are using them from a native thread
* Note: * Note:
* All resources allocated here are never released by application * All resources allocated here are never released by application
@@ -155,13 +155,13 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
jclass clz = jclass clz =
(*env)->FindClass(env, "com/example/hellojnicallback/JniHandler"); (*env)->FindClass(env, "com/example/hellojnicallback/JniHandler");
g_ctx.jniHelperClz = (*env)->NewGlobalRef(env, clz); g_ctx.jniHandlerClz = (*env)->NewGlobalRef(env, clz);
jmethodID jniHelperCtor = jmethodID jniHandlerCtor =
(*env)->GetMethodID(env, g_ctx.jniHelperClz, "<init>", "()V"); (*env)->GetMethodID(env, g_ctx.jniHandlerClz, "<init>", "()V");
jobject handler = (*env)->NewObject(env, g_ctx.jniHelperClz, jniHelperCtor); jobject handler = (*env)->NewObject(env, g_ctx.jniHandlerClz, jniHandlerCtor);
g_ctx.jniHelperObj = (*env)->NewGlobalRef(env, handler); g_ctx.jniHandlerObj = (*env)->NewGlobalRef(env, handler);
queryRuntimeInfo(env, g_ctx.jniHelperObj); queryRuntimeInfo(env, g_ctx.jniHandlerObj);
g_ctx.done = 0; g_ctx.done = 0;
g_ctx.mainActivityObj = NULL; g_ctx.mainActivityObj = NULL;
@@ -169,7 +169,7 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
} }
/* /*
* A helper function to wrap java JniHelper::updateStatus(String msg) * A helper function to wrap java JniHandler::updateStatus(String msg)
* JNI allow us to call this function via an instance even it is * JNI allow us to call this function via an instance even it is
* private function. * private function.
*/ */
@@ -183,7 +183,7 @@ void sendJavaMsg(JNIEnv *env, jobject instance, jmethodID func,
/* /*
* Main working thread function. From a pthread, * Main working thread function. From a pthread,
* calling back to MainActivity::updateTimer() to display ticks on UI * calling back to MainActivity::updateTimer() to display ticks on UI
* calling back to JniHelper::updateStatus(String msg) for msg * calling back to JniHandler::updateStatus(String msg) for msg
*/ */
void *UpdateTicks(void *context) { void *UpdateTicks(void *context) {
TickContext *pctx = (TickContext *)context; TickContext *pctx = (TickContext *)context;
@@ -199,8 +199,8 @@ void *UpdateTicks(void *context) {
} }
jmethodID statusId = (*env)->GetMethodID( jmethodID statusId = (*env)->GetMethodID(
env, pctx->jniHelperClz, "updateStatus", "(Ljava/lang/String;)V"); env, pctx->jniHandlerClz, "updateStatus", "(Ljava/lang/String;)V");
sendJavaMsg(env, pctx->jniHelperObj, statusId, sendJavaMsg(env, pctx->jniHandlerObj, statusId,
"TickerThread status: initializing..."); "TickerThread status: initializing...");
// get mainActivity updateTimer function // get mainActivity updateTimer function
@@ -211,7 +211,7 @@ void *UpdateTicks(void *context) {
const struct timeval kOneSecond = {(__kernel_time_t)1, const struct timeval kOneSecond = {(__kernel_time_t)1,
(__kernel_suseconds_t)0}; (__kernel_suseconds_t)0};
sendJavaMsg(env, pctx->jniHelperObj, statusId, sendJavaMsg(env, pctx->jniHandlerObj, statusId,
"TickerThread status: start ticking ..."); "TickerThread status: start ticking ...");
while (1) { while (1) {
gettimeofday(&beginTime, NULL); gettimeofday(&beginTime, NULL);
@@ -236,12 +236,12 @@ void *UpdateTicks(void *context) {
if (sleepTime.tv_sec <= 1) { if (sleepTime.tv_sec <= 1) {
nanosleep(&sleepTime, NULL); nanosleep(&sleepTime, NULL);
} else { } else {
sendJavaMsg(env, pctx->jniHelperObj, statusId, sendJavaMsg(env, pctx->jniHandlerObj, statusId,
"TickerThread error: processing too long!"); "TickerThread error: processing too long!");
} }
} }
sendJavaMsg(env, pctx->jniHelperObj, statusId, sendJavaMsg(env, pctx->jniHandlerObj, statusId,
"TickerThread status: ticking stopped"); "TickerThread status: ticking stopped");
(*javaVM)->DetachCurrentThread(javaVM); (*javaVM)->DetachCurrentThread(javaVM);
return context; return context;