mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-08-03 18:08:36 +02:00
fix(dialog): remove use of ACTION_PICK (#2871)
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
dialog: patch
|
||||||
|
dialog-js: patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixed an issue that caused the file picker not to open on Android when extension filters were set.
|
||||||
@@ -56,20 +56,18 @@ class DialogPlugin(private val activity: Activity): Plugin(activity) {
|
|||||||
try {
|
try {
|
||||||
val args = invoke.parseArgs(FilePickerOptions::class.java)
|
val args = invoke.parseArgs(FilePickerOptions::class.java)
|
||||||
val parsedTypes = parseFiltersOption(args.filters)
|
val parsedTypes = parseFiltersOption(args.filters)
|
||||||
|
|
||||||
val intent = if (parsedTypes.isNotEmpty()) {
|
// TODO: ACTION_OPEN_DOCUMENT ??
|
||||||
val intent = Intent(Intent.ACTION_PICK)
|
val intent = Intent(Intent.ACTION_GET_CONTENT)
|
||||||
setIntentMimeTypes(intent, parsedTypes)
|
intent.addCategory(Intent.CATEGORY_OPENABLE)
|
||||||
intent
|
intent.type = "*/*"
|
||||||
} else {
|
|
||||||
val intent = Intent(Intent.ACTION_GET_CONTENT)
|
if (parsedTypes.isNotEmpty()) {
|
||||||
intent.addCategory(Intent.CATEGORY_OPENABLE)
|
intent.putExtra(Intent.EXTRA_MIME_TYPES, parsedTypes)
|
||||||
intent.type = "*/*"
|
|
||||||
intent
|
|
||||||
}
|
}
|
||||||
|
|
||||||
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, args.multiple ?: false)
|
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, args.multiple ?: false)
|
||||||
|
|
||||||
startActivityForResult(invoke, intent, "filePickerResult")
|
startActivityForResult(invoke, intent, "filePickerResult")
|
||||||
} catch (ex: Exception) {
|
} catch (ex: Exception) {
|
||||||
val message = ex.message ?: "Failed to pick file"
|
val message = ex.message ?: "Failed to pick file"
|
||||||
@@ -115,7 +113,7 @@ class DialogPlugin(private val activity: Activity): Plugin(activity) {
|
|||||||
callResult.put("files", JSArray.from(uris.toTypedArray()))
|
callResult.put("files", JSArray.from(uris.toTypedArray()))
|
||||||
return callResult
|
return callResult
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun parseFiltersOption(filters: Array<Filter>): Array<String> {
|
private fun parseFiltersOption(filters: Array<Filter>): Array<String> {
|
||||||
val mimeTypes = mutableListOf<String>()
|
val mimeTypes = mutableListOf<String>()
|
||||||
for (filter in filters) {
|
for (filter in filters) {
|
||||||
@@ -132,38 +130,10 @@ class DialogPlugin(private val activity: Activity): Plugin(activity) {
|
|||||||
return mimeTypes.toTypedArray()
|
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
|
@Command
|
||||||
fun showMessageDialog(invoke: Invoke) {
|
fun showMessageDialog(invoke: Invoke) {
|
||||||
val args = invoke.parseArgs(MessageOptions::class.java)
|
val args = invoke.parseArgs(MessageOptions::class.java)
|
||||||
|
|
||||||
if (activity.isFinishing) {
|
if (activity.isFinishing) {
|
||||||
invoke.reject("App is finishing")
|
invoke.reject("App is finishing")
|
||||||
return
|
return
|
||||||
@@ -179,7 +149,7 @@ class DialogPlugin(private val activity: Activity): Plugin(activity) {
|
|||||||
Handler(Looper.getMainLooper())
|
Handler(Looper.getMainLooper())
|
||||||
.post {
|
.post {
|
||||||
val builder = AlertDialog.Builder(activity)
|
val builder = AlertDialog.Builder(activity)
|
||||||
|
|
||||||
if (args.title != null) {
|
if (args.title != null) {
|
||||||
builder.setTitle(args.title)
|
builder.setTitle(args.title)
|
||||||
}
|
}
|
||||||
@@ -213,10 +183,14 @@ class DialogPlugin(private val activity: Activity): Plugin(activity) {
|
|||||||
val parsedTypes = parseFiltersOption(args.filters)
|
val parsedTypes = parseFiltersOption(args.filters)
|
||||||
|
|
||||||
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
|
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
|
||||||
setIntentMimeTypes(intent, parsedTypes)
|
|
||||||
|
|
||||||
intent.addCategory(Intent.CATEGORY_OPENABLE)
|
intent.addCategory(Intent.CATEGORY_OPENABLE)
|
||||||
intent.putExtra(Intent.EXTRA_TITLE, args.fileName ?: "")
|
intent.putExtra(Intent.EXTRA_TITLE, args.fileName ?: "")
|
||||||
|
intent.type = "*/*"
|
||||||
|
|
||||||
|
if (parsedTypes.isNotEmpty()) {
|
||||||
|
intent.putExtra(Intent.EXTRA_MIME_TYPES, parsedTypes)
|
||||||
|
}
|
||||||
|
|
||||||
startActivityForResult(invoke, intent, "saveFileDialogResult")
|
startActivityForResult(invoke, intent, "saveFileDialogResult")
|
||||||
} catch (ex: Exception) {
|
} catch (ex: Exception) {
|
||||||
val message = ex.message ?: "Failed to pick save file"
|
val message = ex.message ?: "Failed to pick save file"
|
||||||
|
|||||||
Reference in New Issue
Block a user