Merge pull request #500 from fatihergin/fix/ISSUE-493-alarms-messed-up-global-alarm-volume

reset device alarm volume to initial after alarm ends
This commit is contained in:
Tibor Kaputa 2023-08-22 10:03:28 +02:00 committed by GitHub
commit a85ad5cef9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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