From 9a23aa8e57c5822e1deacadf87a83bfdcae2a997 Mon Sep 17 00:00:00 2001 From: Rawlin C Date: Thu, 29 Jun 2023 23:21:45 +0530 Subject: [PATCH 1/3] Vibration fixes --- .../clock/activities/ReminderActivity.kt | 22 +++++++++---------- .../clock/extensions/Context.kt | 10 +++------ 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt b/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt index c3a8d324..9e297883 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt @@ -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.post { + vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator + vibrator?.vibrate(VibrationEffect.createWaveform(pattern, 0)) + } } 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() } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt index 403cdf03..5c004dec 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt @@ -384,11 +384,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) } @@ -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_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) From 028e9e7a804660bc8a173a58bd57e9603a34efd7 Mon Sep 17 00:00:00 2001 From: Rawlin C Date: Sat, 1 Jul 2023 15:32:08 +0530 Subject: [PATCH 2/3] Added post delay for vibration & deleting notification channels after their dismissal --- .../clock/activities/ReminderActivity.kt | 4 ++-- .../clock/extensions/Context.kt | 20 +++++++++++++++---- .../clock/receivers/HideAlarmReceiver.kt | 4 ++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt b/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt index 9e297883..f80351fa 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/activities/ReminderActivity.kt @@ -176,10 +176,10 @@ class ReminderActivity : SimpleActivity() { val doVibrate = alarm?.vibrate ?: config.timerVibrate if (doVibrate && isOreoPlus()) { val pattern = LongArray(2) { 500 } - vibrationHandler.post { + vibrationHandler.postDelayed({ vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator vibrator?.vibrate(VibrationEffect.createWaveform(pattern, 0)) - } + }, 500) } val soundUri = if (alarm != null) { diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt index 5c004dec..37f53a27 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt @@ -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) } @@ -397,6 +408,7 @@ fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm): No .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED) .build() + val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val importance = NotificationManager.IMPORTANCE_HIGH NotificationChannel(channelId, label, importance).apply { @@ -409,7 +421,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)) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/receivers/HideAlarmReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/clock/receivers/HideAlarmReceiver.kt index 4cbedea7..e363f2fe 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/receivers/HideAlarmReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/receivers/HideAlarmReceiver.kt @@ -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 { From 83ac361a31b4b6d7d4b67323a20afc1dbf57f059 Mon Sep 17 00:00:00 2001 From: Tibor Kaputa Date: Mon, 3 Jul 2023 12:51:36 +0200 Subject: [PATCH 3/3] removing an empty line --- .../kotlin/com/simplemobiletools/clock/extensions/Context.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt index 37f53a27..5c0ea429 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt @@ -408,7 +408,6 @@ fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm): No .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED) .build() - val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val importance = NotificationManager.IMPORTANCE_HIGH NotificationChannel(channelId, label, importance).apply {