Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(import): import CSV/TSV files
Browse files Browse the repository at this point in the history
CSV files from Gmail on my S21 (Android 14)
are text/csv

text/tsv is also used when coming from Gmail

Fixes 17866 (maybe)
david-allison committed Jan 24, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent a9b9529 commit 2a535c9
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions AnkiDroid/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -241,6 +241,8 @@
<data android:mimeType="application/octet-stream"/>
<data android:mimeType="text/tab-separated-values"/>
<data android:mimeType="text/comma-separated-values"/>
<data android:mimeType="text/csv"/>
<data android:mimeType="text/tsv"/>
</intent-filter>

<!-- Keep it separate as when sharing image AnkiDroid would be shown with Image Occlusion label -->
11 changes: 9 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/IntentHandler.kt
Original file line number Diff line number Diff line change
@@ -299,6 +299,14 @@ class IntentHandler : AbstractIntentHandler() {
private const val CLIPBOARD_INTENT = "com.ichi2.anki.COPY_DEBUG_INFO"
private const val CLIPBOARD_INTENT_EXTRA_DATA = "clip_data"

private val textMimeTypes =
setOf(
"text/tab-separated-values",
"text/tsv",
"text/comma-separated-values",
"text/csv",
)

private fun isValidViewIntent(intent: Intent): Boolean {
// Negating a negative because we want to call specific attention to the fact that it's invalid
// #6312 - Smart Launcher provided an empty ACTION_VIEW, no point in importing here.
@@ -333,8 +341,7 @@ class IntentHandler : AbstractIntentHandler() {
val mimeType = intent.resolveMimeType()
when {
mimeType?.startsWith("image/") == true -> LaunchType.IMAGE_IMPORT
mimeType == "text/tab-separated-values" ||
mimeType == "text/comma-separated-values" -> LaunchType.TEXT_IMPORT
textMimeTypes.contains(mimeType) -> LaunchType.TEXT_IMPORT
else -> LaunchType.FILE_IMPORT
}
} else if ("com.ichi2.anki.DO_SYNC" == action) {
Original file line number Diff line number Diff line change
@@ -103,6 +103,7 @@ class ImportFileSelectionFragment : DialogFragment() {
"text/comma-separated-values",
"text/csv",
"text/tab-separated-values",
"text/tsv",
),
),
)
4 changes: 4 additions & 0 deletions AnkiDroid/src/test/java/com/ichi2/anki/IntentHandlerTest.kt
Original file line number Diff line number Diff line change
@@ -96,10 +96,14 @@ class IntentHandlerTest {
fun textImportIntentReturnsTextImport() {
testIntentType("content://valid", "text/tab-separated-values", LaunchType.TEXT_IMPORT)
testIntentType("content://valid", "text/comma-separated-values", LaunchType.TEXT_IMPORT)
testIntentType("content://valid", "text/csv", LaunchType.TEXT_IMPORT)
testIntentType("content://valid", "text/tsv", LaunchType.TEXT_IMPORT)

// Test for ACTION_SEND
testIntentType("content://valid", "text/tab-separated-values", LaunchType.TEXT_IMPORT, Intent.ACTION_SEND)
testIntentType("content://valid", "text/comma-separated-values", LaunchType.TEXT_IMPORT, Intent.ACTION_SEND)
testIntentType("content://valid", "text/csv", LaunchType.TEXT_IMPORT, Intent.ACTION_SEND)
testIntentType("content://valid", "text/tsv", LaunchType.TEXT_IMPORT, Intent.ACTION_SEND)
}

private fun testIntentType(

0 comments on commit 2a535c9

Please sign in to comment.