Skip to content

Commit

Permalink
feat: Add pause / resume and delete functionality to notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Aug 8, 2023
1 parent e667a4e commit 9d1966e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
70 changes: 64 additions & 6 deletions app/src/main/java/app/myzel394/alibi/services/RecorderService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ abstract class RecorderService: Service() {

override fun onBind(p0: Intent?): IBinder? = binder

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when (intent?.action) {
"changeState" -> {
val newState = intent.getStringExtra("newState")?.let {
RecorderState.valueOf(it)
} ?: RecorderState.IDLE
changeState(newState)
}
}

return super.onStartCommand(intent, flags, startId)
}

inner class RecorderBinder: Binder() {
fun getService(): RecorderService = this@RecorderService
}
Expand Down Expand Up @@ -86,7 +99,10 @@ abstract class RecorderService: Service() {
pause()
isPaused = true
}
RecorderState.IDLE -> stop()
RecorderState.IDLE -> {
stop()
onDestroy()
}
}

when (newState) {
Expand All @@ -98,7 +114,14 @@ abstract class RecorderService: Service() {
}
}

if (PermissionHelper.hasGranted(this, android.Manifest.permission.POST_NOTIFICATIONS)){

if (
arrayOf(
RecorderState.RECORDING,
RecorderState.PAUSED
).contains(newState) &&
PermissionHelper.hasGranted(this, android.Manifest.permission.POST_NOTIFICATIONS)
){
val notification = buildNotification()
NotificationManagerCompat.from(this).notify(
NotificationHelper.RECORDER_CHANNEL_NOTIFICATION_ID,
Expand All @@ -108,11 +131,10 @@ abstract class RecorderService: Service() {
onStateChange?.invoke(newState)
}

// Must be called immediately after the service is created
fun startRecording() {
recordingStart = LocalDateTime.now()

val notification = buildNotification()
val notification = buildStartNotification()
startForeground(NotificationHelper.RECORDER_CHANNEL_NOTIFICATION_ID, notification)

// Start
Expand All @@ -131,18 +153,38 @@ abstract class RecorderService: Service() {
changeState(RecorderState.IDLE)

stopForeground(STOP_FOREGROUND_REMOVE)
NotificationManagerCompat.from(this).cancel(NotificationHelper.RECORDER_CHANNEL_NOTIFICATION_ID)
stopSelf()
}

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)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.build()

private fun getNotificationChangeStateIntent(newState: RecorderState, requestCode: Int): PendingIntent {
return PendingIntent.getService(
this,
requestCode,
Intent(this, AudioRecorderService::class.java).apply {
action = "changeState"
putExtra("newState", newState.name)
},
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
}

private fun buildNotification(): Notification = when(state) {
RecorderState.RECORDING, RecorderState.IDLE -> 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))
.setContentText(getString(R.string.ui_audioRecorder_state_recording_description))
.setSmallIcon(R.drawable.launcher_foreground)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setOngoing(true)
// Convert calendar to epoch seconds
.setWhen(
Date.from(
Calendar
Expand All @@ -163,6 +205,16 @@ abstract class RecorderService: Service() {
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
)
)
.addAction(
R.drawable.ic_cancel,
getString(R.string.ui_audioRecorder_action_delete_label),
getNotificationChangeStateIntent(RecorderState.IDLE, 1),
)
.addAction(
R.drawable.ic_pause,
getString(R.string.ui_audioRecorder_action_pause_label),
getNotificationChangeStateIntent(RecorderState.PAUSED, 2),
)
.build()
RecorderState.PAUSED -> NotificationCompat.Builder(this, NotificationHelper.RECORDER_CHANNEL_ID)
.setContentTitle(getString(R.string.ui_audioRecorder_state_paused_title))
Expand All @@ -183,6 +235,12 @@ abstract class RecorderService: Service() {
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
)
)
.addAction(
R.drawable.ic_play,
getString(R.string.ui_audioRecorder_action_resume_label),
getNotificationChangeStateIntent(RecorderState.RECORDING, 3),
)
.build()
else -> throw IllegalStateException("Invalid state passed to `buildNotification()`")
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_cancel.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,2C6.47,2 2,6.47 2,12s4.47,10 10,10 10,-4.47 10,-10S17.53,2 12,2zM17,15.59L15.59,17 12,13.41 8.41,17 7,15.59 10.59,12 7,8.41 8.41,7 12,10.59 15.59,7 17,8.41 13.41,12 17,15.59z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_pause.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM11,16L9,16L9,8h2v8zM15,16h-2L13,8h2v8z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_play.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10s10,-4.48 10,-10S17.52,2 12,2zM9.5,16.5v-9l7,4.5L9.5,16.5z"/>
</vector>

0 comments on commit 9d1966e

Please sign in to comment.