Skip to content

Commit

Permalink
fix: stopping the alarm not closing the alarm alert window (closes #270)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Apr 17, 2024
1 parent 1a12339 commit 6bf479b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 10 additions & 5 deletions app/src/main/java/com/bnyro/clock/util/services/AlarmService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6bf479b

Please sign in to comment.