Added JNI convenience classes in QtPlatformSupport.
This is used for Android. Change-Id: I049138c140a472b1721390cf4ec2bd88bbe9c471 Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@digia.com> Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com> Reviewed-by: Paul Olav Tvete <paul.tvete@digia.com>
This commit is contained in:
parent
d28f454a67
commit
0666975d6c
9
src/platformsupport/jniconvenience/jniconvenience.pri
Normal file
9
src/platformsupport/jniconvenience/jniconvenience.pri
Normal file
@ -0,0 +1,9 @@
|
||||
android {
|
||||
QT += gui-private
|
||||
|
||||
HEADERS += $$PWD/qjnihelpers_p.h \
|
||||
$$PWD/qjniobject_p.h
|
||||
|
||||
SOURCES += $$PWD/qjnihelpers.cpp \
|
||||
$$PWD/qjniobject.cpp
|
||||
}
|
115
src/platformsupport/jniconvenience/qjnihelpers.cpp
Normal file
115
src/platformsupport/jniconvenience/qjnihelpers.cpp
Normal file
@ -0,0 +1,115 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
||||
** Contact: http://www.qt-project.org/legal
|
||||
**
|
||||
** This file is part of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:LGPL$
|
||||
** 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 Digia. For licensing terms and
|
||||
** conditions see http://qt.digia.com/licensing. For further information
|
||||
** use the contact form at http://qt.digia.com/contact-us.
|
||||
**
|
||||
** GNU Lesser General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU Lesser
|
||||
** General Public License version 2.1 as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.LGPL included in the
|
||||
** packaging of this file. Please review the following information to
|
||||
** ensure the GNU Lesser General Public License version 2.1 requirements
|
||||
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
||||
**
|
||||
** In addition, as a special exception, Digia gives you certain additional
|
||||
** rights. These rights are described in the Digia Qt LGPL Exception
|
||||
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3.0 as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.GPL included in the
|
||||
** packaging of this file. Please review the following information to
|
||||
** ensure the GNU General Public License version 3.0 requirements will be
|
||||
** met: http://www.gnu.org/copyleft/gpl.html.
|
||||
**
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "qjnihelpers_p.h"
|
||||
|
||||
#include <qpa/qplatformnativeinterface.h>
|
||||
#include <qguiapplication.h>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
QString qt_convertJString(jstring string)
|
||||
{
|
||||
QAttachedJNIEnv env;
|
||||
int strLength = env->GetStringLength(string);
|
||||
QString res;
|
||||
res.resize(strLength);
|
||||
env->GetStringRegion(string, 0, strLength, (jchar*)res.utf16());
|
||||
return res;
|
||||
}
|
||||
|
||||
QJNILocalRef<jstring> qt_toJString(const QString &string)
|
||||
{
|
||||
QAttachedJNIEnv env;
|
||||
return QJNILocalRef<jstring>(env->NewString(reinterpret_cast<const jchar*>(string.constData()),
|
||||
string.length()));
|
||||
}
|
||||
|
||||
|
||||
static JavaVM *g_javaVM = 0;
|
||||
|
||||
static JavaVM *getJavaVM()
|
||||
{
|
||||
if (!g_javaVM){
|
||||
QPlatformNativeInterface *nativeInterface = QGuiApplication::platformNativeInterface();
|
||||
g_javaVM = static_cast<JavaVM*>(nativeInterface->nativeResourceForIntegration("JavaVM"));
|
||||
}
|
||||
return g_javaVM;
|
||||
}
|
||||
|
||||
QThreadStorage<int> QAttachedJNIEnv::m_refCount;
|
||||
|
||||
QAttachedJNIEnv::QAttachedJNIEnv()
|
||||
{
|
||||
JavaVM *vm = javaVM();
|
||||
if (vm->GetEnv((void**)&jniEnv, JNI_VERSION_1_6) == JNI_EDETACHED) {
|
||||
if (vm->AttachCurrentThread(&jniEnv, 0) < 0) {
|
||||
jniEnv = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_refCount.hasLocalData())
|
||||
m_refCount.setLocalData(1);
|
||||
else
|
||||
m_refCount.setLocalData(m_refCount.localData() + 1);
|
||||
}
|
||||
|
||||
QAttachedJNIEnv::~QAttachedJNIEnv()
|
||||
{
|
||||
if (!jniEnv)
|
||||
return;
|
||||
|
||||
int newRef = m_refCount.localData() - 1;
|
||||
m_refCount.setLocalData(newRef);
|
||||
|
||||
if (newRef == 0)
|
||||
javaVM()->DetachCurrentThread();
|
||||
|
||||
jniEnv = 0;
|
||||
}
|
||||
|
||||
JavaVM *QAttachedJNIEnv::javaVM()
|
||||
{
|
||||
return getJavaVM();
|
||||
}
|
||||
|
||||
QT_END_NAMESPACE
|
191
src/platformsupport/jniconvenience/qjnihelpers_p.h
Normal file
191
src/platformsupport/jniconvenience/qjnihelpers_p.h
Normal file
@ -0,0 +1,191 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
||||
** Contact: http://www.qt-project.org/legal
|
||||
**
|
||||
** This file is part of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:LGPL$
|
||||
** 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 Digia. For licensing terms and
|
||||
** conditions see http://qt.digia.com/licensing. For further information
|
||||
** use the contact form at http://qt.digia.com/contact-us.
|
||||
**
|
||||
** GNU Lesser General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU Lesser
|
||||
** General Public License version 2.1 as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.LGPL included in the
|
||||
** packaging of this file. Please review the following information to
|
||||
** ensure the GNU Lesser General Public License version 2.1 requirements
|
||||
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
||||
**
|
||||
** In addition, as a special exception, Digia gives you certain additional
|
||||
** rights. These rights are described in the Digia Qt LGPL Exception
|
||||
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3.0 as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.GPL included in the
|
||||
** packaging of this file. Please review the following information to
|
||||
** ensure the GNU General Public License version 3.0 requirements will be
|
||||
** met: http://www.gnu.org/copyleft/gpl.html.
|
||||
**
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef QJNIHELPERS_H
|
||||
#define QJNIHELPERS_H
|
||||
|
||||
#include <jni.h>
|
||||
#include <qglobal.h>
|
||||
#include <QString>
|
||||
#include <QThreadStorage>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
template <typename T>
|
||||
class QJNILocalRef;
|
||||
|
||||
QString qt_convertJString(jstring string);
|
||||
QJNILocalRef<jstring> qt_toJString(const QString &string);
|
||||
|
||||
|
||||
struct QAttachedJNIEnv
|
||||
{
|
||||
QAttachedJNIEnv();
|
||||
~QAttachedJNIEnv();
|
||||
|
||||
static JavaVM *javaVM();
|
||||
|
||||
JNIEnv *operator->()
|
||||
{
|
||||
return jniEnv;
|
||||
}
|
||||
|
||||
operator JNIEnv*() const
|
||||
{
|
||||
return jniEnv;
|
||||
}
|
||||
|
||||
JNIEnv *jniEnv;
|
||||
|
||||
private:
|
||||
static QThreadStorage<int> m_refCount;
|
||||
};
|
||||
|
||||
|
||||
template <typename T>
|
||||
class QJNILocalRef
|
||||
{
|
||||
public:
|
||||
inline QJNILocalRef() : m_obj(0) { }
|
||||
inline explicit QJNILocalRef(T o) : m_obj(o) { }
|
||||
inline QJNILocalRef(const QJNILocalRef<T> &other) : m_obj(other.m_obj)
|
||||
{
|
||||
if (other.m_obj)
|
||||
m_obj = static_cast<T>(m_env->NewLocalRef(other.m_obj));
|
||||
}
|
||||
|
||||
template <typename X>
|
||||
inline QJNILocalRef(const QJNILocalRef<X> &other) : m_obj(other.m_obj)
|
||||
{
|
||||
if (other.m_obj)
|
||||
m_obj = static_cast<T>(m_env->NewLocalRef(other.m_obj));
|
||||
}
|
||||
|
||||
inline ~QJNILocalRef() { release(); }
|
||||
|
||||
inline QJNILocalRef<T> &operator=(const QJNILocalRef<T> &other)
|
||||
{
|
||||
release();
|
||||
m_obj = other.m_obj; // for type checking
|
||||
if (other.m_obj)
|
||||
m_obj = static_cast<T>(m_env->NewLocalRef(other.m_obj));
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename X>
|
||||
inline QJNILocalRef<T> &operator=(const QJNILocalRef<X> &other)
|
||||
{
|
||||
release();
|
||||
m_obj = other.m_obj; // for type checking
|
||||
if (other.m_obj)
|
||||
m_obj = static_cast<T>(m_env->NewLocalRef(other.m_obj));
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline QJNILocalRef<T> &operator=(T o)
|
||||
{
|
||||
release();
|
||||
m_obj = o;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename X>
|
||||
inline QJNILocalRef<T> &operator=(X o)
|
||||
{
|
||||
release();
|
||||
m_obj = o;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline bool operator !() const { return !m_obj; }
|
||||
inline bool isNull() const { return !m_obj; }
|
||||
inline T object() const { return m_obj; }
|
||||
|
||||
private:
|
||||
void release()
|
||||
{
|
||||
if (m_obj) {
|
||||
m_env->DeleteLocalRef(m_obj);
|
||||
m_obj = 0;
|
||||
}
|
||||
}
|
||||
|
||||
QAttachedJNIEnv m_env;
|
||||
T m_obj;
|
||||
|
||||
template <class X> friend class QJNILocalRef;
|
||||
};
|
||||
|
||||
template <class T, class X>
|
||||
bool operator==(const QJNILocalRef<T> &ptr1, const QJNILocalRef<X> &ptr2)
|
||||
{
|
||||
return ptr1.m_obj == ptr2.m_obj;
|
||||
}
|
||||
template <class T, class X>
|
||||
bool operator!=(const QJNILocalRef<T> &ptr1, const QJNILocalRef<X> &ptr2)
|
||||
{
|
||||
return ptr1.m_obj != ptr2.m_obj;
|
||||
}
|
||||
|
||||
template <class T, class X>
|
||||
bool operator==(const QJNILocalRef<T> &ptr1, X ptr2)
|
||||
{
|
||||
return ptr1.m_obj == ptr2;
|
||||
}
|
||||
template <class T, class X>
|
||||
bool operator==(T ptr1, const QJNILocalRef<X> &ptr2)
|
||||
{
|
||||
return ptr1 == ptr2.m_obj;
|
||||
}
|
||||
template <class T, class X>
|
||||
bool operator!=(const QJNILocalRef<T> &ptr1, X ptr2)
|
||||
{
|
||||
return !(ptr1 == ptr2);
|
||||
}
|
||||
template <class T, class X>
|
||||
bool operator!=(const T *ptr1, const QJNILocalRef<X> &ptr2)
|
||||
{
|
||||
return !(ptr2 == ptr1);
|
||||
}
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
||||
#endif // QJNIHELPERS_H
|
3098
src/platformsupport/jniconvenience/qjniobject.cpp
Normal file
3098
src/platformsupport/jniconvenience/qjniobject.cpp
Normal file
File diff suppressed because it is too large
Load Diff
169
src/platformsupport/jniconvenience/qjniobject_p.h
Normal file
169
src/platformsupport/jniconvenience/qjniobject_p.h
Normal file
@ -0,0 +1,169 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
||||
** Contact: http://www.qt-project.org/legal
|
||||
**
|
||||
** This file is part of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:LGPL$
|
||||
** 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 Digia. For licensing terms and
|
||||
** conditions see http://qt.digia.com/licensing. For further information
|
||||
** use the contact form at http://qt.digia.com/contact-us.
|
||||
**
|
||||
** GNU Lesser General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU Lesser
|
||||
** General Public License version 2.1 as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.LGPL included in the
|
||||
** packaging of this file. Please review the following information to
|
||||
** ensure the GNU Lesser General Public License version 2.1 requirements
|
||||
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
||||
**
|
||||
** In addition, as a special exception, Digia gives you certain additional
|
||||
** rights. These rights are described in the Digia Qt LGPL Exception
|
||||
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3.0 as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.GPL included in the
|
||||
** packaging of this file. Please review the following information to
|
||||
** ensure the GNU General Public License version 3.0 requirements will be
|
||||
** met: http://www.gnu.org/copyleft/gpl.html.
|
||||
**
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef QJNIOBJECT_H
|
||||
#define QJNIOBJECT_H
|
||||
|
||||
#include <qglobal.h>
|
||||
#include <jni.h>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
template <typename T>
|
||||
class QJNILocalRef;
|
||||
|
||||
/**
|
||||
* Allows to wrap any Java class and partially hide some of the jni calls.
|
||||
*
|
||||
* Usage example:
|
||||
*
|
||||
* QJNIObject javaString("java/lang/String");
|
||||
* jchar char = javaString.callMethod<jchar>("charAt", "(I)C", 0);
|
||||
*
|
||||
* ----
|
||||
*
|
||||
* jstring string = QJNIObject::callStaticMethod<jstring>("java/lang/String",
|
||||
* "valueOf",
|
||||
* "(I)Ljava/lang/String;", 2);
|
||||
*
|
||||
* ----
|
||||
*
|
||||
* // Constructor with argument
|
||||
* jstring someString;
|
||||
* QJNIObject someObject("java/some/Class", "(Ljava/lang/String;)V", someString);
|
||||
* someObject.setField<jint>("fieldName", 10);
|
||||
* someObject.callMethod<void>("doStuff");
|
||||
*/
|
||||
class QJNIObject
|
||||
{
|
||||
public:
|
||||
QJNIObject(const char *className);
|
||||
QJNIObject(const char *className, const char *sig, ...);
|
||||
QJNIObject(jclass clazz);
|
||||
QJNIObject(jclass clazz, const char *sig, ...);
|
||||
QJNIObject(jobject obj);
|
||||
virtual ~QJNIObject();
|
||||
|
||||
static bool isClassAvailable(const char *className);
|
||||
|
||||
bool isValid() const { return m_jobject != 0; }
|
||||
jobject object() const { return m_jobject; }
|
||||
|
||||
template <typename T>
|
||||
T callMethod(const char *methodName);
|
||||
template <typename T>
|
||||
T callMethod(const char *methodName, const char *sig, ...);
|
||||
template <typename T>
|
||||
QJNILocalRef<T> callObjectMethod(const char *methodName);
|
||||
template <typename T>
|
||||
QJNILocalRef<T> callObjectMethod(const char *methodName, const char *sig, ...);
|
||||
|
||||
template <typename T>
|
||||
static T callStaticMethod(const char *className, const char *methodName);
|
||||
template <typename T>
|
||||
static T callStaticMethod(const char *className, const char *methodName, const char *sig, ...);
|
||||
template <typename T>
|
||||
static QJNILocalRef<T> callStaticObjectMethod(const char *className, const char *methodName);
|
||||
template <typename T>
|
||||
static QJNILocalRef<T> callStaticObjectMethod(const char *className,
|
||||
const char *methodName,
|
||||
const char *sig, ...);
|
||||
template <typename T>
|
||||
static T callStaticMethod(jclass clazz, const char *methodName);
|
||||
template <typename T>
|
||||
static T callStaticMethod(jclass clazz, const char *methodName, const char *sig, ...);
|
||||
template <typename T>
|
||||
static QJNILocalRef<T> callStaticObjectMethod(jclass clazz, const char *methodName);
|
||||
template <typename T>
|
||||
static QJNILocalRef<T> callStaticObjectMethod(jclass clazz,
|
||||
const char *methodName,
|
||||
const char *sig, ...);
|
||||
|
||||
template <typename T>
|
||||
T getField(const char *fieldName);
|
||||
template <typename T>
|
||||
T getField(const char *fieldName, const char *sig);
|
||||
template <typename T>
|
||||
QJNILocalRef<T> getObjectField(const char *fieldName);
|
||||
template <typename T>
|
||||
QJNILocalRef<T> getObjectField(const char *fieldName, const char *sig);
|
||||
|
||||
template <typename T>
|
||||
void setField(const char *fieldName, T value);
|
||||
template <typename T>
|
||||
void setField(const char *fieldName, const char *sig, T value);
|
||||
|
||||
template <typename T>
|
||||
static QJNILocalRef<T> getStaticObjectField(const char *className, const char *fieldName);
|
||||
template <typename T>
|
||||
static QJNILocalRef<T> getStaticObjectField(const char *className,
|
||||
const char *fieldName,
|
||||
const char *sig);
|
||||
template <typename T>
|
||||
static T getStaticField(const char *className, const char *fieldName);
|
||||
template <typename T>
|
||||
static QJNILocalRef<T> getStaticObjectField(jclass clazz, const char *fieldName);
|
||||
template <typename T>
|
||||
static QJNILocalRef<T> getStaticObjectField(jclass clazz, const char *fieldName, const char *sig);
|
||||
template <typename T>
|
||||
static T getStaticField(jclass clazz, const char *fieldName);
|
||||
|
||||
template <typename T>
|
||||
static void setStaticField(const char *className,
|
||||
const char *fieldName,
|
||||
const char *sig,
|
||||
T value);
|
||||
template <typename T>
|
||||
static void setStaticField(const char *className, const char *fieldName, T value);
|
||||
template <typename T>
|
||||
static void setStaticField(jclass clazz, const char *fieldName, const char *sig, T value);
|
||||
template <typename T>
|
||||
static void setStaticField(jclass clazz, const char *fieldName, T value);
|
||||
|
||||
protected:
|
||||
jobject m_jobject;
|
||||
jclass m_jclass;
|
||||
bool m_own_jclass;
|
||||
};
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
||||
#endif // QJNIOBJECT_H
|
@ -22,3 +22,4 @@ include(devicediscovery/devicediscovery.pri)
|
||||
include(services/services.pri)
|
||||
include(themes/themes.pri)
|
||||
include(linuxaccessibility/linuxaccessibility.pri)
|
||||
include(jniconvenience/jniconvenience.pri)
|
||||
|
Loading…
Reference in New Issue
Block a user