diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b272d8e9..dada2d28 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -56,6 +56,16 @@ + + + + + + + + + + 2 ACTION_SHOW -> 3 ACTION_OPEN -> 4 + ACTION_MAP -> 5 else -> 0 } return (identity(alert, action).toInt() shl 3) + actionOffset diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderNotifier.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderNotifier.kt index 215554b4..72f5b0b0 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderNotifier.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderNotifier.kt @@ -5,6 +5,7 @@ import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent import android.content.Context +import android.content.Intent import android.os.Build import android.os.Bundle import android.util.Log @@ -18,6 +19,7 @@ import de.jeanlucmakiola.calendula.data.prefs.CalendarPrefs import de.jeanlucmakiola.calendula.data.prefs.SettingsPrefs import de.jeanlucmakiola.calendula.data.prefs.is24Hour import de.jeanlucmakiola.calendula.data.prefs.resolveFirstDay +import de.jeanlucmakiola.calendula.ui.common.resolvableMapsIntent import de.jeanlucmakiola.floret.android.hasPermission import kotlinx.coroutines.flow.first import kotlinx.datetime.isoDayNumber @@ -110,6 +112,11 @@ class ReminderNotifier @Inject constructor( context.getString(R.string.reminder_action_dismiss), actionIntent(alert, ReminderActionReceiver.ACTION_DISMISS), ) + .apply { + mapIntent(alert)?.let { + addAction(R.drawable.ic_notification_map, context.getString(R.string.event_detail_open_in_maps), it) + } + } .build() val manager = NotificationManagerCompat.from(context) cancelLegacy(alert) @@ -181,6 +188,22 @@ class ReminderNotifier @Inject constructor( PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE, ) + /** + * Opens [alert]'s location in maps like the detail screen does, started + * directly (no receiver trampoline). Leaves the notification up for a later + * snooze or dismiss. Null without a location or an app to open it. + */ + private fun mapIntent(alert: ReminderAlert): PendingIntent? { + val location = alert.location?.takeIf { it.isNotBlank() } ?: return null + val intent = context.resolvableMapsIntent(location) ?: return null + return PendingIntent.getActivity( + context, + ReminderActionReceiver.requestCode(alert, ReminderActionReceiver.ACTION_MAP), + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE, + ) + } + /** Channel creation is idempotent; re-running refreshes the localized name. */ private fun ensureChannel() { val channel = NotificationChannel( diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/MapsIntents.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/MapsIntents.kt new file mode 100644 index 00000000..339609d1 --- /dev/null +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/MapsIntents.kt @@ -0,0 +1,19 @@ +package de.jeanlucmakiola.calendula.ui.common + +import android.content.Context +import android.content.Intent +import android.net.Uri + +/** A `geo:` search for [query], answered by an installed maps app. */ +fun geoSearchIntent(query: String): Intent = + Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=${Uri.encode(query)}")) + +/** A web maps search for [query], the fallback when no app handles `geo:`. */ +fun webMapsSearchIntent(query: String): Intent = + Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com/maps/search/?api=1&query=${Uri.encode(query)}")) + .addCategory(Intent.CATEGORY_BROWSABLE) + +/** The first of [geoSearchIntent] and [webMapsSearchIntent] some installed app can open, or null. */ +fun Context.resolvableMapsIntent(query: String): Intent? = + listOf(geoSearchIntent(query), webMapsSearchIntent(query)) + .firstOrNull { it.resolveActivity(packageManager) != null } diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/detail/EventDetailScreen.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/detail/EventDetailScreen.kt index fc723aca..ca7fe3b7 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/detail/EventDetailScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/detail/EventDetailScreen.kt @@ -90,6 +90,8 @@ import de.jeanlucmakiola.calendula.domain.timeZoneOptionOf import de.jeanlucmakiola.calendula.domain.zoneDescriptor import de.jeanlucmakiola.calendula.domain.zoneOrNull import de.jeanlucmakiola.calendula.ui.common.orUnnamed +import de.jeanlucmakiola.calendula.ui.common.geoSearchIntent +import de.jeanlucmakiola.calendula.ui.common.webMapsSearchIntent import de.jeanlucmakiola.floret.components.IconInfoCard import de.jeanlucmakiola.floret.identity.LocalFloretDarkTheme import de.jeanlucmakiola.floret.identity.predictiveBack @@ -884,9 +886,7 @@ private fun startOrSayNoApp(context: Context, intent: Intent) { /** Open a maps intent for [query]; fall back to a web maps URL if no app handles geo:. */ private fun openInMaps(context: Context, query: String) { - val encoded = Uri.encode(query) - val geo = Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=$encoded")) - if (runCatching { context.startActivity(geo) }.isFailure) { - openWebLink(context, "https://www.google.com/maps/search/?api=1&query=$encoded") + if (runCatching { context.startActivity(geoSearchIntent(query)) }.isFailure) { + startOrSayNoApp(context, webMapsSearchIntent(query)) } } diff --git a/app/src/main/res/drawable/ic_notification_map.xml b/app/src/main/res/drawable/ic_notification_map.xml new file mode 100644 index 00000000..3963fe2c --- /dev/null +++ b/app/src/main/res/drawable/ic_notification_map.xml @@ -0,0 +1,12 @@ + + + + + diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/data/reminders/ReminderActionRequestCodeTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/data/reminders/ReminderActionRequestCodeTest.kt new file mode 100644 index 00000000..d11edf00 --- /dev/null +++ b/app/src/test/java/de/jeanlucmakiola/calendula/data/reminders/ReminderActionRequestCodeTest.kt @@ -0,0 +1,30 @@ +package de.jeanlucmakiola.calendula.data.reminders + +import com.google.common.truth.Truth.assertThat +import org.junit.jupiter.api.Test + +class ReminderActionRequestCodeTest { + + private val alert = ReminderAlert( + key = 123_456_789L, + eventId = 1L, + calendarId = 1L, + beginMillis = 1_800_000_000_000L, + endMillis = 1_800_003_600_000L, + title = "Dentist", + location = "Main St 1", + isAllDay = false, + ) + + @Test + fun `every notification action gets its own request code`() { + val codes = listOf( + ReminderActionReceiver.ACTION_SNOOZE, + ReminderActionReceiver.ACTION_DISMISS, + ReminderActionReceiver.ACTION_OPEN, + ReminderActionReceiver.ACTION_MAP, + ).map { ReminderActionReceiver.requestCode(alert, it) } + + assertThat(codes).containsNoDuplicates() + } +}