summaryrefslogtreecommitdiffstats
path: root/src/qmlandroid/core/qtqmlandroidfunctions.cpp
blob: c83689c3adb71206cbf22910e4a4eac6652840c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "qtqmlandroidfunctions_p.h"
#include <QtCore/qstring.h>
#include <QtCore/private/qjnihelpers_p.h>
#include <QtConcurrent/qtconcurrentrun.h>
#include <QtAndroidExtras/qandroidfunctions.h>
#include <QtAndroidExtras/qandroidjniobject.h>
#include <QtAndroidExtras/qandroidjnienvironment.h>

QT_BEGIN_NAMESPACE

namespace QtQmlAndroid {

void callFunction(std::function<void()> method)
{
    QAndroidJniEnvironment env;
    QRunnable *runnable = new QtConcurrent::StoredFunctorCall0<void, decltype(method)>(method);
    QtAndroidPrivate::runOnUiThread(runnable, env);
}

void callTextMethod(const QAndroidJniObject &obj, const char *method, const QString &text)
{
    if (obj.isValid()) {
        callFunction([=]() {
            obj.callMethod<void>(method, "(Ljava/lang/CharSequence;)V", QAndroidJniObject::fromString(text).object());
        });
    }
}

void callRealMethod(const QAndroidJniObject &obj, const char *method, qreal value)
{
    if (obj.isValid()) {
        callFunction([=]() {
            obj.callMethod<void>(method, "(F)V", value);
        });
    }
}

void callIntMethod(const QAndroidJniObject &obj, const char *method, int value)
{
    if (obj.isValid()) {
        callFunction([=]() {
            obj.callMethod<void>(method, "(I)V", value);
        });
    }
}

void callBoolMethod(const QAndroidJniObject &obj, const char *method, bool value)
{
    if (obj.isValid()) {
        callFunction([=]() {
            obj.callMethod<void>(method, "(Z)V", value);
        });
    }
}

void callVoidMethod(const QAndroidJniObject &obj, const char *method)
{
    if (obj.isValid()) {
        callFunction([=]() {
            obj.callMethod<void>(method);
        });
    }
}

}

QT_END_NAMESPACE