From a80cb74b5a45a367990e46cfdd6be8d77698d7d1 Mon Sep 17 00:00:00 2001 From: Daniel Schubert Date: Fri, 6 Oct 2023 20:22:27 +0200 Subject: [PATCH] Implement workaround for issue #14 Older Android SDKs don't implement MIME type "text/markdown". This leads to markdown files not being available for selection. This workaround checks if "text/markdown" is a known MIME type. Otherwise it will fall back to the broader "*/*". Possible issue with this workaround: Instead of not being able to select files, it is now possible to select any file regardless of file type. This only affects devices, where "text/markdown" is not known. --- .../MarkdownFileWidgetConfigureActivity.kt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/ch/tiim/markdown_widget/MarkdownFileWidgetConfigureActivity.kt b/app/src/main/java/ch/tiim/markdown_widget/MarkdownFileWidgetConfigureActivity.kt index 5b23ec7..28d5fd7 100644 --- a/app/src/main/java/ch/tiim/markdown_widget/MarkdownFileWidgetConfigureActivity.kt +++ b/app/src/main/java/ch/tiim/markdown_widget/MarkdownFileWidgetConfigureActivity.kt @@ -10,6 +10,7 @@ import android.content.pm.PackageManager import android.net.Uri import android.os.Bundle import android.view.View +import android.webkit.MimeTypeMap import android.widget.EditText import android.widget.RadioGroup import android.widget.RemoteViews @@ -34,10 +35,18 @@ class MarkdownFileWidgetConfigureActivity : Activity() { private lateinit var inputFilePath: EditText private lateinit var radioGroup: RadioGroup private val onBrowse = View.OnClickListener { + // Workaround for https://github.com/Tiim/Android-Markdown-Widget/issues/14: + // Check if MIME-Type "text/markdown" is known. Otherwise fall back to + // generic type to still allow file selection. + val mimetype = if (MimeTypeMap.getSingleton().hasMimeType("text/markdown")) { + "text/markdown" + } else { + "*/*" + } // https://developer.android.com/reference/android/content/Intent#ACTION_OPEN_DOCUMENT val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply { addCategory(Intent.CATEGORY_OPENABLE) - type = "text/markdown" + type = mimetype flags = Intent.FLAG_GRANT_READ_URI_PERMISSION.or( Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) } startActivityForResult(Intent.createChooser(intent, "Select a markdown file"), ACTIVITY_RESULT_BROWSE)