mirror of
https://github.com/SimpleMobileTools/Simple-Clock.git
synced 2025-04-20 13:17:30 +02:00
Merge pull request #212 from rien/fix/alarm-vibrate
Fix alarm not vibrating when activiting the ReminderService
This commit is contained in:
commit
db6ceae5c5
@ -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,16 +157,28 @@ 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;
|
||||||
|
if (doVibrate) {
|
||||||
|
val pattern = LongArray(2){ 500 };
|
||||||
|
vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator;
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
vibrator?.vibrate(VibrationEffect.createWaveform(pattern, 0))
|
||||||
|
} else {
|
||||||
|
vibrator?.vibrate(pattern, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val soundUri = if (alarm != null) alarm!!.soundUri else config.timerSoundUri;
|
||||||
|
if (soundUri != SILENT) {
|
||||||
try {
|
try {
|
||||||
mediaPlayer = MediaPlayer().apply {
|
mediaPlayer = MediaPlayer().apply {
|
||||||
setAudioStreamType(AudioManager.STREAM_ALARM)
|
setAudioStreamType(AudioManager.STREAM_ALARM)
|
||||||
setDataSource(this@ReminderActivity, soundUri)
|
setDataSource(this@ReminderActivity, Uri.parse(soundUri))
|
||||||
setVolume(lastVolumeValue, lastVolumeValue)
|
setVolume(lastVolumeValue, lastVolumeValue)
|
||||||
isLooping = true
|
isLooping = true
|
||||||
prepare()
|
prepare()
|
||||||
@ -178,6 +192,7 @@ class ReminderActivity : SimpleActivity() {
|
|||||||
scheduleVolumeIncrease()
|
scheduleVolumeIncrease()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun scheduleVolumeIncrease() {
|
private fun scheduleVolumeIncrease() {
|
||||||
increaseVolumeHandler.postDelayed({
|
increaseVolumeHandler.postDelayed({
|
||||||
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -314,9 +314,7 @@ fun Context.getHideAlarmPendingIntent(alarm: Alarm): PendingIntent {
|
|||||||
@SuppressLint("NewApi")
|
@SuppressLint("NewApi")
|
||||||
fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm): Notification {
|
fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm): Notification {
|
||||||
var soundUri = alarm.soundUri
|
var soundUri = alarm.soundUri
|
||||||
if (soundUri == SILENT) {
|
if (soundUri != SILENT) {
|
||||||
soundUri = ""
|
|
||||||
} else {
|
|
||||||
grantReadUriPermission(soundUri)
|
grantReadUriPermission(soundUri)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -350,12 +348,15 @@ fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm): No
|
|||||||
.setPriority(Notification.PRIORITY_HIGH)
|
.setPriority(Notification.PRIORITY_HIGH)
|
||||||
.setDefaults(Notification.DEFAULT_LIGHTS)
|
.setDefaults(Notification.DEFAULT_LIGHTS)
|
||||||
.setAutoCancel(true)
|
.setAutoCancel(true)
|
||||||
.setSound(Uri.parse(soundUri), AudioManager.STREAM_ALARM)
|
|
||||||
.setChannelId(channelId)
|
.setChannelId(channelId)
|
||||||
.addAction(R.drawable.ic_snooze_vector, getString(R.string.snooze), getSnoozePendingIntent(alarm))
|
.addAction(R.drawable.ic_snooze_vector, getString(R.string.snooze), getSnoozePendingIntent(alarm))
|
||||||
.addAction(R.drawable.ic_cross_vector, getString(R.string.dismiss), getHideAlarmPendingIntent(alarm))
|
.addAction(R.drawable.ic_cross_vector, getString(R.string.dismiss), getHideAlarmPendingIntent(alarm))
|
||||||
|
|
||||||
builder.setVisibility(Notification.VISIBILITY_PUBLIC)
|
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
||||||
|
|
||||||
|
if (soundUri != SILENT) {
|
||||||
|
builder.setSound(Uri.parse(soundUri), AudioManager.STREAM_ALARM);
|
||||||
|
}
|
||||||
|
|
||||||
if (alarm.vibrate) {
|
if (alarm.vibrate) {
|
||||||
val vibrateArray = LongArray(2) { 500 }
|
val vibrateArray = LongArray(2) { 500 }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user