package com.margelo.nitro.fileopener import com.facebook.proguard.annotations.DoNotStrip import android.annotation.SuppressLint import android.content.Intent import java.io.File import com.margelo.nitro.NitroModules import androidx.core.content.FileProvider @DoNotStrip class FileOpener : HybridFileOpenerSpec() { private val MIME_TYPES: Map = mapOf( "txt" to "text/plain", "html" to "text/html", "css" to "text/css", "csv" to "text/csv", "xml" to "application/xml", "jpeg" to "image/jpeg", "jpg" to "image/jpeg", "png" to "image/png", "gif" to "image/gif", "bmp" to "image/bmp", "webp" to "image/webp", "svg" to "image/svg+xml", "ico" to "image/x-icon", "mp4" to "video/mp4", "mkv" to "video/x-matroska", "webm" to "video/webm", "avi" to "video/x-msvideo", "mov" to "video/quicktime", "wmv" to "video/x-ms-wmv", "flv" to "video/x-flv", "mp3" to "audio/mpeg", "aac" to "audio/aac", "wav" to "audio/wav", "ogg" to "audio/ogg", "midi" to "audio/midi", "flac" to "audio/flac", "pdf" to "application/pdf", "doc" to "application/msword", "docx" to "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "xls" to "application/vnd.ms-excel", "xlsx" to "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "ppt" to "application/vnd.ms-powerpoint", "pptx" to "application/vnd.openxmlformats-officedocument.presentationml.presentation", "zip" to "application/zip", "rar" to "application/vnd.rar", "7z" to "application/x-7z-compressed", "tar" to "application/x-tar", "gz" to "application/gzip", "apk" to "application/vnd.android.package-archive", "exe" to "application/vnd.microsoft.portable-executable", "dmg" to "application/x-apple-diskimage", "iso" to "application/x-iso9660-image", "ttf" to "font/ttf", "otf" to "font/otf", "woff" to "font/woff", "woff2" to "font/woff2", "js" to "application/javascript", "ts" to "application/typescript", "json" to "application/json", "java" to "text/x-java-source", "cpp" to "text/x-c", "c" to "text/x-c", "py" to "text/x-python", "php" to "application/x-httpd-php", "rb" to "text/x-ruby", "go" to "text/x-go", "swift" to "text/x-swift", "kt" to "text/x-kotlin", "epub" to "application/epub+zip", "ics" to "text/calendar", "md" to "text/markdown" ) @SuppressLint("QueryPermissionsNeeded") override fun open(path: String): Boolean { try { val file = File(path) if (!file.exists()) { return false } val context = NitroModules.applicationContext ?: return false val fileUri = FileProvider.getUriForFile( context, "${context.packageName}.fileprovider", file ) val fileExtension = file.extension.lowercase() val mimeType = MIME_TYPES[fileExtension] ?: "application/octet-stream" val intent = Intent(Intent.ACTION_VIEW).apply { setDataAndType(fileUri, mimeType) addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) } // if (intent.resolveActivity(context.packageManager) != null) { context.startActivity(intent) return true // } else { // return false // } } catch (e: Exception) { e.printStackTrace() return false } } }