2

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)?

Community
  • 1
  • 1
Trung Le
  • 503
  • 1
  • 7
  • 17

2 Answers2

1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • so my next try could be fetch the ics file to local and pass its local uri as an extras to action_view intent...will try out and update the result but will see if anybody else comes up with another idea. Thanks in advance. – Trung Le Jan 11 '17 at 12:51
  • @TrungLe: "pass its local uri as an extras to action_view intent" -- no, it would be in the "data" facet (e.g., `new Intent(Intent.ACTION_VIEW, uriToYourICS))`). – CommonsWare Jan 11 '17 at 13:12
  • on some phones (e.g wiko phone) .ics extension is not supported hence there is no output but an error toast, some other could open and work fine, I think I would either parse the ics file and start an insert/edit intent to calendar https://github.com/rabidaudio/android-ical-parser Or may be simply ask for more information from api lol – Trung Le Jan 12 '17 at 09:21
  • 1
    @TrungLe Do you have some updates about this, is it possible to subscribe third party calendar to Google-Calendar via some service endpoint, or via passing .ics file via intent. Did you found any solution about this? – Kostadin Georgiev Nov 02 '20 at 13:53
1

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.
humblerookie
  • 4,717
  • 4
  • 25
  • 40