> For the complete documentation index, see [llms.txt](https://docs.hello-lisa.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hello-lisa.com/developers/guides/integration-guide/app-integration/android/file-downloads.md).

# File Downloads

This implementation allows users to download `.ics` files (calendar events) from the LiSA Player running in a WebView, and save them to the device for easy "Add to Calendar" functionality.

### **Update WebViewActivity with `.ics` File Download Support**

```kotlin
import android.Manifest
import android.app.DownloadManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.webkit.*
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import java.io.File

class WebViewActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_webview)

        val webView: WebView = findViewById(R.id.webView)
        webView.webViewClient = WebViewClient()
        webView.webChromeClient = WebChromeClient()

        // Enable JavaScript
        val webSettings: WebSettings = webView.settings
        webSettings.javaScriptEnabled = true

        // Allow Media Playback Inline
        webSettings.mediaPlaybackRequiresUserGesture = false

        // Enable file downloads
        enableFileDownloads(webView)

        // Load the web app URL
        webView.loadUrl("https://{clientId}.loveslisa.tech/s/{showId}")

        // Handle communication from the web app
        webView.addJavascriptInterface(object : Any() {
            @JavascriptInterface
            fun postMessage(message: String) {
                // Process the message from the web app
                println("Message from LiSA: $message")
            }
        }, "MessageFromLiSA")
    }

    private fun enableFileDownloads(webView: WebView) {
        // Request permissions for downloading files
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != 
            PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                1
            )
        }

        // Set up a Download Listener
        webView.setDownloadListener { url, userAgent, contentDisposition, mimeType, _ ->
            if (mimeType == "text/calendar" || url.endsWith(".ics")) {
                // Use DownloadManager to download the file
                val request = DownloadManager.Request(Uri.parse(url))
                request.setMimeType(mimeType)
                request.addRequestHeader("User-Agent", userAgent)
                request.setDescription("Downloading calendar event...")
                request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType))
                request.setDestinationInExternalPublicDir(
                    Environment.DIRECTORY_DOWNLOADS,
                    URLUtil.guessFileName(url, contentDisposition, mimeType)
                )
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)

                val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
                downloadManager.enqueue(request)

                // Inform the user
                println("Downloading .ics file from: $url")
            }
        }
    }
}
```

***

### **Key Changes and Additions**

1. **`setDownloadListener`:**
   * Listens for file download requests from the `WebView` and identifies `.ics` files (MIME type: `text/calendar` or files ending in `.ics`).
2. **Permissions Request:**
   * Requests `WRITE_EXTERNAL_STORAGE` permission at runtime to save the `.ics` file to the device.
3. **Use of `DownloadManager`:**
   * Downloads the `.ics` file and saves it in the `Downloads` directory.
   * Adds a notification when the download is complete.

***

### **Steps to Integrate**

#### **Add the WebViewActivity to Your Android App:**

Use the provided `WebViewActivity` code as a starting point.

#### **Request Permissions in `AndroidManifest.xml`**

Ensure the following permissions are declared in your `AndroidManifest.xml`:

```xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```

#### **Add the WebView to Your Layout**

Define the `WebView` in your `activity_webview.xml` layout file:

```xml
<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
```

#### **Handle File Downloads**

* The `.ics` files are detected based on their MIME type (`text/calendar`) or file extension (`.ics`).
* Files are saved in the `Downloads` directory using `DownloadManager`.

#### **Permissions Handling**

* If running on Android 6.0 (API level 23) or higher, the app requests storage permissions at runtime.
