Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Media Picker: Choose From Device: Fix Video thumbnails do not load #15411

Merged
merged 7 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.wordpress.android.ui.media

import android.content.Context
import android.net.Uri
import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.C.ContentType
Expand All @@ -12,6 +13,7 @@ import com.google.android.exoplayer2.source.hls.HlsMediaSource
import com.google.android.exoplayer2.source.hls.playlist.DefaultHlsPlaylistParserFactory
import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource
import com.google.android.exoplayer2.source.smoothstreaming.manifest.SsManifestParser
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory
import com.google.android.exoplayer2.util.Util
import dagger.Reusable
Expand All @@ -22,7 +24,8 @@ import javax.inject.Inject
@Reusable
class ExoPlayerUtils
@Inject constructor(
private val authenticationUtils: AuthenticationUtils
private val authenticationUtils: AuthenticationUtils,
private val appContext: Context
) {
private var httpDataSourceFactory: DefaultHttpDataSourceFactory? = null

Expand All @@ -34,19 +37,23 @@ class ExoPlayerUtils
return httpDataSourceFactory as DefaultHttpDataSourceFactory
}

private fun buildDefaultDataSourceFactory(httpDataSourceFactory: DefaultHttpDataSourceFactory) =
DefaultDataSourceFactory(appContext, httpDataSourceFactory)

