Bump version to 6.0-36
[LibreOffice.git] / desktop / source / lib / lokandroid.cxx
blobb85619d3dad8daf4d0e298b411ee7178fab0fc4a
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 <unistd.h>
11 #include <jni.h>
13 #include <sal/types.h>
14 #include <vcl/event.hxx>
16 #include <android/log.h>
18 #include <osl/detail/android-bootstrap.h>
20 #include <LibreOfficeKit/LibreOfficeKit.h>
22 /* LibreOfficeKit */
24 namespace
27 jfieldID getHandleField(JNIEnv* pEnv, jobject aObject)
29 jclass clazz = pEnv->GetObjectClass(aObject);
30 return pEnv->GetFieldID(clazz, "handle", "Ljava/nio/ByteBuffer;");
33 template <typename T>
34 T* getHandle(JNIEnv* pEnv, jobject aObject)
36 jobject aHandle = pEnv->GetObjectField(aObject, getHandleField(pEnv, aObject));
37 return reinterpret_cast<T*>(pEnv->GetDirectBufferAddress(aHandle));
40 const char* copyJavaString(JNIEnv* pEnv, jstring aJavaString)
42 const char* pClone = NULL;
44 const char* pTemp = pEnv->GetStringUTFChars(aJavaString, NULL);
45 pClone = strdup(pTemp);
46 pEnv->ReleaseStringUTFChars(aJavaString, pTemp);
48 return pClone;
51 } // anonymous namespace
53 extern "C" SAL_JNI_EXPORT jstring JNICALL Java_org_libreoffice_kit_Office_getError
54 (JNIEnv* pEnv, jobject aObject)
56 LibreOfficeKit* pLibreOfficeKit = getHandle<LibreOfficeKit>(pEnv, aObject);
57 char* pError = pLibreOfficeKit->pClass->getError(pLibreOfficeKit);
58 return pEnv->NewStringUTF(pError);
61 extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Office_destroy
62 (JNIEnv* pEnv, jobject aObject)
64 LibreOfficeKit* pLibreOfficeKit = getHandle<LibreOfficeKit>(pEnv, aObject);
65 pLibreOfficeKit->pClass->destroy(pLibreOfficeKit);
68 extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Office_destroyAndExit(JNIEnv* pEnv, jobject aObject)
70 LibreOfficeKit* pLibreOfficeKit = getHandle<LibreOfficeKit>(pEnv, aObject);
71 pLibreOfficeKit->pClass->destroy(pLibreOfficeKit);
72 // Stopgap fix: _exit() to force the OS to restart the LO activity.
73 // Better than to hang.
74 _exit(0);
77 namespace
80 struct CallbackData
82 jmethodID aJavaCallbackMethod;
83 jclass aClass;
84 jobject aObject;
87 static CallbackData gCallbackData;
88 static CallbackData gCallbackDataLOKit;
90 /**
91 * Handle retrieved callback
93 void messageCallback(int nType, const char* pPayload, void* pData)
95 CallbackData* pCallbackData = (CallbackData*) pData;
97 JavaVM* pJavaVM = lo_get_javavm();
98 JNIEnv* pEnv;
99 bool bIsAttached = false;
101 int status = pJavaVM->GetEnv((void **) &pEnv, JNI_VERSION_1_6);
103 if(status < 0)
105 status = pJavaVM->AttachCurrentThread(&pEnv, NULL);
106 if(status < 0)
108 return;
110 bIsAttached = true;
113 jstring sPayload = pEnv->NewStringUTF(pPayload);
115 jvalue aParameter[2];
116 aParameter[0].i = nType;
117 aParameter[1].l = sPayload;
119 pEnv->CallVoidMethodA(pCallbackData->aObject, pCallbackData->aJavaCallbackMethod, aParameter);
121 pEnv->DeleteLocalRef(sPayload);
123 if (bIsAttached)
125 pJavaVM->DetachCurrentThread();
129 } // anonymous namespace
131 extern "C" SAL_JNI_EXPORT jobject JNICALL Java_org_libreoffice_kit_Office_documentLoadNative
132 (JNIEnv* pEnv, jobject aObject, jstring documentPath)
134 const char* aCloneDocumentPath = copyJavaString(pEnv, documentPath);
135 LibreOfficeKit* pLibreOfficeKit = getHandle<LibreOfficeKit>(pEnv, aObject);
137 LibreOfficeKitDocument* pDocument = pLibreOfficeKit->pClass->documentLoad(pLibreOfficeKit, aCloneDocumentPath);
139 if (pDocument == NULL)
140 return NULL;
142 jobject aHandle = pEnv->NewDirectByteBuffer((void*) pDocument, sizeof(LibreOfficeKitDocument));
144 return aHandle;
147 extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Office_setDocumentPassword
148 (JNIEnv* pEnv, jobject aObject, jstring sUrl, jstring sPassword)
150 LibreOfficeKit* pLibreOfficeKit = getHandle<LibreOfficeKit>(pEnv, aObject);
152 char const* pUrl = copyJavaString(pEnv, sUrl);
153 if (sPassword == NULL) {
154 pLibreOfficeKit->pClass->setDocumentPassword(pLibreOfficeKit, pUrl, nullptr);
155 } else {
156 char const* pPassword = copyJavaString(pEnv, sPassword);
157 pLibreOfficeKit->pClass->setDocumentPassword(pLibreOfficeKit, pUrl, pPassword);
161 extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Office_setOptionalFeatures
162 (JNIEnv* pEnv, jobject aObject, jlong options)
164 LibreOfficeKit* pLibreOfficeKit = getHandle<LibreOfficeKit>(pEnv, aObject);
166 unsigned long long pOptions = (unsigned long long)options;
168 pLibreOfficeKit->pClass->setOptionalFeatures(pLibreOfficeKit, pOptions);
171 /** Implementation of org.libreoffice.kit.Office.bindMessageCallback method */
172 extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Office_bindMessageCallback
173 (JNIEnv* pEnv, jobject aObject)
175 LibreOfficeKit* pLibreOfficeKit = getHandle<LibreOfficeKit>(pEnv, aObject);
177 gCallbackDataLOKit.aObject = (jobject) pEnv->NewGlobalRef(aObject);
178 jclass aClass = pEnv->GetObjectClass(aObject);
179 gCallbackDataLOKit.aClass = (jclass) pEnv->NewGlobalRef(aClass);
181 gCallbackDataLOKit.aJavaCallbackMethod = pEnv->GetMethodID(aClass, "messageRetrievedLOKit", "(ILjava/lang/String;)V");
183 pLibreOfficeKit->pClass->registerCallback(pLibreOfficeKit, messageCallback, (void*) &gCallbackDataLOKit);
186 /* Document */
188 /** Implementation of org.libreoffice.kit.Document.bindMessageCallback method */
189 extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_bindMessageCallback
190 (JNIEnv* pEnv, jobject aObject)
192 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
194 gCallbackData.aObject = (jobject) pEnv->NewGlobalRef(aObject);
195 jclass aClass = pEnv->GetObjectClass(aObject);
196 gCallbackData.aClass = (jclass) pEnv->NewGlobalRef(aClass);
198 gCallbackData.aJavaCallbackMethod = pEnv->GetMethodID(aClass, "messageRetrieved", "(ILjava/lang/String;)V");
200 pDocument->pClass->registerCallback(pDocument, messageCallback, (void*) &gCallbackData);
203 extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_destroy
204 (JNIEnv* pEnv, jobject aObject)
206 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
207 pDocument->pClass->destroy(pDocument);
210 extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_setPart
211 (JNIEnv* pEnv, jobject aObject, jint aPart)
213 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
214 pDocument->pClass->setPart(pDocument, aPart);
217 extern "C" SAL_JNI_EXPORT jint JNICALL Java_org_libreoffice_kit_Document_getPart
218 (JNIEnv* pEnv, jobject aObject)
220 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
221 return (jint) pDocument->pClass->getPart(pDocument);
224 extern "C" SAL_JNI_EXPORT jstring JNICALL Java_org_libreoffice_kit_Document_getPartPageRectangles
225 (JNIEnv* pEnv, jobject aObject)
227 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
228 char* pRectangles = pDocument->pClass->getPartPageRectangles(pDocument);
229 return pEnv->NewStringUTF(pRectangles);
232 extern "C" SAL_JNI_EXPORT jint JNICALL Java_org_libreoffice_kit_Document_getParts
233 (JNIEnv* pEnv, jobject aObject)
235 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
236 return (jint) pDocument->pClass->getParts(pDocument);
239 extern "C" SAL_JNI_EXPORT jstring JNICALL Java_org_libreoffice_kit_Document_getPartName
240 (JNIEnv* pEnv, jobject aObject, jint nPartIndex)
242 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
243 char* pPartName = pDocument->pClass->getPartName(pDocument, nPartIndex);
244 return pEnv->NewStringUTF(pPartName);
247 extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_setPartMode
248 (JNIEnv* pEnv, jobject aObject, jint nPartMode)
250 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
252 pDocument->pClass->setPartMode(pDocument, nPartMode);
255 extern "C" SAL_JNI_EXPORT jint JNICALL Java_org_libreoffice_kit_Document_getDocumentTypeNative
256 (JNIEnv* pEnv, jobject aObject)
258 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
259 return (jint) pDocument->pClass->getDocumentType(pDocument);
262 extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_paintTileNative
263 (JNIEnv* pEnv, jobject aObject, jobject aByteBuffer,
264 jint nCanvasWidth, jint nCanvasHeight, jint nTilePosX, jint nTilePosY,
265 jint nTileWidth, jint nTileHeight)
267 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
269 unsigned char* buffer = (unsigned char*) pEnv->GetDirectBufferAddress(aByteBuffer);
270 pDocument->pClass->paintTile(pDocument, buffer, nCanvasWidth, nCanvasHeight, nTilePosX, nTilePosY, nTileWidth, nTileHeight);
273 extern "C" SAL_JNI_EXPORT jlong JNICALL Java_org_libreoffice_kit_Document_getDocumentHeight
274 (JNIEnv* pEnv, jobject aObject)
276 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
277 long nWidth;
278 long nHeight;
279 pDocument->pClass->getDocumentSize(pDocument, &nWidth, &nHeight);
280 return nHeight;
283 extern "C" SAL_JNI_EXPORT jlong JNICALL Java_org_libreoffice_kit_Document_getDocumentWidth
284 (JNIEnv* pEnv, jobject aObject)
286 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
287 long nWidth;
288 long nHeight;
289 pDocument->pClass->getDocumentSize(pDocument, &nWidth, &nHeight);
290 return nWidth;
293 extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_initializeForRendering
294 (JNIEnv* pEnv, jobject aObject)
296 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
297 pDocument->pClass->initializeForRendering(pDocument, NULL);
300 extern "C" SAL_JNI_EXPORT jint JNICALL Java_org_libreoffice_kit_Document_saveAs
301 (JNIEnv* pEnv, jobject aObject, jstring sUrl, jstring sFormat, jstring sOptions)
303 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
305 const char* pUrl = pEnv->GetStringUTFChars(sUrl, NULL);
306 const char* pFormat = pEnv->GetStringUTFChars(sFormat, NULL);
307 const char* pOptions = pEnv->GetStringUTFChars(sOptions, NULL);
309 int result = pDocument->pClass->saveAs(pDocument, pUrl, pFormat, pOptions);
311 pEnv->ReleaseStringUTFChars(sUrl, pUrl);
312 pEnv->ReleaseStringUTFChars(sFormat, pFormat);
313 pEnv->ReleaseStringUTFChars(sOptions, pOptions);
315 return result;
318 extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_postKeyEvent
319 (JNIEnv* pEnv, jobject aObject, jint nType, jint nCharCode, jint nKeyCode)
321 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
322 pDocument->pClass->postKeyEvent(pDocument, nType, nCharCode, nKeyCode);
325 extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_postMouseEvent
326 (JNIEnv* pEnv, jobject aObject, jint type, jint x, jint y, jint count, jint button, jint modifier)
328 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
329 pDocument->pClass->postMouseEvent(pDocument, type, x, y, count, button, modifier);
332 extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_postUnoCommand
333 (JNIEnv* pEnv, jobject aObject, jstring command, jstring arguments)
335 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
337 const char* pCommand = pEnv->GetStringUTFChars(command, NULL);
338 const char* pArguments = nullptr;
339 if (arguments != NULL)
340 pArguments = pEnv->GetStringUTFChars(arguments, NULL);
342 pDocument->pClass->postUnoCommand(pDocument, pCommand, pArguments, false);
344 pEnv->ReleaseStringUTFChars(command, pCommand);
345 if (arguments != NULL)
346 pEnv->ReleaseStringUTFChars(arguments, pArguments);
349 extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_setTextSelection
350 (JNIEnv* pEnv, jobject aObject, jint type, jint x, jint y)
352 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
353 pDocument->pClass->setTextSelection(pDocument, type, x, y);
356 extern "C" SAL_JNI_EXPORT jstring JNICALL Java_org_libreoffice_kit_Document_getTextSelection
357 (JNIEnv* pEnv, jobject aObject, jstring mimeType)
359 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
361 const char* pMimeType = pEnv->GetStringUTFChars(mimeType, NULL);
363 char* pUsedMimeType = 0;
364 LibreOfficeKitDocumentClass* pcls = pDocument->pClass;
365 char* pSelection = pcls->getTextSelection(pDocument, pMimeType, &pUsedMimeType);
366 free(pUsedMimeType);
368 pEnv->ReleaseStringUTFChars(mimeType, pMimeType);
370 return pEnv->NewStringUTF(pSelection);
373 extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_setGraphicSelection
374 (JNIEnv* pEnv, jobject aObject, jint type, jint x, jint y)
376 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
377 pDocument->pClass->setGraphicSelection(pDocument, type, x, y);
380 extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_resetSelection
381 (JNIEnv* pEnv, jobject aObject)
383 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
384 pDocument->pClass->resetSelection(pDocument);
387 extern "C" SAL_JNI_EXPORT jstring JNICALL Java_org_libreoffice_kit_Document_getCommandValues
388 (JNIEnv* pEnv, jobject aObject, jstring command)
390 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
392 const char* pCommand = pEnv->GetStringUTFChars(command, NULL);
394 char* pValue = pDocument->pClass->getCommandValues(pDocument, pCommand);
396 pEnv->ReleaseStringUTFChars(command, pCommand);
398 return pEnv->NewStringUTF(pValue);
401 extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_setClientZoom
402 (JNIEnv* pEnv, jobject aObject, jint nTilePixelWidth, jint nTilePixelHeight, jint nTileTwipWidth, jint nTileTwipHeight)
404 LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
405 pDocument->pClass->setClientZoom(pDocument, nTilePixelWidth, nTilePixelHeight, nTileTwipWidth, nTileTwipHeight);
408 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */