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 530a4a1d..8e09c79e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt @@ -20,10 +20,13 @@ import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.* class ReminderActivity : SimpleActivity() { - private val INCREASE_VOLUME_DELAY = 300L + companion object { + private const val MIN_ALARM_VOLUME_FOR_INCREASING_ALARMS = 1 + private const val INCREASE_VOLUME_DELAY = 300L + } - private val increaseVolumeHandler = Handler() - private val maxReminderDurationHandler = Handler() + private val increaseVolumeHandler = Handler(Looper.getMainLooper()) + private val maxReminderDurationHandler = Handler(Looper.getMainLooper()) private val swipeGuideFadeHandler = Handler() private val vibrationHandler = Handler(Looper.getMainLooper()) private var isAlarmReminder = false @@ -33,7 +36,7 @@ class ReminderActivity : SimpleActivity() { private var audioManager: AudioManager? = null private var mediaPlayer: MediaPlayer? = null private var vibrator: Vibrator? = null - private var lastVolumeValue = 0.1f + private var initialAlarmVolume: Int? = null private var dragDownX = 0f private val binding: ActivityReminderBinding by viewBinding(ActivityReminderBinding::inflate) @@ -166,10 +169,7 @@ class ReminderActivity : SimpleActivity() { private fun setupEffects() { audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager - val maxVol = audioManager?.getStreamMaxVolume(AudioManager.STREAM_ALARM)?.toFloat() ?: 10f - if (!isAlarmReminder || !config.increaseVolumeGradually) { - lastVolumeValue = maxVol - } + initialAlarmVolume = audioManager?.getStreamVolume(AudioManager.STREAM_ALARM) ?: 7 val doVibrate = alarm?.vibrate ?: config.timerVibrate if (doVibrate && isOreoPlus()) { @@ -191,26 +191,31 @@ class ReminderActivity : SimpleActivity() { mediaPlayer = MediaPlayer().apply { setAudioStreamType(AudioManager.STREAM_ALARM) setDataSource(this@ReminderActivity, Uri.parse(soundUri)) - setVolume(lastVolumeValue, lastVolumeValue) isLooping = true prepare() start() } if (config.increaseVolumeGradually) { - scheduleVolumeIncrease(maxVol) + scheduleVolumeIncrease(MIN_ALARM_VOLUME_FOR_INCREASING_ALARMS.toFloat(), initialAlarmVolume!!.toFloat(), 0) } } catch (e: Exception) { } } } - private fun scheduleVolumeIncrease(maxVolume: Float) { + private fun scheduleVolumeIncrease(lastVolume: Float, maxVolume: Float, delay: Long) { increaseVolumeHandler.postDelayed({ - lastVolumeValue = (lastVolumeValue + 0.1f).coerceAtMost(maxVolume) - audioManager?.setStreamVolume(AudioManager.STREAM_ALARM, lastVolumeValue.toInt(), 0) - scheduleVolumeIncrease(maxVolume) - }, INCREASE_VOLUME_DELAY) + val newLastVolume = (lastVolume + 0.1f).coerceAtMost(maxVolume) + audioManager?.setStreamVolume(AudioManager.STREAM_ALARM, newLastVolume.toInt(), 0) + scheduleVolumeIncrease(newLastVolume, maxVolume, INCREASE_VOLUME_DELAY) + }, delay) + } + + private fun resetVolumeToInitialValue() { + initialAlarmVolume?.apply { + audioManager?.setStreamVolume(AudioManager.STREAM_ALARM, this, 0) + } } override fun onNewIntent(intent: Intent?) { @@ -228,6 +233,10 @@ class ReminderActivity : SimpleActivity() { } private fun destroyEffects() { + if (config.increaseVolumeGradually) { + resetVolumeToInitialValue() + } + mediaPlayer?.stop() mediaPlayer?.release() mediaPlayer = null