mirror of
https://github.com/SimpleMobileTools/Simple-Voice-Recorder.git
synced 2025-06-05 21:59:31 +02:00
prepare for using EventBus for communication with the recording service
This commit is contained in:
@ -39,4 +39,5 @@ android {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'com.simplemobiletools:commons:5.24.3'
|
implementation 'com.simplemobiletools:commons:5.24.3'
|
||||||
|
implementation 'org.greenrobot:eventbus:3.2.0'
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
android:installLocation="auto">
|
android:installLocation="auto">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
<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
|
<uses-permission
|
||||||
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
||||||
android:maxSdkVersion="28" />
|
android:maxSdkVersion="28" />
|
||||||
@ -64,7 +64,13 @@
|
|||||||
android:label="@string/frequently_asked_questions"
|
android:label="@string/frequently_asked_questions"
|
||||||
android:parentActivityName="com.simplemobiletools.commons.activities.AboutActivity" />
|
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>
|
</application>
|
||||||
</manifest>
|
</manifest>
|
||||||
|
@ -19,8 +19,12 @@ 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.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.*
|
||||||
|
import org.greenrobot.eventbus.EventBus
|
||||||
|
import org.greenrobot.eventbus.Subscribe
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@ -28,6 +32,7 @@ import java.util.*
|
|||||||
class MainActivity : SimpleActivity() {
|
class MainActivity : SimpleActivity() {
|
||||||
private var isRecording = false
|
private var isRecording = false
|
||||||
private var recorder: MediaRecorder? = null
|
private var recorder: MediaRecorder? = null
|
||||||
|
private var bus: EventBus? = null
|
||||||
private var currFilePath = ""
|
private var currFilePath = ""
|
||||||
private var duration = 0
|
private var duration = 0
|
||||||
private var timer = Timer()
|
private var timer = Timer()
|
||||||
@ -64,6 +69,11 @@ class MainActivity : SimpleActivity() {
|
|||||||
recorder = null
|
recorder = null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
bus?.unregister(this)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
menuInflater.inflate(R.menu.menu, menu)
|
menuInflater.inflate(R.menu.menu, menu)
|
||||||
return true
|
return true
|
||||||
@ -93,11 +103,19 @@ class MainActivity : SimpleActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun initVoiceRecorder() {
|
private fun initVoiceRecorder() {
|
||||||
|
bus = EventBus.getDefault()
|
||||||
|
bus!!.register(this)
|
||||||
|
|
||||||
duration = 0
|
duration = 0
|
||||||
updateRecordingDuration()
|
updateRecordingDuration()
|
||||||
toggle_recording_button.setOnClickListener {
|
toggle_recording_button.setOnClickListener {
|
||||||
toggleRecording()
|
toggleRecording()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Intent(this@MainActivity, RecorderService::class.java).apply {
|
||||||
|
action = GET_DURATION
|
||||||
|
startService(this)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun toggleRecording() {
|
private fun toggleRecording() {
|
||||||
@ -172,6 +190,11 @@ class MainActivity : SimpleActivity() {
|
|||||||
recorder = null
|
recorder = null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Subscribe
|
||||||
|
fun gotDurationEvent(event: Events.RecordingDuration) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@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)
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
package com.simplemobiletools.voicerecorder.helpers
|
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."
|
||||||
|
const val GET_DURATION = PATH + "GET_DURATION"
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.simplemobiletools.voicerecorder.models
|
||||||
|
|
||||||
|
class Events {
|
||||||
|
class RecordingDuration internal constructor(val duration: Int)
|
||||||
|
}
|
@ -11,14 +11,24 @@ import com.simplemobiletools.commons.extensions.getLaunchIntent
|
|||||||
import com.simplemobiletools.commons.helpers.isOreoPlus
|
import com.simplemobiletools.commons.helpers.isOreoPlus
|
||||||
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.RECORDER_RUNNING_NOTIF_ID
|
import com.simplemobiletools.voicerecorder.helpers.RECORDER_RUNNING_NOTIF_ID
|
||||||
|
import com.simplemobiletools.voicerecorder.models.Events
|
||||||
|
import org.greenrobot.eventbus.EventBus
|
||||||
|
|
||||||
class RecorderService : Service() {
|
class RecorderService : Service() {
|
||||||
override fun onBind(intent: Intent?): IBinder? = null
|
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)
|
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
|
return START_NOT_STICKY
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,4 +64,8 @@ class RecorderService : Service() {
|
|||||||
val intent = getLaunchIntent() ?: Intent(this, SplashActivity::class.java)
|
val intent = getLaunchIntent() ?: Intent(this, SplashActivity::class.java)
|
||||||
return PendingIntent.getActivity(this, RECORDER_RUNNING_NOTIF_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
return PendingIntent.getActivity(this, RECORDER_RUNNING_NOTIF_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun broadcastDuration() {
|
||||||
|
EventBus.getDefault().post(Events.RecordingDuration(3))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user