Created
July 15, 2023 12:14
-
-
Save MostafaGad1911/bbeb5822e88a86b596c999b5d698b1fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object CalendarHelper { | |
const val ORGANIZER = "My App" | |
// Projection array. Creating indices for this array instead of doing | |
// dynamic lookups improves performance. | |
private val EVENT_PROJECTION: Array<String> = arrayOf( | |
Events.ORIGINAL_ID, // 0 | |
Events.TITLE, // 1 | |
Events.ALLOWED_REMINDERS, // 2 | |
Events.DESCRIPTION, // 3 | |
Events.DTSTART, // 4 | |
Events.DTEND, // 5 | |
Events.CALENDAR_ID, // 6 | |
) | |
fun addEventToCalendar( | |
context: Context, | |
installment: Int, | |
title: String?, | |
addInfo: String?, | |
date: Calendar, | |
) { | |
try { | |
if (FirebaseRemoteConfigManager.calendarEventsEnabled) { | |
val startDate: Long | |
val endDate: Long | |
val beginTime: Calendar = Calendar.getInstance() | |
beginTime.set(date[1], date[2], date[5], 9, 0) | |
startDate = beginTime.timeInMillis | |
val endTime: Calendar = Calendar.getInstance() | |
endTime.set(date[1], date[2], date[5], 22, 0) | |
endDate = endTime.timeInMillis | |
val cr = context.contentResolver | |
val event = ContentValues() | |
// id, We need to choose from our mobile foEr primary its 1 | |
event.put(Events.CALENDAR_ID, 1) | |
event.put(Events.TITLE, title) | |
event.put(Events.ORGANIZER, ORGANIZER) | |
event.put(Events.DESCRIPTION, addInfo) | |
event.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().id) | |
event.put(Events.DTSTART, startDate) | |
event.put(Events.DTEND, endDate) | |
event.put(Events.STATUS, 1) //CalendarContract.Events.AVAILABILITY_BUSY | |
event.put(Events.HAS_ALARM, 1) | |
event.put(Events.EVENT_COLOR, ContextCompat.getColor(context, R.color.colorPrimary)) | |
val eventUri = cr.insert(Events.CONTENT_URI, event) | |
val eventID = eventUri?.lastPathSegment?.toLong() ?: return | |
addAlarms(cr, eventID) | |
} | |
} catch (e: IOException) { | |
e.printStackTrace() | |
} | |
} | |
private fun addAlarms( | |
cr: ContentResolver, | |
eventId: Long, | |
) { | |
try { | |
val reminderValues = ContentValues() | |
reminderValues.put(CalendarContract.Reminders.EVENT_ID, eventId) | |
// Default value of the system. Minutes is a integer | |
reminderValues.put(CalendarContract.Reminders.MINUTES, 15) | |
// Alert Methods: Default(0), Alert(1), Email(2), SMS(3) | |
reminderValues.put(CalendarContract.Reminders.METHOD, 1) | |
cr.insert(CalendarContract.Reminders.CONTENT_URI, reminderValues) | |
} catch (e: IOException) { | |
e.printStackTrace() | |
} | |
} | |
fun eventsCount( | |
context: Context, | |
installment: Int = -1, | |
): Int = try { | |
// Run query | |
val uri: Uri = Events.CONTENT_URI | |
val selection = "${Events.ORGANIZER} LIKE ? AND ${Events.CALENDAR_ID} = 1" | |
val selectionArgs: Array<String> = emptyArray() | |
val cur = | |
context.contentResolver.query(uri, EVENT_PROJECTION, selection, selectionArgs, null) | |
val eventsCount = cur?.count ?: 0 | |
cur?.close() | |
eventsCount | |
} catch (e: IOException) { | |
e.printStackTrace() | |
0 | |
} | |
fun deleteEvents( | |
context: Context, | |
installment: Int = -1, | |
): Int { | |
try { | |
// Run query | |
val uri: Uri = Events.CONTENT_URI | |
val selection = "${Events.ORGANIZER} LIKE ? AND ${Events.CALENDAR_ID} = 1" | |
val selectionArgs: Array<String> = emptyArray() | |
return context.contentResolver.delete(uri, selection, selectionArgs) | |
} catch (e: IOException) { | |
e.printStackTrace() | |
return 0 | |
} | |
} | |
fun readAllCalendarEvents(context: Context) { | |
val uri: Uri = Events.CONTENT_URI | |
val selection = "${Events.ORGANIZER} LIKE ? AND ${Events.CALENDAR_ID} = 1" | |
val selectionArgs: Array<String> = emptyArray() | |
val cur = context.contentResolver.query(uri, EVENT_PROJECTION, selection, selectionArgs, null) | |
if (cur != null){ | |
while (cur.moveToNext()){ | |
AppLogger.log("DISPLAY_EVENTS", DatabaseUtils.dumpCursorToString(cur)) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment