Use OkHttpClient to download notification images (#2768)

- Change to the OkHttpClient used elsewhere by the app to download notification images to resolve problems related to cookies and mTLS authentication
This commit is contained in:
Joris Pelgröm 2022-08-06 19:03:38 +02:00 committed by GitHub
parent 906e1f7a9b
commit 22df320fd9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -76,6 +76,8 @@ import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONObject
import java.net.URL
import java.net.URLDecoder
@ -86,6 +88,7 @@ import io.homeassistant.companion.android.common.R as commonR
class MessagingManager @Inject constructor(
@ApplicationContext val context: Context,
private val okHttpClient: OkHttpClient,
private val integrationUseCase: IntegrationRepository,
private val urlUseCase: UrlRepository,
private val authenticationUseCase: AuthenticationRepository,
@ -1332,11 +1335,16 @@ class MessagingManager @Inject constructor(
var image: Bitmap? = null
try {
val uc = url.openConnection()
if (requiresAuth) {
uc.setRequestProperty("Authorization", authenticationUseCase.buildBearerToken())
}
image = BitmapFactory.decodeStream(uc.getInputStream())
val request = Request.Builder().apply {
url(url)
if (requiresAuth) {
addHeader("Authorization", authenticationUseCase.buildBearerToken())
}
}.build()
val response = okHttpClient.newCall(request).execute()
image = BitmapFactory.decodeStream(response.body?.byteStream())
response.close()
} catch (e: Exception) {
Log.e(TAG, "Couldn't download image for notification", e)
}