prepare for using EventBus for communication with the recording service

This commit is contained in:
tibbi 2020-03-30 13:12:25 +02:00
parent a7518e9ed2
commit 8255def4cb
6 changed files with 56 additions and 4 deletions

View File

@ -39,4 +39,5 @@ android {
dependencies {
implementation 'com.simplemobiletools:commons:5.24.3'
implementation 'org.greenrobot:eventbus:3.2.0'
}

View File

@ -5,7 +5,7 @@
android:installLocation="auto">
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
@ -64,7 +64,13 @@
android:label="@string/frequently_asked_questions"
android:parentActivityName="com.simplemobiletools.commons.activities.AboutActivity" />
<service android:name=".services.RecorderService"/>
<service
android:name=".services.RecorderService"
android:exported="false">
<intent-filter>
<action android:name="com.simplemobiletools.voicerecorder.action.GET_DURATION" />
</intent-filter>
</service>
</application>
</manifest>

View File

@ -19,8 +19,12 @@ import com.simplemobiletools.commons.helpers.isQPlus
import com.simplemobiletools.commons.models.FAQItem
import com.simplemobiletools.voicerecorder.BuildConfig
import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.helpers.GET_DURATION
import com.simplemobiletools.voicerecorder.models.Events
import com.simplemobiletools.voicerecorder.services.RecorderService
import kotlinx.android.synthetic.main.activity_main.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import java.io.File
import java.io.IOException
import java.util.*
@ -28,6 +32,7 @@ import java.util.*
class MainActivity : SimpleActivity() {
private var isRecording = false
private var recorder: MediaRecorder? = null
private var bus: EventBus? = null
private var currFilePath = ""
private var duration = 0
private var timer = Timer()
@ -64,6 +69,11 @@ class MainActivity : SimpleActivity() {
recorder = null
}
override fun onDestroy() {
super.onDestroy()
bus?.unregister(this)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu, menu)
return true
@ -93,11 +103,19 @@ class MainActivity : SimpleActivity() {
}
private fun initVoiceRecorder() {
bus = EventBus.getDefault()
bus!!.register(this)
duration = 0
updateRecordingDuration()
toggle_recording_button.setOnClickListener {
toggleRecording()
}
Intent(this@MainActivity, RecorderService::class.java).apply {
action = GET_DURATION
startService(this)
}
}
private fun toggleRecording() {
@ -172,6 +190,11 @@ class MainActivity : SimpleActivity() {
recorder = null
}
@Subscribe
fun gotDurationEvent(event: Events.RecordingDuration) {
}
@SuppressLint("InlinedApi")
private fun addFileInNewMediaStore() {
val audioCollection = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)

View File

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

View File

@ -0,0 +1,5 @@
package com.simplemobiletools.voicerecorder.models
class Events {
class RecordingDuration internal constructor(val duration: Int)
}

View File

@ -11,14 +11,24 @@ import com.simplemobiletools.commons.extensions.getLaunchIntent
import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.activities.SplashActivity
import com.simplemobiletools.voicerecorder.helpers.GET_DURATION
import com.simplemobiletools.voicerecorder.helpers.RECORDER_RUNNING_NOTIF_ID
import com.simplemobiletools.voicerecorder.models.Events
import org.greenrobot.eventbus.EventBus
class RecorderService : Service() {
override fun onBind(intent: Intent?): IBinder? = null
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
startForeground(RECORDER_RUNNING_NOTIF_ID, showNotification())
val action = intent.action
if (action == GET_DURATION) {
broadcastDuration()
} else {
startForeground(RECORDER_RUNNING_NOTIF_ID, showNotification())
}
return START_NOT_STICKY
}
@ -54,4 +64,8 @@ class RecorderService : Service() {
val intent = getLaunchIntent() ?: Intent(this, SplashActivity::class.java)
return PendingIntent.getActivity(this, RECORDER_RUNNING_NOTIF_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
private fun broadcastDuration() {
EventBus.getDefault().post(Events.RecordingDuration(3))
}
}