Skip to content

Commit

Permalink
Catch more HttpException
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
mueller-ma committed Jul 12, 2024
1 parent 13a7929 commit 3a7236a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -60,7 +62,12 @@ 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) {
Log.e(TAG, "Error loading item for image", e)
null
}
item?.state?.asString
} else {
null
Expand All @@ -70,10 +77,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? {
Expand All @@ -84,6 +96,10 @@ data class CloudNotification internal constructor(
null
}
}

companion object {
private val TAG = CloudNotification::class.java.simpleName
}
}

@Throws(JSONException::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions mobile/src/main/java/org/openhab/habdroid/util/ItemClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item>? {
val response = connection.httpClient.get("rest/items")
val contentType = response.response.contentType()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 3a7236a

Please sign in to comment.