After adding calendar event through content resolver it takes log time for it to sync with google calendar

Viewed 567

Through my app I am creating calendar events using ContentResolver. Events are being successfuly created and you can see them in system calendar app. The problems appear when the user is using Google Calendar app. Sometimes it takes few minutes for event to appear there. Also when the event at last appear in Google Calendar and user delete it there it is still visible in system calendar again for few minutes.

In my application I want to create intent to open newly created event in caledndar but because of that synchronization problem it is not being displayed since it is not yet visible. Also before creating new event I am checking if it already exist and again because of synchronization it appears that it wasn't deleted and I am not creating new one when I should be.

Here is how I am creating the events:

     fun addEventToCalendar(context: Context,
                           title: String,
                           description: String?,
                           startDateMillis: Long,
                           endDateInMillis: Long,
                           tag: String): Long? {

        val timeZone = TimeZone.getDefault()

        val values = ContentValues().apply {
            put(CalendarContract.Events.DTSTART, startDateMillis)
            put(CalendarContract.Events.DTEND, endDateInMillis)
            put(CalendarContract.Events.TITLE, title)
            description?.let { put(CalendarContract.Events.DESCRIPTION, description) }
            put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.id)
            put(CalendarContract.Events.CALENDAR_ID, getCalendarId(context))
            put(CalendarContract.Events.UID_2445, tag)
        }


        val uri = context.contentResolver.insert(CalendarContract.Events.CONTENT_URI, values)
        return uri?.lastPathSegment?.toLong()
    }

    fun checkIfEventAlreadyExist(context: Context, tag: String): Long? {
        val projection = arrayOf(CalendarContract.Events._ID, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND, CalendarContract.Events.UID_2445)

        val cursor = context.contentResolver.query(
                CalendarContract.Events.CONTENT_URI,
                projection,
                "$CalendarContract.Events.UID_2445 = ? and $CalendarContract.Events.CALENDAR_ID = ?",
                arrayOf(tag, getCalendarId(context).toString()),
                "$CalendarContract.Events._ID ASC")


        cursor?.let {
            cursor.moveToFirst()
            val eventId = if (cursor.count == 0) null else cursor.getLong(0)
            cursor.close()
            return eventId
        }

        return null
    }

Also I've noticed that when I add event normally through system calendar app or google calendar app then there is no problem. All the events are being displayed everywhere immediately and same goes for deletion.

How could I improve that google calendar synchronisation?

EDIT: During my research I've come accros suggestions to use ContentResolver.requestSync() after adding calendar event. It looked promissing but doesn't work... Am I doing something wrong here? Here is my code for that:

private fun forceSync(context: Context) {
        val extras = Bundle()
        extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true)
        extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
        val account = Account("example.email@gmail.com", "com.google")
        ContentResolver.requestSync(account, CalendarContract.AUTHORITY, extras)
    }

or:

private fun forceSync(context: Context) {
        val cr = context.contentResolver
        val values = ContentValues()
        values.put(CalendarContract.Calendars.SYNC_EVENTS, 1)
        values.put(CalendarContract.Calendars.VISIBLE, 1)

        cr.update(
                ContentUris.withAppendedId(CalendarContract.Calendars.CONTENT_URI,
                        3), values, null, null)
    }
0 Answers
Related