1
0
mirror of https://github.com/etesync/android synced 2024-12-24 23:48:35 +00:00

Email invitation: move the code to handle the invitation generation to a different file.

This commit is contained in:
Tom Hacohen 2019-01-08 18:34:41 +00:00
parent 0e8dc1d266
commit 7b0cdff9dc
2 changed files with 122 additions and 94 deletions

View File

@ -26,6 +26,7 @@ import com.etesync.syncadapter.model.CollectionInfo
import com.etesync.syncadapter.model.SyncEntry import com.etesync.syncadapter.model.SyncEntry
import com.etesync.syncadapter.resource.LocalCalendar import com.etesync.syncadapter.resource.LocalCalendar
import com.etesync.syncadapter.resource.LocalEvent import com.etesync.syncadapter.resource.LocalEvent
import com.etesync.syncadapter.utils.EventEmailInvitation
import net.fortuna.ical4j.model.property.Attendee import net.fortuna.ical4j.model.property.Attendee
import okhttp3.HttpUrl import okhttp3.HttpUrl
import org.acra.attachment.AcraContentProvider import org.acra.attachment.AcraContentProvider
@ -130,28 +131,9 @@ constructor(context: Context, account: Account, settings: AccountSettings, extra
} }
private fun createInviteAttendeesNotification(event: Event, icsContent: String) { private fun createInviteAttendeesNotification(event: Event, icsContent: String) {
val intent = EventEmailInvitation(context, account).createIntent(event, icsContent)
if (intent != null) {
val notificationHelper = NotificationHelper(context, event.uid!!, event.uid!!.hashCode()) val notificationHelper = NotificationHelper(context, event.uid!!, event.uid!!.hashCode())
val intent = Intent(Intent.ACTION_SEND)
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_EMAIL, getEmailAddresses(event.attendees, false))
val dateFormatDate = SimpleDateFormat("EEEE, MMM dd", Locale.US)
intent.putExtra(Intent.EXTRA_SUBJECT,
context.getString(R.string.sync_calendar_attendees_email_subject,
event.summary,
dateFormatDate.format(event.dtStart?.date)))
intent.putExtra(Intent.EXTRA_TEXT,
context.getString(R.string.sync_calendar_attendees_email_content,
event.summary,
formatEventDates(event),
if (event.location != null) event.location else "",
formatAttendees(event.attendees)))
val uri = createAttachmentFromString(context, icsContent)
if (uri == null) {
App.log.severe("Unable to create attachment from calendar event")
return
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.putExtra(Intent.EXTRA_STREAM, uri)
notificationHelper.notify( notificationHelper.notify(
context.getString( context.getString(
R.string.sync_calendar_attendees_notification_title, event.summary), R.string.sync_calendar_attendees_notification_title, event.summary),
@ -159,6 +141,7 @@ constructor(context: Context, account: Account, settings: AccountSettings, extra
intent, intent,
R.drawable.ic_email_black) R.drawable.ic_email_black)
} }
}
@Throws(IOException::class, ContactsStorageException::class, CalendarStorageException::class) @Throws(IOException::class, ContactsStorageException::class, CalendarStorageException::class)
private fun processEvent(newData: Event, localEvent: LocalEvent?): LocalEvent { private fun processEvent(newData: Event, localEvent: LocalEvent?): LocalEvent {
@ -178,71 +161,4 @@ constructor(context: Context, account: Account, settings: AccountSettings, extra
return localEvent return localEvent
} }
private fun getEmailAddresses(attendees: List<Attendee>,
shouldIncludeAccount: Boolean): Array<String> {
val attendeesEmails = ArrayList<String>(attendees.size)
for (attendee in attendees) {
val attendeeEmail = attendee.value.replace("mailto:", "")
if (!shouldIncludeAccount && attendeeEmail == account.name) {
continue
}
attendeesEmails.add(attendeeEmail)
}
return attendeesEmails.toTypedArray()
}
private fun formatAttendees(attendeesList: List<Attendee>): String {
val stringBuilder = StringBuilder()
val attendees = getEmailAddresses(attendeesList, true)
for (attendee in attendees) {
stringBuilder.append("\n ").append(attendee)
}
return stringBuilder.toString()
}
private fun formatEventDates(event: Event): String {
val locale = Locale.getDefault()
val timezone = if (event.dtStart?.timeZone != null) event.dtStart?.timeZone else TimeZone.getTimeZone("UTC")
val dateFormatString = if (event.isAllDay()) "EEEE, MMM dd" else "EEEE, MMM dd @ hh:mm a"
val longDateFormat = SimpleDateFormat(dateFormatString, locale)
longDateFormat.timeZone = timezone
val shortDateFormat = SimpleDateFormat("hh:mm a", locale)
shortDateFormat.timeZone = timezone
val startDate = event.dtStart?.date
val endDate = event.getEndDate(true)!!.date
val tzName = timezone?.getDisplayName(timezone?.inDaylightTime(startDate)!!, TimeZone.SHORT)
val cal1 = Calendar.getInstance()
val cal2 = Calendar.getInstance()
cal1.time = startDate
cal2.time = endDate
val sameDay = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)
if (sameDay && event.isAllDay()) {
return longDateFormat.format(startDate)
}
return if (sameDay)
String.format("%s - %s (%s)",
longDateFormat.format(startDate),
shortDateFormat.format(endDate),
tzName)
else
String.format("%s - %s (%s)", longDateFormat.format(startDate), longDateFormat.format(endDate), tzName)
}
private fun createAttachmentFromString(context: Context, content: String): Uri? {
val name = UUID.randomUUID().toString()
val parentDir = File(context.cacheDir, name)
parentDir.mkdirs()
val cache = File(parentDir, "invite.ics")
try {
IOUtils.writeStringToFile(cache, content)
return AcraContentProvider.getUriForFile(context, cache)
} catch (e: IOException) {
e.printStackTrace()
}
return null
}
} }

