From b4e4cc5cfcb2fb3b20582927d17d18607e9e9d61 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 7 Jul 2018 23:29:38 +0200 Subject: [PATCH] use mediaplayer at the ReminderActivity instead of a notification --- .../clock/activities/ReminderActivity.kt | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 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 082c66cc..14b95317 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt @@ -1,6 +1,9 @@ package com.simplemobiletools.clock.activities import android.content.Intent +import android.media.AudioManager +import android.media.MediaPlayer +import android.net.Uri import android.os.Bundle import android.os.Handler import com.simplemobiletools.clock.R @@ -14,8 +17,10 @@ import kotlinx.android.synthetic.main.activity_reminder.* class ReminderActivity : SimpleActivity() { private val hideNotificationHandler = Handler() + private val maxReminderDurationHandler = Handler() private var isAlarmReminder = false private var alarm: Alarm? = null + private var mediaPlayer: MediaPlayer? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -52,18 +57,19 @@ class ReminderActivity : SimpleActivity() { snoozeClicked() } - Handler().postDelayed({ - if (isAlarmReminder) { - showAlarmNotification(alarm!!) - } else { - showTimerNotification(true) - } + val maxDuration = if (isAlarmReminder) config.alarmMaxReminderSecs else config.timerMaxReminderSecs + maxReminderDurationHandler.postDelayed({ + finishActivity() + }, maxDuration * 1000L) - val maxDuration = if (isAlarmReminder) config.alarmMaxReminderSecs else config.timerMaxReminderSecs - hideNotificationHandler.postDelayed({ - finish() - }, maxDuration * 1000L) - }, 1000L) + val soundUri = Uri.parse(if (alarm != null) alarm!!.soundUri else config.timerSoundUri) + mediaPlayer = MediaPlayer().apply { + setAudioStreamType(AudioManager.STREAM_ALARM) + setDataSource(this@ReminderActivity, soundUri) + isLooping = true + prepare() + start() + } } override fun onNewIntent(intent: Intent?) { @@ -73,12 +79,15 @@ class ReminderActivity : SimpleActivity() { override fun onStop() { super.onStop() - if (isAlarmReminder) { - hideNotification(alarm?.id ?: 0) - } else { - hideTimerNotification() - } hideNotificationHandler.removeCallbacksAndMessages(null) + maxReminderDurationHandler.removeCallbacksAndMessages(null) + destroyPlayer() + } + + private fun destroyPlayer() { + mediaPlayer?.stop() + mediaPlayer?.release() + mediaPlayer = null } private fun snoozeClicked() { @@ -95,6 +104,7 @@ class ReminderActivity : SimpleActivity() { } private fun finishActivity() { + destroyPlayer() finish() overridePendingTransition(0, 0) }