diff --git a/vector/src/main/java/im/vector/app/core/utils/ExternalApplicationsUtil.kt b/vector/src/main/java/im/vector/app/core/utils/ExternalApplicationsUtil.kt index d28f6749a6..f962c6dda2 100644 --- a/vector/src/main/java/im/vector/app/core/utils/ExternalApplicationsUtil.kt +++ b/vector/src/main/java/im/vector/app/core/utils/ExternalApplicationsUtil.kt @@ -40,13 +40,13 @@ import androidx.fragment.app.Fragment import im.vector.app.BuildConfig import im.vector.app.R import im.vector.app.features.notifications.NotificationUtils -import org.matrix.android.sdk.api.extensions.tryOrNull import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import okio.buffer import okio.sink import okio.source +import org.matrix.android.sdk.api.extensions.tryOrNull import timber.log.Timber import java.io.File import java.io.FileInputStream @@ -300,11 +300,24 @@ fun shareMedia(context: Context, file: File, mediaMimeType: String?) { sendIntent.type = mediaMimeType sendIntent.putExtra(Intent.EXTRA_STREAM, mediaUri) - try { - context.startActivity(sendIntent) - } catch (activityNotFoundException: ActivityNotFoundException) { - context.toast(R.string.error_no_external_application_found) - } + sendShareIntent(context, sendIntent) + } +} + +fun shareText(context: Context, text: String) { + val sendIntent = Intent() + sendIntent.action = Intent.ACTION_SEND + sendIntent.type = "text/plain" + sendIntent.putExtra(Intent.EXTRA_TEXT, text) + + sendShareIntent(context, sendIntent) +} + +private fun sendShareIntent(context: Context, intent: Intent) { + try { + context.startActivity(Intent.createChooser(intent, context.getString(R.string.share))) + } catch (activityNotFoundException: ActivityNotFoundException) { + context.toast(R.string.error_no_external_application_found) } } diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt index 51aeda2aab..0a16191dd1 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt @@ -106,6 +106,7 @@ import im.vector.app.core.utils.onPermissionResultVideoIpCall import im.vector.app.core.utils.openUrlInExternalBrowser import im.vector.app.core.utils.saveMedia import im.vector.app.core.utils.shareMedia +import im.vector.app.core.utils.shareText import im.vector.app.core.utils.toast import im.vector.app.features.attachments.AttachmentTypeSelectorView import im.vector.app.features.attachments.AttachmentsHelper @@ -1556,21 +1557,25 @@ class RoomDetailFragment @Inject constructor( } private fun onShareActionClicked(action: EventSharedAction.Share) { - session.fileService().downloadFile( - downloadMode = FileService.DownloadMode.FOR_EXTERNAL_SHARE, - id = action.eventId, - fileName = action.messageContent.body, - mimeType = action.messageContent.mimeType, - url = action.messageContent.getFileUrl(), - elementToDecrypt = action.messageContent.encryptedFileInfo?.toElementToDecrypt(), - callback = object : MatrixCallback { - override fun onSuccess(data: File) { - if (isAdded) { - shareMedia(requireContext(), data, getMimeTypeFromUri(requireContext(), data.toUri())) + if (action.messageContent is MessageTextContent) { + shareText(requireContext(), action.messageContent.body) + } else if (action.messageContent is MessageWithAttachmentContent) { + session.fileService().downloadFile( + downloadMode = FileService.DownloadMode.FOR_EXTERNAL_SHARE, + id = action.eventId, + fileName = action.messageContent.body, + mimeType = action.messageContent.mimeType, + url = action.messageContent.getFileUrl(), + elementToDecrypt = action.messageContent.encryptedFileInfo?.toElementToDecrypt(), + callback = object : MatrixCallback { + override fun onSuccess(data: File) { + if (isAdded) { + shareMedia(requireContext(), data, getMimeTypeFromUri(requireContext(), data.toUri())) + } } } - } - ) + ) + } } private fun onSaveActionClicked(action: EventSharedAction.Save) { diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/action/EventSharedAction.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/action/EventSharedAction.kt index 7693d97c35..c21d552409 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/action/EventSharedAction.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/action/EventSharedAction.kt @@ -21,6 +21,7 @@ import androidx.annotation.StringRes import im.vector.app.R import im.vector.app.core.platform.VectorSharedAction import im.vector.app.features.home.room.detail.timeline.item.MessageInformationData +import org.matrix.android.sdk.api.session.room.model.message.MessageContent import org.matrix.android.sdk.api.session.room.model.message.MessageWithAttachmentContent sealed class EventSharedAction(@StringRes val titleRes: Int, @@ -47,7 +48,7 @@ sealed class EventSharedAction(@StringRes val titleRes: Int, data class Reply(val eventId: String) : EventSharedAction(R.string.reply, R.drawable.ic_reply) - data class Share(val eventId: String, val messageContent: MessageWithAttachmentContent) : + data class Share(val eventId: String, val messageContent: MessageContent) : EventSharedAction(R.string.share, R.drawable.ic_share) data class Save(val eventId: String, val messageContent: MessageWithAttachmentContent) : diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/action/MessageActionsViewModel.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/action/MessageActionsViewModel.kt index 8a25e5fc93..0d1e2261cd 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/action/MessageActionsViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/action/MessageActionsViewModel.kt @@ -275,8 +275,8 @@ class MessageActionsViewModel @AssistedInject constructor(@Assisted add(EventSharedAction.ViewEditHistory(informationData)) } - if (canShare(msgType) && messageContent is MessageWithAttachmentContent) { - add(EventSharedAction.Share(timelineEvent.eventId, messageContent)) + if (canShare(msgType)) { + add(EventSharedAction.Share(timelineEvent.eventId, messageContent!!)) } if (canSave(msgType) && messageContent is MessageWithAttachmentContent) { @@ -409,6 +409,10 @@ class MessageActionsViewModel @AssistedInject constructor(@Assisted private fun canShare(msgType: String?): Boolean { return when (msgType) { + MessageType.MSGTYPE_TEXT, + MessageType.MSGTYPE_NOTICE, + MessageType.MSGTYPE_EMOTE, + MessageType.MSGTYPE_LOCATION, MessageType.MSGTYPE_IMAGE, MessageType.MSGTYPE_AUDIO, MessageType.MSGTYPE_VIDEO,