Improve handling of shared media (#2388)

* support file:// uris

* support subjects on image shares

* improve code

* improve code

* improve code
This commit is contained in:
Konrad Pozniak 2022-03-28 18:39:05 +02:00 committed by GitHub
parent 106585f4fe
commit c47804997c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 38 deletions

View file

@ -236,26 +236,25 @@ class ComposeActivity :
} }
} }
} }
} else if (type == "text/plain" && intent.action == Intent.ACTION_SEND) { }
val subject = intent.getStringExtra(Intent.EXTRA_SUBJECT) val subject = intent.getStringExtra(Intent.EXTRA_SUBJECT)
val text = intent.getStringExtra(Intent.EXTRA_TEXT).orEmpty() val text = intent.getStringExtra(Intent.EXTRA_TEXT).orEmpty()
val shareBody = if (!subject.isNullOrBlank() && subject !in text) { val shareBody = if (!subject.isNullOrBlank() && subject !in text) {
subject + '\n' + text subject + '\n' + text
} else { } else {
text text
} }
if (shareBody.isNotBlank()) { if (shareBody.isNotBlank()) {
val start = binding.composeEditField.selectionStart.coerceAtLeast(0) val start = binding.composeEditField.selectionStart.coerceAtLeast(0)
val end = binding.composeEditField.selectionEnd.coerceAtLeast(0) val end = binding.composeEditField.selectionEnd.coerceAtLeast(0)
val left = min(start, end) val left = min(start, end)
val right = max(start, end) val right = max(start, end)
binding.composeEditField.text.replace(left, right, shareBody, 0, shareBody.length) binding.composeEditField.text.replace(left, right, shareBody, 0, shareBody.length)
// move edittext cursor to first when shareBody parsed // move edittext cursor to first when shareBody parsed
binding.composeEditField.text.insert(0, "\n") binding.composeEditField.text.insert(0, "\n")
binding.composeEditField.setSelection(0) binding.composeEditField.setSelection(0)
}
} }
} }
} }

View file

@ -15,6 +15,7 @@
package com.keylesspalace.tusky.components.compose package com.keylesspalace.tusky.components.compose
import android.content.ContentResolver
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import android.os.Environment import android.os.Environment
@ -37,6 +38,7 @@ import io.reactivex.rxjava3.schedulers.Schedulers
import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody import okhttp3.MultipartBody
import java.io.File import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.IOException import java.io.IOException
import java.util.Date import java.util.Date
@ -83,36 +85,70 @@ class MediaUploader @Inject constructor(
fun prepareMedia(inUri: Uri): Single<PreparedMedia> { fun prepareMedia(inUri: Uri): Single<PreparedMedia> {
return Single.fromCallable { return Single.fromCallable {
var mediaSize = getMediaSize(contentResolver, inUri) var mediaSize = MEDIA_SIZE_UNKNOWN
var uri = inUri var uri = inUri
val mimeType = contentResolver.getType(uri) var mimeType: String? = null
val suffix = "." + MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType ?: "tmp")
try { try {
contentResolver.openInputStream(inUri).use { input -> when (inUri.scheme) {
if (input == null) { ContentResolver.SCHEME_CONTENT -> {
Log.w(TAG, "Media input is null")
uri = inUri mimeType = contentResolver.getType(uri)
return@use
val suffix = "." + MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType ?: "tmp")
contentResolver.openInputStream(inUri).use { input ->
if (input == null) {
Log.w(TAG, "Media input is null")
uri = inUri
return@use
}
val file = File.createTempFile("randomTemp1", suffix, context.cacheDir)
FileOutputStream(file.absoluteFile).use { out ->
input.copyTo(out)
uri = FileProvider.getUriForFile(
context,
BuildConfig.APPLICATION_ID + ".fileprovider",
file
)
mediaSize = getMediaSize(contentResolver, uri)
}
}
} }
val file = File.createTempFile("randomTemp1", suffix, context.cacheDir) ContentResolver.SCHEME_FILE -> {
FileOutputStream(file.absoluteFile).use { out -> val path = uri.path
input.copyTo(out) if (path == null) {
uri = FileProvider.getUriForFile( Log.w(TAG, "empty uri path $uri")
context, throw CouldNotOpenFileException()
BuildConfig.APPLICATION_ID + ".fileprovider", }
file val inputFile = File(path)
) val suffix = inputFile.name.substringAfterLast('.', "tmp")
mediaSize = getMediaSize(contentResolver, uri) mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(suffix)
val file = File.createTempFile("randomTemp1", ".$suffix", context.cacheDir)
val input = FileInputStream(inputFile)
FileOutputStream(file.absoluteFile).use { out ->
input.copyTo(out)
uri = FileProvider.getUriForFile(
context,
BuildConfig.APPLICATION_ID + ".fileprovider",
file
)
mediaSize = getMediaSize(contentResolver, uri)
}
}
else -> {
Log.w(TAG, "Unknown uri scheme $uri")
throw CouldNotOpenFileException()
} }
} }
} catch (e: IOException) { } catch (e: IOException) {
Log.w(TAG, e) Log.w(TAG, e)
uri = inUri throw CouldNotOpenFileException()
} }
if (mediaSize == MEDIA_SIZE_UNKNOWN) { if (mediaSize == MEDIA_SIZE_UNKNOWN) {
throw CouldNotOpenFileException() Log.w(TAG, "Could not determine file size of upload")
throw MediaTypeException()
} }
if (mimeType != null) { if (mimeType != null) {
@ -138,6 +174,7 @@ class MediaUploader @Inject constructor(
} }
} }
} else { } else {
Log.w(TAG, "Could not determine mime type of upload")
throw MediaTypeException() throw MediaTypeException()
} }
} }