KUnifiedPush

connector_android.cpp
1/*
2 SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "connector_p.h"
7#include "logging.h"
8
9#include <QCoreApplication>
11
12using namespace KUnifiedPush;
13
14static QString fromJniString(JNIEnv *env, jstring s)
15{
16 if (!s) {
17 return {};
18 }
19
20 const char *str = env->GetStringUTFChars(s, nullptr);
21 const auto qs = QString::fromUtf8(str);
22 env->ReleaseStringUTFChars(s, str);
23 return qs;
24}
25
26static void newEndpoint(JNIEnv *env, jobject that, jstring token, jstring endpoint)
27{
28 Q_UNUSED(that);
29 for (auto c : ConnectorPrivate::s_instances) {
30 c->NewEndpoint(fromJniString(env, token), fromJniString(env, endpoint));
31 }
32}
33
34static void registrationFailed(JNIEnv *env, jobject that, jstring token, jstring reason)
35{
36 Q_UNUSED(that);
37 // TODO
38 qDebug() << "TODO" << fromJniString(env, token) << fromJniString(env, reason);
39}
40
41static void unregistered(JNIEnv *env, jobject that, jstring token)
42{
43 Q_UNUSED(that);
44 for (auto c : ConnectorPrivate::s_instances) {
45 c->Unregistered(fromJniString(env, token));
46 }
47}
48
49static void message(JNIEnv *env, jobject that, jstring token, jbyteArray message, jstring messageId)
50{
51 Q_UNUSED(that);
52 const auto messageSize = env->GetArrayLength(message);
53 const auto messageData = env->GetByteArrayElements(message, nullptr);
54 const auto messageQBA = QByteArray::fromRawData(reinterpret_cast<const char*>(messageData), messageSize);
55 for (auto c : ConnectorPrivate::s_instances) {
56 c->Message(fromJniString(env, token), messageQBA, fromJniString(env, messageId));
57 }
58 env->ReleaseByteArrayElements(message, messageData, JNI_ABORT);
59}
60
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},
66};
67
68Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void*)
69{
70 static bool initialized = false;
71 if (initialized) {
72 return JNI_VERSION_1_4;
73 }
74 initialized = true;
75
76 JNIEnv *env = nullptr;
77 if (vm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK) {
78 qCWarning(Log) << "Failed to get JNI environment.";
79 return -1;
80 }
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.";
84 return -1;
85 }
86
87 return JNI_VERSION_1_4;
88}
89
90std::vector<ConnectorPrivate*> ConnectorPrivate::s_instances;
91
92void ConnectorPrivate::init()
93{
94 s_instances.push_back(this);
95}
96
97void ConnectorPrivate::deinit()
98{
99 s_instances.erase(std::remove(s_instances.begin(), s_instances.end(), this), s_instances.end());
100}
101
102void ConnectorPrivate::doSetDistributor(const QString &distServiceName)
103{
105 m_distributor = QAndroidJniObject("org.kde.kunifiedpush.Distributor", "(Ljava/lang/String;Landroid/content/Context;)V", QAndroidJniObject::fromString(distServiceName).object(), context.object());
106}
107
108bool ConnectorPrivate::hasDistributor() const
109{
110 return m_distributor.isValid();
111}
112
113void ConnectorPrivate::doRegister()
114{
115 m_distributor.callMethod<void>("register", "(Ljava/lang/String;Ljava/lang/String;)V", QAndroidJniObject::fromString(m_token).object(), QAndroidJniObject::fromString(m_description).object());
116}
117
118void ConnectorPrivate::doUnregister()
119{
120 m_distributor.callMethod<void>("unregister", "(Ljava/lang/String;)V", QAndroidJniObject::fromString(m_token).object());
121}
Client-side integration with UnifiedPush.
Definition connector.h:16
QByteArray fromRawData(const char *data, qsizetype size)
QJniObject fromString(const QString &string)
jobject object() const const
QString fromUtf8(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 12:02:24 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.