diff --git a/app/src/main/java/io/homeassistant/companion/android/notifications/MessagingManager.kt b/app/src/main/java/io/homeassistant/companion/android/notifications/MessagingManager.kt index 076bce24da6..2cbf45bea1a 100644 --- a/app/src/main/java/io/homeassistant/companion/android/notifications/MessagingManager.kt +++ b/app/src/main/java/io/homeassistant/companion/android/notifications/MessagingManager.kt @@ -48,6 +48,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext import io.homeassistant.companion.android.R import io.homeassistant.companion.android.authenticator.Authenticator import io.homeassistant.companion.android.common.R as commonR +import io.homeassistant.companion.android.common.data.HomeAssistantApis import io.homeassistant.companion.android.common.data.prefs.PrefsRepository import io.homeassistant.companion.android.common.data.servers.ServerManager import io.homeassistant.companion.android.common.notifications.DeviceCommandData @@ -1256,7 +1257,7 @@ class MessagingManager @Inject constructor( withContext( Dispatchers.IO ) { - if (url == null || url.path.endsWith("gif").not()) { + if (url == null || !isGif(url, requiresAuth, serverId)) { return@withContext null } @@ -1287,6 +1288,64 @@ class MessagingManager @Inject constructor( return@withContext FileProvider.getUriForFile(context, "${context.packageName}.provider", file) } + private suspend fun isGif(url: URL, requiresAuth: Boolean = false, serverId: Int): Boolean { + withContext( + Dispatchers.IO + ) { + try { + Log.d(TAG, "Checking URL: $url") + + if (url.protocol == "http" || url.protocol == "https") { + Log.d(TAG, "It's an http URL") + + val client = OkHttpClient() + val requestBuilder = Request.Builder().apply { + url(url) + addHeader("User-Agent", HomeAssistantApis.USER_AGENT_STRING) + if (requiresAuth) { + Log.d(TAG, "Authorization required!") + val authToken = serverManager.authenticationRepository(serverId).buildBearerToken() + addHeader("Authorization", authToken) + Log.d(TAG, "Auth Token: $authToken") + } + } + + // Try with HEAD method first + val headRequest = requestBuilder.method("HEAD", null).head().build() + val headResponse = client.newCall(headRequest).execute() + + val responseCode = headResponse.code + val contentType = headResponse.header("Content-Type") + + Log.d(TAG, "Response Code (HEAD): $responseCode") + Log.d(TAG, "Content-Type (HEAD): $contentType") + + headResponse.close() + + if (responseCode == 405) { + Log.d(TAG, "HEAD method not allowed, retrying with GET") + // Retry with GET method + val getRequest = requestBuilder.method("HEAD", null).head().build() + val getResponse = client.newCall(getRequest).execute() + val getContentType = getResponse.header("Content-Type") + val getResponseCode = getResponse.code + getResponse.close() + Log.d(TAG, "Response Code (GET): $getResponseCode") + Log.d(TAG, "Content-Type (GET): $getContentType") + return getContentType != null && getContentType.startsWith("image/gif") + } else { + val contentType = headResponse.header("Content-Type") + Log.d(TAG, "Content-Type (HEAD): $contentType") + return contentType != null && contentType.startsWith("image/gif") + } + } + } catch (e: Exception) { + Log.e(TAG, "Error checking content type", e) + } + return false + } + } + private suspend fun handleVideo( builder: NotificationCompat.Builder, data: Map