Media: fix zoomable image with non content uri
This commit is contained in:
parent
7c7f167692
commit
1086aedb0c
4 changed files with 76 additions and 9 deletions
|
|
@ -18,10 +18,17 @@ package io.element.android.features.messages.impl.media.local
|
||||||
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Parcelable
|
import android.os.Parcelable
|
||||||
|
import kotlinx.parcelize.IgnoredOnParcel
|
||||||
import kotlinx.parcelize.Parcelize
|
import kotlinx.parcelize.Parcelize
|
||||||
|
|
||||||
@Parcelize
|
@Parcelize
|
||||||
data class LocalMedia(
|
data class LocalMedia(
|
||||||
val uri: Uri,
|
val uri: Uri,
|
||||||
val mimeType: String,
|
val mimeType: String,
|
||||||
) : Parcelable
|
) : Parcelable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This tries to convert the uri to a file if applicable, otherwise keep it as uri.
|
||||||
|
*/
|
||||||
|
@IgnoredOnParcel val model: Any = UriToFileMapper.map(uri) ?: uri
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,6 @@
|
||||||
package io.element.android.features.messages.impl.media.local
|
package io.element.android.features.messages.impl.media.local
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.net.Uri
|
|
||||||
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
|
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
|
||||||
import android.widget.FrameLayout
|
import android.widget.FrameLayout
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
|
@ -36,7 +35,10 @@ import androidx.media3.exoplayer.ExoPlayer
|
||||||
import androidx.media3.ui.AspectRatioFrameLayout
|
import androidx.media3.ui.AspectRatioFrameLayout
|
||||||
import androidx.media3.ui.PlayerView
|
import androidx.media3.ui.PlayerView
|
||||||
import io.element.android.libraries.designsystem.utils.OnLifecycleEvent
|
import io.element.android.libraries.designsystem.utils.OnLifecycleEvent
|
||||||
|
import me.saket.telephoto.zoomable.ZoomSpec
|
||||||
import me.saket.telephoto.zoomable.coil.ZoomableAsyncImage
|
import me.saket.telephoto.zoomable.coil.ZoomableAsyncImage
|
||||||
|
import me.saket.telephoto.zoomable.rememberZoomableImageState
|
||||||
|
import me.saket.telephoto.zoomable.rememberZoomableState
|
||||||
|
|
||||||
@SuppressLint("UnsafeOptInUsageError")
|
@SuppressLint("UnsafeOptInUsageError")
|
||||||
@Composable
|
@Composable
|
||||||
|
|
@ -46,11 +48,11 @@ fun LocalMediaView(
|
||||||
) {
|
) {
|
||||||
when {
|
when {
|
||||||
MimeTypes.isImage(localMedia.mimeType) -> MediaImageView(
|
MimeTypes.isImage(localMedia.mimeType) -> MediaImageView(
|
||||||
uri = localMedia.uri,
|
localMedia = localMedia,
|
||||||
modifier = modifier
|
modifier = modifier
|
||||||
)
|
)
|
||||||
MimeTypes.isVideo(localMedia.mimeType) -> MediaVideoView(
|
MimeTypes.isVideo(localMedia.mimeType) -> MediaVideoView(
|
||||||
uri = localMedia.uri,
|
localMedia = localMedia,
|
||||||
modifier = modifier
|
modifier = modifier
|
||||||
)
|
)
|
||||||
else -> Unit
|
else -> Unit
|
||||||
|
|
@ -59,12 +61,16 @@ fun LocalMediaView(
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
private fun MediaImageView(
|
private fun MediaImageView(
|
||||||
uri: Uri,
|
localMedia: LocalMedia,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
|
val zoomableState = rememberZoomableState(
|
||||||
|
zoomSpec = ZoomSpec(maxZoomFactor = 3f)
|
||||||
|
)
|
||||||
ZoomableAsyncImage(
|
ZoomableAsyncImage(
|
||||||
modifier = modifier.fillMaxSize(),
|
modifier = modifier.fillMaxSize(),
|
||||||
model = uri,
|
state = rememberZoomableImageState(zoomableState),
|
||||||
|
model = localMedia.model,
|
||||||
contentDescription = "Image",
|
contentDescription = "Image",
|
||||||
contentScale = ContentScale.Fit,
|
contentScale = ContentScale.Fit,
|
||||||
)
|
)
|
||||||
|
|
@ -73,7 +79,7 @@ private fun MediaImageView(
|
||||||
@UnstableApi
|
@UnstableApi
|
||||||
@Composable
|
@Composable
|
||||||
fun MediaVideoView(
|
fun MediaVideoView(
|
||||||
uri: Uri,
|
localMedia: LocalMedia,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
|
|
@ -84,8 +90,8 @@ fun MediaVideoView(
|
||||||
this.prepare()
|
this.prepare()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
LaunchedEffect(uri) {
|
LaunchedEffect(localMedia.uri) {
|
||||||
val mediaItem = MediaItem.fromUri(uri)
|
val mediaItem = MediaItem.fromUri(localMedia.uri)
|
||||||
exoPlayer.setMediaItem(mediaItem)
|
exoPlayer.setMediaItem(mediaItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2023 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.element.android.features.messages.impl.media.local
|
||||||
|
|
||||||
|
import android.content.ContentResolver
|
||||||
|
import android.net.Uri
|
||||||
|
import io.element.android.libraries.androidutils.uri.ASSET_FILE_PATH_ROOT
|
||||||
|
import io.element.android.libraries.androidutils.uri.firstPathSegment
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tries to convert a URI to a File.
|
||||||
|
* Extracted from Coil [coil.map.FileUriMapper]
|
||||||
|
*/
|
||||||
|
object UriToFileMapper {
|
||||||
|
|
||||||
|
fun map(data: Uri): File? {
|
||||||
|
if (!isApplicable(data)) return null
|
||||||
|
return if (data.scheme == ContentResolver.SCHEME_FILE) {
|
||||||
|
data.path?.let(::File)
|
||||||
|
} else {
|
||||||
|
// If the scheme is not "file", it's null, representing a literal path on disk.
|
||||||
|
// Assume the entire input, regardless of any reserved characters, is valid.
|
||||||
|
File(data.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isApplicable(data: Uri): Boolean {
|
||||||
|
return data.scheme.let { it == null || it == ContentResolver.SCHEME_FILE } &&
|
||||||
|
data.path.orEmpty().startsWith('/') && data.firstPathSegment != null
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isAssetUri(uri: Uri): Boolean {
|
||||||
|
return uri.scheme == ContentResolver.SCHEME_FILE && uri.firstPathSegment == ASSET_FILE_PATH_ROOT
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -18,8 +18,12 @@ package io.element.android.libraries.androidutils.uri
|
||||||
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
|
||||||
|
const val ASSET_FILE_PATH_ROOT = "android_asset"
|
||||||
const val IGNORED_SCHEMA = "ignored"
|
const val IGNORED_SCHEMA = "ignored"
|
||||||
|
|
||||||
fun Uri.isIgnored() = scheme == IGNORED_SCHEMA
|
fun Uri.isIgnored() = scheme == IGNORED_SCHEMA
|
||||||
|
|
||||||
fun createIgnoredUri(path: String): Uri = Uri.parse("$IGNORED_SCHEMA://$path")
|
fun createIgnoredUri(path: String): Uri = Uri.parse("$IGNORED_SCHEMA://$path")
|
||||||
|
|
||||||
|
val Uri.firstPathSegment: String?
|
||||||
|
get() = pathSegments.firstOrNull()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue