From fe69fb5371b193288fcb1b38d70c9fd4ac4fd74a Mon Sep 17 00:00:00 2001 From: Assam Boudjelthia Date: Wed, 4 Mar 2020 16:26:51 +0200 Subject: Introduce new example JNI Messenger This examples shows the proper way to communication between QML/C++ and Java in both ways. This example calls a Java method from QML and C++, then the Java method will also call back a C++ function that will be forwarded to a QML connection. Task-number: QTBUG-80717 Change-Id: I96d9551d1abcaf80899c4dd3c727a267fb6f3c07 Reviewed-by: BogDan Vatra --- examples/androidextras/androidextras.pro | 9 +- .../jnimessenger/android/AndroidManifest.xml | 87 +++++++++++++ .../android/res/drawable-hdpi/icon.png | Bin 0 -> 3654 bytes .../android/res/drawable-ldpi/icon.png | Bin 0 -> 1432 bytes .../android/res/drawable-mdpi/icon.png | Bin 0 -> 1998 bytes .../android/res/drawable-xhdpi/icon.png | Bin 0 -> 4549 bytes .../android/res/drawable-xxhdpi/icon.png | Bin 0 -> 9275 bytes .../android/res/drawable-xxxhdpi/icon.png | Bin 0 -> 12866 bytes .../example/jnimessenger/JniMessenger.java | 64 +++++++++ .../src/qtandroidextras-example-jnimessenger.qdoc | 145 +++++++++++++++++++++ .../androidextras/jnimessenger/jnimessenger.cpp | 86 ++++++++++++ examples/androidextras/jnimessenger/jnimessenger.h | 74 +++++++++++ .../androidextras/jnimessenger/jnimessenger.pro | 21 +++ examples/androidextras/jnimessenger/main.cpp | 76 +++++++++++ examples/androidextras/jnimessenger/main.qml | 103 +++++++++++++++ examples/androidextras/jnimessenger/qml.qrc | 5 + src/androidextras/doc/images/jnimessenger.png | Bin 0 -> 34630 bytes 17 files changed, 668 insertions(+), 2 deletions(-) create mode 100644 examples/androidextras/jnimessenger/android/AndroidManifest.xml create mode 100644 examples/androidextras/jnimessenger/android/res/drawable-hdpi/icon.png create mode 100644 examples/androidextras/jnimessenger/android/res/drawable-ldpi/icon.png create mode 100644 examples/androidextras/jnimessenger/android/res/drawable-mdpi/icon.png create mode 100644 examples/androidextras/jnimessenger/android/res/drawable-xhdpi/icon.png create mode 100644 examples/androidextras/jnimessenger/android/res/drawable-xxhdpi/icon.png create mode 100644 examples/androidextras/jnimessenger/android/res/drawable-xxxhdpi/icon.png create mode 100644 examples/androidextras/jnimessenger/android/src/org/qtproject/example/jnimessenger/JniMessenger.java create mode 100644 examples/androidextras/jnimessenger/doc/src/qtandroidextras-example-jnimessenger.qdoc create mode 100644 examples/androidextras/jnimessenger/jnimessenger.cpp create mode 100644 examples/androidextras/jnimessenger/jnimessenger.h create mode 100644 examples/androidextras/jnimessenger/jnimessenger.pro create mode 100644 examples/androidextras/jnimessenger/main.cpp create mode 100644 examples/androidextras/jnimessenger/main.qml create mode 100644 examples/androidextras/jnimessenger/qml.qrc create mode 100644 src/androidextras/doc/images/jnimessenger.png diff --git a/examples/androidextras/androidextras.pro b/examples/androidextras/androidextras.pro index ce3926f..7239503 100644 --- a/examples/androidextras/androidextras.pro +++ b/examples/androidextras/androidextras.pro @@ -1,7 +1,12 @@ TEMPLATE = subdirs android { qtHaveModule(quick) { - SUBDIRS += notification - EXAMPLE_FILES += notification + SUBDIRS += \ + notification \ + jnimessenger + + EXAMPLE_FILES += \ + notification \ + jnimessenger } } diff --git a/examples/androidextras/jnimessenger/android/AndroidManifest.xml b/examples/androidextras/jnimessenger/android/AndroidManifest.xml new file mode 100644 index 0000000..56617c4 --- /dev/null +++ b/examples/androidextras/jnimessenger/android/AndroidManifest.xml @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/androidextras/jnimessenger/android/res/drawable-hdpi/icon.png b/examples/androidextras/jnimessenger/android/res/drawable-hdpi/icon.png new file mode 100644 index 0000000..d3ccebe Binary files /dev/null and b/examples/androidextras/jnimessenger/android/res/drawable-hdpi/icon.png differ diff --git a/examples/androidextras/jnimessenger/android/res/drawable-ldpi/icon.png b/examples/androidextras/jnimessenger/android/res/drawable-ldpi/icon.png new file mode 100644 index 0000000..2194be1 Binary files /dev/null and b/examples/androidextras/jnimessenger/android/res/drawable-ldpi/icon.png differ diff --git a/examples/androidextras/jnimessenger/android/res/drawable-mdpi/icon.png b/examples/androidextras/jnimessenger/android/res/drawable-mdpi/icon.png new file mode 100644 index 0000000..31812cc Binary files /dev/null and b/examples/androidextras/jnimessenger/android/res/drawable-mdpi/icon.png differ diff --git a/examples/androidextras/jnimessenger/android/res/drawable-xhdpi/icon.png b/examples/androidextras/jnimessenger/android/res/drawable-xhdpi/icon.png new file mode 100644 index 0000000..3aeae64 Binary files /dev/null and b/examples/androidextras/jnimessenger/android/res/drawable-xhdpi/icon.png differ diff --git a/examples/androidextras/jnimessenger/android/res/drawable-xxhdpi/icon.png b/examples/androidextras/jnimessenger/android/res/drawable-xxhdpi/icon.png new file mode 100644 index 0000000..f754fd1 Binary files /dev/null and b/examples/androidextras/jnimessenger/android/res/drawable-xxhdpi/icon.png differ diff --git a/examples/androidextras/jnimessenger/android/res/drawable-xxxhdpi/icon.png b/examples/androidextras/jnimessenger/android/res/drawable-xxxhdpi/icon.png new file mode 100644 index 0000000..d0d043b Binary files /dev/null and b/examples/androidextras/jnimessenger/android/res/drawable-xxxhdpi/icon.png differ diff --git a/examples/androidextras/jnimessenger/android/src/org/qtproject/example/jnimessenger/JniMessenger.java b/examples/androidextras/jnimessenger/android/src/org/qtproject/example/jnimessenger/JniMessenger.java new file mode 100644 index 0000000..e3b7408 --- /dev/null +++ b/examples/androidextras/jnimessenger/android/src/org/qtproject/example/jnimessenger/JniMessenger.java @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtAndroidExtras module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +package org.qtproject.example.jnimessenger; + +public class JniMessenger +{ + private static native void callFromJava(String message); + + public JniMessenger() {} + + public static void printFromJava(String message) + { + System.out.println("This is printed from JAVA, message is: " + message); + callFromJava("Hello from JAVA!"); + } +} diff --git a/examples/androidextras/jnimessenger/doc/src/qtandroidextras-example-jnimessenger.qdoc b/examples/androidextras/jnimessenger/doc/src/qtandroidextras-example-jnimessenger.qdoc new file mode 100644 index 0000000..ddebcce --- /dev/null +++ b/examples/androidextras/jnimessenger/doc/src/qtandroidextras-example-jnimessenger.qdoc @@ -0,0 +1,145 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtAndroidExtras module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \title Qt JNI Messenger + \ingroup examples-qtandroidextras + \example jnimessenger + \brief Demonstrates communication between Java code and QML or C++ using NJI calls. + + \image jnimessenger.png + + This example demonstrates how to add a custom Java class to an Android application, and + how to both call it from C++ and call C++ functions from Java using the JNI convenience + APIs in the Qt Android Extras module. The application UI is created by using Qt Quick. + + When clicking the send button, a message will be sent from QML to Java class though + the C++ class and a log of that is shown in the screen view. Logs also can be seen + from the Android logcat of the messages being exchanged, which would be similar to: + + \badcode + I System.out: This is printed from JAVA, message is: QML sending to Java: Hello from QML + D libjnimessenger_armeabi-v7a.so: qml: QML received a message: Hello from JAVA! + \endcode + + \include examples-run.qdocinc + + \section1 Calling Java Methods from C++ Code + + We define a custom Java class called \c JniMessenger in the JniMessenger.java file: + + \quotefromfile jnimessenger/android/src/org/qtproject/example/jnimessenger/JniMessenger.java + \skipto org.qtproject.example.jnimessenger + \printuntil /^\}/ + + \note The custom Java class can extend other classes like QtActivity, Activity or any + other Java class. + + In the jnimessenger.cpp file, we call the function \c printFromJava(String message) + by first creating a \c QAndroidJniObject for the Java String that we want to send + and then invoking a JNI call with \c callStaticMethod<>() while providing the method + signature: + + \quotefromfile jnimessenger/jnimessenger.cpp + \skipto void JniMessenger::printFromJava + \printuntil } + + That call will then execute the following from Java side, which would print the + message to the \c System.output. + + \quotefromfile jnimessenger/android/src/org/qtproject/example/jnimessenger/JniMessenger.java + \skipto public static void printFromJava + \printuntil ); + + \section2 Calling QML/C++ Functions from Java Code + + Directly after that, our native function \c callFromJava(String message) will be + called, which would be then handled from C++ side. Note, that this method has + to be defined as \c native at the top of the Java class as: + + \code + private static native void callFromJava(String message); + \endcode + + To be able to call C++ functions from Java, in our C++ class JniMessenger.cpp, + we need to define those functions using \c RegisterNatives() as follows: + + \quotefromfile jnimessenger/jnimessenger.cpp + \skipto JNINativeMethod + \printuntil } + + (See \l{Java Native Methods} for more details). + + We would need to register the functions' signatures in \c methods[], which have + the name in Java class, then its parameters and return types, then the function + pointer in the C++ code. + + \code + JNINativeMethod methods[] {{"callFromJava", "(Ljava/lang/String;)V", reinterpret_cast(callFromJava)}}; + \endcode + + This would insure that our C++ function is available from within the Java call. + Now, that function could simply print the message it received from Java to + the debug log, but we want to forward the received message to the QML components + so that it gets displayed in our text view, so we get: + + \quotefromfile jnimessenger/jnimessenger.cpp + \skipto static void callFromJava + \printuntil } + + Now, we need to implement the necessary \c Connections in the QML code to receive + the message from C++, which we would print into the \c Text view with the id \c messengerLog: + + \quotefromfile jnimessenger/main.qml + \skipto Connections + \printuntil /^\ {4}\}/ + + \sa {Qt for Android}, {Qt Android Extras} +*/ diff --git a/examples/androidextras/jnimessenger/jnimessenger.cpp b/examples/androidextras/jnimessenger/jnimessenger.cpp new file mode 100644 index 0000000..3c309ba --- /dev/null +++ b/examples/androidextras/jnimessenger/jnimessenger.cpp @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtAndroidExtras module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "jnimessenger.h" + +#include +#include +#include + +JniMessenger *JniMessenger::m_instance = nullptr; + +static void callFromJava(JNIEnv *env, jobject /*thiz*/, jstring value) +{ + emit JniMessenger::instance()->messageFromJava(env->GetStringUTFChars(value, nullptr)); +} + +JniMessenger::JniMessenger(QObject *parent) : QObject(parent) +{ + m_instance = this; + + JNINativeMethod methods[] {{"callFromJava", "(Ljava/lang/String;)V", reinterpret_cast(callFromJava)}}; + QAndroidJniObject javaClass("org/qtproject/example/jnimessenger/JniMessenger"); + + QAndroidJniEnvironment env; + jclass objectClass = env->GetObjectClass(javaClass.object()); + env->RegisterNatives(objectClass, + methods, + sizeof(methods) / sizeof(methods[0])); + env->DeleteLocalRef(objectClass); +} + +void JniMessenger::printFromJava(const QString &message) +{ + QAndroidJniObject javaMessage = QAndroidJniObject::fromString(message); + QAndroidJniObject::callStaticMethod("org/qtproject/example/jnimessenger/JniMessenger", + "printFromJava", + "(Ljava/lang/String;)V", + javaMessage.object()); +} diff --git a/examples/androidextras/jnimessenger/jnimessenger.h b/examples/androidextras/jnimessenger/jnimessenger.h new file mode 100644 index 0000000..441e476 --- /dev/null +++ b/examples/androidextras/jnimessenger/jnimessenger.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtAndroidExtras module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MYCLASS_H +#define MYCLASS_H + +#include + +class JniMessenger : public QObject +{ + Q_OBJECT + +public: + explicit JniMessenger(QObject *parent = nullptr); + static JniMessenger *instance() { return m_instance; } + Q_INVOKABLE void printFromJava(const QString &message); + +signals: + void messageFromJava(const QString &message); + +public slots: + +private: + static JniMessenger *m_instance; +}; + +#endif // MYCLASS_H diff --git a/examples/androidextras/jnimessenger/jnimessenger.pro b/examples/androidextras/jnimessenger/jnimessenger.pro new file mode 100644 index 0000000..b1a6326 --- /dev/null +++ b/examples/androidextras/jnimessenger/jnimessenger.pro @@ -0,0 +1,21 @@ +QT += quick androidextras + +DEFINES += QT_DEPRECATED_WARNINGS + +SOURCES += \ + jnimessenger.cpp \ + main.cpp + +HEADERS += \ + jnimessenger.h + +RESOURCES += qml.qrc + +ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android +DISTFILES += \ + android/src/org/qtproject/example/jnimessenger/JniMessenger.java \ + android/AndroidManifest.xml + + +target.path = $$[QT_INSTALL_EXAMPLES]/androidextras/jnimessenger +INSTALLS += target diff --git a/examples/androidextras/jnimessenger/main.cpp b/examples/androidextras/jnimessenger/main.cpp new file mode 100644 index 0000000..45e914a --- /dev/null +++ b/examples/androidextras/jnimessenger/main.cpp @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtAndroidExtras module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "jnimessenger.h" + +#include +#include +#include + +int main(int argc, char *argv[]) +{ + QGuiApplication app(argc, argv); + + QQmlApplicationEngine engine; + const QUrl url(QStringLiteral("qrc:/main.qml")); + + JniMessenger *jniMessenger = new JniMessenger(&app); + + engine.rootContext()->setContextProperty(QLatin1String("JniMessenger"), jniMessenger); + + QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, + &app, [url](QObject *obj, const QUrl &objUrl) { + if (!obj && url == objUrl) + QCoreApplication::exit(-1); + }, Qt::QueuedConnection); + engine.load(url); + + return app.exec(); +} diff --git a/examples/androidextras/jnimessenger/main.qml b/examples/androidextras/jnimessenger/main.qml new file mode 100644 index 0000000..57c90ec --- /dev/null +++ b/examples/androidextras/jnimessenger/main.qml @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtAndroidExtras module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.13 +import QtQuick.Window 2.13 +import QtQuick.Controls 2.13 +import QtQuick.Layouts 1.13 + +ApplicationWindow { + id: window + visible: true + + ColumnLayout { + anchors.fill: parent + Text { + id: messengerLog + text: qsTr("") + clip: true + Layout.fillHeight: true + Layout.fillWidth: true + transformOrigin: Item.Center + Layout.alignment: Qt.AlignHCenter | Qt.AlignTop + } + + RowLayout { + id: rowlayout + Layout.bottomMargin: 10 + Layout.alignment: Qt.AlignHCenter | Qt.AlignBottom + anchors.bottom: window.bottom + spacing: 10 + + Button { + text: qsTr("Send to Java via JNI") + onClicked: { + var message = qsTr("QML sending to Java: Hello from QML") + messengerLog.text += "\n" + message + JniMessenger.printFromJava(message) + } + } + + Button { + text: "Clear" + onClicked: messengerLog.text = "" + } + } + } + + Connections { + target: JniMessenger + function onMessageFromJava(message) { + var output = qsTr("QML received a message: %1").arg(message) + print(output) + messengerLog.text += "\n" + output + } + } +} diff --git a/examples/androidextras/jnimessenger/qml.qrc b/examples/androidextras/jnimessenger/qml.qrc new file mode 100644 index 0000000..5f6483a --- /dev/null +++ b/examples/androidextras/jnimessenger/qml.qrc @@ -0,0 +1,5 @@ + + + main.qml + + diff --git a/src/androidextras/doc/images/jnimessenger.png b/src/androidextras/doc/images/jnimessenger.png new file mode 100644 index 0000000..1cb4efd Binary files /dev/null and b/src/androidextras/doc/images/jnimessenger.png differ -- cgit v1.2.3