Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Drag & Drop Support for TEXT/CSV Files in DeckPicker #17865

Merged
merged 1 commit into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ open class DeckPicker :

try {
// Intent is nullable because `clip.getItemAt(0).intent` always returns null
ImportUtils.FileImporter().handleContentProviderFile(this, uri)
ImportUtils.FileImporter().handleContentProviderFile(this, uri, Intent().setData(uri))
onResume()
} catch (e: Exception) {
Timber.w(e)
Expand Down
23 changes: 22 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/utils/ImportUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.ichi2.anki.R
import com.ichi2.anki.dialogs.DialogHandler
import com.ichi2.anki.dialogs.DialogHandlerMessage
import com.ichi2.anki.dialogs.ImportDialog
import com.ichi2.anki.onSelectedCsvForImport
import com.ichi2.anki.showImportDialog
import com.ichi2.annotations.NeedsTest
import com.ichi2.compat.CompatHelper
Expand Down Expand Up @@ -99,6 +100,22 @@ object ImportUtils {
fun isFileAValidDeck(fileName: String): Boolean =
FileImporter.hasExtension(fileName, "apkg") || FileImporter.hasExtension(fileName, "colpkg")

@NeedsTest("Verify that only valid text or data file MIME types return true")
fun isValidTextOrDataFile(
context: Context,
uri: Uri,
): Boolean {
val mimeType = context.contentResolver.getType(uri)
return mimeType in
listOf(
"text/plain",
"text/comma-separated-values",
"text/tab-separated-values",
"text/csv",
"text/tsv",
)
}

@SuppressWarnings("WeakerAccess")
open class FileImporter {
/**
Expand Down Expand Up @@ -200,7 +217,10 @@ object ImportUtils {
}
}
val tempOutDir: String
if (!isValidPackageName(filename)) {
if (isValidTextOrDataFile(context, importPathUri)) {
(context as Activity).onSelectedCsvForImport(intent!!)
return ImportResult.fromSuccess()
} else if (!isValidPackageName(filename)) {
return if (isAnkiDatabase(filename)) {
// .anki2 files aren't supported by Anki Desktop, we should eventually support them, because we can
// but for now, show a "nice" error.
Expand Down Expand Up @@ -242,6 +262,7 @@ object ImportUtils {
return when {
isDeckPackage(fileName) -> true
isCollectionPackage(fileName) -> true
isValidTextOrDataFile(context, importPathUri) -> true
else -> false
}
}
Expand Down