mirror of
https://github.com/SimpleMobileTools/Simple-Clock.git
synced 2025-04-17 11:47:18 +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
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.media.AudioManager
|
||||
import android.media.MediaPlayer
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.*
|
||||
import android.view.MotionEvent
|
||||
import android.view.ViewGroup
|
||||
import android.view.animation.AnimationUtils
|
||||
@ -17,6 +17,7 @@ import com.simplemobiletools.clock.helpers.getPassedSeconds
|
||||
import com.simplemobiletools.clock.models.Alarm
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.commons.helpers.MINUTE_SECONDS
|
||||
import com.simplemobiletools.commons.helpers.SILENT
|
||||
import com.simplemobiletools.commons.helpers.isOreoPlus
|
||||
import kotlinx.android.synthetic.main.activity_reminder.*
|
||||
|
||||
@ -30,6 +31,7 @@ class ReminderActivity : SimpleActivity() {
|
||||
private var didVibrate = false
|
||||
private var alarm: Alarm? = null
|
||||
private var mediaPlayer: MediaPlayer? = null
|
||||
private var vibrator: Vibrator? = null;
|
||||
private var lastVolumeValue = 0.1f
|
||||
private var dragDownX = 0f
|
||||
|
||||
@ -64,7 +66,7 @@ class ReminderActivity : SimpleActivity() {
|
||||
}, maxDuration * 1000L)
|
||||
|
||||
setupButtons()
|
||||
setupAudio()
|
||||
setupEffects()
|
||||
}
|
||||
|
||||
private fun setupButtons() {
|
||||
@ -155,27 +157,40 @@ class ReminderActivity : SimpleActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupAudio() {
|
||||
private fun setupEffects() {
|
||||
if (!isAlarmReminder || !config.increaseVolumeGradually) {
|
||||
lastVolumeValue = 1f
|
||||
}
|
||||
|
||||
val soundUri = Uri.parse(if (alarm != null) alarm!!.soundUri else config.timerSoundUri)
|
||||
try {
|
||||
mediaPlayer = MediaPlayer().apply {
|
||||
setAudioStreamType(AudioManager.STREAM_ALARM)
|
||||
setDataSource(this@ReminderActivity, soundUri)
|
||||
setVolume(lastVolumeValue, lastVolumeValue)
|
||||
isLooping = true
|
||||
prepare()
|
||||
start()
|
||||
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);
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
showErrorToast(e)
|
||||
}
|
||||
|
||||
if (config.increaseVolumeGradually) {
|
||||
scheduleVolumeIncrease()
|
||||
val soundUri = if (alarm != null) alarm!!.soundUri else config.timerSoundUri;
|
||||
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)
|
||||
maxReminderDurationHandler.removeCallbacksAndMessages(null)
|
||||
swipeGuideFadeHandler.removeCallbacksAndMessages(null)
|
||||
destroyPlayer()
|
||||
destroyEffects()
|
||||
}
|
||||
|
||||
private fun destroyPlayer() {
|
||||
private fun destroyEffects() {
|
||||
mediaPlayer?.stop()
|
||||
mediaPlayer?.release()
|
||||
mediaPlayer = null
|
||||
vibrator?.cancel();
|
||||
vibrator = null;
|
||||
}
|
||||
|
||||
private fun snoozeAlarm() {
|
||||
destroyPlayer()
|
||||
destroyEffects()
|
||||
if (config.useSameSnooze) {
|
||||
setupAlarmClock(alarm!!, config.snoozeTime * MINUTE_SECONDS)
|
||||
finishActivity()
|
||||
@ -225,7 +242,7 @@ class ReminderActivity : SimpleActivity() {
|
||||
scheduleNextAlarm(alarm!!, false)
|
||||
}
|
||||
|
||||
destroyPlayer()
|
||||
destroyEffects()
|
||||
finish()
|
||||
overridePendingTransition(0, 0)
|
||||
}
|
||||
|
@ -314,9 +314,7 @@ fun Context.getHideAlarmPendingIntent(alarm: Alarm): PendingIntent {
|
||||
@SuppressLint("NewApi")
|
||||
fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm): Notification {
|
||||
var soundUri = alarm.soundUri
|
||||
if (soundUri == SILENT) {
|
||||
soundUri = ""
|
||||
} else {
|
||||
if (soundUri != SILENT) {
|
||||
grantReadUriPermission(soundUri)
|
||||
}
|
||||
|
||||
@ -350,12 +348,15 @@ fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm): No
|
||||
.setPriority(Notification.PRIORITY_HIGH)
|
||||
.setDefaults(Notification.DEFAULT_LIGHTS)
|
||||
.setAutoCancel(true)
|
||||
.setSound(Uri.parse(soundUri), AudioManager.STREAM_ALARM)
|
||||
.setChannelId(channelId)
|
||||
.addAction(R.drawable.ic_snooze_vector, getString(R.string.snooze), getSnoozePendingIntent(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) {
|
||||
val vibrateArray = LongArray(2) { 500 }
|
||||
|
Loading…
x
Reference in New Issue
Block a user