store the recording status as an int, not just boolean

This commit is contained in:
tibbi
2020-11-05 18:14:36 +01:00
parent ba05a1c49c
commit 3a233a04bc
5 changed files with 39 additions and 18 deletions

View File

@ -65,6 +65,7 @@
<intent-filter>
<action android:name="com.simplemobiletools.voicerecorder.action.GET_RECORDER_INFO" />
<action android:name="com.simplemobiletools.voicerecorder.action.STOP_AMPLITUDE_UPDATE" />
<action android:name="com.simplemobiletools.voicerecorder.action.TOGGLE_PAUSE" />
</intent-filter>
</service>

View File

@ -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()
}
}

View File

@ -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"

View File

@ -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()
}

View File

@ -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))
}
}