Compare commits

...
9 changed files with 159 additions and 6 deletions

No files matched your search

+11
View File
@@ -56,6 +56,17 @@
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent> </intent>
<!-- A reminder's "Open link" button only shows when something can open the link (#464). -->
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
</intent>
</queries> </queries>
<application <application
@@ -5,6 +5,8 @@ import android.app.NotificationChannel
import android.app.NotificationManager import android.app.NotificationManager
import android.app.PendingIntent import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.content.Intent
import androidx.core.net.toUri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
@@ -18,7 +20,10 @@ import de.jeanlucmakiola.calendula.data.prefs.CalendarPrefs
import de.jeanlucmakiola.calendula.data.prefs.SettingsPrefs import de.jeanlucmakiola.calendula.data.prefs.SettingsPrefs
import de.jeanlucmakiola.calendula.data.prefs.is24Hour import de.jeanlucmakiola.calendula.data.prefs.is24Hour
import de.jeanlucmakiola.calendula.data.prefs.resolveFirstDay import de.jeanlucmakiola.calendula.data.prefs.resolveFirstDay
import de.jeanlucmakiola.calendula.domain.reminders.reminderLink
import de.jeanlucmakiola.calendula.domain.reminders.reminderMapQuery
import de.jeanlucmakiola.floret.android.hasPermission import de.jeanlucmakiola.floret.android.hasPermission
import de.jeanlucmakiola.floret.android.resolvableMapsSearchIntent
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.datetime.isoDayNumber import kotlinx.datetime.isoDayNumber
import java.time.DayOfWeek import java.time.DayOfWeek
@@ -110,6 +115,14 @@ class ReminderNotifier @Inject constructor(
context.getString(R.string.reminder_action_dismiss), context.getString(R.string.reminder_action_dismiss),
actionIntent(alert, ReminderActionReceiver.ACTION_DISMISS), actionIntent(alert, ReminderActionReceiver.ACTION_DISMISS),
) )
.apply {
// Room for one more button: a meeting link beats the place, being the likelier next step.
linkIntent(alert)?.let {
addAction(R.drawable.ic_notification_link, context.getString(R.string.reminder_action_open_link), it)
} ?: mapIntent(alert)?.let {
addAction(R.drawable.ic_notification_map, context.getString(R.string.event_detail_open_in_maps), it)
}
}
.build() .build()
val manager = NotificationManagerCompat.from(context) val manager = NotificationManagerCompat.from(context)
cancelLegacy(alert) cancelLegacy(alert)
@@ -181,6 +194,42 @@ class ReminderNotifier @Inject constructor(
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
) )
/**
* Opens the first web link in [alert]'s location, e.g. a meeting, started
* directly like [mapIntent]. Null without a link or an app to open it.
*/
private fun linkIntent(alert: ReminderAlert): PendingIntent? {
val link = reminderLink(alert.location) ?: return null
val intent = Intent(Intent.ACTION_VIEW, link.toUri())
.addCategory(Intent.CATEGORY_BROWSABLE)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
if (intent.resolveActivity(context.packageManager) == null) return null
return PendingIntent.getActivity(
context,
/* requestCode = */ 0,
intent,
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 place to look up (a bare meeting link
* isn't one) or an app to open it.
*/
private fun mapIntent(alert: ReminderAlert): PendingIntent? {
val query = reminderMapQuery(alert.location) ?: return null
val intent = context.resolvableMapsSearchIntent(query) ?: return null
return PendingIntent.getActivity(
context,
// 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,
)
}
/** Channel creation is idempotent; re-running refreshes the localized name. */ /** Channel creation is idempotent; re-running refreshes the localized name. */
private fun ensureChannel() { private fun ensureChannel() {
val channel = NotificationChannel( val channel = NotificationChannel(
@@ -0,0 +1,18 @@
package de.jeanlucmakiola.calendula.domain.reminders
// Same link shape the detail screen linkifies (ui/detail/WebLinks.kt).
private val webLinkRegex = Regex("""https?://[^\s<>"]+""", RegexOption.IGNORE_CASE)
/**
* What a reminder's "Open in maps" button looks up for [location] — the same
* text the detail screen's location card opens in maps — or null when there is
* no place to find: blank, or nothing but web (meeting) links.
*/
fun reminderMapQuery(location: String?): String? {
val trimmed = location?.trim()?.takeIf { it.isNotEmpty() } ?: return null
return trimmed.takeIf { webLinkRegex.replace(it, "").any(Char::isLetterOrDigit) }
}
/** The first web (meeting) link in [location], for the reminder's "Open link" button; null without one. */
fun reminderLink(location: String?): String? =
location?.let { webLinkRegex.find(it)?.value?.trimEnd('.', ',', ';', ')', '>') }
@@ -90,6 +90,7 @@ import de.jeanlucmakiola.calendula.domain.timeZoneOptionOf
import de.jeanlucmakiola.calendula.domain.zoneDescriptor import de.jeanlucmakiola.calendula.domain.zoneDescriptor
import de.jeanlucmakiola.calendula.domain.zoneOrNull import de.jeanlucmakiola.calendula.domain.zoneOrNull
import de.jeanlucmakiola.calendula.ui.common.orUnnamed import de.jeanlucmakiola.calendula.ui.common.orUnnamed
import de.jeanlucmakiola.floret.android.mapsSearchIntents
import de.jeanlucmakiola.floret.components.IconInfoCard import de.jeanlucmakiola.floret.components.IconInfoCard
import de.jeanlucmakiola.floret.identity.LocalFloretDarkTheme import de.jeanlucmakiola.floret.identity.LocalFloretDarkTheme
import de.jeanlucmakiola.floret.identity.predictiveBack import de.jeanlucmakiola.floret.identity.predictiveBack
@@ -884,9 +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:. */ /** 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) { private fun openInMaps(context: Context, query: String) {
val encoded = Uri.encode(query) val opened = mapsSearchIntents(query).any { runCatching { context.startActivity(it) }.isSuccess }
val geo = Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=$encoded")) if (!opened) Toast.makeText(context, R.string.link_open_failed, Toast.LENGTH_SHORT).show()
if (runCatching { context.startActivity(geo) }.isFailure) {
openWebLink(context, "https://www.google.com/maps/search/?api=1&query=$encoded")
}
} }
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Monochrome notification-action mark: Material "link" glyph. -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M3.9,12c0,-1.71 1.39,-3.1 3.1,-3.1h4V7H7c-2.76,0 -5,2.24 -5,5s2.24,5 5,5h4v-1.9H7c-1.71,0 -3.1,-1.39 -3.1,-3.1zM8,13h8v-2H8v2zM17,7h-4v1.9h4c1.71,0 3.1,1.39 3.1,3.1s-1.39,3.1 -3.1,3.1h-4V17h4c2.76,0 5,-2.24 5,-5s-2.24,-5 -5,-5z" />
</vector>
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Monochrome notification-action mark: Material "place" glyph. -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M12,2C8.13,2 5,5.13 5,9c0,5.25 7,13 7,13s7,-7.75 7,-13c0,-3.87 -3.13,-7 -7,-7zM12,11.5c-1.38,0 -2.5,-1.12 -2.5,-2.5s1.12,-2.5 2.5,-2.5 2.5,1.12 2.5,2.5 -1.12,2.5 -2.5,2.5z" />
</vector>
+2
View File
@@ -231,6 +231,8 @@
<string name="event_detail_title">Title</string> <string name="event_detail_title">Title</string>
<string name="event_detail_location">Location</string> <string name="event_detail_location">Location</string>
<string name="event_detail_open_in_maps">Open in maps</string> <string name="event_detail_open_in_maps">Open in maps</string>
<!-- Reminder notification action opening the web (meeting) link in the event's location (#464) -->
<string name="reminder_action_open_link">Open link</string>
<string name="link_open_failed">No app can open this link</string> <string name="link_open_failed">No app can open this link</string>
<string name="event_detail_description">Description</string> <string name="event_detail_description">Description</string>
<string name="event_detail_attendees">Attendees</string> <string name="event_detail_attendees">Attendees</string>
@@ -0,0 +1,51 @@
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("<https://teams.microsoft.com/l/meetup-join/x>")).isNull()
assertThat(reminderMapQuery("https://a.example\nhttps://b.example")).isNull()
}
@Test
fun `dotted place names are still places`() {
assertThat(reminderMapQuery("Hauptstr. 12/3, Wien")).isEqualTo("Hauptstr. 12/3, Wien")
assertThat(reminderMapQuery("St.Gallen/Zentrum")).isEqualTo("St.Gallen/Zentrum")
}
@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")
}
@Test
fun `the first web link is the reminder's link`() {
assertThat(reminderLink("https://meet.google.com/abc-defg-hij")).isEqualTo("https://meet.google.com/abc-defg-hij")
assertThat(reminderLink("<https://teams.microsoft.com/l/meetup-join/x>")).isEqualTo("https://teams.microsoft.com/l/meetup-join/x")
assertThat(reminderLink("Room 4, https://example.com/map.")).isEqualTo("https://example.com/map")
assertThat(reminderLink("https://a.example\nhttps://b.example")).isEqualTo("https://a.example")
}
@Test
fun `no link without one`() {
assertThat(reminderLink(null)).isNull()
assertThat(reminderLink("Hauptstr. 12/3, Wien")).isNull()
}
}