[Libreoffice-commits] online.git: android/app
Libreoffice Gerrit user
logerrit at kemper.freedesktop.org
Sat Feb 16 00:55:52 UTC 2019
android/app/src/main/cpp/androidapp.cpp | 45 +++++++---
android/app/src/main/java/org/libreoffice/androidapp/MainActivity.java | 13 ++
2 files changed, 43 insertions(+), 15 deletions(-)
New commits:
commit 8cd64c8cd8a3be44dbcef76b013299459b103a45
Author: Jan Holesovsky <kendy at collabora.com>
AuthorDate: Sat Feb 16 01:51:54 2019 +0100
Commit: Jan Holesovsky <kendy at collabora.com>
CommitDate: Sat Feb 16 01:51:54 2019 +0100
android: Make sending to JavaScript work.
I haven't tested yet if the message actually gets to the JS, but
definitely the callFakeWebsocketOnMessage() is called and the WebView
does not complain that it is called from another thread.
Change-Id: I0e32d5a7d8937d754ac9dc6aa4ef763b56b44a52
diff --git a/android/app/src/main/cpp/androidapp.cpp b/android/app/src/main/cpp/androidapp.cpp
index ab6f1586e..a1d393e07 100644
--- a/android/app/src/main/cpp/androidapp.cpp
+++ b/android/app/src/main/cpp/androidapp.cpp
@@ -30,8 +30,21 @@ static std::string fileURL;
static LOOLWSD *loolwsd = nullptr;
static int fakeClientFd;
static int closeNotificationPipeForForwardingThread[2];
+static JavaVM* javaVM = nullptr;
-static void send2JS(JNIEnv *env, jobject obj, const std::vector<char>& buffer)
+extern "C" JNIEXPORT jint JNICALL
+JNI_OnLoad(JavaVM* vm, void*) {
+ javaVM = vm;
+
+ JNIEnv* env;
+ if (vm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK) {
+ return JNI_ERR; // JNI version not supported.
+ }
+
+ return JNI_VERSION_1_6;
+}
+
+static void send2JS(jclass mainActivityClz, jobject mainActivityObj, const std::vector<char>& buffer)
{
LOG_TRC_NOFILE("Send to JS: " << LOOLProtocol::getAbbreviatedMessage(buffer.data(), buffer.size()));
@@ -87,17 +100,25 @@ static void send2JS(JNIEnv *env, jobject obj, const std::vector<char>& buffer)
LOG_TRC_NOFILE( "Sending to JavaScript: " << subjs);
- /* TODO commented out, see the other TODO wrt. the NewGlobalRef
+ JNIEnv *env;
+ jint res = javaVM->GetEnv((void**)&env, JNI_VERSION_1_6);
+ if (res != JNI_OK) {
+ LOG_TRC_NOFILE("GetEnv need to attach thread");
+ res = javaVM->AttachCurrentThread(&env, nullptr);
+ if (JNI_OK != res) {
+ LOG_TRC_NOFILE("Failed to AttachCurrentThread");
+ return;
+ }
+ }
+
jstring jstr = env->NewStringUTF(js.c_str());
- jclass clazz = env->FindClass("org/libreoffice/androidapp/MainActivity");
- jmethodID callFakeWebsocket = env->GetMethodID(clazz, "callFakeWebsocketOnMessage", "(V)Ljava/lang/String;");
- env->CallObjectMethod(obj, callFakeWebsocket, jstr);
- */
+ jmethodID callFakeWebsocket = env->GetMethodID(mainActivityClz, "callFakeWebsocketOnMessage", "(Ljava/lang/String;)V");
+ env->CallVoidMethod(mainActivityObj, callFakeWebsocket, jstr);
}
/// Handle a message from JavaScript.
extern "C" JNIEXPORT void JNICALL
-Java_org_libreoffice_androidapp_MainActivity_postMobileMessage(JNIEnv *env, jobject obj, jstring message)
+Java_org_libreoffice_androidapp_MainActivity_postMobileMessage(JNIEnv *env, jobject instance, jstring message)
{
const char *string_value = env->GetStringUTFChars(message, nullptr);
@@ -119,9 +140,11 @@ Java_org_libreoffice_androidapp_MainActivity_postMobileMessage(JNIEnv *env, jobj
fakeSocketPipe2(closeNotificationPipeForForwardingThread);
// Start another thread to read responses and forward them to the JavaScript
- // TODO here we actually need to do NewGlobalRef and pass that to
- // the thread; not the env and obj itself
- std::thread([&env, &obj]
+ jclass clz = env->GetObjectClass(instance);
+ jclass mainActivityClz = (jclass) env->NewGlobalRef(clz);
+ jobject mainActivityObj = env->NewGlobalRef(instance);
+
+ std::thread([&mainActivityClz, &mainActivityObj]
{
Util::setThreadName("app2js");
while (true)
@@ -156,7 +179,7 @@ Java_org_libreoffice_androidapp_MainActivity_postMobileMessage(JNIEnv *env, jobj
return;
std::vector<char> buf(n);
n = fakeSocketRead(fakeClientFd, buf.data(), n);
- send2JS(env, obj, buf);
+ send2JS(mainActivityClz, mainActivityObj, buf);
}
}
else
diff --git a/android/app/src/main/java/org/libreoffice/androidapp/MainActivity.java b/android/app/src/main/java/org/libreoffice/androidapp/MainActivity.java
index 173513e5c..a2027d611 100644
--- a/android/app/src/main/java/org/libreoffice/androidapp/MainActivity.java
+++ b/android/app/src/main/java/org/libreoffice/androidapp/MainActivity.java
@@ -124,7 +124,7 @@ public class MainActivity extends AppCompatActivity {
String cacheDir = getApplication().getCacheDir().getAbsolutePath();
String apkFile = getApplication().getPackageResourcePath();
- String urlToLoad = "file:///android_asset/dist/hello-world.odt";
+ String urlToLoad = "file:///assets/hello-world.odt";
createLOOLWSD(dataDir, cacheDir, apkFile, assetManager, urlToLoad);
@@ -176,9 +176,14 @@ public class MainActivity extends AppCompatActivity {
}
/** Passing message the other way around - from Java to the FakeWebSocket in JS. */
- void callFakeWebsocketOnMessage(String message) {
- Log.i(TAG,"Got JavaScript, forwarding to the WebView: " + message);
- mWebView.loadUrl("javascript:window.TheFakeWebSocket.onmessage({'data': '" + message + "'});");
+ void callFakeWebsocketOnMessage(final String message) {
+ // call from the UI thread
+ mWebView.post(new Runnable() {
+ public void run() {
+ Log.i(TAG,"Forwarding to the WebView: " + message);
+ mWebView.loadUrl("javascript:window.TheFakeWebSocket.onmessage({'data': '" + message + "'});");
+ }
+ });
}
}
More information about the Libreoffice-commits
mailing list