Skip to content

Commit

Permalink
fix(dialog): Fix the issue of Android saving files with a .txt extens…
Browse files Browse the repository at this point in the history
…ion (#1892)

* fix: Fix the issue of Android saving files with a .txt extension

* fix(dialog): pull intent type from extensions filters

* fix covector

---------

Co-authored-by: Lucas Nogueira <[email protected]>
  • Loading branch information
nashaofu and lucasfernog authored Oct 9, 2024
1 parent 9b2840d commit aee14ed
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .changes/fix-android-mime-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"dialog": patch
"dialog-js": patch
---

Set `save` dialog mime type from the `filters` extensions on Android.
5 changes: 4 additions & 1 deletion examples/api/src-tauri/gen/android/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 42 additions & 17 deletions plugins/dialog/android/src/main/java/DialogPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.content.Intent
import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.webkit.MimeTypeMap
import androidx.activity.result.ActivityResult
import app.tauri.Logger
import app.tauri.annotation.ActivityCallback
Expand Down Expand Up @@ -43,6 +44,7 @@ class MessageOptions {
@InvokeArg
class SaveFileDialogOptions {
var fileName: String? = null
lateinit var filters: Array<Filter>
}

@TauriPlugin
Expand All @@ -57,20 +59,7 @@ class DialogPlugin(private val activity: Activity): Plugin(activity) {

val intent = if (parsedTypes.isNotEmpty()) {
val intent = Intent(Intent.ACTION_PICK)
intent.putExtra(Intent.EXTRA_MIME_TYPES, parsedTypes)

var uniqueMimeType = true
var mimeKind: String? = null
for (mime in parsedTypes) {
val kind = mime.split("/")[0]
if (mimeKind == null) {
mimeKind = kind
} else if (mimeKind != kind) {
uniqueMimeType = false
}
}

intent.type = if (uniqueMimeType) Intent.normalizeMimeType("$mimeKind/*") else "*/*"
setIntentMimeTypes(intent, parsedTypes)
intent
} else {
val intent = Intent(Intent.ACTION_GET_CONTENT)
Expand Down Expand Up @@ -130,12 +119,46 @@ class DialogPlugin(private val activity: Activity): Plugin(activity) {
private fun parseFiltersOption(filters: Array<Filter>): Array<String> {
val mimeTypes = mutableListOf<String>()
for (filter in filters) {
for (mime in filter.extensions) {
mimeTypes.add(if (mime == "text/csv") "text/comma-separated-values" else mime)
for (ext in filter.extensions) {
if (ext.contains('/')) {
mimeTypes.add(if (ext == "text/csv") "text/comma-separated-values" else ext)
} else {
MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext)?.let {
mimeTypes.add(it)
}
}
}
}
return mimeTypes.toTypedArray()
}

private fun setIntentMimeTypes(intent: Intent, mimeTypes: Array<String>) {
if (mimeTypes.isNotEmpty()) {
var uniqueMimeKind = true
var mimeKind: String? = null
for (mime in mimeTypes) {
val kind = mime.split("/")[0]
if (mimeKind == null) {
mimeKind = kind
} else if (mimeKind != kind) {
uniqueMimeKind = false
}
}

if (uniqueMimeKind) {
if (mimeTypes.size > 1) {
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
intent.type = Intent.normalizeMimeType("$mimeKind/*")
} else {
intent.type = mimeTypes[0]
}
} else {
intent.type = "*/*"
}
} else {
intent.type = "*/*"
}
}

@Command
fun showMessageDialog(invoke: Invoke) {
Expand Down Expand Up @@ -187,10 +210,12 @@ class DialogPlugin(private val activity: Activity): Plugin(activity) {
fun saveFileDialog(invoke: Invoke) {
try {
val args = invoke.parseArgs(SaveFileDialogOptions::class.java)
val parsedTypes = parseFiltersOption(args.filters)

val intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
setIntentMimeTypes(intent, parsedTypes)

intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.setType("text/plain")
intent.putExtra(Intent.EXTRA_TITLE, args.fileName ?: "")
startActivityForResult(invoke, intent, "saveFileDialogResult")
} catch (ex: Exception) {
Expand Down

0 comments on commit aee14ed

Please sign in to comment.