Media Viewer: rename the shared file with the known name if any.

This commit is contained in:
ganfra
2023-06-02 17:46:48 +02:00
parent 235efeadb6
commit c2fa9e6add
2 changed files with 23 additions and 1 deletions

View File

@@ -16,6 +16,7 @@
package io.element.android.features.messages.impl.media.local
import android.content.ContentResolver
import android.content.ContentValues
import android.content.Context
import android.content.Intent
@@ -45,6 +46,7 @@ class AndroidLocalMediaActions @Inject constructor(
) : LocalMediaActions {
override suspend fun saveOnDisk(localMedia: LocalMedia): Result<Unit> = withContext(coroutineDispatchers.io) {
require(localMedia.uri.scheme == ContentResolver.SCHEME_FILE)
runCatching {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
saveOnDiskUsingMediaStore(localMedia)
@@ -59,6 +61,7 @@ class AndroidLocalMediaActions @Inject constructor(
}
override suspend fun share(localMedia: LocalMedia): Result<Unit> = withContext(coroutineDispatchers.io) {
require(localMedia.uri.scheme == ContentResolver.SCHEME_FILE)
runCatching {
val authority = "${buildMeta.applicationId}.fileprovider"
val uriFromFileProvider = FileProvider.getUriForFile(context, authority, localMedia.toFile())
@@ -109,7 +112,17 @@ class AndroidLocalMediaActions @Inject constructor(
return context.contentResolver.openInputStream(uri)
}
/**
* Tries to extract a file from the uri and rename it using the local media name if defined.
*/
private fun LocalMedia.toFile(): File {
return uri.toFile()
val uriAsFile = uri.toFile()
return if (name != null) {
File(uriAsFile.parentFile, name).apply {
uriAsFile.renameTo(this)
}
} else {
uriAsFile
}
}
}

View File

@@ -17,7 +17,16 @@
package io.element.android.features.messages.impl.media.local
interface LocalMediaActions {
/**
* Will save the current media to the Downloads directory.
* The [LocalMedia.uri] needs to have a file scheme.
*/
suspend fun saveOnDisk(localMedia: LocalMedia): Result<Unit>
/**
* Will try to find a suitable application to share the media with.
* The [LocalMedia.uri] needs to have a file scheme.
*/
suspend fun share(localMedia: LocalMedia): Result<Unit>
}