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
.ics File Download Supportimport 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
setDownloadListener:Listens for file download requests from the
WebViewand identifies.icsfiles (MIME type:text/calendaror files ending in.ics).
Permissions Request:
Requests
WRITE_EXTERNAL_STORAGEpermission at runtime to save the.icsfile to the device.
Use of
DownloadManager:Downloads the
.icsfile and saves it in theDownloadsdirectory.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
AndroidManifest.xmlEnsure the following permissions are declared in your AndroidManifest.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:
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />Handle File Downloads
The
.icsfiles are detected based on their MIME type (text/calendar) or file extension (.ics).Files are saved in the
Downloadsdirectory usingDownloadManager.
Permissions Handling
If running on Android 6.0 (API level 23) or higher, the app requests storage permissions at runtime.
Last updated