Skip to content

fix(import): unify import logic & change prioritization of MIME/extension - #22842

Merged
mikehardy merged 7 commits into
ankidroid:mainfrom
david-allison:air/main-2-33af5124-28e
Oct 29, 2026
Merged

mikehardy merged 7 commits into
ankidroid:mainfrom
david-allison:air/main-2-33af5124-28e

Conversation

@david-allison

Copy link
Copy Markdown
Member

Note

Assisted-by: GPT-6-Astra xhigh

Purpose / Description

We had an issue where a file with .apkg sent a text/plain intent, and was imported using the text file code.

Fixes

Approach

Firstly: this unified all file import logic: files initially come in as FILE_IMPORT, rather than TEXT/FILE_IMPORT.
Secondly, if a file has a MIME text/plain and an *pkg extension, prefer the *pkg extension, treating *pkg as an explicit intention.
Finally, if a file has an apkg/colpkg MIME, treat that as an explicit intent that we should try the package importer.

There are a few import optimizations (background thread loading; ensuring we send the Message on a UI thread)

How Has This Been Tested?

Extensively unit tested

Checklist

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

* launches CSV importer for text
* enqueues a message for *pkg

Assisted-by: GPT-6
Assisted-by: GPT-6-Astra xhigh
apkg/csv now take the same route:
`TEXT_IMPORT` => `FILE_IMPORT`

This is prep for better import handling, we have been misidentifying
`apkg` files as text based on a faulty MIME type, having a single place
to accept a file, and determine its actual contents will resolve this.

A `prepareImport` => `openImport` call chain is used for handling.

Also allows us to move it to a background thread in a later commit.

Prep for 21430

Assisted-by: GPT-6
Assisted-by: GPT-6-Astra xhigh
Rather then a separate CSV import.

Assisted-by: GPT-6
Assisted-by: GPT-6-Astra xhigh

@mikehardy mikehardy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assisted-by: composer-2.5

Thanks for the import unification work — the routing tests are a solid addition.

This act is from required CI only (no local prep on this tip). Head d6c00872.

The diff consolidates import handling in DeckPicker, IntentHandler, Import/ImportUtils, adds compat MIME helpers, and expands unit coverage (ImportRoutingTest, ImportUiThreadTest, and related deck-picker import tests).

Requesting changes: JUnit Tests (windows run 1) failed with four failures in ImportRoutingTest while ubuntu and macos unit jobs passed (run 40727503376):

  • package selected in the text picker keeps its confirmation on the current deck picker — BackendDbException / SQLite CannotOpen ("unable to open database file") from exportPackage (ImportRoutingTest.kt ~196/479).
  • specific collection MIME keeps replacement confirmation with a text or missing filename — same SQLite cannot-open during exportPackage (~145).
  • package picker ignores incorrect provider MIME — FileNotFoundException for Robolectric cache deck.apkg (~208/220).
  • dropped package retains its payload until preparation completes — missing cache deck.apkg (~248/271).

Please address the Windows-only test/setup failures (likely temp cache path or export helper behavior under Robolectric on Windows) and get all three unit matrix legs green.

@mikehardy mikehardy added Needs Author Reply Waiting for a reply from the original author and removed Needs Review labels Oct 28, 2026
Assisted-by: GPT-6
Assisted-by: GPT-6-Astra xhigh
Assisted-by: GPT-6.1-Sol xhigh
Assisted-by: GPT-6
Assisted-by: GPT-6-Astra xhigh
We have cases where `apkg` is `text/plain`, which was previously
imported as a failed CSV ('stream did not contain valid UTF-8').

Now, we check to see if a file has an apkg extension before falling
back to UTF-8

Fixes 21430

Assisted-by: GPT-6
Assisted-by: GPT-6-Astra xhigh
Assisted-by: GPT-6.1-Sol xhigh
A [a/col]pkg MIME type is a signal, whereas `text/plain` is more ambiguous.
So treat *pkg MIME types as a stronger signal than filenames/MIME hints.

Filenames are still used for deck vs collection detection.

Part of 21430

Assisted-by: GPT-6-Astra xhigh

@mikehardy mikehardy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assisted-by: grok-4.7

Thank you — the routing tests that import through the backend make the MIME priority fix credible for #21430.

Rebased seven commits onto main locally (clean replay; no push).

Ran lint (lintAll, ktlintCheck, lint-rules:test), unit (jacocoUnitTestReport), and emulator tests (TEST_RELEASE_BUILD=true on emulator-5556 / API35_GAPI); all passed.

ImportUtils.FileImporter.resolveImportInternal now orders collection filename, deck filename, specific package MIME (intent over provider), then text CSV/TSV/plain. DeckPicker and IntentHandler share onSelectedFileToImport / prepareImport on a background dispatcher with main-thread UI; package vs text still opens the right importer. Drag-and-drop drops the snackbar preflight and uses the same error dialog; API 31+ drop permissions follow the ContentInfo job via :compat. ImportRoutingTest serves a real package as text/plain and confirms backend import — the path that previously sent .apkg to the CSV reader.

some acceptable findings, no action required, just leaving for posterity: :compat adds a coroutines Job for the reachability fence; several refactor: commits also change behavior (removed TEXT_IMPORT, drag errors use the import dialog).

@mikehardy mikehardy added Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) and removed Needs Review labels Oct 28, 2026
@mikehardy
mikehardy added this pull request to the merge queue Oct 29, 2026
Merged via the queue into ankidroid:main with commit fd5e339 Oct 29, 2026
26 checks passed
@github-actions github-actions Bot removed the Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) label Oct 29, 2026
@github-actions github-actions Bot added this to the 2.26 release milestone Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Import fails with "stream did not contain valid UTF-8" on Android 16, even for a byte-identical round-trip of an already-working deck

2 participants