From 103df69d2939dcd982266c4c6650aff132c313fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Sat, 14 Sep 2019 16:21:59 +0200 Subject: [PATCH] Init Chiaki Lib on Android --- android/app/CMakeLists.txt | 5 ++- android/app/src/main/cpp/chiaki-jni.c | 44 ++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/android/app/CMakeLists.txt b/android/app/CMakeLists.txt index 9d755a8..375ea7d 100644 --- a/android/app/CMakeLists.txt +++ b/android/app/CMakeLists.txt @@ -2,4 +2,7 @@ cmake_minimum_required(VERSION 3.2) add_library(chiaki-jni SHARED src/main/cpp/chiaki-jni.c) -target_link_libraries(chiaki-jni chiaki-lib) \ No newline at end of file +target_link_libraries(chiaki-jni chiaki-lib) + +find_library(ANDROID_LIB_LOG log) +target_link_libraries(chiaki-jni "${ANDROID_LIB_LOG}") \ No newline at end of file diff --git a/android/app/src/main/cpp/chiaki-jni.c b/android/app/src/main/cpp/chiaki-jni.c index ff10e06..2b0dc24 100644 --- a/android/app/src/main/cpp/chiaki-jni.c +++ b/android/app/src/main/cpp/chiaki-jni.c @@ -1,9 +1,51 @@ #include +#include #include +#include -JNIEXPORT jstring JNICALL Java_com_metallic_chiaki_lib_Chiaki_stringFromJNI(JNIEnv* env, jobject thiz) +#define LOG_TAG "Chiaki" + +static void log_cb_android(ChiakiLogLevel level, const char *msg, void *user) +{ + int prio; + switch(level) + { + case CHIAKI_LOG_DEBUG: + prio = ANDROID_LOG_DEBUG; + break; + case CHIAKI_LOG_VERBOSE: + prio = ANDROID_LOG_VERBOSE; + break; + case CHIAKI_LOG_INFO: + prio = ANDROID_LOG_INFO; + break; + case CHIAKI_LOG_WARNING: + prio = ANDROID_LOG_ERROR; + break; + case CHIAKI_LOG_ERROR: + prio = ANDROID_LOG_ERROR; + break; + default: + prio = ANDROID_LOG_INFO; + break; + } + __android_log_print(prio, LOG_TAG, "%s", msg); +} + +ChiakiLog log_ctx; + +JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) +{ + chiaki_log_init(&log_ctx, CHIAKI_LOG_ALL & ~CHIAKI_LOG_VERBOSE, log_cb_android, NULL); + CHIAKI_LOGI(&log_ctx, "Loading Chiaki Library"); + ChiakiErrorCode err = chiaki_lib_init(); + CHIAKI_LOGI(&log_ctx, "Chiaki Library Init Result: %s\n", chiaki_error_string(err)); + return JNI_VERSION_1_2; +} + +JNIEXPORT jstring JNICALL Java_com_metallic_chiaki_lib_Chiaki_stringFromJNI(JNIEnv *env, jobject thiz) { return (*env)->NewStringUTF(env, chiaki_error_string(CHIAKI_ERR_SUCCESS)); }