Skip to content

Commit

Permalink
FCM parsing and image loading
Browse files Browse the repository at this point in the history
Signed-off-by: mueller-ma <[email protected]>
  • Loading branch information
mueller-ma committed Jul 9, 2024
1 parent 69ea76a commit 0359833
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion mobile/src/main/res/layout/notificationlist_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
Expand Down

0 comments on commit 0359833

Please sign in to comment.