NotificationManager: implement cancel() for MPRIS notifications

This commit is contained in:
Julian Winkler
2024-07-20 14:13:12 +02:00
parent 9d27fc138b
commit 4ecd541c6c
3 changed files with 35 additions and 4 deletions

View File

@@ -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);
}
}

View File

@@ -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

View File

@@ -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
if (mpris_notification_id == -1) {
nativeShowMPRIS(Context.this_application.getPackageName(), Context.this_application.get_app_label());
mpris_notification_id = id;
}
return;
}
@@ -50,8 +56,13 @@ public class NotificationManager {
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
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();
}