1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
18 #include <sys/types.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")))
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
)
70 const char *dataDirPath
;
71 const char *cacheDirPath
;
72 const char *apkFilePath
;
74 const char program_dir
[] = "/program";
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
);
91 LOGE("Could not open %s", apkFilePath
);
92 (*env
)->ReleaseStringUTFChars(env
, apkFile
, apkFilePath
);
95 if (fstat(fd
, &st
) == -1) {
96 LOGE("Could not fstat %s", apkFilePath
);
98 (*env
)->ReleaseStringUTFChars(env
, apkFile
, apkFilePath
);
101 apk_file
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
104 if (apk_file
== MAP_FAILED
) {
105 LOGE("Could not mmap %s", apkFilePath
);
106 (*env
)->ReleaseStringUTFChars(env
, apkFile
, apkFilePath
);
109 apk_file_size
= st
.st_size
;
111 (*env
)->ReleaseStringUTFChars(env
, apkFile
, apkFilePath
);
115 LOGE("setup_cdir failed");
119 if (!setup_assets_tree())
121 LOGE("setup_assets_tree failed");
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");
144 LOGI("LibreOfficeKit successfully initialized");
149 __attribute__ ((visibility("default")))
150 jobject Java_org_libreoffice_kit_LibreOfficeKit_getLibreOfficeKitHandle
151 (JNIEnv
* env
, jobject clazz
)
153 LibreOfficeKit
* aOffice
;
158 aOffice
= libreofficekit_hook(full_program_dir
);
160 return (*env
)->NewDirectByteBuffer(env
, (void*) aOffice
, sizeof(LibreOfficeKit
));
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */