From 03598338585cc1a30e285b2e40a501dde804a052 Mon Sep 17 00:00:00 2001 From: mueller-ma Date: Tue, 9 Jul 2024 20:59:04 +0200 Subject: [PATCH] FCM parsing and image loading Signed-off-by: mueller-ma --- .../core/FcmMessageListenerService.kt | 8 ++--- .../habdroid/model/CloudNotification.kt | 32 +++++++++++++++---- .../main/res/layout/notificationlist_item.xml | 2 +- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/mobile/src/full/java/org/openhab/habdroid/core/FcmMessageListenerService.kt b/mobile/src/full/java/org/openhab/habdroid/core/FcmMessageListenerService.kt index 63ab47dabf..486dad7e23 100644 --- a/mobile/src/full/java/org/openhab/habdroid/core/FcmMessageListenerService.kt +++ b/mobile/src/full/java/org/openhab/habdroid/core/FcmMessageListenerService.kt @@ -56,12 +56,12 @@ class FcmMessageListenerService : FirebaseMessagingService() { // Older versions of openhab-cloud didn't send the notification generation // timestamp, so use the (undocumented) google.sent_time as a time reference // in that case. If that also isn't present, don't show time at all. - createdTimestamp = data["timestamp"]?.toLongOrNull() ?: message.sentTime, + createdTimestamp = data["timestamp"]?.toLongOrNull() ?: 0, icon = data["icon"].toOH2IconResource(), - severity = data["severity"], + severity = data["tag"], actions = actions, - onClickAction = data["TODO"].toCloudNotificationAction(), - mediaAttachmentUrl = data["TODO"] + onClickAction = data["on-click"].toCloudNotificationAction(), + mediaAttachmentUrl = data["media-attachment-url"] ) runBlocking { diff --git a/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt b/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt index bf0274601a..ba6a65d246 100644 --- a/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt +++ b/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt @@ -15,17 +15,21 @@ package org.openhab.habdroid.model import android.content.Context import android.graphics.Bitmap +import android.graphics.BitmapFactory import android.os.Parcelable +import android.util.Base64 import java.text.ParseException import java.text.SimpleDateFormat import java.util.Locale import java.util.TimeZone import kotlinx.parcelize.Parcelize +import okhttp3.HttpUrl.Companion.toHttpUrlOrNull import org.json.JSONException import org.json.JSONObject import org.openhab.habdroid.core.connection.Connection import org.openhab.habdroid.util.IconBackground import org.openhab.habdroid.util.ImageConversionPolicy +import org.openhab.habdroid.util.ItemClient import org.openhab.habdroid.util.getIconFallbackColor import org.openhab.habdroid.util.map import org.openhab.habdroid.util.optStringOrNull @@ -46,17 +50,33 @@ data class CloudNotification internal constructor( suspend fun loadImage(connection: Connection, context: Context, size: Int): Bitmap? { mediaAttachmentUrl ?: return null - //if (mediaAttachmentUrl.startsWith("item:")) { - // val itemName = mediaAttachmentUrl.removePrefix("item:") - // val item = ItemClient.loadItem(connection, itemName) - // - //} + val url = if (mediaAttachmentUrl.startsWith("item:")) { + val itemName = mediaAttachmentUrl.removePrefix("item:") + val item = ItemClient.loadItem(connection, itemName) + val state = item?.state?.asString ?: return null + if (state.toHttpUrlOrNull() != null) { + state + } else { + return bitmapFromBase64(state) + } + } else { + mediaAttachmentUrl + } val fallbackColor = context.getIconFallbackColor(IconBackground.APP_THEME) return connection.httpClient - .get(mediaAttachmentUrl) + .get(url) .asBitmap(size, fallbackColor, ImageConversionPolicy.PreferTargetSize) .response } + + private fun bitmapFromBase64(itemState: String): Bitmap? { + return try { + val data = Base64.decode(itemState, Base64.DEFAULT) + BitmapFactory.decodeByteArray(data, 0, data.size) + } catch (e: IllegalArgumentException) { + null + } + } } @Throws(JSONException::class) diff --git a/mobile/src/main/res/layout/notificationlist_item.xml b/mobile/src/main/res/layout/notificationlist_item.xml index 8f81114fa2..13a3a6f0c6 100644 --- a/mobile/src/main/res/layout/notificationlist_item.xml +++ b/mobile/src/main/res/layout/notificationlist_item.xml @@ -83,7 +83,7 @@ app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/notificationCreated" - app:srcCompat="@android:mipmap/sym_def_app_icon" /> + tools:srcCompat="@android:mipmap/sym_def_app_icon" />