Skip to content

Commit 6da0ed0

Browse files
authored
feat: better fail to find event fallback 66 (#163)
* docs: plan * docs: test * feat: phase 1 and 2 * feat: draw the rest of the fucking owl lol * docs: update
1 parent 38dcccf commit 6da0ed0

11 files changed

Lines changed: 789 additions & 6 deletions

File tree

android/app/src/main/java/com/github/quarck/calnotify/calendar/CalendarIntents.kt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//
22
// Calendar Notifications Plus
33
// Copyright (C) 2016 Sergey Parshin (s.parshin.sc@gmail.com)
4+
// Copyright (C) 2025 William Harris (wharris+cnplus@upscalews.com)
45
//
56
// This program is free software; you can redistribute it and/or modify
67
// it under the terms of the GNU General Public License as published by
@@ -22,6 +23,7 @@ package com.github.quarck.calnotify.calendar
2223
import android.content.ContentUris
2324
import android.content.Context
2425
import android.content.Intent
26+
import android.net.Uri
2527
import android.provider.CalendarContract
2628
import com.github.quarck.calnotify.logs.DevLog
2729

@@ -69,4 +71,56 @@ object CalendarIntents {
6971

7072
fun viewCalendarEvent(context: Context, eventId: Long)
7173
= context.startActivity(intentForAction(Intent.ACTION_VIEW, eventId))
74+
75+
/**
76+
* Opens calendar at a specific time (fallback when event not found).
77+
*
78+
* Uses the calendar time URI format: content://com.android.calendar/time/{millis}
79+
* This opens the calendar app's day/week view centered on the specified time.
80+
*
81+
* @param context The context to start the activity from
82+
* @param timeMillis The time in milliseconds to open the calendar at
83+
*
84+
* @see <a href="https://github.com/williscool/CalendarNotification/issues/66">Issue #66</a>
85+
*/
86+
fun viewCalendarAtTime(context: Context, timeMillis: Long) {
87+
val uri = Uri.parse("content://com.android.calendar/time/$timeMillis")
88+
val intent = Intent(Intent.ACTION_VIEW).setData(uri)
89+
context.startActivity(intent)
90+
}
91+
92+
/**
93+
* Attempts to view an event in the calendar, with fallback to time-based view if event not found.
94+
*
95+
* This method first checks if the event exists in the system calendar. If found, it opens
96+
* the event directly. If not found (e.g., after phone restore, sync issues, or deletion),
97+
* it falls back to opening the calendar at the event's scheduled time.
98+
*
99+
* @param context The context to start the activity from
100+
* @param calendarProvider The calendar provider to check event existence
101+
* @param event The event to view
102+
* @return true if the event was found and opened directly, false if fallback was used
103+
*
104+
* @see <a href="https://github.com/williscool/CalendarNotification/issues/66">Issue #66</a>
105+
*/
106+
fun viewCalendarEventWithFallback(
107+
context: Context,
108+
calendarProvider: CalendarProviderInterface,
109+
event: EventAlertRecord
110+
): Boolean {
111+
// Check if event exists in system calendar
112+
val calendarEvent = calendarProvider.getEvent(context, event.eventId)
113+
114+
return if (calendarEvent != null) {
115+
// Event exists, open normally
116+
viewCalendarEvent(context, event)
117+
true
118+
} else {
119+
// Event not found, fallback to time-based view
120+
DevLog.info(LOG_TAG, "Event ${event.eventId} not found in calendar, " +
121+
"falling back to time view at ${event.instanceStartTime}")
122+
viewCalendarAtTime(context, event.instanceStartTime)
123+
false
124+
}
125+
}
72126
}

android/app/src/main/java/com/github/quarck/calnotify/ui/ViewEventActivityNoRecents.kt

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//
22
// Calendar Notifications Plus
33
// Copyright (C) 2016 Sergey Parshin (s.parshin.sc@gmail.com)
4+
// Copyright (C) 2025 William Harris (wharris+cnplus@upscalews.com)
45
//
56
// This program is free software; you can redistribute it and/or modify
67
// it under the terms of the GNU General Public License as published by
@@ -102,9 +103,14 @@ class ViewEventById(private val context: Context, internal var eventId: Long) :
102103
}
103104
}
104105

106+
/**
107+
* Runnable that opens an event in the calendar with fallback if event not found.
108+
* Uses CalendarProvider singleton to check event existence.
109+
*/
105110
class ViewEventByEvent(private val context: Context, internal var event: EventAlertRecord) : Runnable {
106111
override fun run() {
107-
CalendarIntents.viewCalendarEvent(context, event)
112+
// Use fallback method - if event not found, opens calendar at event time
113+
CalendarIntents.viewCalendarEventWithFallback(context, CalendarProvider, event)
108114
}
109115
}
110116

@@ -376,7 +382,7 @@ open class ViewEventActivityNoRecents : AppCompatActivity() {
376382

377383
} else {
378384
fab.setOnClickListener { _ ->
379-
CalendarIntents.viewCalendarEvent(this, event)
385+
openEventInCalendar(event)
380386
finish()
381387
}
382388
}
@@ -497,7 +503,7 @@ open class ViewEventActivityNoRecents : AppCompatActivity() {
497503
}
498504

