From 1163a8bcd9792aea87ebd103732a40f53dcbbf34 Mon Sep 17 00:00:00 2001 From: Pavol Franek <> Date: Sat, 14 Mar 2020 14:41:13 +0100 Subject: [PATCH] Clock - Properly handle vibration toggle at timer reminder --- .../clock/extensions/Context.kt | 28 ++++++++++++++----- .../clock/fragments/TimerFragment.kt | 1 + .../simplemobiletools/clock/helpers/Config.kt | 5 ++++ .../clock/helpers/Constants.kt | 1 + 4 files changed, 28 insertions(+), 7 deletions(-) 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 574adec8..9d2b5a98 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt @@ -9,6 +9,7 @@ import android.content.Intent import android.content.SharedPreferences import android.media.AudioAttributes import android.media.AudioManager +import android.media.AudioManager.STREAM_ALARM import android.net.Uri import android.os.PowerManager import android.text.SpannableString @@ -235,24 +236,36 @@ fun Context.getTimerNotification(pendingIntent: PendingIntent, addDeleteIntent: grantReadUriPermission(soundUri) } - val channelId = "simple_timer_channel_$soundUri" + val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + val channelId = config.timerChannelId ?: "simple_timer_channel_${soundUri}_${System.currentTimeMillis()}" + config.timerChannelId = channelId + if (isOreoPlus()) { + try { + notificationManager.deleteNotificationChannel(channelId) + } catch (e: Exception) { + e.printStackTrace() + } + val audioAttributes = AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_ALARM) .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) - .setLegacyStreamType(AudioManager.STREAM_ALARM) - .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED) + .setLegacyStreamType(STREAM_ALARM) .build() - val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val name = getString(R.string.timer) val importance = NotificationManager.IMPORTANCE_HIGH NotificationChannel(channelId, name, importance).apply { setBypassDnd(true) enableLights(true) lightColor = getAdjustedPrimaryColor() - enableVibration(config.timerVibrate) setSound(Uri.parse(soundUri), audioAttributes) + + if (!config.timerVibrate) { + vibrationPattern = longArrayOf(0L) + } + + enableVibration(true) notificationManager.createNotificationChannel(this) } } @@ -263,8 +276,9 @@ fun Context.getTimerNotification(pendingIntent: PendingIntent, addDeleteIntent: .setContentText(getString(R.string.time_expired)) .setSmallIcon(R.drawable.ic_timer) .setContentIntent(pendingIntent) - .setPriority(Notification.PRIORITY_LOW) + .setPriority(NotificationCompat.PRIORITY_MAX) .setDefaults(Notification.DEFAULT_LIGHTS) + .setCategory(Notification.CATEGORY_EVENT) .setAutoCancel(true) .setSound(Uri.parse(soundUri), AudioManager.STREAM_ALARM) .setChannelId(channelId) @@ -274,7 +288,7 @@ fun Context.getTimerNotification(pendingIntent: PendingIntent, addDeleteIntent: builder.setDeleteIntent(reminderActivityIntent) } - builder.setVisibility(Notification.VISIBILITY_PUBLIC) + builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) if (config.timerVibrate) { val vibrateArray = LongArray(2) { 500 } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt index 2f8c24ae..ffd94a6c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt @@ -90,6 +90,7 @@ class TimerFragment : Fragment() { timer_vibrate_holder.setOnClickListener { timer_vibrate.toggle() config.timerVibrate = timer_vibrate.isChecked + config.timerChannelId = null } timer_sound.setOnClickListener { diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Config.kt index e243589e..595323c8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Config.kt @@ -70,4 +70,9 @@ class Config(context: Context) : BaseConfig(context) { gson.fromJson(lastAlarm, Alarm::class.java) } set(alarm) = prefs.edit().putString(ALARM_LAST_CONFIG, gson.toJson(alarm)).apply() + + var timerChannelId: String? + get() = prefs.getString(TIMER_CHANNEL_ID, null) + set(id) = prefs.edit().putString(TIMER_CHANNEL_ID, id).apply() + } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Constants.kt index fe6f409e..ef85a248 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Constants.kt @@ -13,6 +13,7 @@ const val TIMER_STATE = "timer_state" const val TIMER_VIBRATE = "timer_vibrate" const val TIMER_SOUND_URI = "timer_sound_uri" const val TIMER_SOUND_TITLE = "timer_sound_title" +const val TIMER_CHANNEL_ID = "timer_channel_id" const val TIMER_MAX_REMINDER_SECS = "timer_max_reminder_secs" const val ALARM_MAX_REMINDER_SECS = "alarm_max_reminder_secs" const val ALARM_LAST_CONFIG = "alarm_last_config"