Vibration fixes

This commit is contained in:
Rawlin C 2023-06-29 23:21:45 +05:30
parent f3402b5a61
commit 9a23aa8e57
2 changed files with 14 additions and 18 deletions

View File

@ -7,10 +7,7 @@ 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.os.VibrationEffect
import android.os.Vibrator
import android.view.MotionEvent import android.view.MotionEvent
import android.view.ViewGroup import android.view.ViewGroup
import android.view.WindowManager import android.view.WindowManager
@ -29,11 +26,11 @@ import kotlinx.android.synthetic.main.activity_reminder.*
class ReminderActivity : SimpleActivity() { class ReminderActivity : SimpleActivity() {
private val INCREASE_VOLUME_DELAY = 300L private val INCREASE_VOLUME_DELAY = 300L
private val MAX_VOL = 10f // max volume set to level 10
private val increaseVolumeHandler = Handler() private val increaseVolumeHandler = Handler()
private val maxReminderDurationHandler = Handler() private val maxReminderDurationHandler = Handler()
private val swipeGuideFadeHandler = Handler() private val swipeGuideFadeHandler = Handler()
private val vibrationHandler = Handler(Looper.getMainLooper())
private var isAlarmReminder = false private var isAlarmReminder = false
private var didVibrate = false private var didVibrate = false
private var wasAlarmSnoozed = false private var wasAlarmSnoozed = false
@ -171,16 +168,18 @@ class ReminderActivity : SimpleActivity() {
private fun setupEffects() { private fun setupEffects() {
audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
val maxVol = audioManager?.getStreamMaxVolume(AudioManager.STREAM_ALARM)?.toFloat() ?: 10f
if (!isAlarmReminder || !config.increaseVolumeGradually) { if (!isAlarmReminder || !config.increaseVolumeGradually) {
lastVolumeValue = MAX_VOL lastVolumeValue = maxVol
} }
val doVibrate = if (alarm != null) alarm!!.vibrate else config.timerVibrate val doVibrate = alarm?.vibrate ?: config.timerVibrate
if (doVibrate && isOreoPlus()) { if (doVibrate && isOreoPlus()) {
val pattern = LongArray(2) { 500 } val pattern = LongArray(2) { 500 }
vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator vibrationHandler.post {
vibrator?.vibrate(VibrationEffect.createWaveform(pattern, 0)) vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibrator?.vibrate(VibrationEffect.createWaveform(pattern, 0))
}
} }
val soundUri = if (alarm != null) { val soundUri = if (alarm != null) {
@ -201,7 +200,7 @@ class ReminderActivity : SimpleActivity() {
} }
if (config.increaseVolumeGradually) { if (config.increaseVolumeGradually) {
scheduleVolumeIncrease(MAX_VOL) scheduleVolumeIncrease(maxVol)
} }
} catch (e: Exception) { } catch (e: Exception) {
} }
@ -226,6 +225,7 @@ class ReminderActivity : SimpleActivity() {
increaseVolumeHandler.removeCallbacksAndMessages(null) increaseVolumeHandler.removeCallbacksAndMessages(null)
maxReminderDurationHandler.removeCallbacksAndMessages(null) maxReminderDurationHandler.removeCallbacksAndMessages(null)
swipeGuideFadeHandler.removeCallbacksAndMessages(null) swipeGuideFadeHandler.removeCallbacksAndMessages(null)
vibrationHandler.removeCallbacksAndMessages(null)
destroyEffects() destroyEffects()
} }

View File

@ -384,11 +384,8 @@ fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm): No
if (soundUri != SILENT) { if (soundUri != SILENT) {
grantReadUriPermission(soundUri) grantReadUriPermission(soundUri)
} }
val channelId = "simple_alarm_channel_${soundUri}_${alarm.vibrate}"
val channelId = "simple_alarm_channel_$soundUri" val label = alarm.label.ifEmpty {
val label = if (alarm.label.isNotEmpty()) {
alarm.label
} else {
getString(R.string.alarm) getString(R.string.alarm)
} }
@ -425,8 +422,7 @@ fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm): No
.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), dismissIntent) .addAction(R.drawable.ic_cross_vector, getString(R.string.dismiss), dismissIntent)
.setDeleteIntent(dismissIntent) .setDeleteIntent(dismissIntent)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
if (soundUri != SILENT) { if (soundUri != SILENT) {
builder.setSound(Uri.parse(soundUri), STREAM_ALARM) builder.setSound(Uri.parse(soundUri), STREAM_ALARM)