Skip to content

Commit

Permalink
Fixes cursor being disposed before it's returned to the caller in Get…
Browse files Browse the repository at this point in the history
…PlatformCalendar(). (#57)

fixes #48
  • Loading branch information
basvbeek authored Dec 20, 2024
1 parent ab6edb5 commit 8cfd365
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/Plugin.Maui.CalendarStore/CalendarStore.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public async Task<IEnumerable<Calendar>> GetCalendars()
/// <inheritdoc/>
public async Task<Calendar> GetCalendar(string calendarId)
{
var cursor = await GetPlatformCalendar(calendarId);
using var cursor = await GetPlatformCalendar(calendarId);

return ToCalendar(cursor, calendarColumns);
}
Expand Down Expand Up @@ -123,8 +123,9 @@ public async Task UpdateCalendar(string calendarId, string newName, Color? newCo

ContentValues calendarToUpdate = new();

// We just want to know a calendar with this ID exists
_ = await GetPlatformCalendar(calendarId);
// We just want to know a calendar with this ID exists,
// but we also need to dispose the returned cursor.
using var cursor = await GetPlatformCalendar(calendarId);

calendarToUpdate.Put(CalendarContract.Calendars.InterfaceConsts.Id, calendarId);

Expand Down Expand Up @@ -195,7 +196,7 @@ public async Task<IEnumerable<CalendarEvent>> GetEvents(
}

var sDate = startDate ?? DateTimeOffset.Now.Add(defaultStartTimeFromNow);
var eDate = endDate ?? sDate.Add(defaultEndTimeFromStartTime);
var eDate = endDate ?? sDate.Add(defaultEndTimeFromStartTime);

var calendarSpecificEvent =
$"{CalendarContract.Events.InterfaceConsts.Dtend} >= " +
Expand Down Expand Up @@ -260,8 +261,9 @@ public async Task<string> CreateEvent(string calendarId, string title, string de
{
await EnsureWriteCalendarPermission();

// We just want to know a calendar with this ID exists
_ = await GetPlatformCalendar(calendarId);
// We just want to know a calendar with this ID exists,
// but we also need to dispose the returned cursor.
using var cursor = await GetPlatformCalendar(calendarId);

ContentValues eventToInsert = new();
eventToInsert.Put(CalendarContract.Events.InterfaceConsts.Dtstart,
Expand Down Expand Up @@ -459,18 +461,26 @@ async Task<ICursor> GetPlatformCalendar(string calendarId)
$"{CalendarContract.Calendars.InterfaceConsts.Deleted} != 1 AND " +
$"{CalendarContract.Calendars.InterfaceConsts.Id} = {calendarId}";

using var cursor = platformContentResolver.Query(calendarsTableUri,
var cursor = platformContentResolver.Query(calendarsTableUri,
calendarColumns.ToArray(), queryConditions, null, null)
?? throw new CalendarStoreException("Error while querying calendars");

if (cursor.Count <= 0)
try
{
throw InvalidCalendar(calendarId);
}

cursor.MoveToNext();
if (cursor.Count <= 0)
{
throw InvalidCalendar(calendarId);
}

return cursor;
cursor.MoveToNext();

return cursor;
}
catch
{
cursor.Dispose();
throw;
}
}

static IEnumerable<Calendar> ToCalendars(ICursor cursor, List<string> projection)
Expand Down Expand Up @@ -506,7 +516,7 @@ static Calendar ToCalendar(ICursor cursor, List<string> projection)
// See: https://github.com/aosp-mirror/platform_packages_apps_calendar/blob/66d2a697bb910421d4958073be16a0237faf3531/src/com/android/calendar/Utils.kt#L730
static Android.Graphics.Color GetDisplayColorFromColor(Android.Graphics.Color color)
{
if (!OperatingSystem.IsAndroidVersionAtLeast(4, 1))
if (!OperatingSystem.IsAndroidVersionAtLeast(4, 1))
{
return color;
}
Expand All @@ -518,7 +528,7 @@ static Android.Graphics.Color GetDisplayColorFromColor(Android.Graphics.Color co

hsv[2] = hsv[2] * 0.8f;
return Android.Graphics.Color.HSVToColor(hsv);
}
}

IEnumerable<CalendarEvent> ToEvents(ICursor cur, List<string> projection)
{
Expand Down

0 comments on commit 8cfd365

Please sign in to comment.