fix(alarm): improve firing and preferred station
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
|
||||
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
|
||||
|
||||
|
||||
@@ -72,6 +72,15 @@ class AlarmScheduler(private val context: Context) {
|
||||
NotificationManagerCompat.from(context).cancel(
|
||||
PluriWaveAlarmReceiver.notificationIdForAlarm(id)
|
||||
)
|
||||
NotificationManagerCompat.from(context).cancel(
|
||||
PluriWaveAlarmReceiver.fireNotificationIdForAlarm(id)
|
||||
)
|
||||
}
|
||||
|
||||
fun dismissFireNotification(id: String) {
|
||||
NotificationManagerCompat.from(context).cancel(
|
||||
PluriWaveAlarmReceiver.fireNotificationIdForAlarm(id)
|
||||
)
|
||||
}
|
||||
|
||||
fun canScheduleExactAlarms(): Boolean {
|
||||
|
||||
@@ -70,6 +70,15 @@ class MainActivity : AudioServiceActivity() {
|
||||
result.success(null)
|
||||
}
|
||||
}
|
||||
"dismissAlarmNotification" -> {
|
||||
val id = call.argument<String>("id")
|
||||
if (id == null) {
|
||||
result.error("INVALID_ALARM", "Missing alarm id", null)
|
||||
} else {
|
||||
alarmScheduler.dismissFireNotification(id)
|
||||
result.success(null)
|
||||
}
|
||||
}
|
||||
"diagnostics" -> {
|
||||
result.success(
|
||||
mapOf(
|
||||
|
||||
@@ -23,6 +23,7 @@ class PluriWaveAlarmReceiver : BroadcastReceiver() {
|
||||
putExtra(EXTRA_ALARM_TITLE, title)
|
||||
putExtra(EXTRA_ALARM_ACTION, ACTION_FIRE)
|
||||
}
|
||||
showFireNotification(context, alarmId, title, launch)
|
||||
context.startActivity(launch)
|
||||
}
|
||||
ACTION_PRE_NOTICE -> {
|
||||
@@ -41,6 +42,40 @@ class PluriWaveAlarmReceiver : BroadcastReceiver() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun showFireNotification(
|
||||
context: Context,
|
||||
alarmId: String,
|
||||
title: String,
|
||||
launch: Intent
|
||||
) {
|
||||
ensureFireChannel(context)
|
||||
val fullScreenIntent = PendingIntent.getActivity(
|
||||
context,
|
||||
requestCode(alarmId, 10),
|
||||
launch,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
val notification = NotificationCompat.Builder(context, FIRE_CHANNEL_ID)
|
||||
.setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
|
||||
.setContentTitle("Alarma PluriWave")
|
||||
.setContentText(title)
|
||||
.setCategory(NotificationCompat.CATEGORY_ALARM)
|
||||
.setPriority(NotificationCompat.PRIORITY_MAX)
|
||||
.setOngoing(true)
|
||||
.setAutoCancel(false)
|
||||
.setContentIntent(fullScreenIntent)
|
||||
.setFullScreenIntent(fullScreenIntent, true)
|
||||
.build()
|
||||
|
||||
try {
|
||||
NotificationManagerCompat.from(context).notify(
|
||||
fireNotificationIdForAlarm(alarmId),
|
||||
notification,
|
||||
)
|
||||
} catch (_: SecurityException) {
|
||||
}
|
||||
}
|
||||
|
||||
private fun showPreNoticeNotification(context: Context, alarmId: String, title: String) {
|
||||
ensureChannel(context)
|
||||
|
||||
@@ -82,6 +117,23 @@ class PluriWaveAlarmReceiver : BroadcastReceiver() {
|
||||
NotificationManagerCompat.from(context).notify(notificationIdForAlarm(alarmId), notification)
|
||||
}
|
||||
|
||||
private fun ensureFireChannel(context: Context) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
|
||||
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
val existing = manager.getNotificationChannel(FIRE_CHANNEL_ID)
|
||||
if (existing != null) return
|
||||
|
||||
val channel = NotificationChannel(
|
||||
FIRE_CHANNEL_ID,
|
||||
"Alarmas sonando",
|
||||
NotificationManager.IMPORTANCE_HIGH
|
||||
).apply {
|
||||
description = "Pantalla urgente cuando una alarma musical debe sonar"
|
||||
enableVibration(true)
|
||||
}
|
||||
manager.createNotificationChannel(channel)
|
||||
}
|
||||
|
||||
private fun ensureChannel(context: Context) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
|
||||
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
@@ -104,6 +156,7 @@ class PluriWaveAlarmReceiver : BroadcastReceiver() {
|
||||
|
||||
companion object {
|
||||
const val CHANNEL_ID = "pluriwave_alarm_pre_notice"
|
||||
const val FIRE_CHANNEL_ID = "pluriwave_alarm_fire"
|
||||
const val ACTION_FIRE = "es.freetimelab.pluriwave.alarm.FIRE"
|
||||
const val ACTION_PRE_NOTICE = "es.freetimelab.pluriwave.alarm.PRE_NOTICE"
|
||||
const val ACTION_SKIP_NEXT = "es.freetimelab.pluriwave.alarm.SKIP_NEXT"
|
||||
@@ -112,5 +165,6 @@ class PluriWaveAlarmReceiver : BroadcastReceiver() {
|
||||
const val EXTRA_ALARM_ACTION = "alarmAction"
|
||||
|
||||
fun notificationIdForAlarm(alarmId: String): Int = 53 * alarmId.hashCode() + 7
|
||||
fun fireNotificationIdForAlarm(alarmId: String): Int = 59 * alarmId.hashCode() + 9
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user