mirror of
https://github.com/Myzel394/Alibi.git
synced 2025-06-19 07:15:25 +02:00
fix: Properly stop RecorderService onDestroy
This commit is contained in:
parent
5b7ce77ad3
commit
825f0eb33f
@ -19,28 +19,31 @@ class AudioRecorderService : IntervalRecorderService() {
|
|||||||
val filePath: String
|
val filePath: String
|
||||||
get() = "$folder/$counter.${settings!!.fileExtension}"
|
get() = "$folder/$counter.${settings!!.fileExtension}"
|
||||||
|
|
||||||
private fun _setAudioDevice() {
|
private fun clearAudioDevice() {
|
||||||
val audioManger = getSystemService(AUDIO_SERVICE)!! as AudioManager
|
val audioManger = getSystemService(AUDIO_SERVICE)!! as AudioManager
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||||
if (selectedMicrophone == null) {
|
audioManger.clearCommunicationDevice()
|
||||||
audioManger.clearCommunicationDevice()
|
|
||||||
} else {
|
|
||||||
audioManger.setCommunicationDevice(selectedMicrophone!!.deviceInfo)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (selectedMicrophone == null) {
|
audioManger.stopBluetoothSco()
|
||||||
audioManger.stopBluetoothSco()
|
}
|
||||||
} else {
|
}
|
||||||
audioManger.startBluetoothSco()
|
|
||||||
}
|
private fun startAudioDevice() {
|
||||||
|
if (selectedMicrophone == null) {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val audioManger = getSystemService(AUDIO_SERVICE)!! as AudioManager
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||||
|
audioManger.setCommunicationDevice(selectedMicrophone!!.deviceInfo)
|
||||||
|
} else {
|
||||||
|
audioManger.startBluetoothSco()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createRecorder(): MediaRecorder {
|
private fun createRecorder(): MediaRecorder {
|
||||||
_setAudioDevice()
|
|
||||||
|
|
||||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||||
MediaRecorder(this)
|
MediaRecorder(this)
|
||||||
} else {
|
} else {
|
||||||
@ -70,6 +73,7 @@ class AudioRecorderService : IntervalRecorderService() {
|
|||||||
it.stop()
|
it.stop()
|
||||||
it.release()
|
it.release()
|
||||||
}
|
}
|
||||||
|
clearAudioDevice()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,6 +85,7 @@ class AudioRecorderService : IntervalRecorderService() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resetRecorder()
|
resetRecorder()
|
||||||
|
startAudioDevice()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
recorder = newRecorder
|
recorder = newRecorder
|
||||||
|
@ -23,7 +23,7 @@ import java.util.concurrent.ScheduledExecutorService
|
|||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
|
|
||||||
abstract class RecorderService: Service() {
|
abstract class RecorderService : Service() {
|
||||||
private val binder = RecorderBinder()
|
private val binder = RecorderBinder()
|
||||||
|
|
||||||
private var isPaused: Boolean = false
|
private var isPaused: Boolean = false
|
||||||
@ -61,7 +61,7 @@ abstract class RecorderService: Service() {
|
|||||||
return super.onStartCommand(intent, flags, startId)
|
return super.onStartCommand(intent, flags, startId)
|
||||||
}
|
}
|
||||||
|
|
||||||
inner class RecorderBinder: Binder() {
|
inner class RecorderBinder : Binder() {
|
||||||
fun getService(): RecorderService = this@RecorderService
|
fun getService(): RecorderService = this@RecorderService
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,10 +95,12 @@ abstract class RecorderService: Service() {
|
|||||||
start()
|
start()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
RecorderState.PAUSED -> {
|
RecorderState.PAUSED -> {
|
||||||
pause()
|
pause()
|
||||||
isPaused = true
|
isPaused = true
|
||||||
}
|
}
|
||||||
|
|
||||||
RecorderState.IDLE -> {
|
RecorderState.IDLE -> {
|
||||||
stop()
|
stop()
|
||||||
onDestroy()
|
onDestroy()
|
||||||
@ -109,6 +111,7 @@ abstract class RecorderService: Service() {
|
|||||||
RecorderState.RECORDING -> {
|
RecorderState.RECORDING -> {
|
||||||
createRecordingTimeTimer()
|
createRecordingTimeTimer()
|
||||||
}
|
}
|
||||||
|
|
||||||
RecorderState.PAUSED, RecorderState.IDLE -> {
|
RecorderState.PAUSED, RecorderState.IDLE -> {
|
||||||
recordingTimeTimer.shutdown()
|
recordingTimeTimer.shutdown()
|
||||||
}
|
}
|
||||||
@ -121,7 +124,7 @@ abstract class RecorderService: Service() {
|
|||||||
RecorderState.PAUSED
|
RecorderState.PAUSED
|
||||||
).contains(newState) &&
|
).contains(newState) &&
|
||||||
PermissionHelper.hasGranted(this, android.Manifest.permission.POST_NOTIFICATIONS)
|
PermissionHelper.hasGranted(this, android.Manifest.permission.POST_NOTIFICATIONS)
|
||||||
){
|
) {
|
||||||
val notification = buildNotification()
|
val notification = buildNotification()
|
||||||
NotificationManagerCompat.from(this).notify(
|
NotificationManagerCompat.from(this).notify(
|
||||||
NotificationHelper.RECORDER_CHANNEL_NOTIFICATION_ID,
|
NotificationHelper.RECORDER_CHANNEL_NOTIFICATION_ID,
|
||||||
@ -145,22 +148,28 @@ abstract class RecorderService: Service() {
|
|||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
|
|
||||||
|
stop()
|
||||||
changeState(RecorderState.IDLE)
|
changeState(RecorderState.IDLE)
|
||||||
|
|
||||||
stopForeground(STOP_FOREGROUND_REMOVE)
|
stopForeground(STOP_FOREGROUND_REMOVE)
|
||||||
NotificationManagerCompat.from(this).cancel(NotificationHelper.RECORDER_CHANNEL_NOTIFICATION_ID)
|
NotificationManagerCompat.from(this)
|
||||||
|
.cancel(NotificationHelper.RECORDER_CHANNEL_NOTIFICATION_ID)
|
||||||
stopSelf()
|
stopSelf()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun buildStartNotification(): Notification = NotificationCompat.Builder(this, NotificationHelper.RECORDER_CHANNEL_ID)
|
private fun buildStartNotification(): Notification =
|
||||||
.setContentTitle(getString(R.string.ui_audioRecorder_state_recording_title))
|
NotificationCompat.Builder(this, NotificationHelper.RECORDER_CHANNEL_ID)
|
||||||
.setContentText(getString(R.string.ui_audioRecorder_state_recording_description))
|
.setContentTitle(getString(R.string.ui_audioRecorder_state_recording_title))
|
||||||
.setSmallIcon(R.drawable.launcher_foreground)
|
.setContentText(getString(R.string.ui_audioRecorder_state_recording_description))
|
||||||
.setPriority(NotificationCompat.PRIORITY_LOW)
|
.setSmallIcon(R.drawable.launcher_foreground)
|
||||||
.setCategory(NotificationCompat.CATEGORY_SERVICE)
|
.setPriority(NotificationCompat.PRIORITY_LOW)
|
||||||
.build()
|
.setCategory(NotificationCompat.CATEGORY_SERVICE)
|
||||||
|
.build()
|
||||||
|
|
||||||
private fun getNotificationChangeStateIntent(newState: RecorderState, requestCode: Int): PendingIntent {
|
private fun getNotificationChangeStateIntent(
|
||||||
|
newState: RecorderState,
|
||||||
|
requestCode: Int
|
||||||
|
): PendingIntent {
|
||||||
return PendingIntent.getService(
|
return PendingIntent.getService(
|
||||||
this,
|
this,
|
||||||
requestCode,
|
requestCode,
|
||||||
@ -172,8 +181,11 @@ abstract class RecorderService: Service() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun buildNotification(): Notification = when(state) {
|
private fun buildNotification(): Notification = when (state) {
|
||||||
RecorderState.RECORDING -> NotificationCompat.Builder(this, NotificationHelper.RECORDER_CHANNEL_ID)
|
RecorderState.RECORDING -> NotificationCompat.Builder(
|
||||||
|
this,
|
||||||
|
NotificationHelper.RECORDER_CHANNEL_ID
|
||||||
|
)
|
||||||
.setContentTitle(getString(R.string.ui_audioRecorder_state_recording_title))
|
.setContentTitle(getString(R.string.ui_audioRecorder_state_recording_title))
|
||||||
.setContentText(getString(R.string.ui_audioRecorder_state_recording_description))
|
.setContentText(getString(R.string.ui_audioRecorder_state_recording_description))
|
||||||
.setSmallIcon(R.drawable.launcher_foreground)
|
.setSmallIcon(R.drawable.launcher_foreground)
|
||||||
@ -211,7 +223,11 @@ abstract class RecorderService: Service() {
|
|||||||
getNotificationChangeStateIntent(RecorderState.PAUSED, 2),
|
getNotificationChangeStateIntent(RecorderState.PAUSED, 2),
|
||||||
)
|
)
|
||||||
.build()
|
.build()
|
||||||
RecorderState.PAUSED -> NotificationCompat.Builder(this, NotificationHelper.RECORDER_CHANNEL_ID)
|
|
||||||
|
RecorderState.PAUSED -> NotificationCompat.Builder(
|
||||||
|
this,
|
||||||
|
NotificationHelper.RECORDER_CHANNEL_ID
|
||||||
|
)
|
||||||
.setContentTitle(getString(R.string.ui_audioRecorder_state_paused_title))
|
.setContentTitle(getString(R.string.ui_audioRecorder_state_paused_title))
|
||||||
.setContentText(getString(R.string.ui_audioRecorder_state_paused_description))
|
.setContentText(getString(R.string.ui_audioRecorder_state_paused_description))
|
||||||
.setSmallIcon(R.drawable.launcher_foreground)
|
.setSmallIcon(R.drawable.launcher_foreground)
|
||||||
@ -236,6 +252,7 @@ abstract class RecorderService: Service() {
|
|||||||
getNotificationChangeStateIntent(RecorderState.RECORDING, 3),
|
getNotificationChangeStateIntent(RecorderState.RECORDING, 3),
|
||||||
)
|
)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
else -> throw IllegalStateException("Invalid state passed to `buildNotification()`")
|
else -> throw IllegalStateException("Invalid state passed to `buildNotification()`")
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user