From 489516b03b1cac5578bc20730641d10655e0fdcd Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 28 Jun 2021 11:26:13 +1000 Subject: [PATCH] Hook up all voice message recording controls --- .../conversation/v2/ConversationActivityV2.kt | 30 ++++++++++++++----- .../conversation/v2/input_bar/InputBar.kt | 9 +----- .../v2/input_bar/InputBarRecordingView.kt | 4 +++ .../res/layout/view_input_bar_recording.xml | 1 + 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt index a6e3c5f9d6..5153d2309a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt @@ -43,9 +43,7 @@ import org.session.libsession.messaging.sending_receiving.attachments.Attachment import org.session.libsession.utilities.MediaTypes import org.session.libsession.utilities.ServiceUtil import org.session.libsession.utilities.TextSecurePreferences -import org.session.libsession.utilities.concurrent.AssertedSuccessListener import org.session.libsignal.utilities.ListenableFuture -import org.session.libsignal.utilities.guava.Optional import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity import org.thoughtcrime.securesms.audio.AudioRecorder @@ -93,7 +91,7 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe // Attachments private val audioRecorder = AudioRecorder(this) private val stopAudioHandler = Handler(Looper.getMainLooper()) - private val stopVoiceMessageRecordingTask = Runnable { stopVoiceMessageRecording() } + private val stopVoiceMessageRecordingTask = Runnable { sendVoiceMessage() } private val attachmentManager by lazy { AttachmentManager(this, this) } private var isLockViewExpanded = false private var isShowingAttachmentOptions = false @@ -628,11 +626,20 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe } override fun onMicrophoneButtonUp(event: MotionEvent) { - if (isValidLockViewLocation(event.rawX.roundToInt(), event.rawY.roundToInt())) { + val x = event.rawX.roundToInt() + val y = event.rawY.roundToInt() + if (isValidLockViewLocation(x, y)) { inputBarRecordingView.lock() } else { - hideVoiceMessageUI() - stopVoiceMessageRecording() + val recordButtonOverlay = inputBarRecordingView.recordButtonOverlay + val location = IntArray(2) { 0 } + recordButtonOverlay.getLocationOnScreen(location) + val hitRect = Rect(location[0], location[1], location[0] + recordButtonOverlay.width, location[1] + recordButtonOverlay.height) + if (hitRect.contains(x, y)) { + sendVoiceMessage() + } else { + cancelVoiceMessage() + } } } @@ -791,12 +798,14 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe } override fun startRecordingVoiceMessage() { + showVoiceMessageUI() window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) audioRecorder.startRecording() stopAudioHandler.postDelayed(stopVoiceMessageRecordingTask, 60000) // Limit voice messages to 1 minute each } - fun stopVoiceMessageRecording() { + override fun sendVoiceMessage() { + hideVoiceMessageUI() window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) val future = audioRecorder.stopRecording() stopAudioHandler.removeCallbacks(stopVoiceMessageRecordingTask) @@ -814,6 +823,13 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe } }) } + + override fun cancelVoiceMessage() { + hideVoiceMessageUI() + window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) + audioRecorder.stopRecording() + stopAudioHandler.removeCallbacks(stopVoiceMessageRecordingTask) + } // endregion // region General diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBar.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBar.kt index 86d39ffd83..d00dc10f1e 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBar.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBar.kt @@ -52,10 +52,7 @@ class InputBar : RelativeLayout, InputBarEditTextDelegate, QuoteViewDelegate, Li // Microphone button microphoneOrSendButtonContainer.addView(microphoneButton) microphoneButton.layoutParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT) - microphoneButton.onLongPress = { - showVoiceMessageUI() - startRecordingVoiceMessage() - } + microphoneButton.onLongPress = { startRecordingVoiceMessage() } microphoneButton.onMove = { delegate?.onMicrophoneButtonMove(it) } microphoneButton.onCancel = { delegate?.onMicrophoneButtonCancel(it) } microphoneButton.onUp = { delegate?.onMicrophoneButtonUp(it) } @@ -95,10 +92,6 @@ class InputBar : RelativeLayout, InputBarEditTextDelegate, QuoteViewDelegate, Li delegate?.toggleAttachmentOptions() } - private fun showVoiceMessageUI() { - delegate?.showVoiceMessageUI() - } - private fun startRecordingVoiceMessage() { delegate?.startRecordingVoiceMessage() } diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBarRecordingView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBarRecordingView.kt index a4da17434f..0a210d6ae5 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBarRecordingView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBarRecordingView.kt @@ -134,10 +134,14 @@ class InputBarRecordingView : RelativeLayout { } fadeInAnimation.start() recordButtonOverlayImageView.setImageDrawable(ResourcesCompat.getDrawable(resources, R.drawable.ic_arrow_up, context.theme)) + recordButtonOverlay.setOnClickListener { delegate?.sendVoiceMessage() } + inputBarCancelButton.setOnClickListener { delegate?.cancelVoiceMessage() } } } interface InputBarRecordingViewDelegate { fun handleVoiceMessageUIHidden() + fun sendVoiceMessage() + fun cancelVoiceMessage() } diff --git a/app/src/main/res/layout/view_input_bar_recording.xml b/app/src/main/res/layout/view_input_bar_recording.xml index 5a0e32a085..35628d2997 100644 --- a/app/src/main/res/layout/view_input_bar_recording.xml +++ b/app/src/main/res/layout/view_input_bar_recording.xml @@ -128,6 +128,7 @@