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
2223import android.content.ContentUris
2324import android.content.Context
2425import android.content.Intent
26+ import android.net.Uri
2527import android.provider.CalendarContract
2628import 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}
0 commit comments