Skip to content

Commit

Permalink
Add cancel recording button
Browse files Browse the repository at this point in the history
Closes #57
  • Loading branch information
naveensingh committed Jan 10, 2025
1 parent 1e4ceea commit 08e7491
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -166,6 +160,7 @@ class RecorderFragment(
startRecording()
} else {
binding.togglePauseButton.beGone()
binding.cancelRecordingButton.beGone()
stopRecording()
}
}
Expand All @@ -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)
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -70,6 +72,7 @@ class RecorderService : Service() {
GET_RECORDER_INFO -> broadcastRecorderInfo()
STOP_AMPLITUDE_UPDATE -> amplitudeTimer.cancel()
TOGGLE_PAUSE -> togglePause()
CANCEL_RECORDING -> cancelRecording()
else -> startRecording()
}

Expand Down Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/drawable/ic_cancel_vector.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="960" android:viewportHeight="960">
<path android:fillColor="#FFFFFf" android:pathData="M480 575L302 753q-20 20-48 20t-47-20q-20-20-20-47.5t20-47.5l178-178-178-179q-20-20-20-47.5t20-47.5q19-20 47-20t48 20l178 178 178-178q20-20 48-20t47 20q20 20 20 47.5T753 301L575 480l178 178q20 20 20 47.5T753 753q-19 20-47 20t-48-20L480 575z"/>
</vector>
3 changes: 3 additions & 0 deletions app/src/main/res/drawable/ic_pause_recording_vector.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="960" android:viewportHeight="960">
<path android:fillColor="#FFFFFf" android:pathData="M656.95 794.48q-43.73 0-74.86-31.14-31.13-31.14-31.13-74.86V271.52q0-43.72 31.14-74.86t74.87-31.14q43.73 0 74.86 31.14 31.13 31.14 31.13 74.86v416.96q0 43.72-31.14 74.86t-74.87 31.14zm-353.92 0q-43.73 0-74.86-31.14-31.13-31.14-31.13-74.86V271.52q0-43.72 31.14-74.86t74.87-31.14q43.73 0 74.86 31.14 31.13 31.14 31.13 74.86v416.96q0 43.72-31.14 74.86t-74.87 31.14z"/>
</vector>
33 changes: 28 additions & 5 deletions app/src/main/res/layout/fragment_recorder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,32 @@
app:layout_constraintStart_toStartOf="parent"
tools:text="00:00" />

<ImageView
android:id="@+id/cancel_recording_button"
android:layout_width="@dimen/fab_size"
android:layout_height="@dimen/fab_size"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:contentDescription="@string/cancel"
android:focusable="true"
android:padding="@dimen/normal_margin"
android:src="@drawable/ic_cancel_vector"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="@id/toggle_recording_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@id/toggle_recording_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/toggle_recording_button"
tools:visibility="visible" />

<ImageView
android:id="@+id/toggle_recording_button"
android:layout_width="@dimen/toggle_recording_button_size"
android:layout_height="@dimen/toggle_recording_button_size"
android:layout_marginBottom="@dimen/big_margin"
android:background="@drawable/circle_background"
android:clickable="true"
android:focusable="true"
android:padding="@dimen/normal_margin"
android:src="@drawable/ic_microphone_vector"
app:layout_constraintBottom_toBottomOf="parent"
Expand All @@ -49,13 +69,16 @@
android:id="@+id/toggle_pause_button"
android:layout_width="@dimen/fab_size"
android:layout_height="@dimen/fab_size"
android:layout_marginBottom="@dimen/big_margin"
android:background="@drawable/circle_background"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
android:padding="@dimen/normal_margin"
android:src="@drawable/ic_pause_vector"
android:src="@drawable/ic_pause_recording_vector"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toBottomOf="@id/toggle_recording_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/toggle_recording_button" />
app:layout_constraintStart_toEndOf="@+id/toggle_recording_button"
app:layout_constraintTop_toTopOf="@id/toggle_recording_button"
tools:visibility="visible" />

</org.fossify.voicerecorder.fragments.RecorderFragment>

0 comments on commit 08e7491

Please sign in to comment.