From 9885d6136562084bbe3a118994781da9d6c4cd71 Mon Sep 17 00:00:00 2001 From: mueller-ma Date: Fri, 12 Jul 2024 09:46:47 +0200 Subject: [PATCH] Catch more HttpException I see some crashes due to HttpException in Firebase, but there's no origin visible. This PR adds a few more try/catch blocks around network calls. Let's see if that fixes all crashes. Signed-off-by: mueller-ma --- .../habdroid/model/CloudNotification.kt | 25 +++++++++++++++---- .../habdroid/ui/ImageWidgetActivity.kt | 17 ++++++++----- .../org/openhab/habdroid/util/ItemClient.kt | 2 ++ 3 files changed, 33 insertions(+), 11 deletions(-) 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 a3c84d0e1f..90d367ba0f 100644 --- a/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt +++ b/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt @@ -18,6 +18,7 @@ import android.graphics.Bitmap import android.graphics.BitmapFactory import android.os.Parcelable import android.util.Base64 +import android.util.Log import java.text.ParseException import java.text.SimpleDateFormat import java.util.Locale @@ -27,6 +28,7 @@ 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.HttpClient import org.openhab.habdroid.util.IconBackground import org.openhab.habdroid.util.ImageConversionPolicy import org.openhab.habdroid.util.ItemClient @@ -60,7 +62,11 @@ data class CloudNotification internal constructor( } val itemStateFromMedia = if (mediaAttachmentUrl.startsWith("item:")) { val itemName = mediaAttachmentUrl.removePrefix("item:") - val item = ItemClient.loadItem(connection, itemName) + val item = try { + ItemClient.loadItem(connection, itemName) + } catch (e: HttpClient.HttpException) { + null + } item?.state?.asString } else { null @@ -70,10 +76,15 @@ data class CloudNotification internal constructor( return bitmapFromBase64(itemStateFromMedia) } val fallbackColor = context.getIconFallbackColor(IconBackground.APP_THEME) - return connection.httpClient - .get(itemStateFromMedia ?: mediaAttachmentUrl) - .asBitmap(size, fallbackColor, ImageConversionPolicy.PreferTargetSize) - .response + return try { + connection.httpClient + .get(itemStateFromMedia ?: mediaAttachmentUrl) + .asBitmap(size, fallbackColor, ImageConversionPolicy.PreferTargetSize) + .response + } catch (e: HttpClient.HttpException) { + Log.e(TAG, "Error loading image", e) + null + } } private fun bitmapFromBase64(itemState: String): Bitmap? { @@ -84,6 +95,10 @@ data class CloudNotification internal constructor( null } } + + companion object { + private val TAG = CloudNotification::class.java.simpleName + } } @Throws(JSONException::class) diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/ImageWidgetActivity.kt b/mobile/src/main/java/org/openhab/habdroid/ui/ImageWidgetActivity.kt index 7d557f52c4..f73a6e419f 100644 --- a/mobile/src/main/java/org/openhab/habdroid/ui/ImageWidgetActivity.kt +++ b/mobile/src/main/java/org/openhab/habdroid/ui/ImageWidgetActivity.kt @@ -130,12 +130,17 @@ class ImageWidgetActivity : AbstractBaseActivity() { } } else { val link = intent.getStringExtra(WIDGET_LINK)!! - val widgetState = JSONObject( - conn.httpClient - .get(link) - .asText() - .response - ).getString("state") ?: return finish() + val widgetState = try { + JSONObject( + conn.httpClient + .get(link) + .asText() + .response + ).getString("state") ?: return finish() + } catch (e: HttpClient.HttpException) { + Log.d(TAG, "Failed to load image", e) + return finish() + } if (widgetState.matches("data:image/.*;base64,.*".toRegex())) { Log.d(TAG, "Load image from value") diff --git a/mobile/src/main/java/org/openhab/habdroid/util/ItemClient.kt b/mobile/src/main/java/org/openhab/habdroid/util/ItemClient.kt index 9ee28c85ef..763d907a46 100644 --- a/mobile/src/main/java/org/openhab/habdroid/util/ItemClient.kt +++ b/mobile/src/main/java/org/openhab/habdroid/util/ItemClient.kt @@ -34,6 +34,7 @@ import org.xml.sax.SAXException object ItemClient { private val TAG = ItemClient::class.java.simpleName + @Throws(HttpClient.HttpException::class) suspend fun loadItems(connection: Connection): List? { val response = connection.httpClient.get("rest/items") val contentType = response.response.contentType() @@ -72,6 +73,7 @@ object ItemClient { } } + @Throws(HttpClient.HttpException::class) suspend fun loadItem(connection: Connection, itemName: String): Item? { val response = connection.httpClient.get("rest/items/$itemName") val contentType = response.response.contentType()