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