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

View file

@ -184,6 +184,15 @@ fun Context.hideNotification(id: Int) {
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.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)
}
fun Context.getHideAlarmPendingIntent(alarm: Alarm): PendingIntent {
val intent = Intent(this, HideAlarmReceiver::class.java)
intent.putExtra(ALARM_ID, alarm.id)
fun Context.getHideAlarmPendingIntent(alarm: Alarm, channelId: String): PendingIntent {
val intent = Intent(this, HideAlarmReceiver::class.java).apply {
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)
}
@ -384,11 +395,8 @@ fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm): No
if (soundUri != SILENT) {
grantReadUriPermission(soundUri)
}
val channelId = "simple_alarm_channel_$soundUri"
val label = if (alarm.label.isNotEmpty()) {
alarm.label
} else {
val channelId = "simple_alarm_channel_${soundUri}_${alarm.vibrate}"
val label = alarm.label.ifEmpty {
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)
.setContentTitle(label)
.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_cross_vector, getString(R.string.dismiss), dismissIntent)
.setDeleteIntent(dismissIntent)
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
if (soundUri != SILENT) {
builder.setSound(Uri.parse(soundUri), STREAM_ALARM)

View file

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