feat(#464): open in maps action on reminder notifications
This commit is contained in:
1 parent
e9cf7d66ba
commit
5394898af7
7 files changed
+102
-4
No files matched your search
@@ -56,6 +56,16 @@
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent>
|
||||
<!-- The reminder notification's "Open in maps" button: a maps app, else a browser. -->
|
||||
<intent>
|
||||
<action android:name="android.intent.action.VIEW" />
|
||||
<data android:scheme="geo" />
|
||||
</intent>
|
||||
<intent>
|
||||
<action android:name="android.intent.action.VIEW" />
|
||||
<category android:name="android.intent.category.BROWSABLE" />
|
||||
<data android:scheme="https" />
|
||||
</intent>
|
||||
</queries>
|
||||
|
||||
<application
|
||||
|
||||
@@ -74,6 +74,9 @@ 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"
|
||||
@@ -115,6 +118,7 @@ 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
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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 }
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
+30
@@ -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()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user