add broadcasting the recording status too

This commit is contained in:
tibbi
2020-03-30 15:29:17 +02:00
parent 9cea12a1e2
commit 0bac22fd11
5 changed files with 29 additions and 10 deletions

View File

@ -68,7 +68,7 @@
android:name=".services.RecorderService" android:name=".services.RecorderService"
android:exported="false"> android:exported="false">
<intent-filter> <intent-filter>
<action android:name="com.simplemobiletools.voicerecorder.action.GET_DURATION" /> <action android:name="com.simplemobiletools.voicerecorder.action.GET_RECORDER_INFO" />
</intent-filter> </intent-filter>
</service> </service>

View File

@ -12,7 +12,7 @@ import com.simplemobiletools.commons.helpers.isQPlus
import com.simplemobiletools.commons.models.FAQItem import com.simplemobiletools.commons.models.FAQItem
import com.simplemobiletools.voicerecorder.BuildConfig import com.simplemobiletools.voicerecorder.BuildConfig
import com.simplemobiletools.voicerecorder.R import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.helpers.GET_DURATION import com.simplemobiletools.voicerecorder.helpers.GET_RECORDER_INFO
import com.simplemobiletools.voicerecorder.models.Events import com.simplemobiletools.voicerecorder.models.Events
import com.simplemobiletools.voicerecorder.services.RecorderService import com.simplemobiletools.voicerecorder.services.RecorderService
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
@ -93,7 +93,7 @@ class MainActivity : SimpleActivity() {
} }
Intent(this@MainActivity, RecorderService::class.java).apply { Intent(this@MainActivity, RecorderService::class.java).apply {
action = GET_DURATION action = GET_RECORDER_INFO
startService(this) startService(this)
} }
} }
@ -130,6 +130,12 @@ class MainActivity : SimpleActivity() {
updateRecordingDuration(event.duration) updateRecordingDuration(event.duration)
} }
@Subscribe(threadMode = ThreadMode.MAIN)
fun gotStatusEvent(event: Events.RecordingStatus) {
isRecording = event.isRecording
toggle_recording_button.setImageDrawable(getToggleButtonIcon())
}
private fun getToggleButtonIcon(): Drawable { private fun getToggleButtonIcon(): Drawable {
val drawable = if (isRecording) R.drawable.ic_stop_vector else R.drawable.ic_mic_vector val drawable = if (isRecording) R.drawable.ic_stop_vector else R.drawable.ic_mic_vector
return resources.getColoredDrawableWithColor(drawable, getFABIconColor()) return resources.getColoredDrawableWithColor(drawable, getFABIconColor())

View File

@ -3,4 +3,4 @@ package com.simplemobiletools.voicerecorder.helpers
const val RECORDER_RUNNING_NOTIF_ID = 10000 const val RECORDER_RUNNING_NOTIF_ID = 10000
private const val PATH = "com.simplemobiletools.voicerecorder.action." private const val PATH = "com.simplemobiletools.voicerecorder.action."
const val GET_DURATION = PATH + "GET_DURATION" const val GET_RECORDER_INFO = PATH + "GET_RECORDER_INFO"

View File

@ -2,4 +2,5 @@ package com.simplemobiletools.voicerecorder.models
class Events { class Events {
class RecordingDuration internal constructor(val duration: Int) class RecordingDuration internal constructor(val duration: Int)
class RecordingStatus internal constructor(val isRecording: Boolean)
} }

View File

@ -19,7 +19,7 @@ import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.commons.helpers.isQPlus import com.simplemobiletools.commons.helpers.isQPlus
import com.simplemobiletools.voicerecorder.R import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.activities.SplashActivity import com.simplemobiletools.voicerecorder.activities.SplashActivity
import com.simplemobiletools.voicerecorder.helpers.GET_DURATION import com.simplemobiletools.voicerecorder.helpers.GET_RECORDER_INFO
import com.simplemobiletools.voicerecorder.helpers.RECORDER_RUNNING_NOTIF_ID import com.simplemobiletools.voicerecorder.helpers.RECORDER_RUNNING_NOTIF_ID
import com.simplemobiletools.voicerecorder.models.Events import com.simplemobiletools.voicerecorder.models.Events
import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.EventBus
@ -30,6 +30,7 @@ import java.util.*
class RecorderService : Service() { class RecorderService : Service() {
private var currFilePath = "" private var currFilePath = ""
private var duration = 0 private var duration = 0
private var isRecording = false
private var timer = Timer() private var timer = Timer()
private var recorder: MediaRecorder? = null private var recorder: MediaRecorder? = null
@ -39,7 +40,7 @@ class RecorderService : Service() {
super.onStartCommand(intent, flags, startId) super.onStartCommand(intent, flags, startId)
when (intent.action) { when (intent.action) {
GET_DURATION -> broadcastDuration() GET_RECORDER_INFO -> broadcastRecorderInfo()
else -> startRecording() else -> startRecording()
} }
@ -55,10 +56,6 @@ class RecorderService : Service() {
// mp4 output format with aac encoding should produce good enough mp3 files according to https://stackoverflow.com/a/33054794/1967672 // mp4 output format with aac encoding should produce good enough mp3 files according to https://stackoverflow.com/a/33054794/1967672
private fun startRecording() { private fun startRecording() {
startForeground(RECORDER_RUNNING_NOTIF_ID, showNotification())
duration = 0
broadcastDuration()
val baseFolder = if (isQPlus()) { val baseFolder = if (isQPlus()) {
cacheDir cacheDir
} else { } else {
@ -80,16 +77,22 @@ class RecorderService : Service() {
prepare() prepare()
start() start()
duration = 0 duration = 0
isRecording = true
broadcastRecorderInfo()
startForeground(RECORDER_RUNNING_NOTIF_ID, showNotification())
timer = Timer() timer = Timer()
timer.scheduleAtFixedRate(getTimerTask(), 1000, 1000) timer.scheduleAtFixedRate(getTimerTask(), 1000, 1000)
} catch (e: IOException) { } catch (e: IOException) {
showErrorToast(e) showErrorToast(e)
stopRecording()
} }
} }
} }
private fun stopRecording() { private fun stopRecording() {
timer.cancel() timer.cancel()
isRecording = false
recorder?.apply { recorder?.apply {
stop() stop()
@ -106,6 +109,11 @@ class RecorderService : Service() {
recorder = null recorder = null
} }
private fun broadcastRecorderInfo() {
broadcastDuration()
broadcastStatus()
}
@SuppressLint("InlinedApi") @SuppressLint("InlinedApi")
private fun addFileInNewMediaStore() { private fun addFileInNewMediaStore() {
val audioCollection = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY) val audioCollection = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
@ -186,4 +194,8 @@ class RecorderService : Service() {
private fun broadcastDuration() { private fun broadcastDuration() {
EventBus.getDefault().post(Events.RecordingDuration(duration)) EventBus.getDefault().post(Events.RecordingDuration(duration))
} }
private fun broadcastStatus() {
EventBus.getDefault().post(Events.RecordingStatus(isRecording))
}
} }