jboolean Java_org_libreoffice_kit_LibreOfficeKit_initializeNative (JNIEnv* env, jobject clazz, jstring dataDir, jstring cacheDir, jstring apkFile) { struct stat st; int fd; const char *dataDirPath; const char *cacheDirPath; const char *apkFilePath; const char program_dir[] = "/program"; size_t data_dir_len; (void) clazz; dataDirPath = (*env)->GetStringUTFChars(env, dataDir, NULL); data_dir = strdup(dataDirPath); (*env)->ReleaseStringUTFChars(env, dataDir, dataDirPath); cacheDirPath = (*env)->GetStringUTFChars(env, cacheDir, NULL); cache_dir = strdup(cacheDirPath); (*env)->ReleaseStringUTFChars(env, cacheDir, cacheDirPath); apkFilePath = (*env)->GetStringUTFChars(env, apkFile, NULL); fd = open(apkFilePath, O_RDONLY); if (fd == -1) { LOGE("Could not open %s", apkFilePath); (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath); return JNI_FALSE; } if (fstat(fd, &st) == -1) { LOGE("Could not fstat %s", apkFilePath); close(fd); (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath); return JNI_FALSE; } apk_file = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); close(fd); if (apk_file == MAP_FAILED) { LOGE("Could not mmap %s", apkFilePath); (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath); return JNI_FALSE; } apk_file_size = st.st_size; (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath); if (!setup_cdir()) { LOGE("setup_cdir failed"); return JNI_FALSE; } if (!setup_assets_tree()) { LOGE("setup_assets_tree failed"); return JNI_FALSE; } // Extract files from the .apk that can't be used mmapped directly from it extract_files(UNPACK_TREE, UNPACK_TREE, 0); extract_files(UNPACK_TREE_GZ, UNPACK_TREE_GZ, 1); // LibreOfficeKit expects a path to the program/ directory free(full_program_dir); data_dir_len = strlen(data_dir); full_program_dir = malloc(data_dir_len + sizeof(program_dir)); strncpy(full_program_dir, data_dir, data_dir_len); strncpy(full_program_dir + data_dir_len, program_dir, sizeof(program_dir)); // Initialize LibreOfficeKit if (!libreofficekit_hook(full_program_dir)) { LOGE("libreofficekit_hook returned null"); return JNI_FALSE; } LOGI("LibreOfficeKit successfully initialized"); return JNI_TRUE; }
jboolean libreofficekit_initialize(JNIEnv* env, jstring dataDir, jstring cacheDir, jstring apkFile, jobject assetManager) { struct stat st; int fd; const char *dataDirPath; const char *cacheDirPath; const char *apkFilePath; const char *fontsConf = "/etc/fonts/fonts.conf"; char *fontsConfPath; setenv("OOO_DISABLE_RECOVERY", "1", 1); native_asset_manager = AAssetManager_fromJava(env, assetManager); dataDirPath = (*env)->GetStringUTFChars(env, dataDir, NULL); data_dir = strdup(dataDirPath); (*env)->ReleaseStringUTFChars(env, dataDir, dataDirPath); cacheDirPath = (*env)->GetStringUTFChars(env, cacheDir, NULL); cache_dir = strdup(cacheDirPath); (*env)->ReleaseStringUTFChars(env, cacheDir, cacheDirPath); // TMPDIR is used by osl_getTempDirURL() setenv("TMPDIR", cache_dir, 1); fontsConfPath = malloc(strlen(data_dir) + sizeof(fontsConf)); strcpy(fontsConfPath, data_dir); strcat(fontsConfPath, fontsConf); fd = open(fontsConfPath, O_RDONLY); if (fd != -1) { close(fd); LOGI("Setting FONTCONFIG_FILE to %s", fontsConfPath); setenv("FONTCONFIG_FILE", fontsConfPath, 1); } free(fontsConfPath); apkFilePath = (*env)->GetStringUTFChars(env, apkFile, NULL); fd = open(apkFilePath, O_RDONLY); if (fd == -1) { LOGE("Could not open %s", apkFilePath); (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath); return JNI_FALSE; } if (fstat(fd, &st) == -1) { LOGE("Could not fstat %s", apkFilePath); close(fd); (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath); return JNI_FALSE; } apk_file = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); close(fd); if (apk_file == MAP_FAILED) { LOGE("Could not mmap %s", apkFilePath); (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath); return JNI_FALSE; } apk_file_size = st.st_size; (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath); if (!setup_cdir()) { LOGE("setup_cdir failed"); return JNI_FALSE; } if (!setup_assets_tree()) { LOGE("setup_assets_tree failed"); return JNI_FALSE; } LOGI("LibreOfficeKit: libreofficekit_initialize finished"); return JNI_TRUE; }