View File

@ -0,0 +1,112 @@
package com.etesync.syncadapter.utils
import android.accounts.Account
import android.content.Context
import android.content.Intent
import android.net.Uri
import at.bitfire.ical4android.Event
import com.etesync.syncadapter.App
import com.etesync.syncadapter.NotificationHelper
import com.etesync.syncadapter.R
import net.fortuna.ical4j.model.property.Attendee
import org.acra.attachment.AcraContentProvider
import org.acra.util.IOUtils
import java.io.File
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.*
class EventEmailInvitation constructor(val context: Context, val account: Account) {
fun createIntent(event: Event, icsContent: String): Intent? {
val intent = Intent(Intent.ACTION_SEND)
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_EMAIL, getEmailAddresses(event.attendees, false))
val dateFormatDate = SimpleDateFormat("EEEE, MMM dd", Locale.US)
intent.putExtra(Intent.EXTRA_SUBJECT,
context.getString(R.string.sync_calendar_attendees_email_subject,
event.summary,
dateFormatDate.format(event.dtStart?.date)))
intent.putExtra(Intent.EXTRA_TEXT,
context.getString(R.string.sync_calendar_attendees_email_content,
event.summary,
formatEventDates(event),
if (event.location != null) event.location else "",
formatAttendees(event.attendees)))
val uri = createAttachmentFromString(context, icsContent)
if (uri == null) {
App.log.severe("Unable to create attachment from calendar event")
return null
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.putExtra(Intent.EXTRA_STREAM, uri)
return intent
}
private fun getEmailAddresses(attendees: List<Attendee>,
shouldIncludeAccount: Boolean): Array<String> {
val attendeesEmails = ArrayList<String>(attendees.size)
for (attendee in attendees) {
val attendeeEmail = attendee.value.replace("mailto:", "")
if (!shouldIncludeAccount && attendeeEmail == account.name) {
continue
}
attendeesEmails.add(attendeeEmail)
}
return attendeesEmails.toTypedArray()
}
private fun formatAttendees(attendeesList: List<Attendee>): String {
val stringBuilder = StringBuilder()
val attendees = getEmailAddresses(attendeesList, true)
for (attendee in attendees) {
stringBuilder.append("\n ").append(attendee)
}
return stringBuilder.toString()
}
private fun formatEventDates(event: Event): String {
val locale = Locale.getDefault()
val timezone = if (event.dtStart?.timeZone != null) event.dtStart?.timeZone else TimeZone.getTimeZone("UTC")
val dateFormatString = if (event.isAllDay()) "EEEE, MMM dd" else "EEEE, MMM dd @ hh:mm a"
val longDateFormat = SimpleDateFormat(dateFormatString, locale)
longDateFormat.timeZone = timezone
val shortDateFormat = SimpleDateFormat("hh:mm a", locale)
shortDateFormat.timeZone = timezone
val startDate = event.dtStart?.date
val endDate = event.getEndDate(true)!!.date
val tzName = timezone?.getDisplayName(timezone?.inDaylightTime(startDate)!!, TimeZone.SHORT)
val cal1 = Calendar.getInstance()
val cal2 = Calendar.getInstance()
cal1.time = startDate
cal2.time = endDate
val sameDay = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)
if (sameDay && event.isAllDay()) {
return longDateFormat.format(startDate)
}
return if (sameDay)
String.format("%s - %s (%s)",
longDateFormat.format(startDate),
shortDateFormat.format(endDate),
tzName)
else
String.format("%s - %s (%s)", longDateFormat.format(startDate), longDateFormat.format(endDate), tzName)
}
private fun createAttachmentFromString(context: Context, content: String): Uri? {
val name = UUID.randomUUID().toString()
val parentDir = File(context.cacheDir, name)
parentDir.mkdirs()
val cache = File(parentDir, "invite.ics")
try {
IOUtils.writeStringToFile(cache, content)
return AcraContentProvider.getUriForFile(context, cache)
} catch (e: IOException) {
e.printStackTrace()
}
return null
}
}