From 85833c6f41de6cfbfd3e6128942c4c858fd8b55f Mon Sep 17 00:00:00 2001 From: Smarshall Date: Tue, 29 Aug 2023 01:22:57 +0530 Subject: [PATCH 1/4] Implemented media/photo share functionality --- .../com/nextcloud/talk/chat/ChatActivity.kt | 19 +++++++++++ .../talk/ui/dialog/MessageActionsDialog.kt | 10 ++++++ .../res/layout/dialog_message_actions.xml | 33 +++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt index 450f6a0789..1d1856eb42 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt @@ -198,6 +198,7 @@ import com.nextcloud.talk.utils.FileUtils import com.nextcloud.talk.utils.FileViewerUtils import com.nextcloud.talk.utils.ImageEmojiEditText import com.nextcloud.talk.utils.MagicCharPolicy +import com.nextcloud.talk.utils.Mimetype import com.nextcloud.talk.utils.NotificationUtils import com.nextcloud.talk.utils.ParticipantPermissions import com.nextcloud.talk.utils.VibrationUtils @@ -312,6 +313,7 @@ class ChatActivity : var voiceOnly: Boolean = true var isFirstMessagesProcessing = true private var emojiPopup: EmojiPopup? = null + private lateinit var path: String var myFirstMessage: CharSequence? = null var checkingLobbyStatus: Boolean = false @@ -3908,6 +3910,23 @@ class ChatActivity : intent.putExtras(bundle) startActivity(intent) } + fun share(message: ChatMessage) { + val fileName = message.selectedIndividualHashMap!![PreviewMessageViewHolder.KEY_NAME]!! + path = applicationContext.cacheDir.absolutePath + "/" + fileName + val shareUri = FileProvider.getUriForFile( + this, + BuildConfig.APPLICATION_ID, + File(path) + ) + + val shareIntent: Intent = Intent().apply { + action = Intent.ACTION_SEND + putExtra(Intent.EXTRA_STREAM, shareUri) + type = Mimetype.IMAGE_PREFIX_GENERIC + addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + } + startActivity(Intent.createChooser(shareIntent, resources.getText(R.string.send_to))) + } fun openInFilesApp(message: ChatMessage) { val keyID = message.selectedIndividualHashMap!![PreviewMessageViewHolder.KEY_ID] diff --git a/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt b/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt index 2ac437814e..c77bb76c3a 100644 --- a/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt +++ b/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt @@ -113,6 +113,7 @@ class MessageActionsDialog( message.previousMessageId > NO_PREVIOUS_MESSAGE_ID && ChatMessage.MessageType.SYSTEM_MESSAGE != message.getCalculateMessageType() ) + initMenuShare(ChatMessage.MessageType.SINGLE_NC_ATTACHMENT_MESSAGE == message.getCalculateMessageType()) initMenuItemOpenNcApp( ChatMessage.MessageType.SINGLE_NC_ATTACHMENT_MESSAGE == message.getCalculateMessageType() ) @@ -318,6 +319,15 @@ class MessageActionsDialog( dialogMessageActionsBinding.menuTranslateMessage.visibility = getVisibility(visible) } + private fun initMenuShare(visible: Boolean) { + if (visible) { + dialogMessageActionsBinding.menuShare.setOnClickListener { + chatActivity.share(message) + dismiss() + } + } + dialogMessageActionsBinding.menuShare.visibility = getVisibility(visible) + } private fun initMenuItemOpenNcApp(visible: Boolean) { if (visible) { diff --git a/app/src/main/res/layout/dialog_message_actions.xml b/app/src/main/res/layout/dialog_message_actions.xml index 9610f80488..eaabb88bdb 100644 --- a/app/src/main/res/layout/dialog_message_actions.xml +++ b/app/src/main/res/layout/dialog_message_actions.xml @@ -320,6 +320,39 @@ + + + + + + + + Date: Wed, 30 Aug 2023 15:42:47 +0530 Subject: [PATCH 2/4] Fixed media/photo share functionality --- app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt index 1d1856eb42..17fb47a4d2 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt @@ -3911,8 +3911,8 @@ class ChatActivity : startActivity(intent) } fun share(message: ChatMessage) { - val fileName = message.selectedIndividualHashMap!![PreviewMessageViewHolder.KEY_NAME]!! - path = applicationContext.cacheDir.absolutePath + "/" + fileName + val filename = message.selectedIndividualHashMap!!["name"] + path = applicationContext.cacheDir.absolutePath + "/" + filename val shareUri = FileProvider.getUriForFile( this, BuildConfig.APPLICATION_ID, From 1690ad9216982026ae79b5b859ef17ed8ac3152c Mon Sep 17 00:00:00 2001 From: Smarshall Date: Fri, 1 Sep 2023 18:45:49 +0530 Subject: [PATCH 3/4] Fixed media/photo share functionality if not downloaded before --- .../com/nextcloud/talk/chat/ChatActivity.kt | 41 ++++++++++++++++--- .../talk/ui/dialog/MessageActionsDialog.kt | 2 +- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt index 860d9f01a2..2f57f2ea40 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt @@ -188,7 +188,7 @@ import com.nextcloud.talk.ui.dialog.ShowReactionsDialog import com.nextcloud.talk.ui.recyclerview.MessageSwipeActions import com.nextcloud.talk.ui.recyclerview.MessageSwipeCallback import com.nextcloud.talk.utils.ApiUtils -import com.nextcloud.talk.utils.AudioUtils +import com.nextcloud.talk.utils.AudioUtils.audioFileToFloatArray import com.nextcloud.talk.utils.ContactUtils import com.nextcloud.talk.utils.ConversationUtils import com.nextcloud.talk.utils.DateConstants @@ -897,16 +897,33 @@ class ChatActivity : message.isDownloadingVoiceMessage = true adapter?.update(message) CoroutineScope(Dispatchers.Default).launch { - val r = AudioUtils.audioFileToFloatArray(file) - message.voiceMessageFloatArray = r - withContext(Dispatchers.Main) { - startPlayback(message) + try { + val r = audioFileToFloatArray(file) + message.voiceMessageFloatArray = r + withContext(Dispatchers.Main) { + startPlayback(message) + } + } catch (e: Exception) { + e.printStackTrace() } } } else { startPlayback(message) } } + private fun setUpshare(message: ChatMessage) { + val filename = message.selectedIndividualHashMap!!["name"] + val file = File(context.cacheDir, filename!!) + if (file.exists()) { + CoroutineScope(Dispatchers.Default).launch { + withContext(Dispatchers.Main) { + share(message) + } + } + } else { + share(message) + } + } private fun initMessageHolders(): MessageHolders { val messageHolders = MessageHolders() @@ -1946,6 +1963,10 @@ class ChatActivity : if (workInfo.state == WorkInfo.State.SUCCEEDED) { setUpWaveform(message) // startPlayback(message) + setUpshare(message) + } + else { + Log.e(TAG, "Error") } } } @@ -3931,6 +3952,16 @@ class ChatActivity : } startActivity(Intent.createChooser(shareIntent, resources.getText(R.string.send_to))) } + fun checkifsharable(message: ChatMessage) { + val filename = message.selectedIndividualHashMap!!["name"] + path = applicationContext.cacheDir.absolutePath + "/" + filename + val file = File(context.cacheDir, filename!!) + if (file.exists()) { + share(message) + }else{ + downloadFileToCache(message) + } + } fun openInFilesApp(message: ChatMessage) { val keyID = message.selectedIndividualHashMap!![PreviewMessageViewHolder.KEY_ID] diff --git a/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt b/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt index c77bb76c3a..4571959380 100644 --- a/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt +++ b/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt @@ -322,7 +322,7 @@ class MessageActionsDialog( private fun initMenuShare(visible: Boolean) { if (visible) { dialogMessageActionsBinding.menuShare.setOnClickListener { - chatActivity.share(message) + chatActivity.checkifsharable(message) dismiss() } } From abb33893089dd5a88361e595c3dfec879bcfbf7e Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Mon, 4 Sep 2023 11:08:20 +0200 Subject: [PATCH 4/4] fixups and refactoring for sharing by context menu When sharing a file by context menu that is not downloaded yet, do not open it after download but just share it. This is done by 'openWhenDownloaded' variable in chatMessage. Pass a method to downloadFileToCache, so it's more flexible what to do when download finished. Add some minor changes --- .../messages/PreviewMessageViewHolder.kt | 1 + .../com/nextcloud/talk/chat/ChatActivity.kt | 55 ++++++++----------- .../talk/models/json/chat/ChatMessage.kt | 4 +- .../adapters/SharedItemsViewHolder.kt | 6 +- .../talk/ui/dialog/MessageActionsDialog.kt | 2 +- .../nextcloud/talk/utils/FileViewerUtils.kt | 37 ++++++++----- 6 files changed, 54 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/PreviewMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/PreviewMessageViewHolder.kt index 7b1ef8fc8e..2bca297705 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/PreviewMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/PreviewMessageViewHolder.kt @@ -138,6 +138,7 @@ abstract class PreviewMessageViewHolder(itemView: View?, payload: Any?) : message.selectedIndividualHashMap!![KEY_NAME]!!, message.selectedIndividualHashMap!![KEY_ID]!!, message.selectedIndividualHashMap!![KEY_MIMETYPE], + message.openWhenDownloaded, ProgressUi(progressBar, messageText, image) ) } else if (message.getCalculateMessageType() === ChatMessage.MessageType.SINGLE_LINK_GIPHY_MESSAGE) { diff --git a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt index 74d8bfb88b..159a99b416 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt @@ -889,7 +889,9 @@ class ChatActivity : } } else { Log.d(TAG, "Downloaded to cache") - downloadFileToCache(message) + downloadFileToCache(message, true) { + setUpWaveform(message) + } } } } @@ -901,31 +903,14 @@ class ChatActivity : message.isDownloadingVoiceMessage = true adapter?.update(message) CoroutineScope(Dispatchers.Default).launch { - try { - val r = audioFileToFloatArray(file) - message.voiceMessageFloatArray = r - withContext(Dispatchers.Main) { - startPlayback(message) - } - } catch (e: Exception) { - e.printStackTrace() - } - } - } else { - startPlayback(message) - } - } - private fun setUpshare(message: ChatMessage) { - val filename = message.selectedIndividualHashMap!!["name"] - val file = File(context.cacheDir, filename!!) - if (file.exists()) { - CoroutineScope(Dispatchers.Default).launch { + val r = audioFileToFloatArray(file) + message.voiceMessageFloatArray = r withContext(Dispatchers.Main) { - share(message) + startPlayback(message) } } } else { - share(message) + startPlayback(message) } } @@ -1952,8 +1937,13 @@ class ChatActivity : } @SuppressLint("LongLogTag") - private fun downloadFileToCache(message: ChatMessage) { + private fun downloadFileToCache( + message: ChatMessage, + openWhenDownloaded: Boolean, + funToCallWhenDownloadSuccessful: (() -> Unit) + ) { message.isDownloadingVoiceMessage = true + message.openWhenDownloaded = openWhenDownloaded adapter?.update(message) val baseUrl = message.activeUser!!.baseUrl @@ -2004,12 +1994,7 @@ class ChatActivity : WorkManager.getInstance(context).getWorkInfoByIdLiveData(downloadWorker.id) .observeForever { workInfo: WorkInfo -> if (workInfo.state == WorkInfo.State.SUCCEEDED) { - setUpWaveform(message) - // startPlayback(message) - setUpshare(message) - } - else { - Log.e(TAG, "Error") + funToCallWhenDownloadSuccessful() } } } @@ -4019,6 +4004,7 @@ class ChatActivity : intent.putExtras(bundle) startActivity(intent) } + fun share(message: ChatMessage) { val filename = message.selectedIndividualHashMap!!["name"] path = applicationContext.cacheDir.absolutePath + "/" + filename @@ -4036,15 +4022,18 @@ class ChatActivity : } startActivity(Intent.createChooser(shareIntent, resources.getText(R.string.send_to))) } - fun checkifsharable(message: ChatMessage) { + + fun checkIfSharable(message: ChatMessage) { val filename = message.selectedIndividualHashMap!!["name"] path = applicationContext.cacheDir.absolutePath + "/" + filename val file = File(context.cacheDir, filename!!) - if (file.exists()) { + if (file.exists()) { + share(message) + } else { + downloadFileToCache(message, false) { share(message) - }else{ - downloadFileToCache(message) } + } } fun openInFilesApp(message: ChatMessage) { diff --git a/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatMessage.kt b/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatMessage.kt index 0db7ea97bf..e05fea28b0 100644 --- a/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatMessage.kt +++ b/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatMessage.kt @@ -145,7 +145,9 @@ data class ChatMessage( var expandableChildrenAmount: Int = 0, - var hiddenByCollapse: Boolean = false + var hiddenByCollapse: Boolean = false, + + var openWhenDownloaded: Boolean = true ) : Parcelable, MessageContentType, MessageContentType.Image { diff --git a/app/src/main/java/com/nextcloud/talk/shareditems/adapters/SharedItemsViewHolder.kt b/app/src/main/java/com/nextcloud/talk/shareditems/adapters/SharedItemsViewHolder.kt index b011006a42..fb4d97947d 100644 --- a/app/src/main/java/com/nextcloud/talk/shareditems/adapters/SharedItemsViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/shareditems/adapters/SharedItemsViewHolder.kt @@ -83,7 +83,8 @@ abstract class SharedItemsViewHolder( progressBar, null, image - ) + ), + true ) } @@ -91,7 +92,8 @@ abstract class SharedItemsViewHolder( item.name, item.id, item.mimeType, - FileViewerUtils.ProgressUi(progressBar, null, image) + true, + FileViewerUtils.ProgressUi(progressBar, null, image), ) } diff --git a/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt b/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt index 7d603af971..9fd90fbcaa 100644 --- a/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt +++ b/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt @@ -334,7 +334,7 @@ class MessageActionsDialog( private fun initMenuShare(visible: Boolean) { if (visible) { dialogMessageActionsBinding.menuShare.setOnClickListener { - chatActivity.checkifsharable(message) + chatActivity.checkIfSharable(message) dismiss() } } diff --git a/app/src/main/java/com/nextcloud/talk/utils/FileViewerUtils.kt b/app/src/main/java/com/nextcloud/talk/utils/FileViewerUtils.kt index 611f6d6591..29712b59a7 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/FileViewerUtils.kt +++ b/app/src/main/java/com/nextcloud/talk/utils/FileViewerUtils.kt @@ -95,7 +95,8 @@ class FileViewerUtils(private val context: Context, private val user: User) { path, link, mimetype, - progressUi + progressUi, + message.openWhenDownloaded ) } @@ -104,14 +105,16 @@ class FileViewerUtils(private val context: Context, private val user: User) { path: String, link: String?, mimetype: String?, - progressUi: ProgressUi + progressUi: ProgressUi, + openWhenDownloaded: Boolean ) { if (isSupportedForInternalViewer(mimetype) || canBeHandledByExternalApp(mimetype, fileInfo.fileName)) { openOrDownloadFile( fileInfo, path, mimetype, - progressUi + progressUi, + openWhenDownloaded ) } else if (!link.isNullOrEmpty()) { openFileInFilesApp(link, fileInfo.fileId) @@ -138,7 +141,8 @@ class FileViewerUtils(private val context: Context, private val user: User) { fileInfo: FileInfo, path: String, mimetype: String?, - progressUi: ProgressUi + progressUi: ProgressUi, + openWhenDownloaded: Boolean ) { val file = File(context.cacheDir, fileInfo.fileName) if (file.exists()) { @@ -148,7 +152,8 @@ class FileViewerUtils(private val context: Context, private val user: User) { fileInfo, path, mimetype, - progressUi + progressUi, + openWhenDownloaded ) } } @@ -276,7 +281,8 @@ class FileViewerUtils(private val context: Context, private val user: User) { fileInfo: FileInfo, path: String, mimetype: String?, - progressUi: ProgressUi + progressUi: ProgressUi, + openWhenDownloaded: Boolean ) { // check if download worker is already running val workers = WorkManager.getInstance(context).getWorkInfosByTag(fileInfo.fileId!!) @@ -324,7 +330,8 @@ class FileViewerUtils(private val context: Context, private val user: User) { fileInfo.fileName, mimetype, workInfo!!, - progressUi + progressUi, + openWhenDownloaded ) } } @@ -333,7 +340,8 @@ class FileViewerUtils(private val context: Context, private val user: User) { fileName: String, mimetype: String?, workInfo: WorkInfo, - progressUi: ProgressUi + progressUi: ProgressUi, + openWhenDownloaded: Boolean ) { when (workInfo.state) { WorkInfo.State.RUNNING -> { @@ -347,13 +355,12 @@ class FileViewerUtils(private val context: Context, private val user: User) { } } WorkInfo.State.SUCCEEDED -> { - if (progressUi.previewImage.isShown) { + if (progressUi.previewImage.isShown && openWhenDownloaded) { openFileByMimetype(fileName, mimetype) } else { - Log.d( - TAG, - "file " + fileName + - " was downloaded but it's not opened because view is not shown on screen" + Log.d(TAG, "file " + fileName + + " was downloaded but it's not opened because view is not shown on screen or " + + "openWhenDownloaded is false" ) } progressUi.messageText?.text = fileName @@ -372,6 +379,7 @@ class FileViewerUtils(private val context: Context, private val user: User) { fileName: String, fileId: String, mimeType: String?, + openWhenDownloaded: Boolean, progressUi: ProgressUi ) { val workers = WorkManager.getInstance(context).getWorkInfosByTag(fileId) @@ -390,7 +398,8 @@ class FileViewerUtils(private val context: Context, private val user: User) { fileName, mimeType, info!!, - progressUi + progressUi, + openWhenDownloaded ) } }