mirror of
https://github.com/SimpleMobileTools/Simple-Clock.git
synced 2025-06-05 22:19:17 +02:00
Fix ReminderActivity: vibration and audio error
- There was no code to vibrate the device, this was added - If 'no sound' was selected as the alarm audio source, it would show an error toast saying "setDataSource failed" because the URL was "silent" Fixes #139, #189
This commit is contained in:
@ -1,12 +1,12 @@
|
|||||||
package com.simplemobiletools.clock.activities
|
package com.simplemobiletools.clock.activities
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.media.AudioManager
|
import android.media.AudioManager
|
||||||
import android.media.MediaPlayer
|
import android.media.MediaPlayer
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Bundle
|
import android.os.*
|
||||||
import android.os.Handler
|
|
||||||
import android.view.MotionEvent
|
import android.view.MotionEvent
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.view.animation.AnimationUtils
|
import android.view.animation.AnimationUtils
|
||||||
@ -17,6 +17,7 @@ import com.simplemobiletools.clock.helpers.getPassedSeconds
|
|||||||
import com.simplemobiletools.clock.models.Alarm
|
import com.simplemobiletools.clock.models.Alarm
|
||||||
import com.simplemobiletools.commons.extensions.*
|
import com.simplemobiletools.commons.extensions.*
|
||||||
import com.simplemobiletools.commons.helpers.MINUTE_SECONDS
|
import com.simplemobiletools.commons.helpers.MINUTE_SECONDS
|
||||||
|
import com.simplemobiletools.commons.helpers.SILENT
|
||||||
import com.simplemobiletools.commons.helpers.isOreoPlus
|
import com.simplemobiletools.commons.helpers.isOreoPlus
|
||||||
import kotlinx.android.synthetic.main.activity_reminder.*
|
import kotlinx.android.synthetic.main.activity_reminder.*
|
||||||
|
|
||||||
@ -30,6 +31,7 @@ class ReminderActivity : SimpleActivity() {
|
|||||||
private var didVibrate = false
|
private var didVibrate = false
|
||||||
private var alarm: Alarm? = null
|
private var alarm: Alarm? = null
|
||||||
private var mediaPlayer: MediaPlayer? = null
|
private var mediaPlayer: MediaPlayer? = null
|
||||||
|
private var vibrator: Vibrator? = null;
|
||||||
private var lastVolumeValue = 0.1f
|
private var lastVolumeValue = 0.1f
|
||||||
private var dragDownX = 0f
|
private var dragDownX = 0f
|
||||||
|
|
||||||
@ -64,7 +66,7 @@ class ReminderActivity : SimpleActivity() {
|
|||||||
}, maxDuration * 1000L)
|
}, maxDuration * 1000L)
|
||||||
|
|
||||||
setupButtons()
|
setupButtons()
|
||||||
setupAudio()
|
setupEffects()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupButtons() {
|
private fun setupButtons() {
|
||||||
@ -155,27 +157,40 @@ class ReminderActivity : SimpleActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupAudio() {
|
private fun setupEffects() {
|
||||||
if (!isAlarmReminder || !config.increaseVolumeGradually) {
|
if (!isAlarmReminder || !config.increaseVolumeGradually) {
|
||||||
lastVolumeValue = 1f
|
lastVolumeValue = 1f
|
||||||
}
|
}
|
||||||
|
|
||||||
val soundUri = Uri.parse(if (alarm != null) alarm!!.soundUri else config.timerSoundUri)
|
val doVibrate = if (alarm != null) alarm!!.vibrate else config.timerVibrate;
|
||||||
try {
|
if (doVibrate) {
|
||||||
mediaPlayer = MediaPlayer().apply {
|
val pattern = LongArray(2){ 500 };
|
||||||
setAudioStreamType(AudioManager.STREAM_ALARM)
|
vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator;
|
||||||
setDataSource(this@ReminderActivity, soundUri)
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
setVolume(lastVolumeValue, lastVolumeValue)
|
vibrator?.vibrate(VibrationEffect.createWaveform(pattern, 0))
|
||||||
isLooping = true
|
} else {
|
||||||
prepare()
|
vibrator?.vibrate(pattern, 0);
|
||||||
start()
|
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
|
||||||
showErrorToast(e)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.increaseVolumeGradually) {
|
val soundUri = if (alarm != null) alarm!!.soundUri else config.timerSoundUri;
|
||||||
scheduleVolumeIncrease()
|
if (soundUri != SILENT) {
|
||||||
|
try {
|
||||||
|
mediaPlayer = MediaPlayer().apply {
|
||||||
|
setAudioStreamType(AudioManager.STREAM_ALARM)
|
||||||
|
setDataSource(this@ReminderActivity, Uri.parse(soundUri))
|
||||||
|
setVolume(lastVolumeValue, lastVolumeValue)
|
||||||
|
isLooping = true
|
||||||
|
prepare()
|
||||||
|
start()
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
showErrorToast(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.increaseVolumeGradually) {
|
||||||
|
scheduleVolumeIncrease()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,17 +212,19 @@ class ReminderActivity : SimpleActivity() {
|
|||||||
increaseVolumeHandler.removeCallbacksAndMessages(null)
|
increaseVolumeHandler.removeCallbacksAndMessages(null)
|
||||||
maxReminderDurationHandler.removeCallbacksAndMessages(null)
|
maxReminderDurationHandler.removeCallbacksAndMessages(null)
|
||||||
swipeGuideFadeHandler.removeCallbacksAndMessages(null)
|
swipeGuideFadeHandler.removeCallbacksAndMessages(null)
|
||||||
destroyPlayer()
|
destroyEffects()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun destroyPlayer() {
|
private fun destroyEffects() {
|
||||||
mediaPlayer?.stop()
|
mediaPlayer?.stop()
|
||||||
mediaPlayer?.release()
|
mediaPlayer?.release()
|
||||||
mediaPlayer = null
|
mediaPlayer = null
|
||||||
|
vibrator?.cancel();
|
||||||
|
vibrator = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun snoozeAlarm() {
|
private fun snoozeAlarm() {
|
||||||
destroyPlayer()
|
destroyEffects()
|
||||||
if (config.useSameSnooze) {
|
if (config.useSameSnooze) {
|
||||||
setupAlarmClock(alarm!!, config.snoozeTime * MINUTE_SECONDS)
|
setupAlarmClock(alarm!!, config.snoozeTime * MINUTE_SECONDS)
|
||||||
finishActivity()
|
finishActivity()
|
||||||
@ -225,7 +242,7 @@ class ReminderActivity : SimpleActivity() {
|
|||||||
scheduleNextAlarm(alarm!!, false)
|
scheduleNextAlarm(alarm!!, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
destroyPlayer()
|
destroyEffects()
|
||||||
finish()
|
finish()
|
||||||
overridePendingTransition(0, 0)
|
overridePendingTransition(0, 0)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user