diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderActionReceiver.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderActionReceiver.kt index e6fcb77e..c0bd7569 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderActionReceiver.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderActionReceiver.kt @@ -74,9 +74,6 @@ class ReminderActionReceiver : BroadcastReceiver() { */ const val ACTION_OPEN = "de.jeanlucmakiola.calendula.reminders.OPEN" - /** Not handled here either — the "Open in maps" button starts the maps app directly. */ - const val ACTION_MAP = "de.jeanlucmakiola.calendula.reminders.MAP" - private const val EXTRA_ALERT_KEY = "alert_key" private const val EXTRA_EVENT_ID = "event_id" private const val EXTRA_CALENDAR_ID = "calendar_id" @@ -118,7 +115,6 @@ class ReminderActionReceiver : BroadcastReceiver() { ACTION_DISMISS -> 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 72f5b0b0..a00e6a65 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 @@ -19,8 +19,9 @@ 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.calendula.domain.reminders.reminderMapQuery import de.jeanlucmakiola.floret.android.hasPermission +import de.jeanlucmakiola.floret.android.resolvableMapsSearchIntent import kotlinx.coroutines.flow.first import kotlinx.datetime.isoDayNumber import java.time.DayOfWeek @@ -191,14 +192,16 @@ class ReminderNotifier @Inject constructor( /** * 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. + * snooze or dismiss. Null without a place to look up (a bare meeting link + * isn't one) 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 + val query = reminderMapQuery(alert.location) ?: return null + val intent = context.resolvableMapsSearchIntent(query) ?: return null return PendingIntent.getActivity( context, - ReminderActionReceiver.requestCode(alert, ReminderActionReceiver.ACTION_MAP), + // An implicit activity intent: filterEquals already keeps it apart from the others. + /* requestCode = */ 0, intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE, ) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/domain/reminders/ReminderMapQuery.kt b/app/src/main/java/de/jeanlucmakiola/calendula/domain/reminders/ReminderMapQuery.kt new file mode 100644 index 00000000..0ec508a5 --- /dev/null +++ b/app/src/main/java/de/jeanlucmakiola/calendula/domain/reminders/ReminderMapQuery.kt @@ -0,0 +1,10 @@ +package de.jeanlucmakiola.calendula.domain.reminders + +private val webLinkRegex = Regex("""^(?:https?://|www\.)\S+$""", RegexOption.IGNORE_CASE) + +/** + * What a reminder's "Open in maps" button looks up for [location], or null + * when there is no place to find: blank, or nothing but a (meeting) link. + */ +fun reminderMapQuery(location: String?): String? = + location?.trim()?.takeIf { it.isNotEmpty() && !webLinkRegex.matches(it) } 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 deleted file mode 100644 index 339609d1..00000000 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/MapsIntents.kt +++ /dev/null @@ -1,19 +0,0 @@ -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 ca7fe3b7..39f0c1d9 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,8 +90,7 @@ 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.android.mapsSearchIntents import de.jeanlucmakiola.floret.components.IconInfoCard import de.jeanlucmakiola.floret.identity.LocalFloretDarkTheme import de.jeanlucmakiola.floret.identity.predictiveBack @@ -886,7 +885,6 @@ 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) { - if (runCatching { context.startActivity(geoSearchIntent(query)) }.isFailure) { - startOrSayNoApp(context, webMapsSearchIntent(query)) - } + val opened = mapsSearchIntents(query).any { runCatching { context.startActivity(it) }.isSuccess } + if (!opened) Toast.makeText(context, R.string.link_open_failed, Toast.LENGTH_SHORT).show() } 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 deleted file mode 100644 index d11edf00..00000000 --- a/app/src/test/java/de/jeanlucmakiola/calendula/data/reminders/ReminderActionRequestCodeTest.kt +++ /dev/null @@ -1,30 +0,0 @@ -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() - } -} diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/domain/reminders/ReminderMapQueryTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/domain/reminders/ReminderMapQueryTest.kt new file mode 100644 index 00000000..2e784eda --- /dev/null +++ b/app/src/test/java/de/jeanlucmakiola/calendula/domain/reminders/ReminderMapQueryTest.kt @@ -0,0 +1,30 @@ +package de.jeanlucmakiola.calendula.domain.reminders + +import com.google.common.truth.Truth.assertThat +import org.junit.jupiter.api.Test + +class ReminderMapQueryTest { + + @Test + fun `an address is looked up as written`() { + assertThat(reminderMapQuery(" Main St 1, Berlin ")).isEqualTo("Main St 1, Berlin") + } + + @Test + fun `no location means no lookup`() { + assertThat(reminderMapQuery(null)).isNull() + assertThat(reminderMapQuery(" ")).isNull() + } + + @Test + fun `a bare meeting link is not a place`() { + assertThat(reminderMapQuery("https://meet.google.com/abc-defg-hij")).isNull() + assertThat(reminderMapQuery("HTTP://example.com")).isNull() + assertThat(reminderMapQuery("www.example.com/room")).isNull() + } + + @Test + fun `a place with a link beside it is still looked up`() { + assertThat(reminderMapQuery("Room 4, https://example.com/map")).isEqualTo("Room 4, https://example.com/map") + } +} diff --git a/floret-kit b/floret-kit index a039ab5b..476514a3 160000 --- a/floret-kit +++ b/floret-kit @@ -1 +1 @@ -Subproject commit a039ab5bd470a548d754f2e8e610dc03bbcea40e +Subproject commit 476514a37feb52e275cdb90b7b794396b819221f