fun buildMediaSource(uri: Uri): MediaSource? {
val httpDataSourceFactory = buildHttpDataSourceFactory(uri.toString())
val defaultDataSourceFactory = buildDefaultDataSourceFactory(httpDataSourceFactory)
return when (@ContentType val type = Util.inferContentType(uri)) {
C.TYPE_DASH -> Factory(httpDataSourceFactory)
C.TYPE_DASH -> Factory(defaultDataSourceFactory)
.setManifestParser(FilteringManifestParser(DashManifestParser(), null))
.createMediaSource(uri)
C.TYPE_SS -> SsMediaSource.Factory(httpDataSourceFactory)
C.TYPE_SS -> SsMediaSource.Factory(defaultDataSourceFactory)
.setManifestParser(FilteringManifestParser(SsManifestParser(), null))
.createMediaSource(uri)
C.TYPE_HLS -> HlsMediaSource.Factory(httpDataSourceFactory)
C.TYPE_HLS -> HlsMediaSource.Factory(defaultDataSourceFactory)
.setPlaylistParserFactory(DefaultHlsPlaylistParserFactory())
.createMediaSource(uri)
C.TYPE_OTHER -> ExtractorMediaSource.Factory(httpDataSourceFactory).createMediaSource(uri)
C.TYPE_OTHER -> ExtractorMediaSource.Factory(defaultDataSourceFactory).createMediaSource(uri)
else -> {
throw IllegalStateException("$UNSUPPORTED_TYPE $type")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.wordpress.android.ui.media

import android.content.Context
import android.net.Uri
import android.provider.OpenableColumns
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
Expand All @@ -9,6 +12,7 @@ import org.wordpress.android.modules.BG_THREAD
import org.wordpress.android.modules.UI_THREAD
import org.wordpress.android.ui.utils.AuthenticationUtils
import org.wordpress.android.util.AppLog.T
import org.wordpress.android.util.MediaUtilsWrapper
import java.io.IOException
import java.net.URL
import javax.inject.Inject
Expand All @@ -19,7 +23,9 @@ class VideoLoader
@param:Named(BG_THREAD) private val bgDispatcher: CoroutineDispatcher,
@param:Named(UI_THREAD) private val mainDispatcher: CoroutineDispatcher,
private val authenticationUtils: AuthenticationUtils,
private val appLogWrapper: AppLogWrapper
private val appLogWrapper: AppLogWrapper,
private val mediaUtilsWrapper: MediaUtilsWrapper,
private val appContext: Context
) {
fun runIfMediaNotTooBig(
scope: CoroutineScope,
Expand All @@ -31,23 +37,18 @@ class VideoLoader
var length = MIN_SIZE
withContext(bgDispatcher) {
try {
val url = URL(filePath)

val urlConnection = url.openConnection()
for ((key, value) in authenticationUtils.getAuthHeaders(filePath).entries) {
urlConnection.addRequestProperty(key, value)
}

length = urlConnection.contentLength
if (length <= MIN_SIZE) {
length = urlConnection.getHeaderFieldInt("Content-Length", MIN_SIZE)
val uri = Uri.parse(filePath)
length = if (mediaUtilsWrapper.isInMediaStore(uri)) {
getSizeFromContentUri(uri)
} else {
getSizeFromURL(URL(filePath))
}
} catch (ioe: IOException) {
appLogWrapper.e(T.MEDIA, "Failed to load video thumbnail: ${ioe.stackTrace}")
}
}
withContext(mainDispatcher) {
if (length > MIN_SIZE && length < SIZE_LIMIT_10_MB) {
if (length in (MIN_SIZE + 1) until SIZE_LIMIT_10_MB) {
loadAction()
} else {
fallbackAction()
Expand All @@ -56,6 +57,27 @@ class VideoLoader
}
}

private fun getSizeFromContentUri(contentUri: Uri) =
appContext.contentResolver.query(contentUri, null, null, null, null, null).use { cursor ->
cursor?.moveToFirst()?.takeIf { true }?.let {
val sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE)
if (!cursor.isNull(sizeIndex)) cursor.getInt(sizeIndex) else null
} ?: 0
}

private fun getSizeFromURL(url: URL): Int {
val urlConnection = url.openConnection()
for ((key, value) in authenticationUtils.getAuthHeaders(url.toString()).entries) {
urlConnection.addRequestProperty(key, value)
}

var length = urlConnection.contentLength
if (length <= MIN_SIZE) {
length = urlConnection.getHeaderFieldInt("Content-Length", MIN_SIZE)
}
return length
}

companion object {
private const val MIN_SIZE = 0
private const val SIZE_LIMIT_10_MB = 10485760
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,9 @@ class MediaPickerViewModel @Inject constructor(
}
when (identifier) {
is LocalUri -> {
_onNavigate.postValue(Event(PreviewUrl(identifier.value.toString())))
mediaUtilsWrapper.getRealPathFromURI(identifier.value.uri)?.let { path ->
_onNavigate.postValue(Event(PreviewUrl(path)))
}
}
is StockMediaIdentifier -> {
if (identifier.url != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Activity
import android.app.Application
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.net.Uri
import android.text.TextUtils
Expand Down Expand Up @@ -143,8 +144,11 @@ class ImageManager @Inject constructor(
},
fallbackAction = {
if (!context.isAvailable()) return@runIfMediaNotTooBig
val fallbackDrawable = placeholderManager.getErrorResource(imageType)?.let {
ColorDrawable(ContextCompat.getColor(context, it))
}
GlideApp.with(context)
.load(placeholderManager.getErrorResource(imageType))
.load(fallbackDrawable)
.addPlaceholder(imageType)
.addFallback(imageType)
.into(imageView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.wordpress.android.ui.mediapicker

import android.content.Context
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.anyOrNull
import com.nhaarman.mockitokotlin2.doAnswer
import com.nhaarman.mockitokotlin2.inOrder
import com.nhaarman.mockitokotlin2.times
Expand Down Expand Up @@ -278,6 +279,7 @@ class MediaPickerViewModelTest : BaseUnitTest() {

@Test
fun `navigates to preview on item click`() = test {
whenever(mediaUtilsWrapper.getRealPathFromURI(anyOrNull())).thenReturn(firstItem.url)
setupViewModel(listOf(firstItem, secondItem), singleSelectMediaPickerSetup)

viewModel.refreshData(false)
Expand Down