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,

This comment has been minimized.

Copy link
@maniac103

maniac103 Jul 9, 2024

Contributor

Why this change?

This comment has been minimized.

Copy link
@mueller-ma

mueller-ma Jul 9, 2024

Author Member

This comment has been minimized.

Copy link
@maniac103

maniac103 Jul 9, 2024

Contributor

Well, I was referring to the change .toLong() -> .toLongOrNull() there ... not sure how that's related to the sentTime fallback.
I also am not sure what we'd gain from removing the fallback? If present, it seems sufficiently accurate?
At the very least, the comment above would need an update.

This comment has been minimized.

Copy link
@mueller-ma

mueller-ma Jul 9, 2024

Author Member

With sentTime fallback (which is sufficiently accurate), the issue wouldn't have been noticed. But without the fallback all notifications lack the creation time which is something that would have been noticed. Thus I removed the fallback.

This comment has been minimized.

Copy link
@maniac103

maniac103 Jul 9, 2024

Contributor

The issue was noticed because there wasn't a null fallback on parsing errors ... which you also changed here for reasons I do not yet understand. When not using that 'null on parsing errors' fallback, sentTime is also used if no timestamp is sent at all, which to my eye totally makes sense.

This comment has been minimized.

Copy link
@mueller-ma

mueller-ma Jul 10, 2024

Author Member

I reverted the change. The idea behind it was to:

  1. Not crash when the response is unexpected and
  2. Notice that by hiding the timestamp in the notification
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.