6#include "connector_p.h"
9#include <QCoreApplication>
14static QString fromJniString(JNIEnv *env, jstring s)
20 const char *str = env->GetStringUTFChars(s,
nullptr);
22 env->ReleaseStringUTFChars(s, str);
26static void newEndpoint(JNIEnv *env, jobject that, jstring token, jstring endpoint)
29 for (
auto c : ConnectorPrivate::s_instances) {
30 c->NewEndpoint(fromJniString(env, token), fromJniString(env, endpoint));
34static void registrationFailed(JNIEnv *env, jobject that, jstring token, jstring reason)
38 qDebug() <<
"TODO" << fromJniString(env, token) << fromJniString(env, reason);
41static void unregistered(JNIEnv *env, jobject that, jstring token)
44 for (
auto c : ConnectorPrivate::s_instances) {
45 c->Unregistered(fromJniString(env, token));
49static void message(JNIEnv *env, jobject that, jstring token, jbyteArray message, jstring messageId)
52 const auto messageSize = env->GetArrayLength(message);
53 const auto messageData = env->GetByteArrayElements(message,
nullptr);
55 for (
auto c : ConnectorPrivate::s_instances) {
56 c->Message(fromJniString(env, token), messageQBA, fromJniString(env, messageId));
58 env->ReleaseByteArrayElements(message, messageData, JNI_ABORT);
61static const JNINativeMethod methods[] = {
62 {
"newEndpoint",
"(Ljava/lang/String;Ljava/lang/String;)V", (
void*)newEndpoint},
63 {
"registrationFailed",
"(Ljava/lang/String;Ljava/lang/String;)V", (
void*)registrationFailed},
64 {
"unregistered",
"(Ljava/lang/String;)V", (
void*)unregistered},
65 {
"message",
"(Ljava/lang/String;[BLjava/lang/String;)V", (
void*)message},
68Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm,
void*)
70 static bool initialized =
false;
72 return JNI_VERSION_1_4;
76 JNIEnv *env =
nullptr;
77 if (vm->GetEnv((
void**)&env, JNI_VERSION_1_4) != JNI_OK) {
78 qCWarning(
Log) <<
"Failed to get JNI environment.";
81 jclass cls = env->FindClass(
"org/kde/kunifiedpush/MessageReceiver");
82 if (env->RegisterNatives(cls, methods,
sizeof(methods) /
sizeof(JNINativeMethod)) < 0) {
83 qCWarning(
Log) <<
"Failed to register native functions.";
87 return JNI_VERSION_1_4;
90std::vector<ConnectorPrivate*> ConnectorPrivate::s_instances;
92void ConnectorPrivate::init()
94 s_instances.push_back(
this);
97void ConnectorPrivate::deinit()
99 s_instances.erase(std::remove(s_instances.begin(), s_instances.end(),
this), s_instances.end());
102void ConnectorPrivate::doSetDistributor(
const QString &distServiceName)
105 m_distributor = QAndroidJniObject(
"org.kde.kunifiedpush.Distributor",
"(Ljava/lang/String;Landroid/content/Context;)V",
QAndroidJniObject::fromString(distServiceName).
object(), context.
object());
108bool ConnectorPrivate::hasDistributor()
const
110 return m_distributor.isValid();
113void ConnectorPrivate::doRegister()
118void ConnectorPrivate::doUnregister()
Client-side integration with UnifiedPush.
QByteArray fromRawData(const char *data, qsizetype size)
QJniObject fromString(const QString &string)
jobject object() const const
QString fromUtf8(QByteArrayView str)