bump product version to 5.0.4.1
[LibreOffice.git] / sal / android / libreofficekit-jni.c
blob41fa97e8273df724164c637a9da286cce9ade922
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <fcntl.h>
11 #include <errno.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/mman.h>
16 #include <sys/stat.h>
17 #include <sys/time.h>
18 #include <sys/types.h>
19 #include <time.h>
21 #include <jni.h>
23 #include <android/log.h>
25 #include <osl/detail/android-bootstrap.h>
27 #include <LibreOfficeKit/LibreOfficeKit.h>
29 #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "LibreOfficeKit", __VA_ARGS__))
30 #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "LibreOfficeKit", __VA_ARGS__))
32 /* These are valid / used in all apps. */
33 extern const char* data_dir;
34 extern const char* cache_dir;
35 extern void* apk_file;
36 extern int apk_file_size;
38 extern void Java_org_libreoffice_android_Bootstrap_putenv(JNIEnv* env, jobject clazz, jstring string);
39 extern void Java_org_libreoffice_android_Bootstrap_redirect_1stdio(JNIEnv* env, jobject clazz, jboolean state);
41 extern LibreOfficeKit* libreofficekit_hook(const char* install_path);
43 static char *full_program_dir = NULL;
45 /// Call the same method from Bootstrap.
46 __attribute__ ((visibility("default")))
47 void
48 Java_org_libreoffice_kit_LibreOfficeKit_putenv
49 (JNIEnv* env, jobject clazz, jstring string)
51 Java_org_libreoffice_android_Bootstrap_putenv(env, clazz, string);
54 /// Call the same method from Bootstrap.
55 __attribute__ ((visibility("default")))
56 void Java_org_libreoffice_kit_LibreOfficeKit_redirectStdio
57 (JNIEnv* env, jobject clazz, jboolean state)
59 Java_org_libreoffice_android_Bootstrap_redirect_1stdio(env, clazz, state);
62 /// Initialize the LibreOfficeKit.
63 __attribute__ ((visibility("default")))
64 jboolean Java_org_libreoffice_kit_LibreOfficeKit_initializeNative
65 (JNIEnv* env, jobject clazz,
66 jstring dataDir, jstring cacheDir, jstring apkFile)
68 struct stat st;
69 int fd;
70 const char *dataDirPath;
71 const char *cacheDirPath;
72 const char *apkFilePath;
74 const char program_dir[] = "/program";
75 size_t data_dir_len;
77 (void) clazz;
79 dataDirPath = (*env)->GetStringUTFChars(env, dataDir, NULL);
80 data_dir = strdup(dataDirPath);
81 (*env)->ReleaseStringUTFChars(env, dataDir, dataDirPath);
83 cacheDirPath = (*env)->GetStringUTFChars(env, cacheDir, NULL);
84 cache_dir = strdup(cacheDirPath);
85 (*env)->ReleaseStringUTFChars(env, cacheDir, cacheDirPath);
87 apkFilePath = (*env)->GetStringUTFChars(env, apkFile, NULL);
89 fd = open(apkFilePath, O_RDONLY);
90 if (fd == -1) {
91 LOGE("Could not open %s", apkFilePath);
92 (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath);
93 return JNI_FALSE;
95 if (fstat(fd, &st) == -1) {
96 LOGE("Could not fstat %s", apkFilePath);
97 close(fd);
98 (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath);
99 return JNI_FALSE;
101 apk_file = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
102 close(fd);
104 if (apk_file == MAP_FAILED) {
105 LOGE("Could not mmap %s", apkFilePath);
106 (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath);
107 return JNI_FALSE;
109 apk_file_size = st.st_size;
111 (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath);
113 if (!setup_cdir())
115 LOGE("setup_cdir failed");
116 return JNI_FALSE;
119 if (!setup_assets_tree())
121 LOGE("setup_assets_tree failed");
122 return JNI_FALSE;
125 // Extract files from the .apk that can't be used mmapped directly from it
126 extract_files(UNPACK_TREE, UNPACK_TREE, 0);
127 extract_files(UNPACK_TREE_GZ, UNPACK_TREE_GZ, 1);
129 // LibreOfficeKit expects a path to the program/ directory
130 free(full_program_dir);
131 data_dir_len = strlen(data_dir);
132 full_program_dir = malloc(data_dir_len + sizeof(program_dir));
134 strncpy(full_program_dir, data_dir, data_dir_len);
135 strncpy(full_program_dir + data_dir_len, program_dir, sizeof(program_dir));
137 // Initialize LibreOfficeKit
138 if (!libreofficekit_hook(full_program_dir))
140 LOGE("libreofficekit_hook returned null");
141 return JNI_FALSE;
144 LOGI("LibreOfficeKit successfully initialized");
146 return JNI_TRUE;
149 __attribute__ ((visibility("default")))
150 jobject Java_org_libreoffice_kit_LibreOfficeKit_getLibreOfficeKitHandle
151 (JNIEnv* env, jobject clazz)
153 LibreOfficeKit* aOffice;
155 (void) env;
156 (void) clazz;
158 aOffice = libreofficekit_hook(full_program_dir);
160 return (*env)->NewDirectByteBuffer(env, (void*) aOffice, sizeof(LibreOfficeKit));
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */