From cdcc3411aa480de79c0d40791546836032ddedb3 Mon Sep 17 00:00:00 2001 From: Julian Winkler Date: Mon, 18 Mar 2024 15:41:44 +0100 Subject: [PATCH] NotificationManager: delete ongoing and MediaStyle notifications on exit The XDG Portal API doesn't have a concept of ongoing notifications. MediaStyle notifications could be changed to MPRIS in the future. --- .../app/android_app_NotificationManager.c | 19 ++++++++++++++++++- .../android_app_NotificationManager.h | 4 ++-- src/api-impl/android/app/Notification.java | 13 +++++++++++-- .../android/app/NotificationManager.java | 4 ++-- src/main-executable/main.c | 2 ++ 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/api-impl-jni/app/android_app_NotificationManager.c b/src/api-impl-jni/app/android_app_NotificationManager.c index 6217e5e5..2c930529 100644 --- a/src/api-impl-jni/app/android_app_NotificationManager.c +++ b/src/api-impl-jni/app/android_app_NotificationManager.c @@ -6,6 +6,7 @@ #include "../generated_headers/android_app_NotificationManager.h" static XdpPortal *portal = NULL; +static GHashTable *ongoing_notifications = NULL; JNIEXPORT jlong JNICALL Java_android_app_NotificationManager_nativeInitBuilder(JNIEnv *env, jobject this) { @@ -61,7 +62,7 @@ static void natification_callback(GObject* source_object, GAsyncResult* res, gpo callback_pending = 0; } -JNIEXPORT void JNICALL Java_android_app_NotificationManager_nativeShowNotification(JNIEnv *env, jobject this, jlong builder_ptr, jint id, jstring title_jstr, jstring text_jstr, jstring icon_jstr, jint type, jstring action, jstring className) +JNIEXPORT void JNICALL Java_android_app_NotificationManager_nativeShowNotification(JNIEnv *env, jobject this, jlong builder_ptr, jint id, jstring title_jstr, jstring text_jstr, jstring icon_jstr, jboolean ongoing, jint type, jstring action, jstring className) { if (callback_pending) { return; @@ -69,6 +70,7 @@ JNIEXPORT void JNICALL Java_android_app_NotificationManager_nativeShowNotificati if (!portal) { portal = xdp_portal_new(); g_signal_connect(portal, "notification-action-invoked", G_CALLBACK(notification_action_invoked), NULL); + ongoing_notifications = g_hash_table_new(NULL, NULL); } GVariantBuilder *builder = _PTR(builder_ptr); @@ -111,4 +113,19 @@ JNIEXPORT void JNICALL Java_android_app_NotificationManager_nativeShowNotificati callback_pending = 1; xdp_portal_add_notification(portal, id_string, variant, XDP_NOTIFICATION_FLAG_NONE, NULL, natification_callback, NULL); g_free(id_string); + if (ongoing) + g_hash_table_add(ongoing_notifications, GINT_TO_POINTER(id)); +} + +static void remove_ongoing_notification(gpointer key, gpointer value, gpointer user_data) +{ + char *id_string = g_strdup_printf("%d", GPOINTER_TO_INT(key)); + xdp_portal_remove_notification(portal, id_string); + g_free(id_string); +} + +void remove_ongoing_notifications() +{ + if (ongoing_notifications) + g_hash_table_foreach(ongoing_notifications, remove_ongoing_notification, NULL); } diff --git a/src/api-impl-jni/generated_headers/android_app_NotificationManager.h b/src/api-impl-jni/generated_headers/android_app_NotificationManager.h index 6194c9f1..09778313 100644 --- a/src/api-impl-jni/generated_headers/android_app_NotificationManager.h +++ b/src/api-impl-jni/generated_headers/android_app_NotificationManager.h @@ -26,10 +26,10 @@ JNIEXPORT void JNICALL Java_android_app_NotificationManager_nativeAddAction /* * Class: android_app_NotificationManager * Method: nativeShowNotification - * Signature: (JILjava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)V + * Signature: (JILjava/lang/String;Ljava/lang/String;Ljava/lang/String;ZILjava/lang/String;Ljava/lang/String;)V */ JNIEXPORT void JNICALL Java_android_app_NotificationManager_nativeShowNotification - (JNIEnv *, jobject, jlong, jint, jstring, jstring, jstring, jint, jstring, jstring); + (JNIEnv *, jobject, jlong, jint, jstring, jstring, jstring, jboolean, jint, jstring, jstring); #ifdef __cplusplus } diff --git a/src/api-impl/android/app/Notification.java b/src/api-impl/android/app/Notification.java index cd62ff67..2d16467a 100644 --- a/src/api-impl/android/app/Notification.java +++ b/src/api-impl/android/app/Notification.java @@ -52,6 +52,7 @@ public class Notification { List actions = new ArrayList(); PendingIntent intent; String iconPath; + boolean ongoing; public String toString() { return "Notification [" + title + ", " + text + ", " + actions + "]"; @@ -79,7 +80,10 @@ public class Notification { public Builder setLights(int argb, int onMs, int offMs) {return this;} - public Builder setOngoing(boolean ongoing) {return this;} + public Builder setOngoing(boolean ongoing) { + notification.ongoing = ongoing; + return this; + } public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {return this;} @@ -145,7 +149,12 @@ public class Notification { return this; } - public Builder setStyle(Style style) {return this;} + public Builder setStyle(Style style) { + if (style instanceof MediaStyle) { + notification.ongoing = true; + } + return this; + } public Builder setExtras(Bundle extras) {return this;} diff --git a/src/api-impl/android/app/NotificationManager.java b/src/api-impl/android/app/NotificationManager.java index 223a3945..cd09bb98 100644 --- a/src/api-impl/android/app/NotificationManager.java +++ b/src/api-impl/android/app/NotificationManager.java @@ -29,7 +29,7 @@ public class NotificationManager { actionName = notification.intent.intent.getAction(); className = notification.intent.intent.getComponent() != null ? notification.intent.intent.getComponent().getClassName() : null; } - nativeShowNotification(builder, id, notification.title, notification.text, notification.iconPath, intentType, actionName, className); + nativeShowNotification(builder, id, notification.title, notification.text, notification.iconPath, notification.ongoing, intentType, actionName, className); } public void notify(int id, Notification notification) { @@ -55,5 +55,5 @@ public class NotificationManager { protected native long nativeInitBuilder(); protected native void nativeAddAction(long builder, String title, int intentType, String action, String className); - protected native void nativeShowNotification(long builder, int id, String title, String text, String iconPath, int intentType, String action, String className); + protected native void nativeShowNotification(long builder, int id, String title, String text, String iconPath, boolean ongoing, int intentType, String action, String className); } diff --git a/src/main-executable/main.c b/src/main-executable/main.c index 409c68f8..0ced6979 100644 --- a/src/main-executable/main.c +++ b/src/main-executable/main.c @@ -472,6 +472,7 @@ void init_cmd_parameters(GApplication *app, struct jni_callback_data *d) } void init__r_debug(); +void remove_ongoing_notifications(); int main(int argc, char **argv) { @@ -500,6 +501,7 @@ int main(int argc, char **argv) g_signal_connect(app, "open", G_CALLBACK (open), callback_data); status = g_application_run(G_APPLICATION(app), argc, argv); g_object_unref(app); + remove_ongoing_notifications(); return status; }