This repository was archived by the owner on Mar 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathandroidutils.cpp
132 lines (110 loc) · 3.21 KB
/
androidutils.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "androidutils.h"
#include <QtGlobal>
#include <QQmlEngine>
#include <QCoreApplication>
#include <systemdispatcher.h>
#ifdef Q_OS_ANDROID
#include <QtAndroidExtras>
#else
#include <QDebug>
#endif
#include "filechooser.h"
static void setupUtils();
static QObject *createQmlSingleton(QQmlEngine *qmlEngine, QJSEngine *jsEngine);
Q_COREAPP_STARTUP_FUNCTION(setupUtils)
AndroidUtils *AndroidUtils::_instance = nullptr;
void AndroidUtils::javaThrow()
{
#ifdef Q_OS_ANDROID
QAndroidJniEnvironment env;
if (env->ExceptionCheck()) {
auto exception = QAndroidJniObject::fromLocalRef(env->ExceptionOccurred());
JavaException exc;
exc._what = exception.callObjectMethod("getLocalizedMessage", "()Ljava/lang/String;").toString().toUtf8();
QAndroidJniObject stringWriter("java/io/StringWriter");
QAndroidJniObject printWriter("java/io/PrintWriter", "(Ljava/lang/Writer;)V", stringWriter.object());
exception.callMethod<void>("printStackTrace", "(Ljava/lang/PrintWriter;)V", printWriter.object());
exc._stackTrace = stringWriter.callObjectMethod("toString", "()Ljava/lang/String;").toString().toUtf8();
env->ExceptionClear();
throw exc;
}
#endif
}
AndroidUtils::AndroidUtils(QObject *parent) :
QObject(parent)
{}
AndroidUtils *AndroidUtils::instance()
{
if(!_instance)
_instance = new AndroidUtils(qApp);
return _instance;
}
void AndroidUtils::setStatusBarColor(const QColor &color)
{
#ifdef Q_OS_ANDROID
AndroidNative::SystemDispatcher::instance()->dispatch("AndroidUtils.setStatusBarColor", {
{"color", color.name()}
});
#else
Q_UNUSED(color);
#endif
}
void AndroidUtils::showToast(const QString &message, bool showLong)
{
#ifdef Q_OS_ANDROID
AndroidNative::SystemDispatcher::instance()->dispatch("androidnative.Toast.showToast", {
{"text", message},
{"longLength", showLong}
});
#else
Q_UNUSED(showLong)
qInfo() << message;
#endif
}
void AndroidUtils::hapticFeedback(HapticFeedbackConstant constant)
{
#ifdef Q_OS_ANDROID
AndroidNative::SystemDispatcher::instance()->dispatch("AndroidUtils.hapticFeedback", {
{"feedbackConstant", (int)constant}
});
#else
Q_UNUSED(constant);
#endif
}
static void setupUtils()
{
AndroidNative::SystemDispatcher::instance()->loadClass("androidnative.Toast");
AndroidNative::SystemDispatcher::instance()->loadClass("de.skycoder42.androidutils.AndroidUtils");
qmlRegisterSingletonType<AndroidUtils>("de.skycoder42.androidutils", 1, 1, "AndroidUtils", createQmlSingleton);
qmlRegisterType<FileChooser>("de.skycoder42.androidutils", 1, 1, "FileChooser");
//qmlProtectModule("de.skycoder42.quickextras", 1);
}
static QObject *createQmlSingleton(QQmlEngine *qmlEngine, QJSEngine *jsEngine)
{
Q_UNUSED(qmlEngine)
Q_UNUSED(jsEngine)
return new AndroidUtils(qmlEngine);
}
JavaException::JavaException() :
_what(),
_stackTrace()
{}
const char *JavaException::what() const noexcept
{
return _what.constData();
}
const QByteArray JavaException::printStackTrace() const noexcept
{
return _stackTrace;
}
void JavaException::raise() const
{
throw *this;
}
QException *JavaException::clone() const
{
auto e = new JavaException();
e->_what = _what;
e->_stackTrace = _stackTrace;
return e;
}