refactor(#464): maps intents in floret-kit, skip link-only locations

This commit is contained in:
makiolaj committed 2026-11-13 21:35:59 +01:00
1 parent 5394898af7
commit c03dba9f90
8 files changed
+52 -64

No files matched your search

@@ -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
@@ -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,
)
@@ -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) }
@@ -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 }
@@ -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()
}
@@ -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()
}
}
@@ -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")
}
}