forked from Blendman974/faceclaw
Calendar: Fix a timezone issue that could cause all-day events to appear on the preceding day
This commit is contained in:
1 parent
a6291cf937
commit
7ad9fe10ad
2 files changed
+93
-22
No files matched your search
@@ -10,6 +10,9 @@ import org.json.JSONArray
|
|||||||
import org.json.JSONException
|
import org.json.JSONException
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
|
|
||||||
|
import java.util.Calendar
|
||||||
|
import java.util.TimeZone
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads upcoming events from the Android Calendar provider for the Calendar
|
* Reads upcoming events from the Android Calendar provider for the Calendar
|
||||||
* app. Queries the Instances table (rather than Events) so that recurring
|
* app. Queries the Instances table (rather than Events) so that recurring
|
||||||
@@ -20,6 +23,7 @@ import org.json.JSONObject
|
|||||||
class FaceclawCalendarProvider private constructor() {
|
class FaceclawCalendarProvider private constructor() {
|
||||||
companion object {
|
companion object {
|
||||||
private const val TAG = "FaceclawCalendar"
|
private const val TAG = "FaceclawCalendar"
|
||||||
|
private const val DAY_MS = 24L * 60 * 60 * 1000
|
||||||
|
|
||||||
private val PROJECTION = arrayOf(
|
private val PROJECTION = arrayOf(
|
||||||
CalendarContract.Instances.EVENT_ID,
|
CalendarContract.Instances.EVENT_ID,
|
||||||
@@ -31,10 +35,23 @@ class FaceclawCalendarProvider private constructor() {
|
|||||||
CalendarContract.Instances.CALENDAR_DISPLAY_NAME,
|
CalendarContract.Instances.CALENDAR_DISPLAY_NAME,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
private class Event(
|
||||||
|
val id: Long,
|
||||||
|
val title: String,
|
||||||
|
val startMs: Long,
|
||||||
|
val endMs: Long,
|
||||||
|
val allDay: Boolean,
|
||||||
|
val location: String,
|
||||||
|
val calendarName: String,
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSON array of upcoming events starting from now through now+windowMs,
|
* JSON array of events that haven't ended yet and start before
|
||||||
* ordered by start time, capped at maxEvents. Each element carries id,
|
* now+windowMs, ordered by start time, capped at maxEvents. Each
|
||||||
* title, startMs, endMs, allDay, location, and calendarName.
|
* element carries id, title, startMs, endMs, allDay, location, and
|
||||||
|
* calendarName. All-day events are reported as local-midnight
|
||||||
|
* boundaries (matching EventKit on iOS), not the UTC-midnight
|
||||||
|
* boundaries the provider stores them as.
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun getUpcomingEventsJson(context: Context?, maxEvents: Int, windowMs: Long): String {
|
fun getUpcomingEventsJson(context: Context?, maxEvents: Int, windowMs: Long): String {
|
||||||
@@ -45,12 +62,15 @@ class FaceclawCalendarProvider private constructor() {
|
|||||||
val end = now + Math.max(0L, windowMs)
|
val end = now + Math.max(0L, windowMs)
|
||||||
val limit = Math.min(200, maxEvents)
|
val limit = Math.min(200, maxEvents)
|
||||||
|
|
||||||
|
// The provider matches all-day instances by their UTC-midnight
|
||||||
|
// bounds, which can be up to a day away from the local day they
|
||||||
|
// represent; widen the query and filter after converting.
|
||||||
val builder = CalendarContract.Instances.CONTENT_URI.buildUpon()
|
val builder = CalendarContract.Instances.CONTENT_URI.buildUpon()
|
||||||
ContentUris.appendId(builder, now)
|
ContentUris.appendId(builder, now - DAY_MS)
|
||||||
ContentUris.appendId(builder, end)
|
ContentUris.appendId(builder, end + DAY_MS)
|
||||||
val uri = builder.build()
|
val uri = builder.build()
|
||||||
|
|
||||||
val out = JSONArray()
|
val events = ArrayList<Event>()
|
||||||
var cursor: Cursor? = null
|
var cursor: Cursor? = null
|
||||||
try {
|
try {
|
||||||
cursor = context.contentResolver.query(
|
cursor = context.contentResolver.query(
|
||||||
@@ -60,12 +80,20 @@ class FaceclawCalendarProvider private constructor() {
|
|||||||
null,
|
null,
|
||||||
CalendarContract.Instances.BEGIN + " ASC")
|
CalendarContract.Instances.BEGIN + " ASC")
|
||||||
if (cursor != null) {
|
if (cursor != null) {
|
||||||
while (cursor.moveToNext() && out.length() < limit) {
|
val utc = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
|
||||||
try {
|
val local = Calendar.getInstance()
|
||||||
out.put(buildEventJson(cursor))
|
// Rows arrive in raw-BEGIN order, but converting all-day
|
||||||
} catch (e: JSONException) {
|
// events moves them by less than a day. Once `limit`
|
||||||
Log.w(TAG, "failed to serialize calendar event", e)
|
// events are kept, nothing starting after the latest of
|
||||||
}
|
// them can make the cut, and no row whose raw BEGIN is a
|
||||||
|
// day past that can start before it.
|
||||||
|
var cutoff = Long.MAX_VALUE
|
||||||
|
while (cursor.moveToNext()) {
|
||||||
|
if (cutoff != Long.MAX_VALUE && cursor.getLong(2) - DAY_MS > cutoff) break
|
||||||
|
val event = readEvent(cursor, utc, local)
|
||||||
|
if (event.endMs <= now || event.startMs > end || event.startMs > cutoff) continue
|
||||||
|
events.add(event)
|
||||||
|
if (events.size == limit) cutoff = events.maxOf { it.startMs }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: SecurityException) {
|
} catch (e: SecurityException) {
|
||||||
@@ -77,20 +105,62 @@ class FaceclawCalendarProvider private constructor() {
|
|||||||
} finally {
|
} finally {
|
||||||
cursor?.close()
|
cursor?.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
events.sortBy { it.startMs }
|
||||||
|
val out = JSONArray()
|
||||||
|
for (event in events.take(limit)) {
|
||||||
|
try {
|
||||||
|
out.put(buildEventJson(event))
|
||||||
|
} catch (e: JSONException) {
|
||||||
|
Log.w(TAG, "failed to serialize calendar event", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
return out.toString()
|
return out.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun readEvent(cursor: Cursor, utc: Calendar, local: Calendar): Event {
|
||||||
|
val allDay = cursor.getInt(4) != 0
|
||||||
|
var startMs = cursor.getLong(2)
|
||||||
|
var endMs = cursor.getLong(3)
|
||||||
|
if (allDay) {
|
||||||
|
startMs = utcMidnightToLocal(startMs, utc, local)
|
||||||
|
endMs = utcMidnightToLocal(endMs, utc, local)
|
||||||
|
}
|
||||||
|
return Event(
|
||||||
|
id = cursor.getLong(0),
|
||||||
|
title = if (cursor.isNull(1)) "" else cursor.getString(1),
|
||||||
|
startMs = startMs,
|
||||||
|
endMs = endMs,
|
||||||
|
allDay = allDay,
|
||||||
|
location = if (cursor.isNull(5)) "" else cursor.getString(5),
|
||||||
|
calendarName = if (cursor.isNull(6)) "" else cursor.getString(6),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* All-day events are stored as UTC midnights (CalendarContract
|
||||||
|
* requires EVENT_TIMEZONE=UTC for them); re-anchor that calendar
|
||||||
|
* date to midnight in the device's timezone.
|
||||||
|
*/
|
||||||
|
private fun utcMidnightToLocal(utcMs: Long, utc: Calendar, local: Calendar): Long {
|
||||||
|
utc.timeInMillis = utcMs
|
||||||
|
local.timeZone = TimeZone.getDefault()
|
||||||
|
local.clear()
|
||||||
|
local.set(utc.get(Calendar.YEAR), utc.get(Calendar.MONTH), utc.get(Calendar.DAY_OF_MONTH))
|
||||||
|
return local.timeInMillis
|
||||||
|
}
|
||||||
|
|
||||||
@Throws(JSONException::class)
|
@Throws(JSONException::class)
|
||||||
private fun buildEventJson(cursor: Cursor): JSONObject {
|
private fun buildEventJson(event: Event): JSONObject {
|
||||||
val event = JSONObject()
|
val json = JSONObject()
|
||||||
event.put("id", cursor.getLong(0))
|
json.put("id", event.id)
|
||||||
event.put("title", if (cursor.isNull(1)) "" else cursor.getString(1))
|
json.put("title", event.title)
|
||||||
event.put("startMs", cursor.getLong(2))
|
json.put("startMs", event.startMs)
|
||||||
event.put("endMs", cursor.getLong(3))
|
json.put("endMs", event.endMs)
|
||||||
event.put("allDay", cursor.getInt(4) != 0)
|
json.put("allDay", event.allDay)
|
||||||
event.put("location", if (cursor.isNull(5)) "" else cursor.getString(5))
|
json.put("location", event.location)
|
||||||
event.put("calendarName", if (cursor.isNull(6)) "" else cursor.getString(6))
|
json.put("calendarName", event.calendarName)
|
||||||
return event
|
return json
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -18,6 +18,7 @@
|
|||||||
* Navigate app: Add map-without-destination mode
|
* Navigate app: Add map-without-destination mode
|
||||||
* Nightscout app: Glanceboard widget still shows out-of-bounds values when out of space
|
* Nightscout app: Glanceboard widget still shows out-of-bounds values when out of space
|
||||||
* EvenHub: Fix login form being stuck open without a cancel button
|
* EvenHub: Fix login form being stuck open without a cancel button
|
||||||
|
* Calendar: Fix a timezone issue that could cause all-day events to appear on the preceding day
|
||||||
|
|
||||||
### iOS port:
|
### iOS port:
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user