Merge pull request #467 from rawlin/vibration-fix

Vibration fixes
This commit is contained in:
Tibor Kaputa 2023-07-03 12:52:03 +02:00 committed by GitHub
commit d378dd4797
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 22 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.postDelayed({
vibrator?.vibrate(VibrationEffect.createWaveform(pattern, 0)) vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibrator?.vibrate(VibrationEffect.createWaveform(pattern, 0))
}, 500)
} }
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

@ -184,6 +184,15 @@ fun Context.hideNotification(id: Int) {
manager.cancel(id) manager.cancel(id)
} }
fun Context.deleteNotificationChannel(channelId: String) {
if (isOreoPlus()) {
try {
val manager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.deleteNotificationChannel(channelId)
} catch (_: Throwable) {}
}
}
fun Context.hideTimerNotification(timerId: Int) = hideNotification(timerId) fun Context.hideTimerNotification(timerId: Int) = hideNotification(timerId)
fun Context.updateWidgets() { fun Context.updateWidgets() {
@ -373,9 +382,11 @@ fun Context.getHideTimerPendingIntent(timerId: Int): PendingIntent {
return PendingIntent.getBroadcast(this, timerId, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE) return PendingIntent.getBroadcast(this, timerId, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
} }
fun Context.getHideAlarmPendingIntent(alarm: Alarm): PendingIntent { fun Context.getHideAlarmPendingIntent(alarm: Alarm, channelId: String): PendingIntent {
val intent = Intent(this, HideAlarmReceiver::class.java) val intent = Intent(this, HideAlarmReceiver::class.java).apply {
intent.putExtra(ALARM_ID, alarm.id) putExtra(ALARM_ID, alarm.id)
putExtra(ALARM_NOTIFICATION_CHANNEL_ID, channelId)
}
return PendingIntent.getBroadcast(this, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE) return PendingIntent.getBroadcast(this, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
} }
@ -384,11 +395,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)
} }
@ -412,7 +420,7 @@ fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm): No
} }
} }
val dismissIntent = getHideAlarmPendingIntent(alarm) val dismissIntent = getHideAlarmPendingIntent(alarm, channelId)
val builder = NotificationCompat.Builder(this) val builder = NotificationCompat.Builder(this)
.setContentTitle(label) .setContentTitle(label)
.setContentText(getFormattedTime(getPassedSeconds(), false, false)) .setContentText(getFormattedTime(getPassedSeconds(), false, false))
@ -425,8 +433,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)

View file

@ -4,14 +4,18 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import com.simplemobiletools.clock.extensions.dbHelper import com.simplemobiletools.clock.extensions.dbHelper
import com.simplemobiletools.clock.extensions.deleteNotificationChannel
import com.simplemobiletools.clock.extensions.hideNotification import com.simplemobiletools.clock.extensions.hideNotification
import com.simplemobiletools.clock.extensions.updateWidgets import com.simplemobiletools.clock.extensions.updateWidgets
import com.simplemobiletools.clock.helpers.ALARM_ID import com.simplemobiletools.clock.helpers.ALARM_ID
import com.simplemobiletools.clock.helpers.ALARM_NOTIFICATION_CHANNEL_ID
import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.ensureBackgroundThread
class HideAlarmReceiver : BroadcastReceiver() { class HideAlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
val id = intent.getIntExtra(ALARM_ID, -1) val id = intent.getIntExtra(ALARM_ID, -1)
val channelId = intent.getStringExtra(ALARM_NOTIFICATION_CHANNEL_ID)
channelId?.let { context.deleteNotificationChannel(channelId) }
context.hideNotification(id) context.hideNotification(id)
ensureBackgroundThread { ensureBackgroundThread {