Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android improvements #36

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
274 changes: 185 additions & 89 deletions android/src/main/kotlin/sncf/connect/tech/eventide/CalendarImplem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,17 @@ class CalendarImplem(
put(CalendarContract.Calendars.CALENDAR_COLOR, color)
put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER)
put(CalendarContract.Calendars.OWNER_ACCOUNT, resolvedAccount.name)
put(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
put(CalendarContract.Calendars.VISIBLE, 1)
put(CalendarContract.Calendars.SYNC_EVENTS, 1)
}

val uri = calendarContentUri
.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, resolvedAccount.name)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, resolvedAccount.type)
.build()

val calendarUri = contentResolver.insert(uri, values)
val calendarUri = contentResolver.insert(calendarContentUri, values)
if (calendarUri != null) {
val calendarId = calendarUri.lastPathSegment?.toLong()
val calendarId = calendarUri.lastPathSegment
if (calendarId != null) {
val calendar = Calendar(
id = calendarId.toString(),
id = calendarId,
title = title,
color = color,
isWritable = true,
Expand All @@ -68,7 +62,7 @@ class CalendarImplem(
callback(Result.failure(
FlutterError(
code = "NOT_FOUND",
message = "Failed to retrieve calendar ID"
message = "Failed to retrieve calendar ID. It might not have been created"
)
))
}
Expand Down Expand Up @@ -119,22 +113,12 @@ class CalendarImplem(
CalendarContract.Calendars.ACCOUNT_TYPE
)

val (selection, selectionArgs) = Pair(onlyWritableCalendars, from).let { (onlyWritable, account) ->
if (onlyWritable && account != null) {
val selection = CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL + " >= ? AND " + CalendarContract.Calendars.ACCOUNT_NAME + " = ? AND " + CalendarContract.Calendars.ACCOUNT_TYPE + " = ?"
val selectionArgs = arrayOf(CalendarContract.Calendars.CAL_ACCESS_CONTRIBUTOR.toString(), account.name, account.type)
return@let Pair(selection, selectionArgs)
} else if (onlyWritable) {
val selection = CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL + " >= ?"
val selectionArgs = arrayOf(CalendarContract.Calendars.CAL_ACCESS_CONTRIBUTOR.toString())
return@let Pair(selection, selectionArgs)
} else if (account != null) {
val selection = CalendarContract.Calendars.ACCOUNT_NAME + " = ? AND " + CalendarContract.Calendars.ACCOUNT_TYPE + " = ?"
val selectionArgs = arrayOf(account.name, account.type)
return@let Pair(selection, selectionArgs)
} else {
return@let Pair(null, null)
}
var selection: String? = null
var selectionArgs: Array<String>? = null

from?.let { account ->
selection = CalendarContract.Calendars.ACCOUNT_NAME + " = ? AND " + CalendarContract.Calendars.ACCOUNT_TYPE + " = ?"
selectionArgs = arrayOf(account.name, account.type)
}

val cursor = contentResolver.query(calendarContentUri, projection, selection, selectionArgs, null)
Expand All @@ -149,18 +133,21 @@ class CalendarImplem(
val accountName = it.getString(it.getColumnIndexOrThrow(CalendarContract.Calendars.ACCOUNT_NAME))
val accountType = it.getString(it.getColumnIndexOrThrow(CalendarContract.Calendars.ACCOUNT_TYPE))

val calendar = Calendar(
id = id,
title = displayName,
color = color,
isWritable = accessLevel >= CalendarContract.Calendars.CAL_ACCESS_CONTRIBUTOR,
account = Account(
name = accountName,
type = accountType
val isWritable = accessLevel >= CalendarContract.Calendars.CAL_ACCESS_CONTRIBUTOR
if (!onlyWritableCalendars || isWritable) {
val calendar = Calendar(
id = id,
title = displayName,
color = color,
isWritable = isWritable,
account = Account(
name = accountName,
type = accountType
)
)
)

calendars.add(calendar)
calendars.add(calendar)
}
}
}

Expand Down Expand Up @@ -195,17 +182,34 @@ class CalendarImplem(
val selection = CalendarContract.Calendars._ID + " = ?"
val selectionArgs = arrayOf(calendarId)

val deleted = contentResolver.delete(calendarContentUri, selection, selectionArgs)
if (deleted > 0) {
callback(Result.success(Unit))
if (isCalendarWritable(calendarId)) {
val deleted = contentResolver.delete(calendarContentUri, selection, selectionArgs)
if (deleted > 0) {
callback(Result.success(Unit))
} else {
callback(
Result.failure(
FlutterError(
code = "GENERIC_ERROR",
message = "An error occurred during deletion"
)
)
)
}
} else {
callback(Result.failure(
FlutterError(
code = "NOT_FOUND",
message = "Failed to delete calendar"
callback(
Result.failure(
FlutterError(
code = "NOT_EDITABLE",
message = "Calendar is not writable"
)
)
))
)
}

} catch (e: FlutterError) {
callback(Result.failure(e))

} catch (e: Exception) {
callback(Result.failure(
FlutterError(
Expand Down Expand Up @@ -241,46 +245,65 @@ class CalendarImplem(
if (granted) {
CoroutineScope(Dispatchers.IO).launch {
try {
val eventValues = ContentValues().apply {
put(CalendarContract.Events.CALENDAR_ID, calendarId)
put(CalendarContract.Events.TITLE, title)
put(CalendarContract.Events.DESCRIPTION, description)
put(CalendarContract.Events.DTSTART, startDate)
put(CalendarContract.Events.DTEND, endDate)
put(CalendarContract.Events.EVENT_TIMEZONE, "UTC")
put(CalendarContract.Events.ALL_DAY, isAllDay)
}
if (isCalendarWritable(calendarId)) {
val eventValues = ContentValues().apply {
put(CalendarContract.Events.CALENDAR_ID, calendarId)
put(CalendarContract.Events.TITLE, title)
put(CalendarContract.Events.DESCRIPTION, description)
put(CalendarContract.Events.DTSTART, startDate)
put(CalendarContract.Events.DTEND, endDate)
put(CalendarContract.Events.EVENT_TIMEZONE, "UTC")
put(CalendarContract.Events.ALL_DAY, isAllDay)
}

val eventUri = contentResolver.insert(eventContentUri, eventValues)
if (eventUri != null) {
val eventId = eventUri.lastPathSegment?.toLong()
if (eventId != null) {
val event = Event(
id = eventId.toString(),
title = title,
startDate = startDate,
endDate = endDate,
calendarId = calendarId,
description = description,
isAllDay = isAllDay
)
callback(Result.success(event))
val eventUri = contentResolver.insert(eventContentUri, eventValues)
if (eventUri != null) {
val eventId = eventUri.lastPathSegment?.toLong()
if (eventId != null) {
val event = Event(
id = eventId.toString(),
title = title,
startDate = startDate,
endDate = endDate,
calendarId = calendarId,
description = description,
isAllDay = isAllDay
)
callback(Result.success(event))
} else {
callback(
Result.failure(
FlutterError(
code = "NOT_FOUND",
message = "Failed to retrieve event ID"
)
)
)
}
} else {
callback(Result.failure(
FlutterError(
code = "NOT_FOUND",
message = "Failed to retrieve event ID"
callback(
Result.failure(
FlutterError(
code = "GENERIC_ERROR",
message = "Failed to create event"
)
)
))
)
}
} else {
callback(Result.failure(
FlutterError(
code = "GENERIC_ERROR",
message = "Failed to create event"
callback(
Result.failure(
FlutterError(
code = "NOT_EDITABLE",
message = "Calendar is not writable"
)
)
))
)
}

} catch (e: FlutterError) {
callback(Result.failure(e))

} catch (e: Exception) {
callback(Result.failure(
FlutterError(
Expand Down Expand Up @@ -404,20 +427,38 @@ class CalendarImplem(
if (granted) {
CoroutineScope(Dispatchers.IO).launch {
try {
val selection = CalendarContract.Events._ID + " = ?"
val selectionArgs = arrayOf(eventId)

val deleted = contentResolver.delete(eventContentUri, selection, selectionArgs)
if (deleted > 0) {
callback(Result.success(Unit))
val calendarId = getCalendarId(eventId)
if (isCalendarWritable(calendarId)) {
val selection = CalendarContract.Events._ID + " = ?"
val selectionArgs = arrayOf(eventId)

val deleted = contentResolver.delete(eventContentUri, selection, selectionArgs)
if (deleted > 0) {
callback(Result.success(Unit))
} else {
callback(
Result.failure(
FlutterError(
code = "NOT_FOUND",
message = "Failed to delete event"
)
)
)
}
} else {
callback(Result.failure(
FlutterError(
code = "NOT_FOUND",
message = "Failed to delete event"
callback(
Result.failure(
FlutterError(
code = "NOT_EDITABLE",
message = "Calendar is not writable"
)
)
))
)
}

} catch (e: FlutterError) {
callback(Result.failure(e))

} catch (e: Exception) {
callback(Result.failure(
FlutterError(
Expand Down Expand Up @@ -514,6 +555,61 @@ class CalendarImplem(
}
}

private fun isCalendarWritable(
calendarId: String,
): Boolean {
val projection = arrayOf(
CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL
)
val selection = CalendarContract.Calendars._ID + " = ?"
val selectionArgs = arrayOf(calendarId)

val cursor = contentResolver.query(calendarContentUri, projection, selection, selectionArgs, null)
cursor?.use {
if (it.moveToNext()) {
val accessLevel = it.getInt(it.getColumnIndexOrThrow(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL))
return accessLevel >= CalendarContract.Calendars.CAL_ACCESS_CONTRIBUTOR
} else {
throw FlutterError(
code = "NOT_FOUND",
message = "Failed to retrieve calendar"
)
}
}

throw FlutterError(
code = "GENERIC_ERROR",
message = "An error occurred"
)
}

private fun getCalendarId(
eventId: String,
): String {
val projection = arrayOf(
CalendarContract.Events.CALENDAR_ID
)
val selection = CalendarContract.Events._ID + " = ?"
val selectionArgs = arrayOf(eventId)

val cursor = contentResolver.query(eventContentUri, projection, selection, selectionArgs, null)
cursor?.use {
if (it.moveToNext()) {
return it.getLong(it.getColumnIndexOrThrow(CalendarContract.Events.CALENDAR_ID)).toString()
} else {
throw FlutterError(
code = "NOT_FOUND",
message = "Failed to retrieve event"
)
}
}

throw FlutterError(
code = "GENERIC_ERROR",
message = "An error occurred"
)
}

private fun retrieveEvent(
eventId: String,
callback: (Result<Event>) -> Unit
Expand Down
Loading