5#ifndef QCOREAPPLICATION_PLATFORM_H
6#define QCOREAPPLICATION_PLATFORM_H
17#include <QtCore/qglobal.h>
18#include <QtCore/qnativeinterface.h>
19#include <QtCore/qcoreapplication.h>
21#if defined(Q_OS_ANDROID) || defined(Q_QDOC)
22#include <QtCore/qjnitypes.h>
23#if QT_CONFIG(future) && !defined(QT_NO_QOBJECT)
24#include <QtCore/qfuture.h>
25#include <QtCore/qvariant.h>
29#if defined(Q_OS_ANDROID)
31typedef _jobject* jobject;
34#if defined(Q_OS_HARMONY) || defined(Q_QDOC)
35#include <QtCore/qharmonyjscontext.h>
43namespace QNativeInterface
45#if defined(Q_OS_ANDROID) || defined(Q_QDOC)
46struct Q_CORE_EXPORT QAndroidApplication
48 QT_DECLARE_NATIVE_INTERFACE(QAndroidApplication, 1, QCoreApplication)
50 static QJniObject context();
52 static QtJniTypes::Context context();
54 static bool isActivityContext();
55 static int sdkVersion();
56 static void hideSplashScreen(
int duration = 0);
58#if QT_CONFIG(future) && !defined(QT_NO_QOBJECT)
59 static QFuture<QVariant> runOnAndroidMainThread(
const std::function<QVariant()> &runnable,
60 const QDeadlineTimer timeout = QDeadlineTimer::Forever);
63 std::enable_if_t<std::is_invocable_v<T> && std::is_same_v<std::invoke_result_t<T>,
void>,
64 QFuture<
void>>
static runOnAndroidMainThread(
const T &runnable,
65 const QDeadlineTimer timeout = QDeadlineTimer::Forever)
67 std::function<QVariant()> func = [runnable](){ runnable();
return QVariant(); };
68 return static_cast<QFuture<
void>>(runOnAndroidMainThread(func, timeout));
74#if defined(Q_OS_HARMONY) || defined(Q_QDOC)
75struct Q_CORE_EXPORT QHarmonyApplication
77 QT_DECLARE_NATIVE_INTERFACE(QHarmonyApplication, 1, QCoreApplication)
79 static void runOnJsThread(std::function<
void(QHarmonyJsContext &)> task);
80 static void runOnJsThreadAndWait(
const std::function<
void(QHarmonyJsContext &)> &task);
83 template <
typename Func,
typename Result>
84 static Result runOnJsThreadAndWait(Func &&task);
86 template <
typename Func,
typename Result = std::invoke_result_t<Func, QHarmonyJsContext &>>
87 static std::enable_if_t<!std::is_void_v<Result>, Result> runOnJsThreadAndWait(Func &&task)
89 static_assert(!std::is_reference_v<Result>,
"The task must return a value, not a reference");
90 static_assert(std::is_default_constructible_v<Result>,
"The task's result type must be default-constructible");
92 !std::is_convertible_v<Result, napi_value> && !std::is_convertible_v<Result, napi_ref>,
93 "A napi_value or napi_ref result must not leave the JS thread");
94 std::optional<Result> optResult;
96 [&](QHarmonyJsContext &jsContext) {
97 auto result = std::invoke(std::forward<Func>(task), jsContext);
98 bool exceptionPending =
false;
99 napi_is_exception_pending(jsContext.env(), &exceptionPending);
100 if (!exceptionPending)
101 optResult.emplace(std::move(result));
103 return optResult ? std::move(*optResult) : Result();
107 static void runOnQtThread(std::function<
void()> task);