Skip to content

Commit

Permalink
feat: gradually increasing alarm volume (closes #273)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Jun 13, 2024
1 parent ffd84f0 commit d5d39ab
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions app/src/main/java/com/bnyro/clock/util/services/AlarmService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import android.media.MediaPlayer
import android.media.RingtoneManager
import android.net.Uri
import android.os.Build
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.os.Vibrator
import android.util.Log
import androidx.core.app.NotificationCompat
Expand All @@ -39,6 +41,20 @@ class AlarmService : Service() {

val timer = Timer()

private var volume: Float = 0.1f

private val volumeHandler = Handler(Looper.getMainLooper())
private val volumeRunnable = object : Runnable {
override fun run() {
if (volume < MAX_VOLUME) {
mediaPlayer!!.setVolume(volume, volume)
volume += VOLUME_INCREASE_STEP
volumeHandler.postDelayed(this, VOLUME_INCREASE_INTERVAL)
}
}
}


private val alarmActionReciever = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
when (intent?.getStringExtra(ACTION_EXTRA_KEY)) {
Expand Down Expand Up @@ -138,6 +154,8 @@ class AlarmService : Service() {
player.setAudioAttributes(NotificationHelper.audioAttributes)
player.prepare()
player.start()

volumeHandler.post(volumeRunnable)
}

/**
Expand All @@ -147,6 +165,9 @@ class AlarmService : Service() {
if (!isPlaying) return
isPlaying = false

volumeHandler.removeCallbacks(volumeRunnable)
volume = 0.1f

// Stop audio playing
if (mediaPlayer != null) {
mediaPlayer?.stop()
Expand Down Expand Up @@ -224,6 +245,11 @@ class AlarmService : Service() {
const val ACTION_EXTRA_KEY = "action"
const val DISMISS_ACTION = "DISMISS"
const val SNOOZE_ACTION = "SNOOZE"

const val AUTO_SNOOZE_MINUTES = 10

private const val MAX_VOLUME: Float = 1.0f
private const val VOLUME_INCREASE_STEP: Float = 0.05f
private const val VOLUME_INCREASE_INTERVAL: Long = 1000
}
}

0 comments on commit d5d39ab

Please sign in to comment.