diff --git a/app/src/main/java/com/example/lostmyphone/RingService.kt b/app/src/main/java/com/example/lostmyphone/RingService.kt index ac24488..d2fc68b 100644 --- a/app/src/main/java/com/example/lostmyphone/RingService.kt +++ b/app/src/main/java/com/example/lostmyphone/RingService.kt @@ -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 { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ca89bcd..45c4dd2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -7,6 +7,7 @@ Lost My Phone — Ringing Ringing for %1$d seconds (sound: %2$s) + Stop Action Required: DND Access Permission Required: Please open settings and enable "Do Not Disturb Access" for "Lost My Phone" to ring your device.