From 4ecd541c6c3d8f841e4852b58506dc574eef23e4 Mon Sep 17 00:00:00 2001 From: Julian Winkler Date: Sat, 20 Jul 2024 14:13:12 +0200 Subject: [PATCH] NotificationManager: implement cancel() for MPRIS notifications --- .../app/android_app_NotificationManager.c | 15 +++++++++++++-- .../android_app_NotificationManager.h | 8 ++++++++ .../android/app/NotificationManager.java | 16 ++++++++++++++-- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/api-impl-jni/app/android_app_NotificationManager.c b/src/api-impl-jni/app/android_app_NotificationManager.c index beeb0ab2..145be2c8 100644 --- a/src/api-impl-jni/app/android_app_NotificationManager.c +++ b/src/api-impl-jni/app/android_app_NotificationManager.c @@ -145,6 +145,7 @@ void remove_ongoing_notifications() } static MediaPlayer2 *mpris = NULL; +static int dbus_name_id = 0; extern MediaPlayer2Player *mpris_player; extern GtkWindow *window; @@ -169,8 +170,9 @@ JNIEXPORT void JNICALL Java_android_app_NotificationManager_nativeShowMPRIS(JNIE if (!mpris) { mpris = media_player2_skeleton_new(); g_signal_connect(mpris, "handle-raise", G_CALLBACK(on_media_player_handle_raise), NULL); - - g_bus_own_name(G_BUS_TYPE_SESSION, MPRIS_BUS_NAME_PREFIX "ATL", G_BUS_NAME_OWNER_FLAGS_NONE, + } + if (!dbus_name_id) { + dbus_name_id = g_bus_own_name(G_BUS_TYPE_SESSION, MPRIS_BUS_NAME_PREFIX "ATL", G_BUS_NAME_OWNER_FLAGS_NONE, on_bus_acquired, NULL, NULL, mpris, NULL); } media_player2_set_can_raise(mpris, TRUE); @@ -185,3 +187,12 @@ JNIEXPORT void JNICALL Java_android_app_NotificationManager_nativeShowMPRIS(JNIE (*env)->ReleaseStringUTFChars(env, identity_jstr, identity); } } + +JNIEXPORT void JNICALL Java_android_app_NotificationManager_nativeCancelMPRIS(JNIEnv *env, jobject this) +{ + if (dbus_name_id) { + g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(mpris)); + g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(mpris_player)); + g_clear_handle_id (&dbus_name_id, g_bus_unown_name); + } +} 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 e1917725..8e69870c 100644 --- a/src/api-impl-jni/generated_headers/android_app_NotificationManager.h +++ b/src/api-impl-jni/generated_headers/android_app_NotificationManager.h @@ -47,6 +47,14 @@ JNIEXPORT void JNICALL Java_android_app_NotificationManager_nativeShowMPRIS JNIEXPORT void JNICALL Java_android_app_NotificationManager_nativeCancel (JNIEnv *, jobject, jint); +/* + * Class: android_app_NotificationManager + * Method: nativeCancelMPRIS + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_android_app_NotificationManager_nativeCancelMPRIS + (JNIEnv *, jobject); + #ifdef __cplusplus } #endif diff --git a/src/api-impl/android/app/NotificationManager.java b/src/api-impl/android/app/NotificationManager.java index d5231a34..b0d6177f 100644 --- a/src/api-impl/android/app/NotificationManager.java +++ b/src/api-impl/android/app/NotificationManager.java @@ -8,11 +8,17 @@ import android.os.Handler; import android.os.Looper; public class NotificationManager { + + private static int mpris_notification_id = -1; + public void cancelAll() {} public void notify(String tag, int id, Notification notification) { if (notification.style instanceof MediaStyle) { // MPRIS content is handled by MediaSession implementation - nativeShowMPRIS(Context.this_application.getPackageName(), Context.this_application.get_app_label()); + if (mpris_notification_id == -1) { + nativeShowMPRIS(Context.this_application.getPackageName(), Context.this_application.get_app_label()); + mpris_notification_id = id; + } return; } @@ -50,7 +56,12 @@ public class NotificationManager { new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { @Override public void run() { - nativeCancel(id); + if (mpris_notification_id == id) { + mpris_notification_id = -1; + nativeCancelMPRIS(); + } else { + nativeCancel(id); + } } }, 100); } @@ -79,4 +90,5 @@ public class NotificationManager { protected native void nativeShowNotification(long builder, int id, String title, String text, String iconPath, boolean ongoing, int intentType, String action, String className); protected native void nativeShowMPRIS(String packageName, String identiy); protected native void nativeCancel(int id); + protected native void nativeCancelMPRIS(); }