mirror of
https://github.com/home-assistant/android
synced 2024-10-15 20:43:06 +00:00
Use OkHttpClient to download notification videos (#2779)
- Change to the OkHttpClient used elsewhere by the app to download notification videos to resolve problems related to cookies and mTLS authentication. As a result, storing and deleting the file is now handled by the app instead of by the system.
This commit is contained in:
parent
d3c22cf341
commit
3436f2169d
|
@ -79,6 +79,8 @@ import kotlinx.coroutines.withContext
|
|||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import org.json.JSONObject
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.net.URL
|
||||
import java.net.URLDecoder
|
||||
import java.util.Locale
|
||||
|
@ -1395,19 +1397,31 @@ class MessagingManager @Inject constructor(
|
|||
Dispatchers.IO
|
||||
) {
|
||||
url ?: return@withContext null
|
||||
val videoFile = File(context.applicationContext.cacheDir.absolutePath + "/notifications/video-${System.currentTimeMillis()}")
|
||||
val processingFrames = mutableListOf<Deferred<Bitmap?>>()
|
||||
var processingFramesSize = 0
|
||||
var singleFrame = 0
|
||||
|
||||
try {
|
||||
MediaMetadataRetriever().let { mediaRetriever ->
|
||||
|
||||
val request = Request.Builder().apply {
|
||||
url(url)
|
||||
if (requiresAuth) {
|
||||
mediaRetriever.setDataSource(url.toString(), mapOf("Authorization" to authenticationUseCase.buildBearerToken()))
|
||||
} else {
|
||||
mediaRetriever.setDataSource(url.toString(), hashMapOf())
|
||||
addHeader("Authorization", authenticationUseCase.buildBearerToken())
|
||||
}
|
||||
}.build()
|
||||
val response = okHttpClient.newCall(request).execute()
|
||||
|
||||
if (!videoFile.exists()) {
|
||||
videoFile.parentFile?.mkdirs()
|
||||
videoFile.createNewFile()
|
||||
}
|
||||
FileOutputStream(videoFile).use { output ->
|
||||
response.body?.byteStream()?.copyTo(output)
|
||||
}
|
||||
response.close()
|
||||
|
||||
mediaRetriever.setDataSource(videoFile.absolutePath)
|
||||
val durationInMicroSeconds = ((mediaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toLongOrNull() ?: VIDEO_GUESS_MILLISECONDS)) * 1000
|
||||
|
||||
// Start at 100 milliseconds and get frames every 0.75 seconds until reaching the end
|
||||
|
@ -1435,7 +1449,9 @@ class MessagingManager @Inject constructor(
|
|||
Log.e(TAG, "Couldn't download video for notification", e)
|
||||
}
|
||||
|
||||
return@withContext processingFrames.awaitAll().filterNotNull()
|
||||
val frames = processingFrames.awaitAll().filterNotNull()
|
||||
videoFile.delete()
|
||||
return@withContext frames
|
||||
}
|
||||
|
||||
private fun Bitmap.getCompressedFrame(): Bitmap? {
|
||||
|
|
Loading…
Reference in a new issue