Changeset View
Changeset View
Standalone View
Standalone View
src/notifybyandroid.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2018 Volker Krause <vkrause@kde.org> | ||||
3 | | ||||
4 | This program is free software; you can redistribute it and/or modify it | ||||
5 | under the terms of the GNU Library General Public License as published by | ||||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||||
7 | option) any later version. | ||||
8 | | ||||
9 | This program is distributed in the hope that it will be useful, but WITHOUT | ||||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||||
12 | License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU General Public License | ||||
15 | along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||
16 | */ | ||||
17 | | ||||
18 | #include "notifybyandroid.h" | ||||
19 | #include "knotification.h" | ||||
20 | #include "debug_p.h" | ||||
21 | | ||||
22 | #include <QtAndroid> | ||||
23 | #include <QAndroidJniEnvironment> | ||||
24 | | ||||
25 | #include <QBuffer> | ||||
26 | #include <QDebug> | ||||
27 | #include <QIcon> | ||||
28 | | ||||
29 | static NotifyByAndroid *s_instance = nullptr; | ||||
30 | | ||||
31 | static void notificationFinished(JNIEnv *env, jobject that, jint notificationId) | ||||
32 | { | ||||
33 | Q_UNUSED(env); | ||||
34 | Q_UNUSED(that); | ||||
35 | if (s_instance) { | ||||
36 | s_instance->notificationFinished(notificationId); | ||||
37 | } | ||||
38 | } | ||||
39 | | ||||
40 | static void notificationActionInvoked(JNIEnv *env, jobject that, jint id, jint action) | ||||
41 | { | ||||
42 | Q_UNUSED(env); | ||||
43 | Q_UNUSED(that); | ||||
44 | if (s_instance) { | ||||
45 | s_instance->notificationActionInvoked(id, action); | ||||
46 | } | ||||
47 | } | ||||
48 | | ||||
49 | static const JNINativeMethod methods[] = { | ||||
50 | {"notificationFinished", "(I)V", (void*)notificationFinished}, | ||||
51 | {"notificationActionInvoked", "(II)V", (void*)notificationActionInvoked} | ||||
52 | }; | ||||
53 | | ||||
54 | KNOTIFICATIONS_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void*) | ||||
55 | { | ||||
56 | static bool initialized = false; | ||||
57 | if (initialized) { | ||||
58 | return JNI_VERSION_1_4; | ||||
59 | } | ||||
60 | initialized = true; | ||||
61 | | ||||
62 | JNIEnv *env = nullptr; | ||||
63 | if (vm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK) { | ||||
64 | qCWarning(LOG_KNOTIFICATIONS) << "Failed to get JNI environment."; | ||||
65 | return -1; | ||||
66 | } | ||||
67 | jclass cls = env->FindClass("org/kde/knotifications/NotifyByAndroid"); | ||||
68 | if (env->RegisterNatives(cls, methods, sizeof(methods) / sizeof(JNINativeMethod)) < 0) { | ||||
69 | qCWarning(LOG_KNOTIFICATIONS) << "Failed to register native functions."; | ||||
70 | return -1; | ||||
71 | } | ||||
72 | | ||||
73 | return JNI_VERSION_1_4; | ||||
74 | } | ||||
75 | | ||||
76 | NotifyByAndroid::NotifyByAndroid(QObject* parent) : | ||||
77 | KNotificationPlugin(parent) | ||||
78 | { | ||||
79 | s_instance = this; | ||||
broulik: Can there be multiple instances of this `NotifyByAndroid`? | |||||
vkrause: KNotificationManager prevents that IIUC. | |||||
80 | #if __ANDROID_API__ >= 23 | ||||
81 | m_backend = QAndroidJniObject("org/kde/knotifications/NotifyByAndroid", "(Landroid/content/Context;)V", QtAndroid::androidContext().object<jobject>()); | ||||
82 | #endif | ||||
83 | } | ||||
84 | | ||||
85 | NotifyByAndroid::~NotifyByAndroid() | ||||
86 | { | ||||
87 | s_instance = nullptr; | ||||
88 | } | ||||
89 | | ||||
90 | QString NotifyByAndroid::optionName() | ||||
91 | { | ||||
92 | return QStringLiteral("Popup"); | ||||
93 | } | ||||
94 | | ||||
95 | void NotifyByAndroid::notify(KNotification *notification, KNotifyConfig *config) | ||||
96 | { | ||||
97 | // HACK work around that notification->id() is only populated after returning from here | ||||
98 | QMetaObject::invokeMethod(this, [this, notification, config](){ notifyDeferred(notification, config); }, Qt::QueuedConnection); | ||||
99 | } | ||||
100 | | ||||
101 | void NotifyByAndroid::notifyDeferred(KNotification* notification, const KNotifyConfig* config) | ||||
102 | { | ||||
103 | Q_UNUSED(config); | ||||
104 | | ||||
105 | #if __ANDROID_API__ >= 23 | ||||
106 | QAndroidJniEnvironment env; | ||||
107 | | ||||
108 | QAndroidJniObject n("org/kde/knotifications/KNotification", "()V"); | ||||
109 | n.setField("id", notification->id()); | ||||
110 | n.setField("text", QAndroidJniObject::fromString(notification->text()).object<jstring>()); | ||||
111 | n.setField("title", QAndroidJniObject::fromString(notification->title()).object<jstring>()); | ||||
112 | | ||||
113 | // icon | ||||
114 | QPixmap pixmap; | ||||
115 | if (!notification->iconName().isEmpty()) { | ||||
116 | const auto icon = QIcon::fromTheme(notification->iconName()); | ||||
117 | pixmap = icon.pixmap(32, 32); | ||||
118 | } else { | ||||
119 | pixmap = notification->pixmap(); | ||||
120 | } | ||||
121 | QByteArray iconData; | ||||
122 | QBuffer buffer(&iconData); | ||||
123 | buffer.open(QIODevice::WriteOnly); | ||||
124 | pixmap.save(&buffer, "PNG"); | ||||
125 | auto jIconData = env->NewByteArray(iconData.length()); | ||||
broulik: Does this need a `FreeLocalRef` call? | |||||
vkrause: Possible, I'll investigate. | |||||
Addressed in D18526. vkrause: Addressed in D18526. | |||||
126 | env->SetByteArrayRegion(jIconData, 0, iconData.length(), reinterpret_cast<const jbyte*>(iconData.constData())); | ||||
127 | n.callMethod<void>("setIconFromData", "([BI)V", jIconData, iconData.length()); | ||||
128 | | ||||
129 | // actions | ||||
130 | const auto actions = notification->actions(); | ||||
131 | for (const auto &action : actions) { | ||||
132 | n.callMethod<void>("addAction", "(Ljava/lang/String;)V", QAndroidJniObject::fromString(action).object<jstring>()); | ||||
133 | } | ||||
134 | | ||||
135 | m_notifications.insert(notification->id(), notification); | ||||
136 | | ||||
137 | m_backend.callMethod<void>("notify", "(Lorg/kde/knotifications/KNotification;)V", n.object<jobject>()); | ||||
138 | #else | ||||
139 | Q_UNUSED(notification); | ||||
140 | #endif | ||||
141 | } | ||||
142 | | ||||
143 | void NotifyByAndroid::close(KNotification* notification) | ||||
144 | { | ||||
145 | #if __ANDROID_API__ >= 23 | ||||
146 | m_backend.callMethod<void>("close", "(I)V", notification->id()); | ||||
147 | #endif | ||||
148 | KNotificationPlugin::close(notification); | ||||
149 | } | ||||
150 | | ||||
151 | void NotifyByAndroid::notificationFinished(int id) | ||||
152 | { | ||||
153 | qCDebug(LOG_KNOTIFICATIONS) << id; | ||||
154 | const auto it = m_notifications.find(id); | ||||
155 | if (it == m_notifications.end()) { | ||||
156 | return; | ||||
157 | } | ||||
158 | m_notifications.erase(it); | ||||
159 | if (it.value()) { | ||||
160 | finish(it.value()); | ||||
161 | } | ||||
162 | } | ||||
163 | | ||||
164 | void NotifyByAndroid::notificationActionInvoked(int id, int action) | ||||
165 | { | ||||
166 | qCDebug(LOG_KNOTIFICATIONS) << id << action; | ||||
167 | emit actionInvoked(id, action); | ||||
168 | } |
Can there be multiple instances of this NotifyByAndroid?