Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qandroidquickviewembedding.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <QtCore/private/qandroidtypeconverter_p.h>
6#include <QtCore/private/qandroidtypes_p.h>
7#include <QtQuick/private/qandroidquickviewembedding_p.h>
8#include <QtQuick/private/qandroidviewsignalmanager_p.h>
9#include <QtCore/private/qmetaobject_p.h>
10#include <QtCore/qmetatype.h>
11
12#include <QtCore/qcoreapplication.h>
13#include <QtCore/qjnienvironment.h>
14#include <QtCore/qjniobject.h>
15#include <QtCore/qjniarray.h>
16#include <QtCore/qjnitypes.h>
17#include <QtQml/qqmlengine.h>
18#include <QtQuick/qquickitem.h>
19#include <functional>
20#include <jni.h>
21#include <memory>
22
24
25Q_DECLARE_JNI_CLASS(QtDelegate, "org/qtproject/qt/android/QtEmbeddedContextDelegate");
26Q_DECLARE_JNI_CLASS(QtQuickView, "org/qtproject/qt/android/QtQuickView");
27Q_DECLARE_JNI_CLASS(QtWindow, "org/qtproject/qt/android/QtWindow");
28Q_DECLARE_JNI_CLASS(View, "android/view/View");
29
31{
32 constexpr const char *uninitializedViewMessage = "because QtQuickView is not loaded or ready yet.";
33
34 static void onQQuickViewStatusChanged(const QJniObject &qtViewObject,
35 QAndroidQuickView::Status status)
36 {
37 auto future = QNativeInterface::QAndroidApplication::runOnAndroidMainThread(
38 [qtViewObject, status] {
39 qtViewObject.callMethod<void>("handleStatusChange", status);
40 }, QDeadlineTimer(1000));
41 future.waitForFinished(); // Wait for the user to handle status change.
42 }
43
44 void createQuickView(JNIEnv *, jobject nativeWindow, jstring qmlUri, jint width, jint height,
45 jlong parentWindowReference, jlong viewReference,
46 const QJniArray<jstring> &qmlImportPaths)
47 {
48 static_assert (sizeof(jlong) >= sizeof(void*),
49 "Insufficient size of Java type to hold the c++ pointer");
50 const QUrl qmlUrl(QJniObject(qmlUri).toString());
51
52 const QStringList importPaths = qmlImportPaths.toContainer();
53 QMetaObject::invokeMethod(qApp, [qtViewObject = QJniObject(nativeWindow),
54 parentWindowReference,
55 viewReference,
56 width,
57 height,
58 qmlUrl,
59 importPaths] {
60 // If the view does not exists (viewReference==0) we should create and set it up.
61 // Else we only reset the source of the view.
62 QAndroidQuickView *view = reinterpret_cast<QAndroidQuickView *>(viewReference);
63 if (!view) {
64 QWindow *parentWindow = reinterpret_cast<QWindow *>(parentWindowReference);
65 view = new QAndroidQuickView(parentWindow);
66 QObject::connect(
67 view, &QAndroidQuickView::statusChanged,
68 std::bind(&onQQuickViewStatusChanged, qtViewObject, std::placeholders::_1));
69 view->setResizeMode(QAndroidQuickView::SizeRootObjectToView);
70 view->setColor(QColor(Qt::transparent));
71 view->setWidth(width);
72 view->setHeight(height);
73 QQmlEngine *engine = view->engine();
74 for (const QString &path : importPaths)
75 engine->addImportPath(path);
76
77 QObject::connect(engine, &QQmlEngine::quit, QCoreApplication::instance(),
78 &QCoreApplication::quit);
79
80 const QtJniTypes::QtWindow window = reinterpret_cast<jobject>(view->winId());
81 qtViewObject.callMethod<void>("addQtWindow",
82 window,
83 reinterpret_cast<jlong>(view),
84 parentWindowReference);
85 }
86 view->setSource(qmlUrl);
87 });
88 }
89
91 {
92 QAndroidQuickView *view = reinterpret_cast<QAndroidQuickView *>(windowReference);
93 QQuickItem *rootObject = Q_LIKELY(view) ? view->rootObject() : nullptr;
94 return std::make_pair(view, rootObject);
95 }
96
97 void setRootObjectProperty(JNIEnv *env, jobject object, jlong windowReference,
98 jstring propertyName, jobject value)
99 {
100 Q_UNUSED(env);
101 Q_UNUSED(object);
102
103 auto [_, rootObject] = getViewAndRootObject(windowReference);
104 if (!rootObject) {
105 qWarning("Cannot set property %s %s", qPrintable(QJniObject(propertyName).toString()),
107 return;
108 }
109
110 const QString property = QJniObject(propertyName).toString();
111 const QMetaObject *rootMetaObject = rootObject->metaObject();
112 int propertyIndex = rootMetaObject->indexOfProperty(qPrintable(property));
113 if (propertyIndex < 0) {
114 qWarning("Property %s does not exist in the root QML object.", qPrintable(property));
115 return;
116 }
117
118 const QJniObject propertyValue(value);
119 const QVariant variantToWrite = QAndroidTypeConverter::toQVariant(propertyValue);
120
121 if (!variantToWrite.isValid()) {
122 qWarning("Setting the property type of %s is not supported.",
123 propertyValue.className().data());
124 } else {
125 QMetaObject::invokeMethod(rootObject,
126 [metaProperty = rootMetaObject->property(propertyIndex),
127 rootObject = rootObject,
128 variantToWrite] {
129 metaProperty.write(rootObject, variantToWrite);
130 });
131 }
132 }
133
134 jobject getRootObjectProperty(JNIEnv *env, jobject object, jlong windowReference,
135 jstring propertyName)
136 {
137 Q_UNUSED(object);
138 Q_ASSERT(env);
139
140 const QString property = QJniObject(propertyName).toString();
141 auto [_, rootObject] = getViewAndRootObject(windowReference);
142 if (!rootObject) {
143 qWarning("Cannot get property %s %s", qPrintable(property), uninitializedViewMessage);
144 return nullptr;
145 }
146
147 const QMetaObject *rootMetaObject = rootObject->metaObject();
148 int propertyIndex = rootMetaObject->indexOfProperty(property.toUtf8().constData());
149 if (propertyIndex < 0) {
150 qWarning("Cannot get property %s as it does not exist in the root QML object.",
151 qPrintable(property));
152 return nullptr;
153 }
154
155 QMetaProperty metaProperty = rootMetaObject->property(propertyIndex);
156 QVariant propertyValue;
157 if (QCoreApplication::instance()->thread()->isCurrentThread()) {
158 propertyValue = metaProperty.read(rootObject);
159 } else {
160 QMetaObject::invokeMethod(rootObject,
161 [&propertyValue, &metaProperty, rootObject = rootObject] {
162 propertyValue = metaProperty.read(rootObject);
163 }, Qt::BlockingQueuedConnection);
164 }
165 jobject jObject = QAndroidTypeConverter::toJavaObject(propertyValue, env);
166 if (!jObject) {
167 qWarning("Property %s cannot be converted to a supported Java data type.",
168 qPrintable(property));
169 }
170 return jObject;
171 }
172
173 bool addRootObjectSignalListener(JNIEnv *env, jobject, jlong windowReference,
174 jstring signalName, QJniArray<jclass> argTypes,
175 jobject listener, jint id)
176 {
177 Q_ASSERT(env);
178
179 auto [view, _] = getViewAndRootObject(windowReference);
180 if (!view) {
181 qWarning("Cannot connect to signal %s %s",
182 qPrintable(QJniObject(signalName).toString()), uninitializedViewMessage);
183 return false;
184 }
185
186 QAndroidViewSignalManager *signalManager = view->signalManager();
187 return signalManager->addConnection(QJniObject(signalName).toString(), argTypes,
188 QJniObject(listener), id);
189 }
190
191 bool removeRootObjectSignalListener(JNIEnv *, jobject, jlong windowReference,
192 jint signalListenerId)
193 {
194 auto [view, rootObject] = getViewAndRootObject(windowReference);
195 if (!rootObject) {
196 qWarning("Cannot disconnect the signal connection with id: %i %s", signalListenerId,
198 return false;
199 }
200 view->signalManager()->removeConnection(signalListenerId);
201 return true;
202 }
203
205 {
206 switch (type) {
207 case QMetaType::Bool:
208 return QVariant::fromValue(
209 QtJniTypes::Boolean::construct(obj).callMethod<bool>("booleanValue"));
210 break;
211 case QMetaType::Int:
212 return QVariant::fromValue(
213 QtJniTypes::Integer::construct(obj).callMethod<int>("intValue"));
214 break;
215 case QMetaType::Double:
216 return QVariant::fromValue(
217 QtJniTypes::Double::construct(obj).callMethod<double>("doubleValue"));
218 break;
219 case QMetaType::Float:
220 return QVariant::fromValue(
221 QtJniTypes::Float::construct(obj).callMethod<float>("floatValue"));
222 break;
223 case QMetaType::QString:
225 break;
226 default:
227 qWarning("Unsupported metatype: %s", QMetaType(type).name());
228 return QVariant();
229 }
230 }
231
232 QMetaMethod findMethod(const QString &name, int paramCount, const QMetaObject &object)
233 {
234 for (auto i = object.methodOffset(); i < object.methodCount(); ++i) {
235 QMetaMethod method = object.method(i);
236 const auto paramMatch = method.parameterCount() == paramCount;
237 const auto nameMatch = method.name() == name.toUtf8();
238 if (paramMatch && nameMatch)
239 return method;
240 }
241 return QMetaMethod();
242 }
243
244 void invokeMethod(JNIEnv *, jobject, jlong viewReference, QtJniTypes::String methodName,
245 QJniArray<jobject> jniParams)
246 {
247 auto [_, rootObject] = getViewAndRootObject(viewReference);
248 if (!rootObject) {
249 qWarning() << "Cannot invoke QML method" << methodName.toString()
250 << "as the QML view has not been loaded yet.";
251 return;
252 }
253
254 const auto paramCount = jniParams.size();
255 QMetaMethod method =
256 findMethod(methodName.toString(), paramCount, *rootObject->metaObject());
257 if (!method.isValid()) {
258 qWarning() << "Failed to find method" << QJniObject(methodName).toString()
259 << "in QQuickView";
260 return;
261 }
262
263 // Invoke and leave early if there are no params to pass on
264 if (paramCount == 0) {
265 method.invoke(rootObject, Qt::QueuedConnection);
266 return;
267 }
268
269 QList<QVariant> variants;
270 variants.reserve(jniParams.size());
271 variants.emplace_back(QVariant{}); // "Data" for the return value
272
273 for (auto i = 0; i < paramCount; ++i) {
274 const auto type = method.parameterType(i);
275 if (type == QMetaType::UnknownType) {
276 qWarning("Unknown metatypes are not supported.");
277 return;
278 }
279
280 jobject rawParam = jniParams.at(i);
281 auto variant = variants.emplace_back(
282 jobjectToVariant(static_cast<QMetaType::Type>(type), rawParam));
283 if (variant.isNull()) {
284 auto className = QJniObject(rawParam).className();
285 qWarning("Failed to convert param with class name '%s' to QVariant",
286 className.constData());
287 return;
288 }
289 }
290
291 // Initialize the data arrays for params, typenames and type conversion interfaces.
292 // Note that this is adding an element, this is for the return value which is at idx 0.
293 const int paramsCount = method.parameterCount() + 1;
294 const auto paramTypes = std::make_unique<const char *[]>(paramsCount);
295 const auto params = std::make_unique<const void *[]>(paramsCount);
296 const auto metaTypes =
297 std::make_unique<const QtPrivate::QMetaTypeInterface *[]>(paramsCount);
298
299 // We're not expecting a return value, so index 0 can be all nulls.
300 paramTypes[0] = nullptr;
301 params[0] = nullptr;
302 metaTypes[0] = nullptr;
303
304 for (auto i = 1; i < variants.size(); ++i) {
305 const auto &variant = variants.at(i);
306 paramTypes[i] = variant.typeName();
307 params[i] = variant.data();
308 metaTypes[i] = variant.metaType().iface();
309 }
310
311 auto reason = QMetaMethodInvoker::invokeImpl(method,
312 rootObject,
313 Qt::QueuedConnection,
314 paramsCount,
315 params.get(),
316 paramTypes.get(),
317 metaTypes.get());
318
319 if (reason != QMetaMethodInvoker::InvokeFailReason::None)
320 qWarning() << "Failed to invoke function" << methodName.toString()
321 << ", Reason:" << int(reason);
322 }
323
324 bool registerNatives(QJniEnvironment& env) {
325 return env.registerNativeMethods(QtJniTypes::Traits<QtJniTypes::QtQuickView>::className(),
326 {Q_JNI_NATIVE_SCOPED_METHOD(createQuickView,
327 QtAndroidQuickViewEmbedding),
328 Q_JNI_NATIVE_SCOPED_METHOD(setRootObjectProperty,
329 QtAndroidQuickViewEmbedding),
330 Q_JNI_NATIVE_SCOPED_METHOD(getRootObjectProperty,
331 QtAndroidQuickViewEmbedding),
332 Q_JNI_NATIVE_SCOPED_METHOD(addRootObjectSignalListener,
333 QtAndroidQuickViewEmbedding),
334 Q_JNI_NATIVE_SCOPED_METHOD(removeRootObjectSignalListener,
335 QtAndroidQuickViewEmbedding),
336 Q_JNI_NATIVE_SCOPED_METHOD(invokeMethod,
337 QtAndroidQuickViewEmbedding)});
338 }
339}
340
341extern "C" Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
342{
343 Q_UNUSED(vm)
344 Q_UNUSED(reserved)
345
346 static bool initialized = false;
347 if (initialized)
348 return JNI_VERSION_1_6;
349 initialized = true;
350
351 QJniEnvironment env;
352 if (!env.isValid())
353 return JNI_ERR;
354 if (!QtAndroidQuickViewEmbedding::registerNatives(env))
355 return JNI_ERR;
356 return JNI_VERSION_1_6;
357}
358
359QT_END_NAMESPACE
std::pair< QAndroidQuickView *, QQuickItem * > getViewAndRootObject(jlong windowReference)
void setRootObjectProperty(JNIEnv *env, jobject object, jlong windowReference, jstring propertyName, jobject value)
bool addRootObjectSignalListener(JNIEnv *env, jobject, jlong windowReference, jstring signalName, QJniArray< jclass > argTypes, jobject listener, jint id)
constexpr const char * uninitializedViewMessage
bool removeRootObjectSignalListener(JNIEnv *, jobject, jlong windowReference, jint signalListenerId)
void invokeMethod(JNIEnv *, jobject, jlong viewReference, QtJniTypes::String methodName, QJniArray< jobject > jniParams)
void createQuickView(JNIEnv *, jobject nativeWindow, jstring qmlUri, jint width, jint height, jlong parentWindowReference, jlong viewReference, const QJniArray< jstring > &qmlImportPaths)
static void onQQuickViewStatusChanged(const QJniObject &qtViewObject, QAndroidQuickView::Status status)
bool registerNatives(QJniEnvironment &env)
jobject getRootObjectProperty(JNIEnv *env, jobject object, jlong windowReference, jstring propertyName)
QVariant jobjectToVariant(QMetaType::Type type, jobject &obj)
QMetaMethod findMethod(const QString &name, int paramCount, const QMetaObject &object)
Q_DECLARE_JNI_CLASS(MotionEvent, "android/view/MotionEvent")