Trung Le
Trung Le

Reputation: 523

How to add a Calendar event with ICS file (URL) in Android from an Intent?

I'm having an *.ics file that returned from API response and what I'm trying is to add it to users Calendar (through the Calendar Add Event activity). Would be something like this in iOS: How can I open calendar .ics files in ios?

I've been searching around but seems like there is no default action handler to add ics file from url? So far I have tried these solutions:

but none are available for handling the ICS file.

They are working fine for the same purpose but in my current situation, we want the action handlers to be the same through all platforms and using the same api response (returns a downloadable uri of .ics file).

Has anybody run into the same situation? Or if anybody knows how the Gmail works if you send/attach an ics file within (it's able to add that event to calendar)?

Upvotes: 4

Views: 18158

Answers (2)

humblerookie
humblerookie

Reputation: 4947

This isn't straightforward but here's what ended up working for me

  1. Ensure you're able to query packages
<queries>
    <intent>
         <action android:name="android.intent.action.VIEW" />
         <data android:mimeType="text/calendar" />
    </intent>
</queries>
  1. Launch Intent to read the file, Its important to set the component info otherwise android doesn't forward the ics information to the calendar app.
val file = File("path-to-file/cal.ics")
val uri = FileProvider.getUriForFile(
    context,
    "com.myapp.provider",
    file,
)
val mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(file.extension)
val intent = Intent(Intent.ACTION_VIEW).apply {
    setDataAndType(uri, mime)
    addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
// Get the first matched package, 
// generally gcal or maybe some other app for samsung devices. 
// You can further filter these package names.
val resolvedComponentInfo = context.packageManager.queryIntentActivities(
    intent,
    PackageManager.MATCH_ALL
).firstOrNull()?.activityInfo

intent.component = ComponentName.createRelative(
    resolvedComponentInfo!!.packageName,
    resolvedComponentInfo.name
)
startActivity(intent)               
  1. Lastly ensure you try catch this, in case a calendar app isn't installed on the device.

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1007534

there is no default action handler to add ics file from url?

Correct. Android is an OS. It is not a calendar app.

Out of the thousands of Android device models, many may ship a calendar app. Out of those that do, many may ship Google Calendar, if they have licensed Google's proprietary apps. However, this means that:

  • Not all devices will have a calendar app
  • Not all devices will have Google Calendar

And, not all users want to use Google Calendar. They might want to use some other calendar app, either pre-installed on their device or downloaded from an app distribution channel, such as the Play Store.

It is up to the calendar app to offer ICS import. There is no requirement that every calendar app offer this, and there is no requirement that they do so via some Intent action that third-party apps can invoke.

IMHO, the most likely scenario for a calendar app to offer ICS import would be via ACTION_VIEW with whatever the MIME type is for .ics (text/calendar?). Such an activity may or may not support http or https schemes, as they may expect the ICS file to be local (e.g., email attachment) and support only file and content.

Upvotes: 1

Related Questions