preserve initial device alarm volume

This commit is contained in:
fatih ergin 2023-08-21 22:58:49 +03:00
parent a51886870e
commit c2d1a08d1d

View File

@ -20,7 +20,9 @@ import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.helpers.*
class ReminderActivity : SimpleActivity() { class ReminderActivity : SimpleActivity() {
private val INCREASE_VOLUME_DELAY = 300L companion object {
private const val INCREASE_VOLUME_DELAY = 300L
}
private val increaseVolumeHandler = Handler(Looper.getMainLooper()) private val increaseVolumeHandler = Handler(Looper.getMainLooper())
private val maxReminderDurationHandler = Handler(Looper.getMainLooper()) private val maxReminderDurationHandler = Handler(Looper.getMainLooper())
@ -33,6 +35,7 @@ class ReminderActivity : SimpleActivity() {
private var audioManager: AudioManager? = null private var audioManager: AudioManager? = null
private var mediaPlayer: MediaPlayer? = null private var mediaPlayer: MediaPlayer? = null
private var vibrator: Vibrator? = null private var vibrator: Vibrator? = null
private var initialAlarmVolume: Int? = null
private var lastVolumeValue = 0.1f private var lastVolumeValue = 0.1f
private var dragDownX = 0f private var dragDownX = 0f
private val binding: ActivityReminderBinding by viewBinding(ActivityReminderBinding::inflate) private val binding: ActivityReminderBinding by viewBinding(ActivityReminderBinding::inflate)
@ -198,6 +201,7 @@ class ReminderActivity : SimpleActivity() {
} }
if (config.increaseVolumeGradually) { if (config.increaseVolumeGradually) {
initialAlarmVolume = audioManager?.getStreamVolume(AudioManager.STREAM_ALARM)
scheduleVolumeIncrease(maxVol, 0) scheduleVolumeIncrease(maxVol, 0)
} }
} catch (e: Exception) { } catch (e: Exception) {
@ -213,6 +217,12 @@ class ReminderActivity : SimpleActivity() {
}, delay) }, delay)
} }
private fun resetVolumeToInitialValue() {
initialAlarmVolume?.apply {
audioManager?.setStreamVolume(AudioManager.STREAM_ALARM, this, 0)
}
}
override fun onNewIntent(intent: Intent?) { override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent) super.onNewIntent(intent)
finishActivity() finishActivity()
@ -228,6 +238,10 @@ class ReminderActivity : SimpleActivity() {
} }
private fun destroyEffects() { private fun destroyEffects() {
if (config.increaseVolumeGradually) {
resetVolumeToInitialValue()
}
mediaPlayer?.stop() mediaPlayer?.stop()
mediaPlayer?.release() mediaPlayer?.release()
mediaPlayer = null mediaPlayer = null