Merge pull request #181 from SimpleMobileTools/fix_vibration

Clock - Properly handle vibration toggle at timer reminder
This commit is contained in:
Tibor Kaputa 2020-03-14 14:54:47 +01:00 committed by GitHub
commit dc362e8949
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 7 deletions

View File

@ -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 }

View File

@ -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 {

View File

@ -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()
}

View File

@ -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"