mirror of
https://github.com/Myzel394/Alibi.git
synced 2025-06-18 23:05:26 +02:00
feat: Add support for custom notification
This commit is contained in:
parent
6ef60942e0
commit
d0d8996227
@ -40,7 +40,8 @@ class AppSettingsSerializer: Serializer<AppSettings> {
|
||||
}
|
||||
|
||||
class LocalDateTimeSerializer : KSerializer<LocalDateTime> {
|
||||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("LocalDateTime", PrimitiveKind.STRING)
|
||||
override val descriptor: SerialDescriptor =
|
||||
PrimitiveSerialDescriptor("LocalDateTime", PrimitiveKind.STRING)
|
||||
|
||||
override fun deserialize(decoder: Decoder): LocalDateTime {
|
||||
return LocalDateTime.parse(decoder.decodeString())
|
||||
|
@ -40,6 +40,7 @@ abstract class RecorderService: Service() {
|
||||
private set
|
||||
private lateinit var recordingTimeTimer: ScheduledExecutorService
|
||||
var onRecordingTimeChange: ((Long) -> Unit)? = null
|
||||
var notificationDetails: NotificationDetails? = null
|
||||
|
||||
protected abstract fun start()
|
||||
protected abstract fun pause()
|
||||
@ -95,10 +96,12 @@ abstract class RecorderService: Service() {
|
||||
start()
|
||||
}
|
||||
}
|
||||
|
||||
RecorderState.PAUSED -> {
|
||||
pause()
|
||||
isPaused = true
|
||||
}
|
||||
|
||||
RecorderState.IDLE -> {
|
||||
stop()
|
||||
onDestroy()
|
||||
@ -109,6 +112,7 @@ abstract class RecorderService: Service() {
|
||||
RecorderState.RECORDING -> {
|
||||
createRecordingTimeTimer()
|
||||
}
|
||||
|
||||
RecorderState.PAUSED, RecorderState.IDLE -> {
|
||||
recordingTimeTimer.shutdown()
|
||||
}
|
||||
@ -148,11 +152,13 @@ abstract class RecorderService: Service() {
|
||||
changeState(RecorderState.IDLE)
|
||||
|
||||
stopForeground(STOP_FOREGROUND_REMOVE)
|
||||
NotificationManagerCompat.from(this).cancel(NotificationHelper.RECORDER_CHANNEL_NOTIFICATION_ID)
|
||||
NotificationManagerCompat.from(this)
|
||||
.cancel(NotificationHelper.RECORDER_CHANNEL_NOTIFICATION_ID)
|
||||
stopSelf()
|
||||
}
|
||||
|
||||
private fun buildStartNotification(): Notification = NotificationCompat.Builder(this, NotificationHelper.RECORDER_CHANNEL_ID)
|
||||
private fun buildStartNotification(): Notification =
|
||||
NotificationCompat.Builder(this, NotificationHelper.RECORDER_CHANNEL_ID)
|
||||
.setContentTitle(getString(R.string.ui_audioRecorder_state_recording_title))
|
||||
.setContentText(getString(R.string.ui_audioRecorder_state_recording_description))
|
||||
.setSmallIcon(R.drawable.launcher_foreground)
|
||||
@ -160,7 +166,10 @@ abstract class RecorderService: Service() {
|
||||
.setCategory(NotificationCompat.CATEGORY_SERVICE)
|
||||
.build()
|
||||
|
||||
private fun getNotificationChangeStateIntent(newState: RecorderState, requestCode: Int): PendingIntent {
|
||||
private fun getNotificationChangeStateIntent(
|
||||
newState: RecorderState,
|
||||
requestCode: Int
|
||||
): PendingIntent {
|
||||
return PendingIntent.getService(
|
||||
this,
|
||||
requestCode,
|
||||
@ -173,13 +182,12 @@ abstract class RecorderService: Service() {
|
||||
}
|
||||
|
||||
private fun buildNotification(): Notification = when (state) {
|
||||
RecorderState.RECORDING -> NotificationCompat.Builder(this, NotificationHelper.RECORDER_CHANNEL_ID)
|
||||
.setContentTitle(getString(R.string.ui_audioRecorder_state_recording_title))
|
||||
.setContentText(getString(R.string.ui_audioRecorder_state_recording_description))
|
||||
.setSmallIcon(R.drawable.launcher_foreground)
|
||||
RecorderState.RECORDING -> NotificationCompat.Builder(
|
||||
this,
|
||||
NotificationHelper.RECORDER_CHANNEL_ID
|
||||
)
|
||||
.setPriority(NotificationCompat.PRIORITY_LOW)
|
||||
.setCategory(NotificationCompat.CATEGORY_SERVICE)
|
||||
.setOngoing(true)
|
||||
.setWhen(
|
||||
Date.from(
|
||||
Calendar
|
||||
@ -210,8 +218,24 @@ abstract class RecorderService: Service() {
|
||||
getString(R.string.ui_audioRecorder_action_pause_label),
|
||||
getNotificationChangeStateIntent(RecorderState.PAUSED, 2),
|
||||
)
|
||||
.apply {
|
||||
setContentTitle(
|
||||
notificationDetails?.title
|
||||
?: getString(R.string.ui_audioRecorder_state_recording_title)
|
||||
)
|
||||
setContentText(
|
||||
notificationDetails?.description
|
||||
?: getString(R.string.ui_audioRecorder_state_recording_description)
|
||||
)
|
||||
setSmallIcon(notificationDetails?.icon ?: R.drawable.launcher_foreground)
|
||||
setOngoing(notificationDetails?.isOngoing ?: true)
|
||||
}
|
||||
.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))
|
||||
.setContentText(getString(R.string.ui_audioRecorder_state_paused_description))
|
||||
.setSmallIcon(R.drawable.launcher_foreground)
|
||||
@ -236,6 +260,14 @@ abstract class RecorderService: Service() {
|
||||
getNotificationChangeStateIntent(RecorderState.RECORDING, 3),
|
||||
)
|
||||
.build()
|
||||
|
||||
else -> throw IllegalStateException("Invalid state passed to `buildNotification()`")
|
||||
}
|
||||
|
||||
data class NotificationDetails(
|
||||
val title: String,
|
||||
val description: String,
|
||||
val icon: Int,
|
||||
val isOngoing: Boolean,
|
||||
)
|
||||
}
|
@ -48,7 +48,8 @@ class AudioRecorderModel: ViewModel() {
|
||||
|
||||
private val connection = object : ServiceConnection {
|
||||
override fun onServiceConnected(className: ComponentName, service: IBinder) {
|
||||
recorderService = ((service as RecorderService.RecorderBinder).getService() as AudioRecorderService).also {recorder ->
|
||||
recorderService =
|
||||
((service as RecorderService.RecorderBinder).getService() as AudioRecorderService).also { recorder ->
|
||||
recorder.onStateChange = { state ->
|
||||
recorderState = state
|
||||
}
|
||||
@ -117,6 +118,10 @@ class AudioRecorderModel: ViewModel() {
|
||||
recorderService!!.changeState(RecorderState.RECORDING)
|
||||
}
|
||||
|
||||
fun setNotificationDetails(details: RecorderService.NotificationDetails) {
|
||||
recorderService?.notificationDetails = details
|
||||
}
|
||||
|
||||
fun setMaxAmplitudesAmount(amount: Int) {
|
||||
recorderService?.amplitudesAmount = amount
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user