Skip to content

Commit f17caf2

Browse files
nashaofulucasfernog
authored andcommitted
fix(dialog): Fix the issue of Android saving files with a .txt extension (tauri-apps#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]>
1 parent fea5f87 commit f17caf2

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed

.changes/fix-android-mime-type.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"dialog": patch
3+
"dialog-js": patch
4+
---
5+
6+
Set `save` dialog mime type from the `filters` extensions on Android.

examples/api/src-tauri/gen/android/.idea/gradle.xml

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/dialog/android/src/main/java/DialogPlugin.kt

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.content.Intent
1010
import android.net.Uri
1111
import android.os.Handler
1212
import android.os.Looper
13+
import android.webkit.MimeTypeMap
1314
import androidx.activity.result.ActivityResult
1415
import app.tauri.Logger
1516
import app.tauri.annotation.ActivityCallback
@@ -43,6 +44,7 @@ class MessageOptions {
4344
@InvokeArg
4445
class SaveFileDialogOptions {
4546
var fileName: String? = null
47+
lateinit var filters: Array<Filter>
4648
}
4749

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

5860
val intent = if (parsedTypes.isNotEmpty()) {
5961
val intent = Intent(Intent.ACTION_PICK)
60-
intent.putExtra(Intent.EXTRA_MIME_TYPES, parsedTypes)
61-
62-
var uniqueMimeType = true
63-
var mimeKind: String? = null
64-
for (mime in parsedTypes) {
65-
val kind = mime.split("/")[0]
66-
if (mimeKind == null) {
67-
mimeKind = kind
68-
} else if (mimeKind != kind) {
69-
uniqueMimeType = false
70-
}
71-
}
72-
73-
intent.type = if (uniqueMimeType) Intent.normalizeMimeType("$mimeKind/*") else "*/*"
62+
setIntentMimeTypes(intent, parsedTypes)
7463
intent
7564
} else {
7665
val intent = Intent(Intent.ACTION_GET_CONTENT)
@@ -130,12 +119,46 @@ class DialogPlugin(private val activity: Activity): Plugin(activity) {
130119
private fun parseFiltersOption(filters: Array<Filter>): Array<String> {
131120
val mimeTypes = mutableListOf<String>()
132121
for (filter in filters) {
133-
for (mime in filter.extensions) {
134-
mimeTypes.add(if (mime == "text/csv") "text/comma-separated-values" else mime)
122+
for (ext in filter.extensions) {
123+
if (ext.contains('/')) {
124+
mimeTypes.add(if (ext == "text/csv") "text/comma-separated-values" else ext)
125+
} else {
126+
MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext)?.let {
127+
mimeTypes.add(it)
128+
}
129+
}
135130
}
136131
}
137132
return mimeTypes.toTypedArray()
138133
}
134+
135+
private fun setIntentMimeTypes(intent: Intent, mimeTypes: Array<String>) {
136+
if (mimeTypes.isNotEmpty()) {
137+
var uniqueMimeKind = true
138+
var mimeKind: String? = null
139+
for (mime in mimeTypes) {
140+
val kind = mime.split("/")[0]
141+
if (mimeKind == null) {
142+
mimeKind = kind
143+
} else if (mimeKind != kind) {
144+
uniqueMimeKind = false
145+
}
146+
}
147+
148+
if (uniqueMimeKind) {
149+
if (mimeTypes.size > 1) {
150+
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
151+
intent.type = Intent.normalizeMimeType("$mimeKind/*")
152+
} else {
153+
intent.type = mimeTypes[0]
154+
}
155+
} else {
156+
intent.type = "*/*"
157+
}
158+
} else {
159+
intent.type = "*/*"
160+
}
161+
}
139162

140163
@Command
141164
fun showMessageDialog(invoke: Invoke) {
@@ -187,10 +210,12 @@ class DialogPlugin(private val activity: Activity): Plugin(activity) {
187210
fun saveFileDialog(invoke: Invoke) {
188211
try {
189212
val args = invoke.parseArgs(SaveFileDialogOptions::class.java)
213+
val parsedTypes = parseFiltersOption(args.filters)
190214

191215
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
216+
setIntentMimeTypes(intent, parsedTypes)
217+
192218
intent.addCategory(Intent.CATEGORY_OPENABLE)
193-
intent.setType("text/plain")
194219
intent.putExtra(Intent.EXTRA_TITLE, args.fileName ?: "")
195220
startActivityForResult(invoke, intent, "saveFileDialogResult")
196221
} catch (ex: Exception) {

0 commit comments

Comments
 (0)