499505
R.id.action_open_in_calendar -> {
500-
CalendarIntents.viewCalendarEvent(this, event)
506+
openEventInCalendar(event)
501507
finish()
502508
true
503509
}
@@ -555,7 +561,20 @@ open class ViewEventActivityNoRecents : AppCompatActivity() {
555561

556562
@Suppress("unused", "UNUSED_PARAMETER")
557563
fun OnButtonEventDetailsClick(v: View?) {
558-
CalendarIntents.viewCalendarEvent(this, event)
564+
openEventInCalendar(event)
565+
}
566+
567+
/**
568+
* Opens an event in the calendar with fallback to time-based view if event not found.
569+
* Shows a toast if the event was not found in the system calendar.
570+
*
571+
* @see <a href="https://github.com/williscool/CalendarNotification/issues/66">Issue #66</a>
572+
*/
573+
private fun openEventInCalendar(event: EventAlertRecord) {
574+
val found = CalendarIntents.viewCalendarEventWithFallback(this, calendarProvider, event)
575+
if (!found) {
576+
Toast.makeText(this, R.string.event_not_found_opening_calendar_at_time, Toast.LENGTH_LONG).show()
577+
}
559578
}
560579

561580
private fun snoozeEvent(snoozeDelay: Long) {
@@ -852,7 +871,7 @@ open class ViewEventActivityNoRecents : AppCompatActivity() {
852871
// Show
853872
if (Settings(this).viewAfterEdit) {
854873
handler.postDelayed({
855-
CalendarIntents.viewCalendarEvent(this, event)
874+
openEventInCalendar(event)
856875
finish()
857876
}, 100)
858877
}

android/app/src/main/res/values-de/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@
305305
<string name="event_was_created_reminder_at" formatted="false">Termin wurde erstellt, nächste Erinnerung: %s</string>
306306

307307
<string name="event_not_found">Termin nicht gefunden</string>
308+
<string name="event_not_found_opening_calendar_at_time">Termin nicht im Kalender gefunden - zeige Kalender zur Terminzeit</string>
308309
<string name="calendar_not_found">Kalender nicht gefunden</string>
309310
<string name="failed_to_update_event_details">Fehler beim Aktualisieren der Details</string>
310311
<string name="failed_to_move_event">Termin konnte nicht verschoben werden: </string>

android/app/src/main/res/values-pl/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@
304304
<string name="event_was_created_reminder_at" formatted="false">Dodano wydarzenie, następne przypomnienie: %s</string>
305305

306306
<string name="event_not_found">Nie znaleziono wydarzenia</string>
307+
<string name="event_not_found_opening_calendar_at_time">Nie znaleziono wydarzenia w kalendarzu - pokazuję kalendarz w czasie wydarzenia</string>
307308
<string name="calendar_not_found">Nie znaleziono kalendarza</string>
308309
<string name="failed_to_update_event_details">Aktualizacja szczegółów wydarzenia nie powiodła się</string>
309310
<string name="failed_to_move_event">Nie powiodło się przeniesienie wydarzenia: </string>

android/app/src/main/res/values-ru/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@
245245
<string name="event_was_created">Событие создано</string>
246246
<string name="event_was_created_reminder_at" formatted="false">Событие создано, сработает в: %s</string>
247247
<string name="event_not_found">Событие не найдено</string>
248+
<string name="event_not_found_opening_calendar_at_time">Событие не найдено в календаре - показываю календарь на время события</string>
248249
<string name="calendar_not_found">Календарь не найден</string>
249250
<string name="failed_to_update_event_details">Не удалось обновить детали события</string>
250251
<string name="failed_to_move_event">Не удалось переместить событие: </string>

android/app/src/main/res/values-uk/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
<string name="event_was_created">Подія створена</string>
245245
<string name="event_was_created_reminder_at" formatted="false">Подія створена, спрацює о: %s</string>
246246
<string name="event_not_found">Подія не знайдена</string>
247+
<string name="event_not_found_opening_calendar_at_time">Подія не знайдена в календарі - показую календар на час події</string>
247248
<string name="calendar_not_found">Календар не знайдений</string>
248249
<string name="failed_to_update_event_details">Не вдалося оновити деталі події</string>
249250
<string name="failed_to_move_event">Не вдалося перемістити подію: </string>

android/app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@
340340
<string name="event_was_created_reminder_at" formatted="false">Event was created, next reminder: %s</string>
341341

342342
<string name="event_not_found">Event not found</string>
343+
<string name="event_not_found_opening_calendar_at_time">Event not found in calendar - showing calendar at event time</string>
343344
<string name="calendar_not_found">Calendar not found</string>
344345
<string name="failed_to_update_event_details">Failed to update event details</string>
345346
<string name="failed_to_move_event">Failed to move event: </string>

0 commit comments

Comments
 (0)