I am not able to view added event in calendar on some devices Android

23 Views Asked by At

I am working on project where I have to add add and manipulate event on google calendar, it is working fine in some device like android 13(Samsung) but when I add event in android 11 it is added I am getting Id of event added but when i tried to view addd event using intent it is giving message "The request event was not found" error message of android 13 also when i open google calendar it is not showing anything added I refer this documentation Documentation to add event

I followed all documentation as given in official docs, and also tried to search on inherent for this but i get nothing.

steps to reproduce the bug in Android 11 in which google calendar is default one

  1. I call addEventToGoogleCalender funtion from code in event contains all necessary information, which is working fine in Andorid 13 and some devices, but when i add event with same code and want to view with that id I am unable to view that event, gives toast message event not found, if event is not added than why i am getting event id(from Uri) return by insert. when I try to use that id it gives toast message event not found , refer links for detail information

Devices on which it is working Samsung A32 Os 13, not working on Nokia TA-1152 os 11

code used to add event

 suspend fun addEventToGoogleCalender(
        featuredEvent: FeaturedEvent?,
        onEventAdded: suspend () -> Unit
    ) {
        featuredEvent?.let { event ->
            val startMillis: Long = featuredEvent.startDate.toInstant().toEpochMilliseconds()
            val endMillis: Long = featuredEvent.endDate.toInstant().toEpochMilliseconds()

            getCalendarId(context = app)?.let { calID ->
                app.contentResolver?.let {
                    val eventValues = ContentValues().apply {
                        put(CalendarContract.Events.DTSTART, startMillis)
                        put(CalendarContract.Events.DTEND, endMillis)
                        put(CalendarContract.Events.TITLE, event.eventName)
                        put(CalendarContract.Events.DESCRIPTION, event.description)
                        put(CalendarContract.Events.CALENDAR_ID, calID)
                        put(CalendarContract.Events.EVENT_LOCATION, "US")
                        put(CalendarContract.Events.EVENT_TIMEZONE, "America/Los_Angeles")
                        put(CalendarContract.Events.HAS_ALARM, 1)
                    }
                    val insertedUri = it.insert(CalendarContract.Events.CONTENT_URI, eventValues)
                    val eventId = insertedUri?.lastPathSegment?.toLong()
                    eventId?.let {
                        queryAddedEventId(event.eventName, startMillis, endMillis)
                        onEventAdded()
                    }
                    Log.d(TAG, "addEventToGoogleCalender: $eventId")
                }
            }

            /* queryAddedEventId(app, event.eventName, startMillis, endMillis)?.let {
                 viewAddedEvent(it)
             } ?: run {

             }*/


            /*  val intent = Intent(Intent.ACTION_INSERT)
                  .setData(CalendarContract.Events.CONTENT_URI)
                  .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startMillis)
                  .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endMillis)
                  .putExtra(CalendarContract.Events.TITLE, featuredEvent.eventName)
                  .putExtra(CalendarContract.Events.DESCRIPTION, featuredEvent.description)
                  .putExtra(CalendarContract.Events.EVENT_LOCATION, "US")
                  .putExtra(CalendarContract.Events.AVAILABILITY, CalendarContract.Events.AVAILABILITY_BUSY)
  //                .putExtra(Intent.EXTRA_EMAIL, "[email protected],[email protected]")

              intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

              intent.resolveActivity(app.packageManager)?.let {
                  app.startActivity(intent)
              }*/
        }
    }

    private fun getCalendarId(context: Context): Long? {
        val projection = arrayOf(
            CalendarContract.Calendars._ID,
            CalendarContract.Calendars.CALENDAR_DISPLAY_NAME
        )

        var calCursor = context.contentResolver.query(
            CalendarContract.Calendars.CONTENT_URI,
            projection,
            CalendarContract.Calendars.VISIBLE + " = 1 AND " + CalendarContract.Calendars.IS_PRIMARY + "=1",
            null,
            CalendarContract.Calendars._ID + " ASC"
        )

        if (calCursor != null && calCursor.count <= 0) {
            calCursor = context.contentResolver.query(
                CalendarContract.Calendars.CONTENT_URI,
                projection,
                CalendarContract.Calendars.VISIBLE + " = 1",
                null,
                CalendarContract.Calendars._ID + " ASC"
            )
        }

        if (calCursor != null) {
            if (calCursor.moveToFirst()) {
                val calName: String
                val calID: String
                val nameCol = calCursor.getColumnIndex(projection[1])
                val idCol = calCursor.getColumnIndex(projection[0])

                calName = calCursor.getString(nameCol)
                calID = calCursor.getString(idCol)

                Log.d(TAG, "Calendar name = $calName Calendar ID = $calID")

                calCursor.close()
                return calID.toLong()
            }
        }
        return null
    }

    fun queryAddedEventId(
        titleText: String,
        dtStart: Long,
        dtEnd: Long
    ): Long? {
        var eventId: Long? = null
        if (app.hasPermissions(
                arrayOf(
                    Manifest.permission.READ_CALENDAR,
                    Manifest.permission.WRITE_CALENDAR
                )
            )
        ) {

            val INSTANCE_PROJECTION: Array<String> = arrayOf(
                CalendarContract.Instances.EVENT_ID, // 0
                CalendarContract.Instances.BEGIN, // 1
                CalendarContract.Instances.TITLE // 2
            )

// The indices for the projection array above.
            val PROJECTION_ID_INDEX: Int = 0
            val PROJECTION_BEGIN_INDEX: Int = 1
            val PROJECTION_TITLE_INDEX: Int = 2


// The ID of the recurring event whose instances you are searching
// for in the Instances table
            val selection: String = "${CalendarContract.Instances.TITLE} = ?"
            val selectionArgs: Array<String> = arrayOf(titleText)

// Construct the query with the desired date range.
            val builder: Uri.Builder = CalendarContract.Instances.CONTENT_URI.buildUpon()
            ContentUris.appendId(builder, dtStart)
            ContentUris.appendId(builder, dtEnd)

// Submit the query
            val cur = app.contentResolver.query(
                builder.build(),
                INSTANCE_PROJECTION,
                selection,
                selectionArgs, null
            )
            cur?.use {
                while (cur.moveToNext()) {
                    // Get the field values
                    eventId = cur.getLong(PROJECTION_ID_INDEX)
                    val beginVal: Long = cur.getLong(PROJECTION_BEGIN_INDEX)
                    val title: String = cur.getString(PROJECTION_TITLE_INDEX)
                }
            }
        }
        _eventIdMapppedToCalender.value = eventId
        return eventId
    }

    fun viewAddedEvent(id: Long) {
        val uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, id)
        val intent = Intent(Intent.ACTION_VIEW).setData(uri)
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
        intent.resolveActivity(app.packageManager)?.let {
            app.startActivity(intent)
        } ?: run {

        }
    }
0

There are 0 best solutions below