From 08e74911444e95887f06928484d73b710f3f5e4b Mon Sep 17 00:00:00 2001 From: Naveen Singh Date: Fri, 10 Jan 2025 19:28:34 +0530 Subject: [PATCH] Add cancel recording button Closes https://github.com/FossifyOrg/Voice-Recorder/issues/57 --- .../fragments/RecorderFragment.kt | 36 ++++++++++++------- .../voicerecorder/helpers/Constants.kt | 1 + .../voicerecorder/services/RecorderService.kt | 28 +++++++++++++++ .../main/res/drawable/ic_cancel_vector.xml | 3 ++ .../drawable/ic_pause_recording_vector.xml | 3 ++ app/src/main/res/layout/fragment_recorder.xml | 33 ++++++++++++++--- 6 files changed, 86 insertions(+), 18 deletions(-) create mode 100644 app/src/main/res/drawable/ic_cancel_vector.xml create mode 100644 app/src/main/res/drawable/ic_pause_recording_vector.xml diff --git a/app/src/main/kotlin/org/fossify/voicerecorder/fragments/RecorderFragment.kt b/app/src/main/kotlin/org/fossify/voicerecorder/fragments/RecorderFragment.kt index 9af07c2e..9a21aa76 100644 --- a/app/src/main/kotlin/org/fossify/voicerecorder/fragments/RecorderFragment.kt +++ b/app/src/main/kotlin/org/fossify/voicerecorder/fragments/RecorderFragment.kt @@ -7,7 +7,6 @@ import android.graphics.drawable.Drawable import android.os.Handler import android.os.Looper import android.util.AttributeSet -import android.view.WindowManager import org.fossify.commons.activities.BaseSimpleActivity import org.fossify.commons.compose.extensions.getActivity import org.fossify.commons.dialogs.PermissionRequiredDialog @@ -26,6 +25,7 @@ import org.fossify.voicerecorder.extensions.config import org.fossify.voicerecorder.extensions.ensureStoragePermission import org.fossify.voicerecorder.extensions.setDebouncedClickListener import org.fossify.voicerecorder.extensions.setKeepScreenAwake +import org.fossify.voicerecorder.helpers.CANCEL_RECORDING import org.fossify.voicerecorder.helpers.GET_RECORDER_INFO import org.fossify.voicerecorder.helpers.RECORDING_PAUSED import org.fossify.voicerecorder.helpers.RECORDING_RUNNING @@ -99,7 +99,8 @@ class RecorderFragment( } } - binding.togglePauseButton.setOnClickListener { + binding.cancelRecordingButton.setDebouncedClickListener { cancelRecording() } + binding.togglePauseButton.setDebouncedClickListener { Intent(context, RecorderService::class.java).apply { action = TOGGLE_PAUSE context.startService(this) @@ -116,24 +117,17 @@ class RecorderFragment( } private fun setupColors() { + val properTextColor = context.getProperTextColor() val properPrimaryColor = context.getProperPrimaryColor() binding.toggleRecordingButton.apply { setImageDrawable(getToggleButtonIcon()) background.applyColorFilter(properPrimaryColor) } - binding.togglePauseButton.apply { - setImageDrawable( - resources.getColoredDrawableWithColor( - drawableId = org.fossify.commons.R.drawable.ic_pause_vector, - color = properPrimaryColor.getContrastColor() - ) - ) - background.applyColorFilter(properPrimaryColor) - } - + binding.cancelRecordingButton.applyColorFilter(properTextColor) + binding.togglePauseButton.applyColorFilter(properTextColor) binding.recorderVisualizer.chunkColor = properPrimaryColor - binding.recordingDuration.setTextColor(context.getProperTextColor()) + binding.recordingDuration.setTextColor(properTextColor) } private fun updateRecordingDuration(duration: Int) { @@ -166,6 +160,7 @@ class RecorderFragment( startRecording() } else { binding.togglePauseButton.beGone() + binding.cancelRecordingButton.beGone() stopRecording() } } @@ -177,6 +172,20 @@ class RecorderFragment( binding.recorderVisualizer.recreate() } + private fun cancelRecording() { + status = if (status == RECORDING_RUNNING || status == RECORDING_PAUSED) { + RECORDING_STOPPED + } else { + RECORDING_RUNNING + } + + Intent(context, RecorderService::class.java).apply { + action = CANCEL_RECORDING + context.startService(this) + } + refreshView() + } + private fun stopRecording() { Intent(context, RecorderService::class.java).apply { context.stopService(this) @@ -200,6 +209,7 @@ class RecorderFragment( private fun refreshView() { binding.toggleRecordingButton.setImageDrawable(getToggleButtonIcon()) binding.togglePauseButton.beVisibleIf(status != RECORDING_STOPPED) + binding.cancelRecordingButton.beVisibleIf(status != RECORDING_STOPPED) pauseBlinkTimer.cancel() if (status == RECORDING_PAUSED) { diff --git a/app/src/main/kotlin/org/fossify/voicerecorder/helpers/Constants.kt b/app/src/main/kotlin/org/fossify/voicerecorder/helpers/Constants.kt index 6d5372b5..16b2686b 100644 --- a/app/src/main/kotlin/org/fossify/voicerecorder/helpers/Constants.kt +++ b/app/src/main/kotlin/org/fossify/voicerecorder/helpers/Constants.kt @@ -8,6 +8,7 @@ private const val PATH = "com.fossify.voicerecorder.action." const val GET_RECORDER_INFO = PATH + "GET_RECORDER_INFO" const val STOP_AMPLITUDE_UPDATE = PATH + "STOP_AMPLITUDE_UPDATE" const val TOGGLE_PAUSE = PATH + "TOGGLE_PAUSE" +const val CANCEL_RECORDING = PATH + "CANCEL_RECORDING" const val EXTENSION_M4A = 0 const val EXTENSION_MP3 = 1 diff --git a/app/src/main/kotlin/org/fossify/voicerecorder/services/RecorderService.kt b/app/src/main/kotlin/org/fossify/voicerecorder/services/RecorderService.kt index cf9d1b90..35144b23 100644 --- a/app/src/main/kotlin/org/fossify/voicerecorder/services/RecorderService.kt +++ b/app/src/main/kotlin/org/fossify/voicerecorder/services/RecorderService.kt @@ -11,6 +11,7 @@ import android.content.Intent import android.media.MediaScannerConnection import android.net.Uri import android.os.IBinder +import android.provider.DocumentsContract import androidx.core.app.NotificationCompat import org.fossify.commons.extensions.createDocumentUriUsingFirstParentTreeUri import org.fossify.commons.extensions.createSAFFileSdk30 @@ -29,6 +30,7 @@ import org.fossify.voicerecorder.R import org.fossify.voicerecorder.activities.SplashActivity import org.fossify.voicerecorder.extensions.config import org.fossify.voicerecorder.extensions.updateWidgets +import org.fossify.voicerecorder.helpers.CANCEL_RECORDING import org.fossify.voicerecorder.helpers.EXTENSION_MP3 import org.fossify.voicerecorder.helpers.GET_RECORDER_INFO import org.fossify.voicerecorder.helpers.RECORDER_RUNNING_NOTIF_ID @@ -70,6 +72,7 @@ class RecorderService : Service() { GET_RECORDER_INFO -> broadcastRecorderInfo() STOP_AMPLITUDE_UPDATE -> amplitudeTimer.cancel() TOGGLE_PAUSE -> togglePause() + CANCEL_RECORDING -> cancelRecording() else -> startRecording() } @@ -172,6 +175,31 @@ class RecorderService : Service() { recorder = null } + private fun cancelRecording() { + durationTimer.cancel() + amplitudeTimer.cancel() + status = RECORDING_STOPPED + + recorder?.apply { + try { + stop() + release() + } catch (ignored: Exception) { + } + } + + recorder = null + if (isRPlus()) { + val recordingUri = createDocumentUriUsingFirstParentTreeUri(recordingFile) + DocumentsContract.deleteDocument(contentResolver, recordingUri) + } else { + File(recordingFile).delete() + } + + EventBus.getDefault().post(Events.RecordingCompleted()) + stopSelf() + } + private fun broadcastRecorderInfo() { broadcastDuration() broadcastStatus() diff --git a/app/src/main/res/drawable/ic_cancel_vector.xml b/app/src/main/res/drawable/ic_cancel_vector.xml new file mode 100644 index 00000000..845e3fb6 --- /dev/null +++ b/app/src/main/res/drawable/ic_cancel_vector.xml @@ -0,0 +1,3 @@ + + + diff --git a/app/src/main/res/drawable/ic_pause_recording_vector.xml b/app/src/main/res/drawable/ic_pause_recording_vector.xml new file mode 100644 index 00000000..4f658886 --- /dev/null +++ b/app/src/main/res/drawable/ic_pause_recording_vector.xml @@ -0,0 +1,3 @@ + + + diff --git a/app/src/main/res/layout/fragment_recorder.xml b/app/src/main/res/layout/fragment_recorder.xml index 5bb5b0e1..94285945 100644 --- a/app/src/main/res/layout/fragment_recorder.xml +++ b/app/src/main/res/layout/fragment_recorder.xml @@ -33,12 +33,32 @@ app:layout_constraintStart_toStartOf="parent" tools:text="00:00" /> + + + app:layout_constraintStart_toEndOf="@+id/toggle_recording_button" + app:layout_constraintTop_toTopOf="@id/toggle_recording_button" + tools:visibility="visible" />