diff --git a/src/android/org/kde/knotifications/NotifyByAndroid.java b/src/android/org/kde/knotifications/NotifyByAndroid.java index eea34db..76c7bbd 100644 --- a/src/android/org/kde/knotifications/NotifyByAndroid.java +++ b/src/android/org/kde/knotifications/NotifyByAndroid.java @@ -1,147 +1,151 @@ /* Copyright (C) 2018 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package org.kde.knotifications; import android.app.Activity; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.graphics.drawable.Icon; import android.os.Build; import android.util.Log; import java.util.HashSet; /** Java side of the Android notfication backend. */ public class NotifyByAndroid extends BroadcastReceiver { private static final String TAG = "org.kde.knotifications"; private static final String NOTIFICATION_ACTION = ".org.kde.knotifications.NOTIFICATION_ACTION"; private static final String NOTIFICATION_DELETED = ".org.kde.knotifications.NOTIFICATION_DELETED"; private static final String NOTIFICATION_OPENED = ".org.kde.knotifications.NOTIFICATION_OPENED"; private static final String NOTIFICATION_ID_EXTRA = "org.kde.knotifications.NOTIFICATION_ID"; private static final String NOTIFICATION_ACTION_ID_EXTRA = "org.kde.knotifications.NOTIFICATION_ACTION_ID"; private android.content.Context m_ctx; private NotificationManager m_notificationManager; private int m_uniquePendingIntentId = 0; private HashSet m_channels = new HashSet(); public NotifyByAndroid(android.content.Context context) { Log.i(TAG, context.getPackageName()); m_ctx = context; m_notificationManager = (NotificationManager)m_ctx.getSystemService(Context.NOTIFICATION_SERVICE); IntentFilter filter = new IntentFilter(); filter.addAction(m_ctx.getPackageName() + NOTIFICATION_ACTION); filter.addAction(m_ctx.getPackageName() + NOTIFICATION_DELETED); filter.addAction(m_ctx.getPackageName() + NOTIFICATION_OPENED); m_ctx.registerReceiver(this, filter); } public void notify(KNotification notification) { Log.i(TAG, notification.text); // notification channel if (!m_channels.contains(notification.channelId)) { m_channels.add(notification.channelId); if (Build.VERSION.SDK_INT >= 26) { NotificationChannel channel = new NotificationChannel(notification.channelId, notification.channelName, NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription(notification.channelDescription); m_notificationManager.createNotificationChannel(channel); } } Notification.Builder builder; if (Build.VERSION.SDK_INT >= 26) { builder = new Notification.Builder(m_ctx, notification.channelId); } else { builder = new Notification.Builder(m_ctx); } if (Build.VERSION.SDK_INT >= 23) { builder.setSmallIcon((Icon)notification.icon); } else { builder.setSmallIcon(m_ctx.getApplicationInfo().icon); } builder.setContentTitle(notification.title); builder.setContentText(notification.text); + // regular notifications show only a single line of content, if we have more + // we need the "BigTextStyle" expandable notifications to make everything readable + // in the single line case this behaves like the regular one, so no special-casing needed + builder.setStyle(new Notification.BigTextStyle().bigText(notification.text)); // taping the notification shows the app Intent intent = new Intent(m_ctx.getPackageName() + NOTIFICATION_OPENED); intent.putExtra(NOTIFICATION_ID_EXTRA, notification.id); PendingIntent contentIntent = PendingIntent.getBroadcast(m_ctx, m_uniquePendingIntentId++, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(contentIntent); // actions int actionId = 1; for (String actionName : notification.actions) { Intent actionIntent = new Intent(m_ctx.getPackageName() + NOTIFICATION_ACTION); actionIntent.putExtra(NOTIFICATION_ID_EXTRA, notification.id); actionIntent.putExtra(NOTIFICATION_ACTION_ID_EXTRA, actionId); PendingIntent pendingIntent = PendingIntent.getBroadcast(m_ctx, m_uniquePendingIntentId++, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT); Notification.Action action = new Notification.Action.Builder(0, actionName, pendingIntent).build(); builder.addAction(action); ++actionId; } // notfication about user closing the notification Intent deleteIntent = new Intent(m_ctx.getPackageName() + NOTIFICATION_DELETED); deleteIntent.putExtra(NOTIFICATION_ID_EXTRA, notification.id); Log.i(TAG, deleteIntent.getExtras() + " " + notification.id); builder.setDeleteIntent(PendingIntent.getBroadcast(m_ctx, m_uniquePendingIntentId++, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT)); m_notificationManager.notify(notification.id, builder.build()); } public void close(int id) { m_notificationManager.cancel(id); } @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); int id = intent.getIntExtra(NOTIFICATION_ID_EXTRA, -1); Log.i(TAG, action + ": " + id + " " + intent.getExtras()); if (action.equals(m_ctx.getPackageName() + NOTIFICATION_ACTION)) { int actionId = intent.getIntExtra(NOTIFICATION_ACTION_ID_EXTRA, -1); notificationActionInvoked(id, actionId); } else if (action.equals(m_ctx.getPackageName() + NOTIFICATION_DELETED)) { notificationFinished(id); } else if (action.equals(m_ctx.getPackageName() + NOTIFICATION_OPENED)) { Intent newintent = new Intent(m_ctx, m_ctx.getClass()); newintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); m_ctx.startActivity(newintent); notificationActionInvoked(id, 0); } } public native void notificationFinished(int notificationId); public native void notificationActionInvoked(int notificationId, int action); } diff --git a/src/notifybyandroid.cpp b/src/notifybyandroid.cpp index 452475d..2d8055c 100644 --- a/src/notifybyandroid.cpp +++ b/src/notifybyandroid.cpp @@ -1,166 +1,166 @@ /* Copyright (C) 2018 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "notifybyandroid.h" #include "knotification.h" #include "knotifyconfig.h" #include "debug_p.h" #include #include #include #include static NotifyByAndroid *s_instance = nullptr; static void notificationFinished(JNIEnv *env, jobject that, jint notificationId) { Q_UNUSED(env); Q_UNUSED(that); if (s_instance) { s_instance->notificationFinished(notificationId); } } static void notificationActionInvoked(JNIEnv *env, jobject that, jint id, jint action) { Q_UNUSED(env); Q_UNUSED(that); if (s_instance) { s_instance->notificationActionInvoked(id, action); } } static const JNINativeMethod methods[] = { {"notificationFinished", "(I)V", (void*)notificationFinished}, {"notificationActionInvoked", "(II)V", (void*)notificationActionInvoked} }; KNOTIFICATIONS_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void*) { static bool initialized = false; if (initialized) { return JNI_VERSION_1_4; } initialized = true; JNIEnv *env = nullptr; if (vm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK) { qCWarning(LOG_KNOTIFICATIONS) << "Failed to get JNI environment."; return -1; } jclass cls = env->FindClass("org/kde/knotifications/NotifyByAndroid"); if (env->RegisterNatives(cls, methods, sizeof(methods) / sizeof(JNINativeMethod)) < 0) { qCWarning(LOG_KNOTIFICATIONS) << "Failed to register native functions."; return -1; } return JNI_VERSION_1_4; } NotifyByAndroid::NotifyByAndroid(QObject* parent) : KNotificationPlugin(parent) { s_instance = this; m_backend = QAndroidJniObject("org/kde/knotifications/NotifyByAndroid", "(Landroid/content/Context;)V", QtAndroid::androidContext().object()); } NotifyByAndroid::~NotifyByAndroid() { s_instance = nullptr; } QString NotifyByAndroid::optionName() { return QStringLiteral("Popup"); } void NotifyByAndroid::notify(KNotification *notification, KNotifyConfig *config) { Q_UNUSED(config); // HACK work around that notification->id() is only populated after returning from here // note that config will be invalid at that point, so we can't pass that along QMetaObject::invokeMethod(this, [this, notification](){ notifyDeferred(notification); }, Qt::QueuedConnection); } void NotifyByAndroid::notifyDeferred(KNotification* notification) { KNotifyConfig config(notification->appName(), notification->contexts(), notification->eventId()); QAndroidJniEnvironment env; QAndroidJniObject n("org/kde/knotifications/KNotification", "()V"); n.setField("id", notification->id()); - n.setField("text", QAndroidJniObject::fromString(notification->text()).object()); - n.setField("title", QAndroidJniObject::fromString(notification->title()).object()); + n.setField("text", QAndroidJniObject::fromString(stripRichText(notification->text())).object()); + n.setField("title", QAndroidJniObject::fromString(stripRichText(notification->title())).object()); n.setField("channelId", QAndroidJniObject::fromString(notification->eventId()).object()); n.setField("channelName", QAndroidJniObject::fromString(config.readEntry(QLatin1String("Name"))).object()); n.setField("channelDescription", QAndroidJniObject::fromString(config.readEntry(QLatin1String("Comment"))).object()); // icon QPixmap pixmap; if (!notification->iconName().isEmpty()) { const auto icon = QIcon::fromTheme(notification->iconName()); pixmap = icon.pixmap(32, 32); } else { pixmap = notification->pixmap(); } QByteArray iconData; QBuffer buffer(&iconData); buffer.open(QIODevice::WriteOnly); pixmap.save(&buffer, "PNG"); auto jIconData = env->NewByteArray(iconData.length()); env->SetByteArrayRegion(jIconData, 0, iconData.length(), reinterpret_cast(iconData.constData())); n.callMethod("setIconFromData", "([BI)V", jIconData, iconData.length()); env->DeleteLocalRef(jIconData); // actions const auto actions = notification->actions(); for (const auto &action : actions) { n.callMethod("addAction", "(Ljava/lang/String;)V", QAndroidJniObject::fromString(action).object()); } m_notifications.insert(notification->id(), notification); m_backend.callMethod("notify", "(Lorg/kde/knotifications/KNotification;)V", n.object()); } void NotifyByAndroid::close(KNotification* notification) { m_backend.callMethod("close", "(I)V", notification->id()); KNotificationPlugin::close(notification); } void NotifyByAndroid::notificationFinished(int id) { qCDebug(LOG_KNOTIFICATIONS) << id; const auto it = m_notifications.find(id); if (it == m_notifications.end()) { return; } m_notifications.erase(it); if (it.value()) { finish(it.value()); } } void NotifyByAndroid::notificationActionInvoked(int id, int action) { qCDebug(LOG_KNOTIFICATIONS) << id << action; emit actionInvoked(id, action); }