From 6bf479b7c97c154048f4b073ed3e167c29cccbda Mon Sep 17 00:00:00 2001 From: Suhas Dissanayake Date: Wed, 17 Apr 2024 11:23:01 +0530 Subject: [PATCH] fix: stopping the alarm not closing the alarm alert window (closes #270) --- .../screens/alarm/AlarmActivity.kt | 29 +++++++++++++++++++ .../bnyro/clock/util/services/AlarmService.kt | 15 ++++++---- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/bnyro/clock/presentation/screens/alarm/AlarmActivity.kt b/app/src/main/java/com/bnyro/clock/presentation/screens/alarm/AlarmActivity.kt index a29fb181..9b9df372 100644 --- a/app/src/main/java/com/bnyro/clock/presentation/screens/alarm/AlarmActivity.kt +++ b/app/src/main/java/com/bnyro/clock/presentation/screens/alarm/AlarmActivity.kt @@ -1,6 +1,10 @@ package com.bnyro.clock.presentation.screens.alarm +import android.annotation.SuppressLint +import android.content.BroadcastReceiver +import android.content.Context import android.content.Intent +import android.content.IntentFilter import android.os.Bundle import android.view.KeyEvent import android.view.Window @@ -11,6 +15,7 @@ import androidx.activity.enableEdgeToEdge import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue +import androidx.core.content.ContextCompat import com.bnyro.clock.data.database.DatabaseHolder import com.bnyro.clock.domain.model.Alarm import com.bnyro.clock.util.AlarmHelper @@ -19,10 +24,26 @@ import kotlinx.coroutines.runBlocking class AlarmActivity : ComponentActivity() { private var alarm by mutableStateOf(Alarm(0, 0)) + + private val closeAlertReciever = object : BroadcastReceiver() { + override fun onReceive(context: Context?, intent: Intent?) { + if (intent?.getStringExtra(ACTION_EXTRA_KEY) == CLOSE_ACTION) { + finish() + } + } + } + + @SuppressLint("UnspecifiedRegisterReceiverFlag") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) requestWindowFeature(Window.FEATURE_NO_TITLE) + ContextCompat.registerReceiver( + this, closeAlertReciever, IntentFilter( + ALARM_ALERT_CLOSE_ACTION + ), ContextCompat.RECEIVER_NOT_EXPORTED + ) + window.addFlags(windowFlags) enableEdgeToEdge() setContent { @@ -71,7 +92,15 @@ class AlarmActivity : ComponentActivity() { } } + override fun onDestroy() { + unregisterReceiver(closeAlertReciever) + super.onDestroy() + } + companion object { + const val ALARM_ALERT_CLOSE_ACTION = "com.bnyro.clock.ALARM_ALERT_CLOSE_ACTION" + const val ACTION_EXTRA_KEY = "action" + const val CLOSE_ACTION = "CLOSE" private const val windowFlags = WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON } diff --git a/app/src/main/java/com/bnyro/clock/util/services/AlarmService.kt b/app/src/main/java/com/bnyro/clock/util/services/AlarmService.kt index 4e5462a5..9cf17f1b 100644 --- a/app/src/main/java/com/bnyro/clock/util/services/AlarmService.kt +++ b/app/src/main/java/com/bnyro/clock/util/services/AlarmService.kt @@ -42,10 +42,10 @@ class AlarmService : Service() { private val alarmActionReciever = object : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { when (intent?.getStringExtra(ACTION_EXTRA_KEY)) { - DISMISS_ACTION -> onDestroy() + DISMISS_ACTION -> stopSelf() SNOOZE_ACTION -> { AlarmHelper.snooze(this@AlarmService, currentAlarm!!) - onDestroy() + stopSelf() } } } @@ -72,7 +72,6 @@ class AlarmService : Service() { override fun onDestroy() { stop() timer.cancel() - Log.d("Alarm Service", "Destroying service") unregisterReceiver(alarmActionReciever) ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE) super.onDestroy() @@ -91,9 +90,9 @@ class AlarmService : Service() { startForeground(notificationId, createNotification(this, alarm)) play(alarm) currentAlarm = alarm - timer.scheduleAtFixedRate(object : TimerTask() { + timer.schedule(object : TimerTask() { override fun run() { - onDestroy() + stopSelf() } }, AUTO_SNOOZE_MINUTES * 60 * 1000L, AUTO_SNOOZE_MINUTES * 60 * 1000L) return START_STICKY @@ -155,6 +154,12 @@ class AlarmService : Service() { // Stop vibrator vibrator?.cancel() NotificationManagerCompat.from(this).cancel(notificationId) + + val closeAlarmAlertIntent = Intent(AlarmActivity.ALARM_ALERT_CLOSE_ACTION).apply { + putExtra(AlarmActivity.ACTION_EXTRA_KEY, AlarmActivity.CLOSE_ACTION) + `package` = packageName + } + sendBroadcast(closeAlarmAlertIntent) } private fun createNotification(context: Context, alarm: Alarm): Notification {