use mediaplayer at the ReminderActivity instead of a notification

This commit is contained in:
tibbi
2018-07-07 23:29:38 +02:00
parent d771ec5e75
commit b4e4cc5cfc

View File

@ -1,6 +1,9 @@
package com.simplemobiletools.clock.activities package com.simplemobiletools.clock.activities
import android.content.Intent import android.content.Intent
import android.media.AudioManager
import android.media.MediaPlayer
import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import com.simplemobiletools.clock.R import com.simplemobiletools.clock.R
@ -14,8 +17,10 @@ import kotlinx.android.synthetic.main.activity_reminder.*
class ReminderActivity : SimpleActivity() { class ReminderActivity : SimpleActivity() {
private val hideNotificationHandler = Handler() private val hideNotificationHandler = Handler()
private val maxReminderDurationHandler = Handler()
private var isAlarmReminder = false private var isAlarmReminder = false
private var alarm: Alarm? = null private var alarm: Alarm? = null
private var mediaPlayer: MediaPlayer? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -52,18 +57,19 @@ class ReminderActivity : SimpleActivity() {
snoozeClicked() snoozeClicked()
} }
Handler().postDelayed({ val maxDuration = if (isAlarmReminder) config.alarmMaxReminderSecs else config.timerMaxReminderSecs
if (isAlarmReminder) { maxReminderDurationHandler.postDelayed({
showAlarmNotification(alarm!!) finishActivity()
} else { }, maxDuration * 1000L)
showTimerNotification(true)
}
val maxDuration = if (isAlarmReminder) config.alarmMaxReminderSecs else config.timerMaxReminderSecs val soundUri = Uri.parse(if (alarm != null) alarm!!.soundUri else config.timerSoundUri)
hideNotificationHandler.postDelayed({ mediaPlayer = MediaPlayer().apply {
finish() setAudioStreamType(AudioManager.STREAM_ALARM)
}, maxDuration * 1000L) setDataSource(this@ReminderActivity, soundUri)
}, 1000L) isLooping = true
prepare()
start()
}
} }
override fun onNewIntent(intent: Intent?) { override fun onNewIntent(intent: Intent?) {
@ -73,12 +79,15 @@ class ReminderActivity : SimpleActivity() {
override fun onStop() { override fun onStop() {
super.onStop() super.onStop()
if (isAlarmReminder) {
hideNotification(alarm?.id ?: 0)
} else {
hideTimerNotification()
}
hideNotificationHandler.removeCallbacksAndMessages(null) hideNotificationHandler.removeCallbacksAndMessages(null)
maxReminderDurationHandler.removeCallbacksAndMessages(null)
destroyPlayer()
}
private fun destroyPlayer() {
mediaPlayer?.stop()
mediaPlayer?.release()
mediaPlayer = null
} }
private fun snoozeClicked() { private fun snoozeClicked() {
@ -95,6 +104,7 @@ class ReminderActivity : SimpleActivity() {
} }
private fun finishActivity() { private fun finishActivity() {
destroyPlayer()
finish() finish()
overridePendingTransition(0, 0) overridePendingTransition(0, 0)
} }