From acb00a8beb226075daa96a9556d23cd71668d63e Mon Sep 17 00:00:00 2001 From: Julian Winkler Date: Mon, 18 Mar 2024 14:37:56 +0100 Subject: [PATCH] NotificationManager: add support for icons --- .../app/android_app_NotificationManager.c | 18 +++++++++++++++++- .../android_app_NotificationManager.h | 4 ++-- src/api-impl-jni/util.h | 1 + src/api-impl/android/app/Notification.java | 6 +++++- .../android/app/NotificationManager.java | 4 ++-- 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/api-impl-jni/app/android_app_NotificationManager.c b/src/api-impl-jni/app/android_app_NotificationManager.c index 62f4ac47..6217e5e5 100644 --- a/src/api-impl-jni/app/android_app_NotificationManager.c +++ b/src/api-impl-jni/app/android_app_NotificationManager.c @@ -61,7 +61,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, 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, jint type, jstring action, jstring className) { if (callback_pending) { return; @@ -85,6 +85,22 @@ JNIEXPORT void JNICALL Java_android_app_NotificationManager_nativeShowNotificati g_variant_builder_add(builder, "{sv}", "body", g_variant_new_string(text)); (*env)->ReleaseStringUTFChars(env, text_jstr, text); } + if (icon_jstr) { + const char *icon_path = (*env)->GetStringUTFChars(env, icon_jstr, NULL); + extract_from_apk(icon_path, icon_path); + char *icon_path_full = g_strdup_printf("%s/%s", get_app_data_dir(), icon_path); + GMappedFile *icon_file = g_mapped_file_new(icon_path_full, FALSE, NULL); + GBytes *icon_bytes = g_mapped_file_get_bytes(icon_file); + GIcon *icon = g_bytes_icon_new(icon_bytes); + GVariant *icon_serialized = g_icon_serialize(icon); + g_variant_builder_add(builder, "{sv}", "icon", icon_serialized); + g_variant_unref(icon_serialized); + g_object_unref(icon); + g_bytes_unref(icon_bytes); + g_mapped_file_unref(icon_file); + g_free(icon_path_full); + (*env)->ReleaseStringUTFChars(env, icon_jstr, icon_path); + } g_variant_builder_add(builder, "{sv}", "default-action", g_variant_new_string("default-action")); g_variant_builder_add(builder, "{sv}", "default-action-target", serialize_intent(env, type, action, className)); g_variant_builder_add(builder, "{sv}", "buttons", buttons); 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 565237cd..6194c9f1 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;ILjava/lang/String;Ljava/lang/String;)V + * Signature: (JILjava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)V */ JNIEXPORT void JNICALL Java_android_app_NotificationManager_nativeShowNotification - (JNIEnv *, jobject, jlong, jint, jstring, jstring, jint, jstring, jstring); + (JNIEnv *, jobject, jlong, jint, jstring, jstring, jstring, jint, jstring, jstring); #ifdef __cplusplus } diff --git a/src/api-impl-jni/util.h b/src/api-impl-jni/util.h index 8e04549c..b40135f6 100644 --- a/src/api-impl-jni/util.h +++ b/src/api-impl-jni/util.h @@ -110,6 +110,7 @@ const char * attribute_set_get_string(JNIEnv *env, jobject attrs, char *attribut int attribute_set_get_int(JNIEnv *env, jobject attrs, char *attribute, char *schema, int default_value); void set_up_handle_cache(JNIEnv *env); void extract_from_apk(const char *path, const char *target); +char *get_app_data_dir(); void prepare_main_looper(JNIEnv* env); diff --git a/src/api-impl/android/app/Notification.java b/src/api-impl/android/app/Notification.java index 96fc7cd9..cd62ff67 100644 --- a/src/api-impl/android/app/Notification.java +++ b/src/api-impl/android/app/Notification.java @@ -51,6 +51,7 @@ public class Notification { String title; List actions = new ArrayList(); PendingIntent intent; + String iconPath; public String toString() { return "Notification [" + title + ", " + text + ", " + actions + "]"; @@ -65,7 +66,10 @@ public class Notification { public Builder setWhen(long when) {return this;} - public Builder setSmallIcon(int icon, int level) {return this;} + public Builder setSmallIcon(int icon, int level) { + notification.iconPath = Context.this_application.getString(icon); + return this; + } public Builder setContent(RemoteViews contentView) {return this;} diff --git a/src/api-impl/android/app/NotificationManager.java b/src/api-impl/android/app/NotificationManager.java index c4b76e38..223a3945 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, intentType, actionName, className); + nativeShowNotification(builder, id, notification.title, notification.text, notification.iconPath, 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, 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); }