feat: add Stop action to the ringing notification

Lets the user silence the ring early from the notification instead of waiting
out the full configured duration. ACTION_STOP stops playback, restores
volume/focus, and removes the foreground notification.
This commit is contained in:
avi 2026-08-07 13:44:26 -05:00
commit e83c7c3344
2 changed files with 18 additions and 0 deletions

View file

@ -43,6 +43,7 @@ class RingService : Service() {
private const val TAG = "RingService"
const val ACTION_RING = "com.example.lostmyphone.action.RING"
const val ACTION_STOP = "com.example.lostmyphone.action.STOP"
const val EXTRA_DND_GRANTED = "extra_dnd_granted"
private const val CHANNEL_ID = "ring_channel"
@ -82,6 +83,13 @@ class RingService : Service() {
return START_NOT_STICKY
}
if (intent.action == ACTION_STOP) {
// User tapped "Stop" on the ringing notification.
Log.i(TAG, "Stop requested. Stopping playback.")
restoreAndStop()
return START_NOT_STICKY
}
// ---- CRITICAL: DND permission gate --------------------------------
// The SMS receiver already communicates whether permission was granted,
// but we ALWAYS re-verify server-side here because it is the source of truth.
@ -319,6 +327,10 @@ class RingService : Service() {
this, 0, Intent(this, MainActivity::class.java),
PendingIntent.FLAG_IMMUTABLE
)
val stopIntent = PendingIntent.getService(
this, 2, Intent(this, RingService::class.java).setAction(ACTION_STOP),
PendingIntent.FLAG_IMMUTABLE
)
return NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(android.R.drawable.ic_media_play)
.setContentTitle(getString(R.string.notif_ringing_title))
@ -326,6 +338,11 @@ class RingService : Service() {
.setContentIntent(openIntent)
.setOngoing(true)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.addAction(
android.R.drawable.ic_menu_close_clear_cancel,
getString(R.string.notif_action_stop),
stopIntent
)
}
private fun buildDeniedNotification(): NotificationCompat.Builder {

View file

@ -7,6 +7,7 @@
<string name="notif_ringing_title">Lost My Phone — Ringing</string>
<string name="notif_ringing">Ringing for %1$d seconds (sound: %2$s)</string>
<string name="notif_action_stop">Stop</string>
<string name="notif_permission_denied_title">Action Required: DND Access</string>
<string name="dnd_permission_required">Permission Required: Please open settings and enable "Do Not Disturb Access" for "Lost My Phone" to ring your device.</string>