From d69ed5543c1c71c9f74128f7dd34dc4322541edb Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 7 Jul 2018 23:46:00 +0200 Subject: [PATCH] add gradual volume increasing at the ReminderActivity as appropriate --- .../clock/activities/ReminderActivity.kt | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt b/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt index 14b95317..e9be8bbe 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt @@ -16,11 +16,14 @@ import com.simplemobiletools.commons.helpers.MINUTE_SECONDS import kotlinx.android.synthetic.main.activity_reminder.* class ReminderActivity : SimpleActivity() { - private val hideNotificationHandler = Handler() + private val INCREASE_VOLUME_DELAY = 1000L + + private val increaseVolumeHandler = Handler() private val maxReminderDurationHandler = Handler() private var isAlarmReminder = false private var alarm: Alarm? = null private var mediaPlayer: MediaPlayer? = null + private var lastVolumeValue = 0.1f override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -62,14 +65,31 @@ class ReminderActivity : SimpleActivity() { finishActivity() }, maxDuration * 1000L) + if (!isAlarmReminder || !config.increaseVolumeGradually) { + lastVolumeValue = 1f + } + val soundUri = Uri.parse(if (alarm != null) alarm!!.soundUri else config.timerSoundUri) mediaPlayer = MediaPlayer().apply { setAudioStreamType(AudioManager.STREAM_ALARM) setDataSource(this@ReminderActivity, soundUri) + setVolume(lastVolumeValue, lastVolumeValue) isLooping = true prepare() start() } + + if (config.increaseVolumeGradually) { + scheduleVolumeIncrease() + } + } + + private fun scheduleVolumeIncrease() { + increaseVolumeHandler.postDelayed({ + lastVolumeValue = Math.min(lastVolumeValue + 0.1f, 1f) + mediaPlayer?.setVolume(lastVolumeValue, lastVolumeValue) + scheduleVolumeIncrease() + }, INCREASE_VOLUME_DELAY) } override fun onNewIntent(intent: Intent?) { @@ -79,7 +99,7 @@ class ReminderActivity : SimpleActivity() { override fun onStop() { super.onStop() - hideNotificationHandler.removeCallbacksAndMessages(null) + increaseVolumeHandler.removeCallbacksAndMessages(null) maxReminderDurationHandler.removeCallbacksAndMessages(null) destroyPlayer() }