add gradual volume increasing at the ReminderActivity as appropriate

This commit is contained in:
tibbi 2018-07-07 23:46:00 +02:00
parent b4e4cc5cfc
commit d69ed5543c

View File

@ -16,11 +16,14 @@ import com.simplemobiletools.commons.helpers.MINUTE_SECONDS
import kotlinx.android.synthetic.main.activity_reminder.* import kotlinx.android.synthetic.main.activity_reminder.*
class ReminderActivity : SimpleActivity() { class ReminderActivity : SimpleActivity() {
private val hideNotificationHandler = Handler() private val INCREASE_VOLUME_DELAY = 1000L
private val increaseVolumeHandler = Handler()
private val maxReminderDurationHandler = 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 private var mediaPlayer: MediaPlayer? = null
private var lastVolumeValue = 0.1f
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -62,14 +65,31 @@ class ReminderActivity : SimpleActivity() {
finishActivity() finishActivity()
}, maxDuration * 1000L) }, maxDuration * 1000L)
if (!isAlarmReminder || !config.increaseVolumeGradually) {
lastVolumeValue = 1f
}
val soundUri = Uri.parse(if (alarm != null) alarm!!.soundUri else config.timerSoundUri) val soundUri = Uri.parse(if (alarm != null) alarm!!.soundUri else config.timerSoundUri)
mediaPlayer = MediaPlayer().apply { mediaPlayer = MediaPlayer().apply {
setAudioStreamType(AudioManager.STREAM_ALARM) setAudioStreamType(AudioManager.STREAM_ALARM)
setDataSource(this@ReminderActivity, soundUri) setDataSource(this@ReminderActivity, soundUri)
setVolume(lastVolumeValue, lastVolumeValue)
isLooping = true isLooping = true
prepare() prepare()
start() 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?) { override fun onNewIntent(intent: Intent?) {
@ -79,7 +99,7 @@ class ReminderActivity : SimpleActivity() {
override fun onStop() { override fun onStop() {
super.onStop() super.onStop()
hideNotificationHandler.removeCallbacksAndMessages(null) increaseVolumeHandler.removeCallbacksAndMessages(null)
maxReminderDurationHandler.removeCallbacksAndMessages(null) maxReminderDurationHandler.removeCallbacksAndMessages(null)
destroyPlayer() destroyPlayer()
} }