diff --git a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt index 0a4dd9332..ed590cd15 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt @@ -236,26 +236,25 @@ class ComposeActivity : } } } - } else if (type == "text/plain" && intent.action == Intent.ACTION_SEND) { + } - val subject = intent.getStringExtra(Intent.EXTRA_SUBJECT) - val text = intent.getStringExtra(Intent.EXTRA_TEXT).orEmpty() - val shareBody = if (!subject.isNullOrBlank() && subject !in text) { - subject + '\n' + text - } else { - text - } + val subject = intent.getStringExtra(Intent.EXTRA_SUBJECT) + val text = intent.getStringExtra(Intent.EXTRA_TEXT).orEmpty() + val shareBody = if (!subject.isNullOrBlank() && subject !in text) { + subject + '\n' + text + } else { + text + } - if (shareBody.isNotBlank()) { - val start = binding.composeEditField.selectionStart.coerceAtLeast(0) - val end = binding.composeEditField.selectionEnd.coerceAtLeast(0) - val left = min(start, end) - val right = max(start, end) - binding.composeEditField.text.replace(left, right, shareBody, 0, shareBody.length) - // move edittext cursor to first when shareBody parsed - binding.composeEditField.text.insert(0, "\n") - binding.composeEditField.setSelection(0) - } + if (shareBody.isNotBlank()) { + val start = binding.composeEditField.selectionStart.coerceAtLeast(0) + val end = binding.composeEditField.selectionEnd.coerceAtLeast(0) + val left = min(start, end) + val right = max(start, end) + binding.composeEditField.text.replace(left, right, shareBody, 0, shareBody.length) + // move edittext cursor to first when shareBody parsed + binding.composeEditField.text.insert(0, "\n") + binding.composeEditField.setSelection(0) } } } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/compose/MediaUploader.kt b/app/src/main/java/com/keylesspalace/tusky/components/compose/MediaUploader.kt index 5966bb532..0e3ac9e84 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/compose/MediaUploader.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/compose/MediaUploader.kt @@ -15,6 +15,7 @@ package com.keylesspalace.tusky.components.compose +import android.content.ContentResolver import android.content.Context import android.net.Uri import android.os.Environment @@ -37,6 +38,7 @@ import io.reactivex.rxjava3.schedulers.Schedulers import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MultipartBody import java.io.File +import java.io.FileInputStream import java.io.FileOutputStream import java.io.IOException import java.util.Date @@ -83,36 +85,70 @@ class MediaUploader @Inject constructor( fun prepareMedia(inUri: Uri): Single { return Single.fromCallable { - var mediaSize = getMediaSize(contentResolver, inUri) + var mediaSize = MEDIA_SIZE_UNKNOWN var uri = inUri - val mimeType = contentResolver.getType(uri) - - val suffix = "." + MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType ?: "tmp") + var mimeType: String? = null try { - contentResolver.openInputStream(inUri).use { input -> - if (input == null) { - Log.w(TAG, "Media input is null") - uri = inUri - return@use + when (inUri.scheme) { + ContentResolver.SCHEME_CONTENT -> { + + mimeType = contentResolver.getType(uri) + + 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) - FileOutputStream(file.absoluteFile).use { out -> - input.copyTo(out) - uri = FileProvider.getUriForFile( - context, - BuildConfig.APPLICATION_ID + ".fileprovider", - file - ) - mediaSize = getMediaSize(contentResolver, uri) + ContentResolver.SCHEME_FILE -> { + val path = uri.path + if (path == null) { + Log.w(TAG, "empty uri path $uri") + throw CouldNotOpenFileException() + } + val inputFile = File(path) + val suffix = inputFile.name.substringAfterLast('.', "tmp") + 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) { Log.w(TAG, e) - uri = inUri + throw CouldNotOpenFileException() } if (mediaSize == MEDIA_SIZE_UNKNOWN) { - throw CouldNotOpenFileException() + Log.w(TAG, "Could not determine file size of upload") + throw MediaTypeException() } if (mimeType != null) { @@ -138,6 +174,7 @@ class MediaUploader @Inject constructor( } } } else { + Log.w(TAG, "Could not determine mime type of upload") throw MediaTypeException() } }