From 3a233a04bc4828ce140d7fb956e03b01bf81563e Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 5 Nov 2020 18:14:36 +0100 Subject: [PATCH] store the recording status as an int, not just boolean --- app/src/main/AndroidManifest.xml | 1 + .../fragments/RecorderFragment.kt | 29 +++++++++++++------ .../voicerecorder/helpers/Constants.kt | 6 ++++ .../voicerecorder/models/Events.kt | 2 +- .../voicerecorder/services/RecorderService.kt | 19 +++++++----- 5 files changed, 39 insertions(+), 18 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8cd5083..242b90a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -65,6 +65,7 @@ + diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/RecorderFragment.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/RecorderFragment.kt index f02ffaa..bdfc84f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/RecorderFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/RecorderFragment.kt @@ -7,7 +7,7 @@ import android.util.AttributeSet import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.voicerecorder.R import com.simplemobiletools.voicerecorder.extensions.config -import com.simplemobiletools.voicerecorder.helpers.GET_RECORDER_INFO +import com.simplemobiletools.voicerecorder.helpers.* import com.simplemobiletools.voicerecorder.models.Events import com.simplemobiletools.voicerecorder.services.RecorderService import kotlinx.android.synthetic.main.fragment_recorder.view.* @@ -16,8 +16,7 @@ import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.ThreadMode class RecorderFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) { - private var isRecording = false - private var isPaused = false + private var status = RECORDING_STOPPED private var bus: EventBus? = null override fun onResume() { @@ -40,6 +39,13 @@ class RecorderFragment(context: Context, attributeSet: AttributeSet) : MyViewPag toggleRecording() } + toggle_pause_button.setOnClickListener { + Intent(context, RecorderService::class.java).apply { + action = TOGGLE_PAUSE + context.startService(this) + } + } + Intent(context, RecorderService::class.java).apply { action = GET_RECORDER_INFO context.startService(this) @@ -67,15 +73,20 @@ class RecorderFragment(context: Context, attributeSet: AttributeSet) : MyViewPag } private fun getToggleButtonIcon(): Drawable { - val drawable = if (isRecording) R.drawable.ic_stop_vector else R.drawable.ic_microphone_vector + val drawable = if (status == RECORDING_RUNNING) R.drawable.ic_stop_vector else R.drawable.ic_microphone_vector return resources.getColoredDrawableWithColor(drawable, context.getFABIconColor()) } private fun toggleRecording() { - isRecording = !isRecording + status = if (status == RECORDING_RUNNING || status == RECORDING_PAUSED) { + RECORDING_STOPPED + } else { + RECORDING_RUNNING + } + toggle_recording_button.setImageDrawable(getToggleButtonIcon()) - if (isRecording) { + if (status == RECORDING_RUNNING) { startRecording() } else { toggle_pause_button.beGone() @@ -102,10 +113,10 @@ class RecorderFragment(context: Context, attributeSet: AttributeSet) : MyViewPag @Subscribe(threadMode = ThreadMode.MAIN) fun gotStatusEvent(event: Events.RecordingStatus) { - isRecording = event.isRecording + status = event.status toggle_recording_button.setImageDrawable(getToggleButtonIcon()) - toggle_pause_button.beVisibleIf(isRecording) - if (isRecording) { + toggle_pause_button.beVisibleIf(status != RECORDING_STOPPED) + if (status == RECORDING_RUNNING) { recorder_visualizer.recreate() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/Constants.kt index 5835345..f00baaf 100644 --- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/Constants.kt @@ -12,9 +12,15 @@ const val RECORDER_RUNNING_NOTIF_ID = 10000 private const val PATH = "com.simplemobiletools.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 EXTENSION_M4A = 0 const val EXTENSION_MP3 = 1 +const val RECORDING_RUNNING = 0 +const val RECORDING_STOPPED = 1 +const val RECORDING_PAUSED = 2 + // shared preferences const val HIDE_NOTIFICATION = "hide_notification" const val SAVE_RECORDINGS = "save_recordings" diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/models/Events.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/models/Events.kt index 520edc1..927b190 100644 --- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/models/Events.kt +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/models/Events.kt @@ -2,7 +2,7 @@ package com.simplemobiletools.voicerecorder.models class Events { class RecordingDuration internal constructor(val duration: Int) - class RecordingStatus internal constructor(val isRecording: Boolean) + class RecordingStatus internal constructor(val status: Int) class RecordingAmplitude internal constructor(val amplitude: Int) class RecordingCompleted internal constructor() } diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/services/RecorderService.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/services/RecorderService.kt index 8e57e19..a194a3f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/services/RecorderService.kt +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/services/RecorderService.kt @@ -21,9 +21,7 @@ import com.simplemobiletools.commons.helpers.isQPlus import com.simplemobiletools.voicerecorder.R import com.simplemobiletools.voicerecorder.activities.SplashActivity import com.simplemobiletools.voicerecorder.extensions.config -import com.simplemobiletools.voicerecorder.helpers.GET_RECORDER_INFO -import com.simplemobiletools.voicerecorder.helpers.RECORDER_RUNNING_NOTIF_ID -import com.simplemobiletools.voicerecorder.helpers.STOP_AMPLITUDE_UPDATE +import com.simplemobiletools.voicerecorder.helpers.* import com.simplemobiletools.voicerecorder.models.Events import org.greenrobot.eventbus.EventBus import java.io.File @@ -34,7 +32,7 @@ class RecorderService : Service() { private var currFilePath = "" private var duration = 0 - private var isRecording = false + private var status = RECORDING_STOPPED private var durationTimer = Timer() private var amplitudeTimer = Timer() private var recorder: MediaRecorder? = null @@ -47,6 +45,7 @@ class RecorderService : Service() { when (intent.action) { GET_RECORDER_INFO -> broadcastRecorderInfo() STOP_AMPLITUDE_UPDATE -> amplitudeTimer.cancel() + TOGGLE_PAUSE -> togglePause() else -> startRecording() } @@ -93,7 +92,7 @@ class RecorderService : Service() { prepare() start() duration = 0 - isRecording = true + status = RECORDING_RUNNING broadcastRecorderInfo() startForeground(RECORDER_RUNNING_NOTIF_ID, showNotification()) @@ -111,7 +110,7 @@ class RecorderService : Service() { private fun stopRecording() { durationTimer.cancel() amplitudeTimer.cancel() - isRecording = false + status = RECORDING_STOPPED recorder?.apply { try { @@ -137,7 +136,7 @@ class RecorderService : Service() { broadcastDuration() broadcastStatus() - if (isRecording) { + if (status == RECORDING_RUNNING) { startAmplitudeUpdates() } } @@ -148,6 +147,10 @@ class RecorderService : Service() { amplitudeTimer.scheduleAtFixedRate(getAmplitudeUpdateTask(), 0, AMPLITUDE_UPDATE_MS) } + private fun togglePause() { + + } + @SuppressLint("InlinedApi") private fun addFileInNewMediaStore() { val audioCollection = Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY) @@ -256,6 +259,6 @@ class RecorderService : Service() { } private fun broadcastStatus() { - EventBus.getDefault().post(Events.RecordingStatus(isRecording)) + EventBus.getDefault().post(Events.RecordingStatus(status)) } }