Skip to content

Colota - Android GPS Location tracker

feat: import location history from external files (#390)

Max Dietrich0b37088d1766
diff --git a/apps/docs/docs/development/architecture.md b/apps/docs/docs/development/architecture.md
index 436e5274..b5a2b126 100644
--- a/apps/docs/docs/development/architecture.md
+++ b/apps/docs/docs/development/architecture.md
@@ -54,7 +54,7 @@ All native code lives in `apps/mobile/android/app/src/`, organized by build flav
### Bridge Modules
-Two React Native bridge modules are registered by `LocationServicePackage`: `LocationServiceModule` (the primary tracking-side bridge) and `BackupServiceModule` (the encrypted-backup bridge).
+Three React Native bridge modules are registered by `LocationServicePackage`: `LocationServiceModule` (the primary tracking-side bridge), `BackupServiceModule` (the encrypted-backup bridge) and `ImportServiceModule` (the external-file-import bridge). The two file-touching modules (Backup and Import) share `SafPickerCoordinator` (`util/SafPickerCoordinator.kt`) for the SAF `ACTION_CREATE_DOCUMENT` / `ACTION_OPEN_DOCUMENT` plumbing - each module holds its own instance with its own request codes so multiple coordinators coexist without interfering.
### LocationServiceModule
@@ -103,6 +103,36 @@ Native-only modules in `backup/` package. The on-disk format is documented in `B
| `BackupOrphanCleanup` | Singleton kicked off from `MainApplication.onCreate` on a daemon thread. Sweeps `cacheDir/backup_temp`, `cacheDir/restore_temp`, `cacheDir/pending_backup.colota`, and `<dbDir>/Colota.db.incoming` left behind by a process death mid-operation. Exposes a `CompletableDeferred` so `BackupServiceModule` can await completion before claiming the operation mutex. |
| `PasswordStrength` | Mirror of the JS-side `passwordStrength.ts`. Enforces the 12-character floor and ~50-bit entropy floor; sequential runs and `<4` distinct chars cap the score. |
+### ImportServiceModule
+
+Third React Native bridge module (exposed as `"ImportServiceModule"`). Owns the location-import pipeline end-to-end: file picker, streaming parse, dedup against the live DB, and the two-stage commit. JS-callable methods:
+
+- `pickImportSource` - launches the SAF `ACTION_OPEN_DOCUMENT` picker via the shared `SafPickerCoordinator`. MIME hints cover JSON, XML, CSV and `octet-stream`; the bare `type = "*/*"` keeps `.geojson` and `.gpx` from being filtered out by strict providers.
+- `importLocationsFromFile(uri)` - acquires the operation mutex, opens the file once, sniffs 4 KB for format detection, then prepends those bytes back via `SequenceInputStream` so the format-specific parser sees the full document without a second SAF open. Stashes the surviving rows on the module instance for the commit step.
+- `commitImport(asQueued)` - applies the staged rows in a single transaction. When `asQueued=true` it also reads the live `ServiceConfig`, rebuilds payloads via `PayloadBuilder` (the same one the live tracking path uses), and inserts paired queue rows. Rejects with `E_IMPORT_SYNC_UNAVAILABLE` if no endpoint or offline mode is on, so a stale UI state can't bypass the gate.
+- `cancelImport` - sets the cancellation flag and clears the stash.
+
+The staged rows are guarded by a 15-minute TTL job - a preview-then-walk-away doesn't pin memory indefinitely. Cancellation flows through an `AtomicBoolean` that the parsers and dedup walker check at array boundaries.
+
+Errors surface to JS as `E_IMPORT_*` codes: `E_IMPORT_UNSUPPORTED`, `E_IMPORT_CANCELLED`, `E_IMPORT_NO_PENDING`, `E_IMPORT_SYNC_UNAVAILABLE`, `E_IMPORT_FAILED`, plus the shared `E_BUSY` when another operation holds the mutex.
+
+### Import Pipeline
+
+Native-only modules in the `importer/` package. Format-specific parsers all return the same `ParseResult(rows, invalid)`; the dispatch logic in `LocationImporter` picks one based on a 4 KB content sniff.
+
+| Module | Purpose |
+| --- | --- |
+| `LocationImporter` | Orchestrator. Detects format from the sniff (XML root for GPX/KML, JSON top-level keys for GeoJSON / Google Timeline, CSV header for CSV), dispatches to the per-format parser, runs streaming **merge-walk dedup** against an ASC-ordered DB cursor (no existing-key `HashSet` is materialised - memory is O(1) on top of the parsed-rows list, so the dedup scales to multi-million-row user histories without OOM). On commit, the recovery path writes `sent = 1` and skips the queue; the migration path (`asQueued=true`) builds payloads via the live `PayloadBuilder` against a synthetic `android.location.Location` so the import path can't drift from the live tracking path. |
+| `GeoJsonParser` | Streaming `android.util.JsonReader`-based parser for `FeatureCollection` of `Point` features. Tolerates foreign shapes - any feature without a recognised time / non-Point geometry is counted as invalid and dropped, but parsing continues. |
+| `GoogleTimelineParser` | Handles both Google Timeline schemas in a single pass: legacy Takeout (`locations[].latitudeE7/longitudeE7/timestampMs`) and the new on-device export (`semanticSegments[].timelinePath[]` + `rawSignals[].position` with degree-suffix coord strings). Visit/activity inferences are intentionally skipped. |
+| `GpxParser` | `XmlPullParser`-based. Collects `<wpt>` + `<rtept>` + `<trkpt>` uniformly into the flat locations table. Recognises Garmin's nested `TrackPointExtension` wrapper so sport-watch metadata (speed, course) lands on the row. |
+| `KmlParser` | `XmlPullParser`-based. Reads `Placemark/Point/coordinates` (KML's `lon,lat[,alt]` order, flipped back internally) with `TimeStamp/when`. `LineString`-only Placemarks are dropped + counted as invalid since the KML schema doesn't carry per-vertex timestamps. |
+| `CsvParser` | Header-driven; maps columns by name with aliases (`lat`/`latitude`, `lon`/`lng`/`longitude`, `time`/`timestamp`/`iso_time`, etc.) so foreign column orders work without renaming. Rejects headers missing the required lat+lon+time columns with `UnsupportedFormatException`. |
+| `JsonReadHelpers` | Shared `JsonReader` extensions (`readNullableInt`, `readNullableDouble`) plus `parseIso8601Seconds` backed by pre-built immutable `DateTimeFormatter` instances (thread-safe and ~10× faster than `SimpleDateFormat`, which matters for Timeline files with 100k+ timestamps). |
+| `ImportFormat` / `ImportRow` / `UnsupportedFormatException` | Shared data types. `ImportRow` is the normalised location shape produced by every parser before dedup; format-specific quirks (E7 coords, ISO timestamps, degree-suffix strings) are resolved before construction so the orchestrator never sees them. |
+
+The on-disk format is **never** authoritative for sync decisions: imported rows go in with `sent = 1` by default (no re-upload), and only the explicit "Import + Queue for Sync" button flips that to `sent = 0` + queue rows. See [Data Import](../guides/data-import.md) for the user-facing semantics.
+
### LocationProvider Abstraction
Location services are abstracted behind a `LocationProvider` interface (`location/LocationProvider.kt`), with flavor-specific implementations:
@@ -266,7 +296,8 @@ For backups, two `internal` methods support the export/import flow without expos
| `LocationInspectorScreen` | Calendar day picker with activity dots, map tab with trip-colored tracks, trips tab with trip cards and export |
| `TripDetailScreen` | Full trip view with dedicated map, stats grid, speed and elevation profile charts, per-trip export, and per-trip delete |
| `LocationSummaryScreen` | Aggregated stats for selectable periods (week/month/30 days) with daily breakdown and tap-to-inspect navigation |
-| `ExportDataScreen` | Export all tracked locations via native streaming converters as CSV, GeoJSON, GPX, or KML |
+| `ExportLocationsScreen` | Export all tracked locations via native streaming converters as CSV, GeoJSON, GPX, or KML |
+| `ImportLocationsScreen` | Import external location files (GeoJSON, Google Timeline legacy + new, GPX, KML, CSV) with auto format detection, dedup preview, and recovery vs migration (queue-for-sync) commit choice |
| `AutoExportScreen` | Configure scheduled auto-export: directory, format, frequency, time of day, weekday or day-of-month, export range and file retention |
| `OfflineMapsScreen` | Download and manage offline map areas - interactive bounding box picker, size estimate, progress tracking, and area deletion |
| `DataManagementScreen` | Clear sent history, delete old data, vacuum database, sync controls |
@@ -284,6 +315,7 @@ For backups, two `internal` methods support the export/import flow without expos
| `ProfileService` | Thin wrapper over `NativeLocationService` for tracking profile CRUD and trip event queries |
| `SettingsService` | Bridges UI state to native SQLite with type conversion (seconds↔ms, objects↔JSON) |
| `BackupService` | Thin TypeScript wrapper over the native `BackupServiceModule` exposing `pickBackupDestination`, `pickBackupSource`, `createBackup`, `restoreBackup`, `applyRestore`. Surfaces typed `BackupErrorCode` strings for screen-side messaging |
+| `ImportService` | Thin TypeScript wrapper over the native `ImportServiceModule` exposing `pickImportSource`, `importLocationsFromFile`, `commitImport(asQueued)`, `cancelImport`. Returns a typed `ImportPreview` with format, counts, date range, and `canQueueForSync` so the UI can gate the queue-for-sync button |
| `modalService` | Centralized alert and confirm dialogs via `showAlert()` and `showConfirm()` |
### Map Components
diff --git a/apps/docs/docs/guides/backup-restore.md b/apps/docs/docs/guides/backup-restore.md
index 3a2d1cef..36c3432c 100644
--- a/apps/docs/docs/guides/backup-restore.md
+++ b/apps/docs/docs/guides/backup-restore.md
@@ -6,7 +6,7 @@ sidebar_position: 3
Bundle your locations, settings and credentials into a single password-encrypted file you can store anywhere - cloud drive, USB stick, another device.
-Use this when you want a full archive of your data, when you're moving to a new device, or as insurance before clearing data. For day-to-day analysis or sharing tracks, use [Data Export](data-export.md) instead.
+Use this when you want a full archive of your data, when you're moving to a new device, or as insurance before clearing data. For day-to-day analysis or sharing tracks, use [Data Export](data-export.md) instead. If you only need to **merge** locations from another file into your existing history without overwriting, use [Data Import](data-import.md) - that path is additive with duplicate-skipping, while Restore here is replace-everything.
## What's Included
diff --git a/apps/docs/docs/guides/data-import.md b/apps/docs/docs/guides/data-import.md
new file mode 100644
index 00000000..92ea57d0
--- /dev/null
+++ b/apps/docs/docs/guides/data-import.md
@@ -0,0 +1,111 @@
+---
+sidebar_position: 3
+---
+
+# Data Import
+
+Merge location history from external files into your Colota database.
+
+:::tip Looking for a full archive restore?
+
+Data Import is **additive**: existing locations are preserved and duplicates are skipped. Use this to recover from a Colota export, migrate from Google Timeline, or pull historical data from another tracker. If you want to **replace everything** on the device from an encrypted Colota archive (locations, settings, credentials), use [Backup & Restore](backup-restore.md).
+
+:::
+
+## Supported Formats
+
+The format is detected automatically from the file content; you don't pick it.
+
+| Format | Extension | Source |
+| --- | --- | --- |
+| **GeoJSON** | `.geojson` | Colota's own export. Any FeatureCollection of Point features with a `time` property also works. |
+| **Google Timeline (legacy)** | `Records.json` | Older bulk Location History export from [Google Takeout](https://takeout.google.com). Google removed Location History from Takeout in late 2024 - use this format for files exported before then. |
+| **Google Timeline (new)** | `.json` | On-device export from **Android Settings → Location → Location services → Timeline → Export Timeline data**. |
+| **GPX** | `.gpx` | GPS Exchange Format. Sport watches (Garmin TrackPointExtension supported), Strava, generic trackers. |
+| **KML** | `.kml` | Keyhole Markup Language. Google Earth, My Maps. Placemarks with TimeStamps only. |
+| **CSV** | `.csv` | Any CSV with `latitude`, `longitude`, and a time column. Column order doesn't matter. |
+
+For exotic formats (Strava `.fit`, OwnTracks `.rec`, custom tracker dumps) - convert them to one of the above first.
+
+### Getting a Google Timeline file
+
+Google moved Location History off the cloud onto the phone in late 2024, so the export path depends on when you saved your data:
+
+- **Google Takeout** (legacy `Records.json`): visit [takeout.google.com](https://takeout.google.com), choose **Location History** → **Export**. Google removed Location History from Takeout in late 2024, so this only succeeds for accounts that exported before then.
+- **Google Maps app** (current): open Google Maps → tap your profile picture → **Your Timeline** → **Settings** → **Export Timeline data**. Exact menu names shift between Maps versions, so look for an "Export Timeline data" entry.
+- **Android device settings** (current): **Settings → Location → Location services → Timeline → Export Timeline data**.
+
+The exported file is saved to your phone's `Downloads` folder.
+
+## How to Import
+
+1. Go to **Settings → Import Locations**
+2. Tap **Choose File** and pick the file you want to import
+3. Wait for the parse to finish (large Google Timeline files can take 10+ seconds)
+4. Review the preview: format, points found, duplicates that will be skipped, invalid rows, date range
+5. Choose **Import** (or **Import + Queue for Sync** - see below)
+
+import ScreenshotGallery from '@site/src/components/ScreenshotGallery'
+
+<ScreenshotGallery screenshots={[ { src: "/img/screenshots/ImportLocations.png", label: "Import Locations" }, ]} />
+
+### How duplicates are handled
+
+Two rows are considered duplicates when they share the same timestamp (1-second precision) **and** their latitude/longitude match to ~10 cm. This means:
+
+- Re-importing the same file is safe: every row is recognised as a duplicate, nothing changes.
+- Two genuinely different samples taken seconds apart from nearby coordinates are both kept.
+- Both file-internal duplicates (same point listed twice in the source) and DB-vs-file duplicates (the row already exists in your history) are detected.
+
+### Invalid rows
+
+Rows are dropped silently as "invalid" when any of the following hold:
+
+- Missing latitude, longitude, or timestamp
+- Coordinates out of range (latitude not in [-90, 90] or longitude not in [-180, 180])
+- Timestamp more than 5 minutes in the future
+- Geometry type isn't a single Point (LineString-only KML Placemarks for example)
+
+The preview shows the invalid count so you can decide whether to proceed.
+
+## Import vs Import + Queue for Sync
+
+If you've configured an optional sync backend in **Settings → Connection**, the confirm dialog offers two import buttons. The choice only affects whether the imported rows are also pushed out to that backend; either way the points land in Colota's local history.
+
+### Import
+
+- Rows are written into Colota with `sent=1` - flagged as already replicated.
+- **The sync engine will not push them to your backend.**
+- Use this when the backend already holds these points - for example, you're re-importing your own Colota export, or repopulating local history after a "Clear Sent History".
+
+### Import + Queue for Sync
+
+- Rows are written into Colota with `sent=0` and enqueued for upload.
+- The next sync cycle replicates them to your configured backend.
+- Use this when the points are new to your backend - for example, you imported a Google Timeline archive and want the backend copy to mirror it too.
+
+This button is **only shown when a sync endpoint is configured and offline mode is off**. If it's missing, configure sync in **Settings → Connection** first.
+
+:::warning Queueing fans the points out to your backend - irreversible there
+
+Once queued, the rows are uploaded as soon as the next sync runs. Removing them from the backend afterwards isn't something Colota can do for you. If you're importing a multi-year archive (100k+ points), expect the queue to fire that many upload requests against your backend; make sure it can handle the load.
+
+:::
+
+## After Import
+
+- Imported rows show up immediately on the **Dashboard** and **Location History** screens.
+- Trip detection re-runs on demand the next time you open a screen that uses it (the trip computation is derived from the locations table on the fly).
+- If you imported with **Import + Queue for Sync**, the queue counter in **Data Management** reflects the new pending rows; the next sync cycle replicates them to your configured backend.
+
+## Edge Cases
+
+- **KML LineString-only tracks** are skipped. KML's schema doesn't carry per-vertex timestamps, so we can't invent them. The vertices count as "invalid" in the preview so you know data was dropped.
+- **GPX without `<time>`** elements are skipped. Same reason - the locations table requires a timestamp.
+- **CSV without lat/lon/time columns** is rejected with a clear error rather than silently producing zero rows.
+- **Very large files** (500 MB+ Google Timeline exports) parse without loading the full document into memory. Expect the preview phase to take some seconds; the actual commit is fast once you confirm.
+- **The Import + Queue button is greyed out** if you're in offline mode or have no sync endpoint configured - this is intentional, the queue has no destination in that state.
+
+## Tip: back up before importing
+
+Imports can't be selectively undone. If you import the wrong file, removing only the imported rows means finding them by timestamp range - which also kills any legitimate data in that range. Take a [Backup](backup-restore.md) before a large import if you're unsure.
diff --git a/apps/docs/docs/guides/data-management.md b/apps/docs/docs/guides/data-management.md
index f6b77c27..523d06aa 100644
--- a/apps/docs/docs/guides/data-management.md
+++ b/apps/docs/docs/guides/data-management.md
@@ -12,19 +12,24 @@ import ScreenshotGallery from '@site/src/components/ScreenshotGallery'
## Actions
-| Action | Description |
-| ---------------------------- | ----------------------------------------------------------- |
-| **Sync Now** | Manually flush the queue and upload pending locations |
-| **Clear Sent History** | Remove locations that have already been synced |
-| **Clear Queue** | Remove unsent locations from the upload queue |
-| **Delete Older Than X Days** | Clean up old data past a specified age |
-| **Vacuum Database** | Reclaim disk space after deletions |
-| **Export Data** | Export location history - see [Data Export](data-export.md) |
+| Action | Description |
+| ---------------------------- | -------------------------------------------------------------------------- |
+| **Sync Now** | Manually flush the queue and upload pending locations |
+| **Clear Sent History** | Remove locations that have already been synced |
+| **Clear Queue** | Remove unsent locations from the upload queue |
+| **Delete Older Than X Days** | Clean up old data past a specified age |
+| **Vacuum Database** | Reclaim disk space after deletions |
+| **Export Locations** | Export location history - see [Data Export](data-export.md) |
+| **Import Locations** | Merge external files into your history - see [Data Import](data-import.md) |
In [offline mode](/docs/configuration/server-settings#offline-mode), sync-related actions (Sync Now, Clear Sent History, Clear Queue) are hidden since no queue is used. A **Delete All Locations** action is available instead. Data export remains fully available - see [Data Export](data-export.md).
For a full archive of locations, settings and credentials in a single password-encrypted file, use **Settings → Backup & Restore** - see [Backup & Restore](backup-restore.md).
+## Imported locations and the queue
+
+Locations brought in via [Data Import](data-import.md) are marked as already synced by default, so they **do not show up in the queue counter** and don't get re-uploaded to your backend. If you used the **Import + Queue for Sync** button on the import dialog instead (the "migration" path), the imported rows do land in the queue and the next sync drains them - the counters here will reflect that until they finish uploading.
+
## Storage Reference
- ~200 bytes per location
diff --git a/apps/mobile/App.tsx b/apps/mobile/App.tsx
index 9af25ee4..4c1e6920 100644
--- a/apps/mobile/App.tsx
+++ b/apps/mobile/App.tsx
@@ -25,7 +25,8 @@ import {
DataManagementScreen,
LocationHistoryScreen,
LocationSummaryScreen,
- ExportDataScreen,
+ ExportLocationsScreen,
+ ImportLocationsScreen,
AboutScreen,
TrackingProfilesScreen,
ProfileEditorScreen,
@@ -94,9 +95,14 @@ const SCREEN_CONFIG: readonly ScreenConfig[] = [
title: "Summary"
},
{
- name: "Export Data",
- component: ExportDataScreen,
- title: "Export Data"
+ name: "Export Locations",
+ component: ExportLocationsScreen,
+ title: "Export Locations"
+ },
+ {
+ name: "Import Locations",
+ component: ImportLocationsScreen,
+ title: "Import Locations"
},
{
name: "Auto-Export",
diff --git a/apps/mobile/android/app/src/main/java/com/colota/bridge/BackupServiceModule.kt b/apps/mobile/android/app/src/main/java/com/colota/bridge/BackupServiceModule.kt
index 5555498d..ae8783eb 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/bridge/BackupServiceModule.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/bridge/BackupServiceModule.kt
@@ -13,7 +13,6 @@ import com.Colota.backup.BackupBuilder
import com.Colota.backup.BackupException
import com.Colota.backup.BackupForegroundService
import com.Colota.backup.BackupOrphanCleanup
-import com.Colota.backup.BackupPickerCoordinator
import com.Colota.backup.BackupRestorer
import com.Colota.backup.PasswordStrength
import com.Colota.data.DatabaseHelper
@@ -22,6 +21,7 @@ import com.Colota.export.AutoExportScheduler
import com.Colota.export.AutoExportWorker
import com.Colota.service.LocationForegroundService
import com.Colota.util.AppLogger
+import com.Colota.util.SafPickerCoordinator
import com.facebook.react.ReactApplication
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.Promise
@@ -60,11 +60,17 @@ class BackupServiceModule(reactContext: ReactApplicationContext) :
private const val SERVICE_STOP_TIMEOUT_MS = 5_000L
private const val SERVICE_STOP_POLL_MS = 50L
+
+ private const val REQUEST_PICK_DESTINATION = 9101
+ private const val REQUEST_PICK_SOURCE = 9102
+
+ private const val DEFAULT_BACKUP_FILENAME = "colota_backup.colota"
+ private const val MIME_BACKUP = "application/octet-stream"
}
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
private val operationMutex = Mutex()
- private val pickerCoordinator = BackupPickerCoordinator(reactContext, scope)
+ private val pickerCoordinator = SafPickerCoordinator(reactContext, scope)
override fun getName() = "BackupServiceModule"
@@ -76,12 +82,22 @@ class BackupServiceModule(reactContext: ReactApplicationContext) :
@ReactMethod
fun pickBackupDestination(promise: Promise) {
- pickerCoordinator.pickDestination(promise)
+ pickerCoordinator.pickCreateDocument(
+ mime = MIME_BACKUP,
+ defaultName = DEFAULT_BACKUP_FILENAME,
+ requestCode = REQUEST_PICK_DESTINATION,
+ promise = promise,
+ )
}
@ReactMethod
fun pickBackupSource(promise: Promise) {
- pickerCoordinator.pickSource(promise)
+ pickerCoordinator.pickOpenDocument(
+ mime = MIME_BACKUP,
+ extraMimes = null,
+ requestCode = REQUEST_PICK_SOURCE,
+ promise = promise,
+ )
}
@ReactMethod
diff --git a/apps/mobile/android/app/src/main/java/com/colota/bridge/ImportServiceModule.kt b/apps/mobile/android/app/src/main/java/com/colota/bridge/ImportServiceModule.kt
new file mode 100644
index 00000000..03973e5d
--- /dev/null
+++ b/apps/mobile/android/app/src/main/java/com/colota/bridge/ImportServiceModule.kt
@@ -0,0 +1,250 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.bridge
+
+import android.net.Uri
+import com.Colota.data.DatabaseHelper
+import com.Colota.importer.CommitOptions
+import com.Colota.importer.ImportFormat
+import com.Colota.importer.ImportRow
+import com.Colota.importer.LocationImporter
+import com.Colota.importer.PreviewResult
+import com.Colota.importer.UnsupportedFormatException
+import com.Colota.service.ServiceConfig
+import com.Colota.sync.PayloadBuilder
+import com.Colota.util.AppLogger
+import com.Colota.util.SafPickerCoordinator
+import com.facebook.react.bridge.Arguments
+import com.facebook.react.bridge.Promise
+import com.facebook.react.bridge.ReactApplicationContext
+import com.facebook.react.bridge.ReactContextBaseJavaModule
+import com.facebook.react.bridge.ReactMethod
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.Job
+import kotlinx.coroutines.SupervisorJob
+import kotlinx.coroutines.cancel
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.sync.Mutex
+import java.util.concurrent.atomic.AtomicBoolean
+
+class ImportServiceModule(reactContext: ReactApplicationContext) :
+ ReactContextBaseJavaModule(reactContext) {
+
+ companion object {
+ private const val TAG = "ImportServiceModule"
+ private const val REQUEST_PICK_IMPORT = 9201
+
+ // A large Google Timeline preview can hold 80+ MB of parsed rows on the instance;
+ // discard if the user walks away without committing.
+ private const val STASH_TTL_MS = 15L * 60_000L
+
+ // Bare type stays "*/*" because some SAF providers honour EXTRA_MIME_TYPES strictly
+ // and refuse `.geojson` / `.gpx` without the wildcard. Including "*/*" here would
+ // defeat the hint.
+ private val IMPORT_MIME_TYPES = arrayOf(
+ "application/json",
+ "application/geo+json",
+ "application/gpx+xml",
+ "application/vnd.google-earth.kml+xml",
+ "application/xml",
+ "text/xml",
+ "text/csv",
+ "application/octet-stream",
+ "text/plain",
+ )
+ }
+
+ private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
+ private val operationMutex = Mutex()
+ private val pickerCoordinator = SafPickerCoordinator(reactApplicationContext, scope)
+
+ // Guards both pendingImportRows and stashTimeoutJob; parse runs outside the lock,
+ // only the assignment crosses it.
+ private val stashLock = Any()
+ private var pendingImportRows: List<ImportRow>? = null
+ private var stashTimeoutJob: Job? = null
+ private val importCancelled = AtomicBoolean(false)
+
+ override fun getName() = "ImportServiceModule"
+
+ override fun invalidate() {
+ super.invalidate()
+ pickerCoordinator.dispose()
+ importCancelled.set(true)
+ clearStash()
+ scope.cancel()
+ }
+
+ @ReactMethod
+ fun pickImportSource(promise: Promise) {
+ pickerCoordinator.pickOpenDocument(
+ mime = "*/*",
+ extraMimes = IMPORT_MIME_TYPES,
+ requestCode = REQUEST_PICK_IMPORT,
+ promise = promise,
+ )
+ }
+
+ @ReactMethod
+ fun importLocationsFromFile(uriString: String, promise: Promise) {
+ scope.launch {
+ if (!operationMutex.tryLock()) {
+ promise.reject("E_BUSY", "Another import is already in progress")
+ return@launch
+ }
+ try {
+ // Reset inside the mutex so a stray cancelImport can't flip the flag back
+ // to true between unlock and this set.
+ importCancelled.set(false)
+ val uri = Uri.parse(uriString)
+ val db = DatabaseHelper.getInstance(reactApplicationContext)
+ val preview = LocationImporter.preview(
+ contentResolver = reactApplicationContext.contentResolver,
+ uri = uri,
+ db = db,
+ cancelled = importCancelled,
+ )
+ stash(preview.rowsToCommit)
+ promise.resolve(previewToMap(preview, db))
+ } catch (e: InterruptedException) {
+ clearStash()
+ promise.reject("E_IMPORT_CANCELLED", "Import cancelled")
+ } catch (e: UnsupportedFormatException) {
+ clearStash()
+ AppLogger.w(TAG, "Unsupported import format: ${e.message}")
+ promise.reject("E_IMPORT_UNSUPPORTED", e.message ?: "Unsupported file format")
+ } catch (e: Exception) {
+ clearStash()
+ AppLogger.e(TAG, "Import preview failed", e)
+ promise.reject("E_IMPORT_FAILED", e.message ?: "Import failed", e)
+ } finally {
+ operationMutex.unlock()
+ }
+ }
+ }
+
+ @ReactMethod
+ fun commitImport(asQueued: Boolean, promise: Promise) {
+ scope.launch {
+ if (!operationMutex.tryLock()) {
+ promise.reject("E_BUSY", "Another import is already in progress")
+ return@launch
+ }
+ try {
+ val rows = synchronized(stashLock) { pendingImportRows }
+ if (rows == null) {
+ promise.reject("E_IMPORT_NO_PENDING", "No import to commit. Pick a file first.")
+ return@launch
+ }
+ val db = DatabaseHelper.getInstance(reactApplicationContext)
+ if (asQueued && !canQueueForSync(db)) {
+ promise.reject(
+ "E_IMPORT_SYNC_UNAVAILABLE",
+ "Cannot queue imports for sync: no endpoint configured or offline mode is on",
+ )
+ return@launch
+ }
+ val options = buildCommitOptions(db, asQueued)
+ val inserted = LocationImporter.commit(db, rows, options)
+ clearStash()
+ promise.resolve(inserted)
+ } catch (e: Exception) {
+ AppLogger.e(TAG, "commitImport failed", e)
+ promise.reject("E_IMPORT_FAILED", e.message ?: "Import commit failed", e)
+ } finally {
+ operationMutex.unlock()
+ }
+ }
+ }
+
+ @ReactMethod
+ fun cancelImport(promise: Promise) {
+ // Signal then clear: covers both an in-flight parse and the case where the parse
+ // already finished and we're racing with its staged-rows assignment.
+ importCancelled.set(true)
+ clearStash()
+ promise.resolve(true)
+ }
+
+ private fun stash(rows: List<ImportRow>) {
+ synchronized(stashLock) {
+ pendingImportRows = rows
+ stashTimeoutJob?.cancel()
+ // Identity-check self on expiry so a newer stash replacing this job isn't cleared.
+ stashTimeoutJob = scope.launch {
+ val self = coroutineContext[Job]
+ delay(STASH_TTL_MS)
+ synchronized(stashLock) {
+ if (stashTimeoutJob === self) {
+ AppLogger.w(
+ TAG,
+ "Import stash expired without commit, discarding ${pendingImportRows?.size ?: 0} rows",
+ )
+ pendingImportRows = null
+ stashTimeoutJob = null
+ }
+ }
+ }
+ }
+ }
+
+ private fun clearStash() {
+ synchronized(stashLock) {
+ pendingImportRows = null
+ stashTimeoutJob?.cancel()
+ stashTimeoutJob = null
+ }
+ }
+
+ private fun previewToMap(preview: PreviewResult, db: DatabaseHelper) = Arguments.createMap().apply {
+ putString("format", formatToWireName(preview.format))
+ putInt("totalParsed", preview.totalParsed)
+ putInt("invalid", preview.invalid)
+ putInt("duplicates", preview.duplicates)
+ putInt("newRows", preview.rowsToCommit.size)
+ if (preview.dateRangeStartSec != null) {
+ putDouble("dateRangeStartSec", preview.dateRangeStartSec.toDouble())
+ } else {
+ putNull("dateRangeStartSec")
+ }
+ if (preview.dateRangeEndSec != null) {
+ putDouble("dateRangeEndSec", preview.dateRangeEndSec.toDouble())
+ } else {
+ putNull("dateRangeEndSec")
+ }
+ // Reflects DB state at preview time; checked again on commit.
+ putBoolean("canQueueForSync", canQueueForSync(db))
+ }
+
+ private fun canQueueForSync(db: DatabaseHelper): Boolean {
+ val config = ServiceConfig.fromDatabase(db)
+ return !config.isOfflineMode && config.endpoint.isNotBlank()
+ }
+
+ private fun buildCommitOptions(db: DatabaseHelper, asQueued: Boolean): CommitOptions {
+ if (!asQueued) return CommitOptions(asQueued = false)
+ val config = ServiceConfig.fromDatabase(db)
+ return CommitOptions(
+ asQueued = true,
+ endpoint = config.endpoint,
+ isOfflineMode = config.isOfflineMode,
+ fieldMap = PayloadBuilder.parseFieldMap(config.fieldMap).orEmpty(),
+ customFields = PayloadBuilder.parseCustomFields(config.customFields).orEmpty(),
+ apiFormat = config.apiFormat,
+ )
+ }
+
+ private fun formatToWireName(format: ImportFormat): String = when (format) {
+ ImportFormat.GEOJSON -> "geojson"
+ ImportFormat.GOOGLE_TIMELINE_LEGACY -> "google_timeline_legacy"
+ ImportFormat.GOOGLE_TIMELINE_NEW -> "google_timeline_new"
+ ImportFormat.GPX -> "gpx"
+ ImportFormat.KML -> "kml"
+ ImportFormat.CSV -> "csv"
+ }
+}
diff --git a/apps/mobile/android/app/src/main/java/com/colota/bridge/LocationServicePackage.kt b/apps/mobile/android/app/src/main/java/com/colota/bridge/LocationServicePackage.kt
index 66ba0f74..c79069d1 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/bridge/LocationServicePackage.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/bridge/LocationServicePackage.kt
@@ -24,7 +24,8 @@ class LocationServicePackage : ReactPackage {
LocationServiceModule(reactContext),
MtlsBridgeModule(reactContext),
BuildConfigModule(reactContext),
- BackupServiceModule(reactContext)
+ BackupServiceModule(reactContext),
+ ImportServiceModule(reactContext)
)
}
diff --git a/apps/mobile/android/app/src/main/java/com/colota/data/DatabaseHelper.kt b/apps/mobile/android/app/src/main/java/com/colota/data/DatabaseHelper.kt
index f923b39f..83c3c5ea 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/data/DatabaseHelper.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/data/DatabaseHelper.kt
@@ -361,6 +361,91 @@ class DatabaseHelper private constructor(context: Context) :
}
}
+ // Single-transaction bulk insert for imports. Rows go in with sent=1 (skip the queue)
+ // by default; pass non-null payloads to also enqueue them with sent=0.
+ fun bulkInsertImportedLocations(
+ rows: List<com.Colota.importer.ImportRow>,
+ payloads: List<String>? = null,
+ ): Int {
+ requireNotRestoring()
+ if (rows.isEmpty()) return 0
+ require(payloads == null || payloads.size == rows.size) {
+ "payloads length (${payloads?.size}) must match rows length (${rows.size})"
+ }
+ val db = writableDatabase
+ val locationSql = """
+ INSERT INTO $TABLE_LOCATIONS
+ (latitude, longitude, accuracy, altitude, speed, bearing, battery, timestamp, sent, created_at)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+ """.trimIndent()
+ val queueSql = """
+ INSERT INTO $TABLE_QUEUE
+ (location_id, payload, retry_count, created_at)
+ VALUES (?, ?, 0, ?)
+ """.trimIndent()
+ val sentFlag: Long = if (payloads == null) 1L else 0L
+ val nowSec = System.currentTimeMillis() / 1000
+ var inserted = 0
+ db.beginTransaction()
+ try {
+ val locationStmt = db.compileStatement(locationSql)
+ val queueStmt = if (payloads != null) db.compileStatement(queueSql) else null
+ try {
+ for ((i, row) in rows.withIndex()) {
+ locationStmt.clearBindings()
+ locationStmt.bindDouble(1, row.latitude)
+ locationStmt.bindDouble(2, row.longitude)
+ if (row.accuracy != null) locationStmt.bindLong(3, row.accuracy.toLong()) else locationStmt.bindNull(3)
+ if (row.altitude != null) locationStmt.bindLong(4, row.altitude.toLong()) else locationStmt.bindNull(4)
+ if (row.speed != null) locationStmt.bindLong(5, row.speed.toLong()) else locationStmt.bindNull(5)
+ if (row.bearing != null) locationStmt.bindDouble(6, row.bearing) else locationStmt.bindNull(6)
+ if (row.battery != null) locationStmt.bindLong(7, row.battery.toLong()) else locationStmt.bindNull(7)
+ locationStmt.bindLong(8, row.timestamp)
+ locationStmt.bindLong(9, sentFlag)
+ locationStmt.bindLong(10, nowSec)
+ val locationId = locationStmt.executeInsert()
+ if (locationId == -1L) continue
+ inserted++
+ if (queueStmt != null && payloads != null) {
+ queueStmt.clearBindings()
+ queueStmt.bindLong(1, locationId)
+ queueStmt.bindString(2, payloads[i])
+ queueStmt.bindLong(3, nowSec)
+ queueStmt.executeInsert()
+ }
+ }
+ } finally {
+ locationStmt.close()
+ queueStmt?.close()
+ }
+ db.setTransactionSuccessful()
+ } finally {
+ db.endTransaction()
+ }
+ return inserted
+ }
+
+ // ASC-sorted stream of (ts, lat, lon) triples for merge-walk dedup. Memory stays
+ // O(1) because the caller never materialises the existing-key set.
+ fun forEachLocationKeyInRange(
+ startTs: Long,
+ endTs: Long,
+ consumer: (Long, Double, Double) -> Unit,
+ ) {
+ readableDatabase.query(
+ TABLE_LOCATIONS,
+ arrayOf("timestamp", "latitude", "longitude"),
+ "timestamp >= ? AND timestamp <= ?",
+ arrayOf(startTs.toString(), endTs.toString()),
+ null, null,
+ "timestamp ASC, latitude ASC, longitude ASC",
+ ).use { cursor ->
+ while (cursor.moveToNext()) {
+ consumer(cursor.getLong(0), cursor.getDouble(1), cursor.getDouble(2))
+ }
+ }
+ }
+
fun saveLocation(
latitude: Double,
longitude: Double,
diff --git a/apps/mobile/android/app/src/main/java/com/colota/importer/CsvParser.kt b/apps/mobile/android/app/src/main/java/com/colota/importer/CsvParser.kt
new file mode 100644
index 00000000..f5dfd6e9
--- /dev/null
+++ b/apps/mobile/android/app/src/main/java/com/colota/importer/CsvParser.kt
@@ -0,0 +1,126 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.importer
+
+import java.io.BufferedReader
+import java.io.InputStream
+import java.io.InputStreamReader
+import java.util.concurrent.atomic.AtomicBoolean
+
+/** Streaming CSV parser. Column order is driven by the header; lat/lon plus one of
+ * the timestamp aliases are required, the rest are optional. */
+object CsvParser {
+
+ private val LAT_ALIASES = setOf("latitude", "lat")
+ private val LON_ALIASES = setOf("longitude", "lon", "lng", "long")
+ private val TS_NUMERIC_ALIASES = setOf("timestamp", "ts")
+ private val TS_ISO_ALIASES = setOf("iso_time", "time", "datetime", "date_time")
+ private val ACCURACY_ALIASES = setOf("accuracy", "acc")
+ private val ALTITUDE_ALIASES = setOf("altitude", "alt", "elevation", "ele")
+ private val SPEED_ALIASES = setOf("speed", "velocity", "vel")
+ private val BEARING_ALIASES = setOf("bearing", "heading", "course", "bear")
+ private val BATTERY_ALIASES = setOf("battery", "batt")
+
+ fun parse(
+ input: InputStream,
+ cancelled: AtomicBoolean,
+ nowSec: Long,
+ ): ParseResult {
+ val rows = ArrayList<ImportRow>()
+ var invalid = 0
+
+ BufferedReader(InputStreamReader(input, Charsets.UTF_8)).use { reader ->
+ val headerLine = reader.readLine()
+ ?: return ParseResult(rows, invalid)
+ val cols = parseHeader(headerLine)
+ if (cols.lat < 0 || cols.lon < 0 || (cols.tsNumeric < 0 && cols.tsIso < 0)) {
+ throw UnsupportedFormatException("CSV header lacks required columns: latitude, longitude, and a timestamp column")
+ }
+
+ var line = reader.readLine()
+ while (line != null) {
+ if (cancelled.get()) throw InterruptedException("Import cancelled")
+ if (line.isBlank()) { line = reader.readLine(); continue }
+ val row = readDataRow(line, cols, nowSec)
+ if (row != null) rows.add(row) else invalid++
+ line = reader.readLine()
+ }
+ }
+
+ return ParseResult(rows, invalid)
+ }
+
+ // Column indices into the parsed header row; -1 means "absent".
+ internal data class HeaderCols(
+ val lat: Int,
+ val lon: Int,
+ val tsNumeric: Int,
+ val tsIso: Int,
+ val accuracy: Int,
+ val altitude: Int,
+ val speed: Int,
+ val bearing: Int,
+ val battery: Int,
+ )
+
+ internal fun parseHeader(line: String): HeaderCols {
+ val parts = splitCsvLine(line).map { it.lowercase().trim() }
+ fun indexOfAny(aliases: Set<String>): Int = parts.indexOfFirst { it in aliases }
+ return HeaderCols(
+ lat = indexOfAny(LAT_ALIASES),
+ lon = indexOfAny(LON_ALIASES),
+ tsNumeric = indexOfAny(TS_NUMERIC_ALIASES),
+ tsIso = indexOfAny(TS_ISO_ALIASES),
+ accuracy = indexOfAny(ACCURACY_ALIASES),
+ altitude = indexOfAny(ALTITUDE_ALIASES),
+ speed = indexOfAny(SPEED_ALIASES),
+ bearing = indexOfAny(BEARING_ALIASES),
+ battery = indexOfAny(BATTERY_ALIASES),
+ )
+ }
+
+ private fun readDataRow(line: String, cols: HeaderCols, nowSec: Long): ImportRow? {
+ val parts = splitCsvLine(line)
+ // Leftover `"` means a quoted field had an unescaped comma; column alignment is
+ // unsafe, drop the row rather than risk shifted lat/lon.
+ if (parts.any { it.contains('"') }) return null
+ fun field(idx: Int): String? = if (idx >= 0 && idx < parts.size) parts[idx].trim().takeIf { it.isNotEmpty() } else null
+
+ val lat = field(cols.lat)?.toDoubleOrNull() ?: return null
+ val lon = field(cols.lon)?.toDoubleOrNull() ?: return null
+
+ val ts = field(cols.tsNumeric)?.let { v ->
+ val n = v.toDoubleOrNull() ?: return@let null
+ // Tolerate foreign CSVs that write milliseconds; Colota's own export is seconds.
+ if (n > 1e12) (n / 1000).toLong() else n.toLong()
+ } ?: field(cols.tsIso)?.let { parseIso8601Seconds(it) } ?: return null
+
+ if (!isValidLocation(lat, lon, ts, nowSec)) return null
+
+ return ImportRow(
+ timestamp = ts,
+ latitude = lat,
+ longitude = lon,
+ accuracy = field(cols.accuracy)?.toDoubleOrNull()?.toInt(),
+ altitude = field(cols.altitude)?.toDoubleOrNull()?.toInt(),
+ speed = field(cols.speed)?.toDoubleOrNull()?.toInt(),
+ bearing = field(cols.bearing)?.toDoubleOrNull(),
+ battery = field(cols.battery)?.toDoubleOrNull()?.toInt(),
+ )
+ }
+
+ // Single-layer quote stripping. Embedded commas in quoted fields aren't supported.
+ internal fun splitCsvLine(line: String): List<String> {
+ return line.split(",").map { field ->
+ val trimmed = field.trim()
+ if (trimmed.length >= 2 && trimmed.startsWith("\"") && trimmed.endsWith("\"")) {
+ trimmed.substring(1, trimmed.length - 1)
+ } else {
+ trimmed
+ }
+ }
+ }
+}
diff --git a/apps/mobile/android/app/src/main/java/com/colota/importer/GeoJsonParser.kt b/apps/mobile/android/app/src/main/java/com/colota/importer/GeoJsonParser.kt
new file mode 100644
index 00000000..53e603d1
--- /dev/null
+++ b/apps/mobile/android/app/src/main/java/com/colota/importer/GeoJsonParser.kt
@@ -0,0 +1,179 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.importer
+
+import android.util.JsonReader
+import android.util.JsonToken
+import java.io.InputStream
+import java.util.concurrent.atomic.AtomicBoolean
+
+/** Streaming GeoJSON parser. Reads FeatureCollection of Point features; non-Point
+ * features are counted invalid and skipped. */
+object GeoJsonParser {
+
+ fun parse(
+ input: InputStream,
+ cancelled: AtomicBoolean,
+ nowSec: Long,
+ ): ParseResult {
+ val rows = ArrayList<ImportRow>()
+ var invalid = 0
+
+ JsonReader(input.reader(Charsets.UTF_8)).use { reader ->
+ reader.beginObject()
+ while (reader.hasNext()) {
+ if (cancelled.get()) throw InterruptedException("Import cancelled")
+ when (reader.nextName()) {
+ "features" -> {
+ reader.beginArray()
+ while (reader.hasNext()) {
+ if (cancelled.get()) throw InterruptedException("Import cancelled")
+ val row = readFeature(reader, nowSec)
+ if (row != null) rows.add(row) else invalid++
+ }
+ reader.endArray()
+ }
+ else -> reader.skipValue()
+ }
+ }
+ reader.endObject()
+ }
+
+ return ParseResult(rows = rows, invalid = invalid)
+ }
+
+ private fun readFeature(reader: JsonReader, nowSec: Long): ImportRow? {
+ var lat: Double? = null
+ var lon: Double? = null
+ var ts: Long? = null
+ var accuracy: Int? = null
+ var altitude: Int? = null
+ var speed: Int? = null
+ var bearing: Double? = null
+ var battery: Int? = null
+ var geometryType: String? = null
+
+ reader.beginObject()
+ while (reader.hasNext()) {
+ when (reader.nextName()) {
+ "geometry" -> {
+ if (reader.peek() == JsonToken.NULL) {
+ reader.nextNull()
+ continue
+ }
+ reader.beginObject()
+ while (reader.hasNext()) {
+ when (reader.nextName()) {
+ "type" -> geometryType = reader.nextString()
+ "coordinates" -> {
+ if (reader.peek() == JsonToken.NULL) {
+ reader.nextNull()
+ } else {
+ val coords = readCoordinates(reader)
+ if (coords != null) {
+ lon = coords.first
+ lat = coords.second
+ }
+ }
+ }
+ else -> reader.skipValue()
+ }
+ }
+ reader.endObject()
+ }
+ "properties" -> {
+ if (reader.peek() == JsonToken.NULL) {
+ reader.nextNull()
+ continue
+ }
+ reader.beginObject()
+ while (reader.hasNext()) {
+ when (reader.nextName()) {
+ "time", "timestamp" -> ts = readTimestamp(reader)
+ "accuracy" -> accuracy = reader.readNullableInt()
+ "altitude", "elevation", "ele" -> altitude = reader.readNullableInt()
+ "speed", "velocity" -> speed = reader.readNullableInt()
+ "bearing", "heading" -> bearing = reader.readNullableDouble()
+ "battery" -> battery = reader.readNullableInt()
+ else -> reader.skipValue()
+ }
+ }
+ reader.endObject()
+ }
+ else -> reader.skipValue()
+ }
+ }
+ reader.endObject()
+
+ if (geometryType != "Point") return null
+ val tsVal = ts ?: return null
+ val latVal = lat ?: return null
+ val lonVal = lon ?: return null
+ if (!isValidLocation(latVal, lonVal, tsVal, nowSec)) return null
+
+ return ImportRow(
+ timestamp = tsVal,
+ latitude = latVal,
+ longitude = lonVal,
+ accuracy = accuracy,
+ altitude = altitude,
+ speed = speed,
+ bearing = bearing,
+ battery = battery,
+ )
+ }
+
+ // Returns null on non-Point shapes; still consumes the array to keep the reader aligned.
+ private fun readCoordinates(reader: JsonReader): Pair<Double, Double>? {
+ reader.beginArray()
+ if (!reader.hasNext() || reader.peek() != JsonToken.NUMBER) {
+ while (reader.hasNext()) reader.skipValue()
+ reader.endArray()
+ return null
+ }
+ val first = reader.nextDouble()
+ if (!reader.hasNext() || reader.peek() != JsonToken.NUMBER) {
+ while (reader.hasNext()) reader.skipValue()
+ reader.endArray()
+ return null
+ }
+ val second = reader.nextDouble()
+ while (reader.hasNext()) reader.skipValue()
+ reader.endArray()
+ return first to second
+ }
+
+ private fun readTimestamp(reader: JsonReader): Long? {
+ return when (reader.peek()) {
+ JsonToken.NULL -> { reader.nextNull(); null }
+ JsonToken.NUMBER -> {
+ val n = reader.nextDouble()
+ if (n > 1e12) (n / 1000).toLong() else n.toLong()
+ }
+ JsonToken.STRING -> {
+ val s = reader.nextString()
+ parseIso8601Seconds(s)
+ }
+ else -> { reader.skipValue(); null }
+ }
+ }
+
+}
+
+internal fun isValidLocation(lat: Double, lon: Double, tsSec: Long, nowSec: Long): Boolean {
+ if (lat.isNaN() || lon.isNaN()) return false
+ if (lat < -90.0 || lat > 90.0) return false
+ if (lon < -180.0 || lon > 180.0) return false
+ if (tsSec <= 0) return false
+ // 5-minute clock-skew tolerance; further into the future is treated as corrupt.
+ if (tsSec > nowSec + 5 * 60) return false
+ return true
+}
+
+data class ParseResult(
+ val rows: List<ImportRow>,
+ val invalid: Int,
+)
diff --git a/apps/mobile/android/app/src/main/java/com/colota/importer/GoogleTimelineParser.kt b/apps/mobile/android/app/src/main/java/com/colota/importer/GoogleTimelineParser.kt
new file mode 100644
index 00000000..7575eca3
--- /dev/null
+++ b/apps/mobile/android/app/src/main/java/com/colota/importer/GoogleTimelineParser.kt
@@ -0,0 +1,278 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.importer
+
+import android.util.JsonReader
+import android.util.JsonToken
+import java.io.InputStream
+import java.util.concurrent.atomic.AtomicBoolean
+
+/** Streaming Google Timeline parser. Handles legacy Takeout `Records.json` and the
+ * on-device `semanticSegments` / `rawSignals` schema in one pass. */
+object GoogleTimelineParser {
+
+ fun parse(
+ input: InputStream,
+ cancelled: AtomicBoolean,
+ nowSec: Long,
+ ): ParseResult {
+ val rows = ArrayList<ImportRow>()
+ var invalid = 0
+
+ JsonReader(input.reader(Charsets.UTF_8)).use { reader ->
+ reader.beginObject()
+ while (reader.hasNext()) {
+ if (cancelled.get()) throw InterruptedException("Import cancelled")
+ when (reader.nextName()) {
+ "locations" -> invalid += readLegacyLocations(reader, cancelled, rows, nowSec)
+ "semanticSegments" -> invalid += readSemanticSegments(reader, cancelled, rows, nowSec)
+ "rawSignals" -> invalid += readRawSignals(reader, cancelled, rows, nowSec)
+ else -> reader.skipValue()
+ }
+ }
+ reader.endObject()
+ }
+
+ return ParseResult(rows, invalid)
+ }
+
+ private fun readLegacyLocations(
+ reader: JsonReader,
+ cancelled: AtomicBoolean,
+ rows: ArrayList<ImportRow>,
+ nowSec: Long,
+ ): Int {
+ var invalid = 0
+ reader.beginArray()
+ while (reader.hasNext()) {
+ if (cancelled.get()) throw InterruptedException("Import cancelled")
+ val row = readLegacyLocation(reader, nowSec)
+ if (row != null) rows.add(row) else invalid++
+ }
+ reader.endArray()
+ return invalid
+ }
+
+ private fun readLegacyLocation(reader: JsonReader, nowSec: Long): ImportRow? {
+ var latE7: Long? = null
+ var lonE7: Long? = null
+ var ts: Long? = null
+ var accuracy: Int? = null
+ var altitude: Int? = null
+ var speed: Int? = null
+
+ reader.beginObject()
+ while (reader.hasNext()) {
+ when (reader.nextName()) {
+ "latitudeE7" -> latE7 = reader.nextLong()
+ "longitudeE7" -> lonE7 = reader.nextLong()
+ "timestampMs" -> ts = when (reader.peek()) {
+ JsonToken.STRING -> reader.nextString().toLongOrNull()?.div(1000)
+ JsonToken.NUMBER -> reader.nextLong() / 1000
+ else -> { reader.skipValue(); null }
+ }
+ "timestamp" -> ts = when (reader.peek()) {
+ JsonToken.STRING -> parseIso8601Seconds(reader.nextString())
+ else -> { reader.skipValue(); null }
+ }
+ "accuracy" -> accuracy = reader.readNullableInt()
+ "altitude" -> altitude = reader.readNullableInt()
+ "velocity" -> speed = reader.readNullableInt()
+ else -> reader.skipValue()
+ }
+ }
+ reader.endObject()
+
+ val lat = latE7?.let { it / 1e7 } ?: return null
+ val lon = lonE7?.let { it / 1e7 } ?: return null
+ val tsVal = ts ?: return null
+ if (!isValidLocation(lat, lon, tsVal, nowSec)) return null
+
+ return ImportRow(
+ timestamp = tsVal,
+ latitude = lat,
+ longitude = lon,
+ accuracy = accuracy,
+ altitude = altitude,
+ speed = speed,
+ bearing = null,
+ battery = null,
+ )
+ }
+
+ // semanticSegments[] also carries `visit` / `activity` summaries - skip those, they're
+ // higher-level inferences without per-sample fixes.
+ private fun readSemanticSegments(
+ reader: JsonReader,
+ cancelled: AtomicBoolean,
+ rows: ArrayList<ImportRow>,
+ nowSec: Long,
+ ): Int {
+ var invalid = 0
+ reader.beginArray()
+ while (reader.hasNext()) {
+ if (cancelled.get()) throw InterruptedException("Import cancelled")
+ invalid += readSemanticSegment(reader, cancelled, rows, nowSec)
+ }
+ reader.endArray()
+ return invalid
+ }
+
+ private fun readSemanticSegment(
+ reader: JsonReader,
+ cancelled: AtomicBoolean,
+ rows: ArrayList<ImportRow>,
+ nowSec: Long,
+ ): Int {
+ var invalid = 0
+ reader.beginObject()
+ while (reader.hasNext()) {
+ when (reader.nextName()) {
+ "timelinePath" -> invalid += readTimelinePath(reader, cancelled, rows, nowSec)
+ else -> reader.skipValue()
+ }
+ }
+ reader.endObject()
+ return invalid
+ }
+
+ private fun readTimelinePath(
+ reader: JsonReader,
+ cancelled: AtomicBoolean,
+ rows: ArrayList<ImportRow>,
+ nowSec: Long,
+ ): Int {
+ var invalid = 0
+ reader.beginArray()
+ while (reader.hasNext()) {
+ if (cancelled.get()) throw InterruptedException("Import cancelled")
+ var pointStr: String? = null
+ var timeStr: String? = null
+ reader.beginObject()
+ while (reader.hasNext()) {
+ when (reader.nextName()) {
+ "point" -> pointStr = if (reader.peek() == JsonToken.STRING) reader.nextString() else { reader.skipValue(); null }
+ "time" -> timeStr = if (reader.peek() == JsonToken.STRING) reader.nextString() else { reader.skipValue(); null }
+ else -> reader.skipValue()
+ }
+ }
+ reader.endObject()
+
+ val coords = pointStr?.let(::parseLatLngString)
+ val ts = timeStr?.let(::parseIso8601Seconds)
+ if (coords != null && ts != null && isValidLocation(coords.first, coords.second, ts, nowSec)) {
+ rows.add(
+ ImportRow(
+ timestamp = ts,
+ latitude = coords.first,
+ longitude = coords.second,
+ accuracy = null,
+ altitude = null,
+ speed = null,
+ bearing = null,
+ battery = null,
+ )
+ )
+ } else {
+ invalid++
+ }
+ }
+ reader.endArray()
+ return invalid
+ }
+
+ private fun readRawSignals(
+ reader: JsonReader,
+ cancelled: AtomicBoolean,
+ rows: ArrayList<ImportRow>,
+ nowSec: Long,
+ ): Int {
+ var invalid = 0
+ reader.beginArray()
+ while (reader.hasNext()) {
+ if (cancelled.get()) throw InterruptedException("Import cancelled")
+ val row = readRawSignal(reader, nowSec)
+ if (row != null) rows.add(row) else invalid++
+ }
+ reader.endArray()
+ return invalid
+ }
+
+ private fun readRawSignal(reader: JsonReader, nowSec: Long): ImportRow? {
+ var pos: PositionData? = null
+ reader.beginObject()
+ while (reader.hasNext()) {
+ when (reader.nextName()) {
+ "position" -> {
+ if (reader.peek() == JsonToken.NULL) {
+ reader.nextNull()
+ } else {
+ pos = readPosition(reader)
+ }
+ }
+ else -> reader.skipValue()
+ }
+ }
+ reader.endObject()
+
+ val p = pos ?: return null
+ val coords = p.latLng?.let(::parseLatLngString) ?: return null
+ val ts = p.timestamp ?: return null
+ if (!isValidLocation(coords.first, coords.second, ts, nowSec)) return null
+
+ return ImportRow(
+ timestamp = ts,
+ latitude = coords.first,
+ longitude = coords.second,
+ accuracy = p.accuracyMeters,
+ altitude = p.altitudeMeters,
+ speed = p.speedMetersPerSecond,
+ bearing = null,
+ battery = null,
+ )
+ }
+
+ private data class PositionData(
+ val latLng: String?,
+ val accuracyMeters: Int?,
+ val altitudeMeters: Int?,
+ val speedMetersPerSecond: Int?,
+ val timestamp: Long?,
+ )
+
+ private fun readPosition(reader: JsonReader): PositionData {
+ var latLng: String? = null
+ var accuracy: Int? = null
+ var altitude: Int? = null
+ var speed: Int? = null
+ var ts: Long? = null
+ reader.beginObject()
+ while (reader.hasNext()) {
+ when (reader.nextName()) {
+ "LatLng", "latLng" -> latLng = if (reader.peek() == JsonToken.STRING) reader.nextString() else { reader.skipValue(); null }
+ "accuracyMeters" -> accuracy = reader.readNullableInt()
+ "altitudeMeters" -> altitude = reader.readNullableInt()
+ "speedMetersPerSecond" -> speed = reader.readNullableInt()
+ "timestamp" -> ts = if (reader.peek() == JsonToken.STRING) parseIso8601Seconds(reader.nextString()) else { reader.skipValue(); null }
+ else -> reader.skipValue()
+ }
+ }
+ reader.endObject()
+ return PositionData(latLng, accuracy, altitude, speed, ts)
+ }
+
+ // New format encodes coords as e.g. "37.422065°,-122.084089°". The degree sign is
+ // sometimes absent but the shape is always lat,lon comma-separated.
+ internal fun parseLatLngString(s: String): Pair<Double, Double>? {
+ val cleaned = s.replace("°", "").trim()
+ val parts = cleaned.split(",")
+ if (parts.size != 2) return null
+ val lat = parts[0].trim().toDoubleOrNull() ?: return null
+ val lon = parts[1].trim().toDoubleOrNull() ?: return null
+ return lat to lon
+ }
+
+}
diff --git a/apps/mobile/android/app/src/main/java/com/colota/importer/GpxParser.kt b/apps/mobile/android/app/src/main/java/com/colota/importer/GpxParser.kt
new file mode 100644
index 00000000..8e699c31
--- /dev/null
+++ b/apps/mobile/android/app/src/main/java/com/colota/importer/GpxParser.kt
@@ -0,0 +1,143 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.importer
+
+import android.util.Xml
+import org.xmlpull.v1.XmlPullParser
+import java.io.InputStream
+import java.util.concurrent.atomic.AtomicBoolean
+
+/** Streaming GPX parser. wpt, rtept, trkpt are all collected into the flat locations table. */
+object GpxParser {
+
+ private val POINT_ELEMENTS = setOf("wpt", "rtept", "trkpt")
+
+ fun parse(
+ input: InputStream,
+ cancelled: AtomicBoolean,
+ nowSec: Long,
+ ): ParseResult {
+ val rows = ArrayList<ImportRow>()
+ var invalid = 0
+
+ val parser = Xml.newPullParser().apply {
+ setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true)
+ setInput(input, null)
+ }
+
+ var event = parser.eventType
+ while (event != XmlPullParser.END_DOCUMENT) {
+ if (cancelled.get()) throw InterruptedException("Import cancelled")
+ if (event == XmlPullParser.START_TAG && parser.name in POINT_ELEMENTS) {
+ val row = readPoint(parser, nowSec)
+ if (row != null) rows.add(row) else invalid++
+ }
+ event = parser.next()
+ }
+
+ return ParseResult(rows, invalid)
+ }
+
+ private fun readPoint(parser: XmlPullParser, nowSec: Long): ImportRow? {
+ val pointName = parser.name
+ val lat = parser.getAttributeValue(null, "lat")?.toDoubleOrNull()
+ val lon = parser.getAttributeValue(null, "lon")?.toDoubleOrNull()
+
+ var ts: Long? = null
+ var altitude: Int? = null
+ var accuracy: Int? = null
+ var speed: Int? = null
+ var bearing: Double? = null
+ var battery: Int? = null
+
+ var event = parser.next()
+ while (!(event == XmlPullParser.END_TAG && parser.name == pointName)) {
+ if (event == XmlPullParser.START_TAG) {
+ when (parser.name) {
+ "ele" -> altitude = readTextOrNull(parser)?.toDoubleOrNull()?.toInt()
+ "time" -> ts = readTextOrNull(parser)?.let { parseIso8601Seconds(it) }
+ "extensions" -> readExtensions(parser) { extName, value ->
+ when (extName.lowercase()) {
+ "accuracy" -> accuracy = value.toDoubleOrNull()?.toInt()
+ "speed" -> speed = value.toDoubleOrNull()?.toInt()
+ "bearing", "heading", "course" -> bearing = value.toDoubleOrNull()
+ "battery" -> battery = value.toDoubleOrNull()?.toInt()
+ }
+ }
+ else -> skipElement(parser)
+ }
+ }
+ event = parser.next()
+ }
+
+ if (lat == null || lon == null || ts == null) return null
+ if (!isValidLocation(lat, lon, ts, nowSec)) return null
+ return ImportRow(
+ timestamp = ts,
+ latitude = lat,
+ longitude = lon,
+ accuracy = accuracy,
+ altitude = altitude,
+ speed = speed,
+ bearing = bearing,
+ battery = battery,
+ )
+ }
+
+ // Recurses one level into namespaced wrappers like <gpxtpx:TrackPointExtension>
+ // so leaf children (speed, course, ...) are picked up.
+ private fun readExtensions(parser: XmlPullParser, onValue: (String, String) -> Unit) {
+ var event = parser.next()
+ while (!(event == XmlPullParser.END_TAG && parser.name == "extensions")) {
+ if (event == XmlPullParser.START_TAG) {
+ val name = parser.name
+ val text = readTextOrNull(parser)
+ if (text != null) {
+ onValue(name, text)
+ } else {
+ readNestedExtensionContainer(parser, name, onValue)
+ }
+ }
+ event = parser.next()
+ }
+ }
+
+ private fun readNestedExtensionContainer(
+ parser: XmlPullParser,
+ containerName: String,
+ onValue: (String, String) -> Unit,
+ ) {
+ var event = parser.eventType
+ while (!(event == XmlPullParser.END_TAG && parser.name == containerName)) {
+ if (event == XmlPullParser.START_TAG) {
+ val leafName = parser.name
+ val text = readTextOrNull(parser)
+ if (text != null) onValue(leafName, text) else skipElement(parser)
+ }
+ event = parser.next()
+ }
+ }
+
+ private fun readTextOrNull(parser: XmlPullParser): String? {
+ val event = parser.next()
+ if (event != XmlPullParser.TEXT) return null
+ val text = parser.text?.trim()?.takeIf { it.isNotEmpty() }
+ parser.next()
+ return text
+ }
+
+ private fun skipElement(parser: XmlPullParser) {
+ if (parser.eventType != XmlPullParser.START_TAG) return
+ var depth = 1
+ while (depth > 0) {
+ when (parser.next()) {
+ XmlPullParser.END_TAG -> depth--
+ XmlPullParser.START_TAG -> depth++
+ XmlPullParser.END_DOCUMENT -> return
+ }
+ }
+ }
+}
diff --git a/apps/mobile/android/app/src/main/java/com/colota/importer/ImportFormat.kt b/apps/mobile/android/app/src/main/java/com/colota/importer/ImportFormat.kt
new file mode 100644
index 00000000..e691d0cc
--- /dev/null
+++ b/apps/mobile/android/app/src/main/java/com/colota/importer/ImportFormat.kt
@@ -0,0 +1,17 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.importer
+
+enum class ImportFormat {
+ GEOJSON,
+ GOOGLE_TIMELINE_LEGACY,
+ GOOGLE_TIMELINE_NEW,
+ GPX,
+ KML,
+ CSV,
+}
+
+class UnsupportedFormatException(message: String) : Exception(message)
diff --git a/apps/mobile/android/app/src/main/java/com/colota/importer/ImportRow.kt b/apps/mobile/android/app/src/main/java/com/colota/importer/ImportRow.kt
new file mode 100644
index 00000000..0ff49001
--- /dev/null
+++ b/apps/mobile/android/app/src/main/java/com/colota/importer/ImportRow.kt
@@ -0,0 +1,18 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.importer
+
+/** Normalised location row produced by a parser. Timestamp is Unix seconds. */
+data class ImportRow(
+ val timestamp: Long,
+ val latitude: Double,
+ val longitude: Double,
+ val accuracy: Int?,
+ val altitude: Int?,
+ val speed: Int?,
+ val bearing: Double?,
+ val battery: Int?,
+)
diff --git a/apps/mobile/android/app/src/main/java/com/colota/importer/JsonReadHelpers.kt b/apps/mobile/android/app/src/main/java/com/colota/importer/JsonReadHelpers.kt
new file mode 100644
index 00000000..d159b29c
--- /dev/null
+++ b/apps/mobile/android/app/src/main/java/com/colota/importer/JsonReadHelpers.kt
@@ -0,0 +1,49 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.importer
+
+import android.util.JsonReader
+import android.util.JsonToken
+import java.time.OffsetDateTime
+import java.time.ZonedDateTime
+import java.time.format.DateTimeFormatter
+import java.time.format.DateTimeParseException
+
+internal fun JsonReader.readNullableInt(): Int? = when (peek()) {
+ JsonToken.NULL -> { nextNull(); null }
+ JsonToken.NUMBER -> nextDouble().toInt()
+ JsonToken.STRING -> nextString().toIntOrNull()
+ else -> { skipValue(); null }
+}
+
+internal fun JsonReader.readNullableDouble(): Double? = when (peek()) {
+ JsonToken.NULL -> { nextNull(); null }
+ JsonToken.NUMBER -> nextDouble()
+ JsonToken.STRING -> nextString().toDoubleOrNull()
+ else -> { skipValue(); null }
+}
+
+// DateTimeFormatter is thread-safe and ~10x faster than SimpleDateFormat - the
+// per-instance cost matters for Timeline files with 100k+ ISO timestamps.
+private val ISO_FORMATTERS = listOf(
+ DateTimeFormatter.ISO_OFFSET_DATE_TIME,
+ DateTimeFormatter.ISO_ZONED_DATE_TIME,
+)
+
+internal fun parseIso8601Seconds(s: String): Long? {
+ for (formatter in ISO_FORMATTERS) {
+ try {
+ return OffsetDateTime.parse(s, formatter).toEpochSecond()
+ } catch (_: DateTimeParseException) {
+ }
+ }
+ // ZonedDateTime tolerates bracketed zone IDs like "...+02:00[Europe/Berlin]".
+ return try {
+ ZonedDateTime.parse(s).toEpochSecond()
+ } catch (_: DateTimeParseException) {
+ null
+ }
+}
diff --git a/apps/mobile/android/app/src/main/java/com/colota/importer/KmlParser.kt b/apps/mobile/android/app/src/main/java/com/colota/importer/KmlParser.kt
new file mode 100644
index 00000000..a3802703
--- /dev/null
+++ b/apps/mobile/android/app/src/main/java/com/colota/importer/KmlParser.kt
@@ -0,0 +1,189 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.importer
+
+import android.util.Xml
+import org.xmlpull.v1.XmlPullParser
+import java.io.InputStream
+import java.util.concurrent.atomic.AtomicBoolean
+
+/** Streaming KML parser. Placemark/Point/TimeStamp only; LineString tracks have no
+ * per-vertex timestamps so they're counted as invalid rather than fabricated. */
+object KmlParser {
+
+ fun parse(
+ input: InputStream,
+ cancelled: AtomicBoolean,
+ nowSec: Long,
+ ): ParseResult {
+ val rows = ArrayList<ImportRow>()
+ var invalid = 0
+
+ val parser = Xml.newPullParser().apply {
+ setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true)
+ setInput(input, null)
+ }
+
+ var event = parser.eventType
+ while (event != XmlPullParser.END_DOCUMENT) {
+ if (cancelled.get()) throw InterruptedException("Import cancelled")
+ if (event == XmlPullParser.START_TAG && parser.name == "Placemark") {
+ val (added, badRows) = readPlacemark(parser, nowSec)
+ rows.addAll(added)
+ invalid += badRows
+ }
+ event = parser.next()
+ }
+
+ return ParseResult(rows, invalid)
+ }
+
+ private fun readPlacemark(parser: XmlPullParser, nowSec: Long): Pair<List<ImportRow>, Int> {
+ val rows = ArrayList<ImportRow>()
+ var pointCoord: Pair<Double, Double>? = null
+ var pointAltitude: Int? = null
+ var lineCoords: List<Triple<Double, Double, Int?>>? = null
+ var ts: Long? = null
+
+ var event = parser.next()
+ while (!(event == XmlPullParser.END_TAG && parser.name == "Placemark")) {
+ if (event == XmlPullParser.START_TAG) {
+ when (parser.name) {
+ "Point" -> {
+ val parsed = readGeometryCoordinates(parser, "Point")
+ if (parsed.isNotEmpty()) {
+ pointCoord = parsed[0].first to parsed[0].second
+ pointAltitude = parsed[0].third
+ }
+ }
+ "LineString" -> {
+ lineCoords = readGeometryCoordinates(parser, "LineString")
+ }
+ "TimeStamp" -> ts = readTimeStampWhen(parser)
+ "TimeSpan" -> ts = ts ?: readTimeSpanBegin(parser)
+ else -> skipElement(parser)
+ }
+ }
+ event = parser.next()
+ }
+
+ var invalid = 0
+ if (pointCoord != null) {
+ val tsVal = ts
+ if (tsVal == null) {
+ invalid++
+ } else if (isValidLocation(pointCoord.first, pointCoord.second, tsVal, nowSec)) {
+ rows.add(
+ ImportRow(
+ timestamp = tsVal,
+ latitude = pointCoord.first,
+ longitude = pointCoord.second,
+ accuracy = null,
+ altitude = pointAltitude,
+ speed = null,
+ bearing = null,
+ battery = null,
+ )
+ )
+ } else {
+ invalid++
+ }
+ }
+ // No per-vertex timestamps in KML LineString; count the vertices as invalid.
+ if (lineCoords != null && pointCoord == null && lineCoords.isNotEmpty()) {
+ invalid += lineCoords.size
+ }
+ return rows to invalid
+ }
+
+ // KML coordinates are `lon,lat[,alt]` tuples - lon first, opposite of the usual order.
+ private fun readGeometryCoordinates(
+ parser: XmlPullParser,
+ containerName: String,
+ ): List<Triple<Double, Double, Int?>> {
+ val result = ArrayList<Triple<Double, Double, Int?>>()
+ var event = parser.next()
+ while (!(event == XmlPullParser.END_TAG && parser.name == containerName)) {
+ if (event == XmlPullParser.START_TAG) {
+ if (parser.name == "coordinates") {
+ val text = readTextOrNull(parser)
+ if (text != null) {
+ result.addAll(parseCoordinatesText(text))
+ }
+ } else {
+ skipElement(parser)
+ }
+ }
+ event = parser.next()
+ }
+ return result
+ }
+
+ internal fun parseCoordinatesText(text: String): List<Triple<Double, Double, Int?>> {
+ return text.trim().split(Regex("\\s+")).mapNotNull { tuple ->
+ val parts = tuple.split(",")
+ if (parts.size < 2) return@mapNotNull null
+ val lon = parts[0].trim().toDoubleOrNull() ?: return@mapNotNull null
+ val lat = parts[1].trim().toDoubleOrNull() ?: return@mapNotNull null
+ val alt = if (parts.size >= 3) parts[2].trim().toDoubleOrNull()?.toInt() else null
+ Triple(lat, lon, alt)
+ }
+ }
+
+ private fun readTimeStampWhen(parser: XmlPullParser): Long? {
+ var ts: Long? = null
+ var event = parser.next()
+ while (!(event == XmlPullParser.END_TAG && parser.name == "TimeStamp")) {
+ if (event == XmlPullParser.START_TAG) {
+ if (parser.name == "when") {
+ val text = readTextOrNull(parser)
+ if (text != null) ts = parseIso8601Seconds(text)
+ } else {
+ skipElement(parser)
+ }
+ }
+ event = parser.next()
+ }
+ return ts
+ }
+
+ private fun readTimeSpanBegin(parser: XmlPullParser): Long? {
+ var ts: Long? = null
+ var event = parser.next()
+ while (!(event == XmlPullParser.END_TAG && parser.name == "TimeSpan")) {
+ if (event == XmlPullParser.START_TAG) {
+ if (parser.name == "begin" && ts == null) {
+ val text = readTextOrNull(parser)
+ if (text != null) ts = parseIso8601Seconds(text)
+ } else {
+ skipElement(parser)
+ }
+ }
+ event = parser.next()
+ }
+ return ts
+ }
+
+ private fun readTextOrNull(parser: XmlPullParser): String? {
+ val event = parser.next()
+ if (event != XmlPullParser.TEXT) return null
+ val text = parser.text?.trim()?.takeIf { it.isNotEmpty() }
+ parser.next()
+ return text
+ }
+
+ private fun skipElement(parser: XmlPullParser) {
+ if (parser.eventType != XmlPullParser.START_TAG) return
+ var depth = 1
+ while (depth > 0) {
+ when (parser.next()) {
+ XmlPullParser.END_TAG -> depth--
+ XmlPullParser.START_TAG -> depth++
+ XmlPullParser.END_DOCUMENT -> return
+ }
+ }
+ }
+}
diff --git a/apps/mobile/android/app/src/main/java/com/colota/importer/LocationImporter.kt b/apps/mobile/android/app/src/main/java/com/colota/importer/LocationImporter.kt
new file mode 100644
index 00000000..179fe8bf
--- /dev/null
+++ b/apps/mobile/android/app/src/main/java/com/colota/importer/LocationImporter.kt
@@ -0,0 +1,301 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.importer
+
+import android.content.ContentResolver
+import android.location.Location
+import android.net.Uri
+import android.util.JsonReader
+import android.util.JsonToken
+import com.Colota.data.DatabaseHelper
+import com.Colota.sync.ApiFormat
+import com.Colota.sync.PayloadBuilder
+import com.Colota.util.AppLogger
+import java.io.ByteArrayInputStream
+import java.io.InputStream
+import java.io.SequenceInputStream
+import java.util.concurrent.atomic.AtomicBoolean
+import kotlin.math.roundToLong
+
+object LocationImporter {
+
+ private const val TAG = "LocationImporter"
+ private const val SNIFF_BYTES = 4096
+
+ // 6dp ~ 10cm. Tight enough to catch round-tripped exports, loose enough that
+ // genuinely distinct nearby fixes don't collide.
+ private const val DEDUP_PRECISION = 1_000_000L
+
+ fun preview(
+ contentResolver: ContentResolver,
+ uri: Uri,
+ db: DatabaseHelper,
+ cancelled: AtomicBoolean,
+ nowSec: Long = System.currentTimeMillis() / 1000,
+ ): PreviewResult {
+ val parseStart = System.currentTimeMillis()
+ val (sniffBytes, sniffString) = readSniff(contentResolver, uri)
+ val format = detectFormat(sniffString)
+ ?: throw UnsupportedFormatException("Could not detect file format")
+
+ // Prepend the sniff buffer back so the parser sees the full document without a
+ // second SAF open - some cloud providers charge a network round-trip per open.
+ val parse = openStream(contentResolver, uri).use { rest ->
+ val combined = SequenceInputStream(ByteArrayInputStream(sniffBytes), rest)
+ when (format) {
+ ImportFormat.GEOJSON -> GeoJsonParser.parse(combined, cancelled, nowSec)
+ ImportFormat.GOOGLE_TIMELINE_LEGACY,
+ ImportFormat.GOOGLE_TIMELINE_NEW -> GoogleTimelineParser.parse(combined, cancelled, nowSec)
+ ImportFormat.GPX -> GpxParser.parse(combined, cancelled, nowSec)
+ ImportFormat.KML -> KmlParser.parse(combined, cancelled, nowSec)
+ ImportFormat.CSV -> CsvParser.parse(combined, cancelled, nowSec)
+ }
+ }
+ val parseMs = System.currentTimeMillis() - parseStart
+ val rate = if (parseMs > 0) parse.rows.size * 1000L / parseMs else parse.rows.size.toLong()
+ AppLogger.i(TAG, "Parsed ${parse.rows.size} rows (${parse.invalid} invalid) in ${parseMs}ms (~$rate rows/s) format=$format")
+
+ if (cancelled.get()) throw InterruptedException("Import cancelled")
+ if (parse.rows.isEmpty()) {
+ return PreviewResult(
+ format = format,
+ totalParsed = 0,
+ invalid = parse.invalid,
+ duplicates = 0,
+ dateRangeStartSec = null,
+ dateRangeEndSec = null,
+ rowsToCommit = emptyList(),
+ )
+ }
+
+ val dedupStart = System.currentTimeMillis()
+ val sortedIncoming = parse.rows.sortedWith(
+ compareBy<ImportRow> { it.timestamp }
+ .thenBy { it.latitude }
+ .thenBy { it.longitude },
+ )
+ // Fold in-file dupes first or the merge-walk's cmp==0 branch can leak a
+ // second copy past an already-matched DB key.
+ val (deduped, inFileDuplicates) = foldInFileDuplicates(sortedIncoming)
+ val minTs = deduped.first().timestamp
+ val maxTs = deduped.last().timestamp
+ val (unique, dbDuplicates) = mergeWalkDedup(db, deduped, minTs, maxTs, cancelled)
+ val duplicates = inFileDuplicates + dbDuplicates
+ val dedupMs = System.currentTimeMillis() - dedupStart
+ AppLogger.i(TAG, "Dedup: ${unique.size} new, $duplicates duplicates in ${dedupMs}ms")
+
+ return PreviewResult(
+ format = format,
+ totalParsed = parse.rows.size,
+ invalid = parse.invalid,
+ duplicates = duplicates,
+ dateRangeStartSec = minTs,
+ dateRangeEndSec = maxTs,
+ rowsToCommit = unique,
+ )
+ }
+
+ private fun foldInFileDuplicates(sorted: List<ImportRow>): Pair<List<ImportRow>, Int> {
+ if (sorted.size <= 1) return sorted to 0
+ val out = ArrayList<ImportRow>(sorted.size)
+ out.add(sorted[0])
+ var lastKey = dedupKey(sorted[0].timestamp, sorted[0].latitude, sorted[0].longitude)
+ var dupes = 0
+ for (i in 1 until sorted.size) {
+ val k = dedupKey(sorted[i].timestamp, sorted[i].latitude, sorted[i].longitude)
+ if (k == lastKey) {
+ dupes++
+ } else {
+ out.add(sorted[i])
+ lastKey = k
+ }
+ }
+ return out to dupes
+ }
+
+ fun commit(db: DatabaseHelper, rows: List<ImportRow>, options: CommitOptions = CommitOptions()): Int {
+ if (rows.isEmpty()) return 0
+ if (!options.asQueued) {
+ return db.bulkInsertImportedLocations(rows, payloads = null)
+ }
+ require(options.endpoint.isNotBlank()) {
+ "asQueued requires a non-blank endpoint"
+ }
+ require(!options.isOfflineMode) {
+ "asQueued is not permitted while the app is in offline mode"
+ }
+ val payloads = rows.map { row -> buildPayloadForRow(row, options).toString() }
+ return db.bulkInsertImportedLocations(rows, payloads = payloads)
+ }
+
+ private fun buildPayloadForRow(row: ImportRow, options: CommitOptions): org.json.JSONObject {
+ // Setters also flip the has* flags PayloadBuilder gates optional fields on.
+ val location = Location("import").apply {
+ latitude = row.latitude
+ longitude = row.longitude
+ accuracy = (row.accuracy ?: 0).toFloat()
+ if (row.altitude != null) altitude = row.altitude.toDouble()
+ if (row.speed != null) speed = row.speed.toFloat()
+ if (row.bearing != null) bearing = row.bearing.toFloat()
+ time = row.timestamp * 1000L
+ }
+ return PayloadBuilder.buildLocationPayload(
+ location = location,
+ timestamp = row.timestamp,
+ batteryLevel = row.battery ?: 0,
+ batteryStatus = 0,
+ fieldMap = options.fieldMap,
+ customFields = options.customFields,
+ apiFormat = options.apiFormat,
+ )
+ }
+
+ // Merge-walks the sorted incoming list against an ASC DB cursor in `[minTs, maxTs]`
+ // so peak memory doesn't scale with the size of the existing history.
+ private fun mergeWalkDedup(
+ db: DatabaseHelper,
+ sortedIncoming: List<ImportRow>,
+ minTs: Long,
+ maxTs: Long,
+ cancelled: AtomicBoolean,
+ ): Pair<List<ImportRow>, Int> {
+ val unique = ArrayList<ImportRow>(sortedIncoming.size)
+ var duplicates = 0
+ var idx = 0
+ db.forEachLocationKeyInRange(minTs, maxTs) { dbTs, dbLat, dbLon ->
+ if (cancelled.get()) throw InterruptedException("Import cancelled")
+ val dbKey = dedupKey(dbTs, dbLat, dbLon)
+ while (idx < sortedIncoming.size) {
+ val row = sortedIncoming[idx]
+ val incomingKey = dedupKey(row.timestamp, row.latitude, row.longitude)
+ val cmp = compareDedupKey(incomingKey, dbKey)
+ when {
+ cmp < 0 -> { unique.add(row); idx++ }
+ cmp == 0 -> { duplicates++; idx++; return@forEachLocationKeyInRange }
+ else -> return@forEachLocationKeyInRange
+ }
+ }
+ }
+ while (idx < sortedIncoming.size) {
+ if (cancelled.get()) throw InterruptedException("Import cancelled")
+ unique.add(sortedIncoming[idx])
+ idx++
+ }
+ return unique to duplicates
+ }
+
+ private fun compareDedupKey(a: DedupKey, b: DedupKey): Int {
+ if (a.ts != b.ts) return a.ts.compareTo(b.ts)
+ if (a.latE6 != b.latE6) return a.latE6.compareTo(b.latE6)
+ return a.lonE6.compareTo(b.lonE6)
+ }
+
+ internal fun detectFormat(sniff: String): ImportFormat? {
+ val firstChar = sniff.firstOrNull { !it.isWhitespace() } ?: return null
+ return when (firstChar) {
+ '<' -> detectXmlFormat(sniff)
+ '{' -> detectJsonFormat(sniff)
+ else -> detectCsvFormat(sniff)
+ }
+ }
+
+ private fun detectXmlFormat(sniff: String): ImportFormat? {
+ val lower = sniff.lowercase()
+ // No legitimate GPX/KML in the wild ships a DOCTYPE; refuse to dodge billion-laughs.
+ if ("<!doctype" in lower) {
+ throw UnsupportedFormatException("XML files with DOCTYPE declarations are not supported")
+ }
+ val afterDecl = lower.substringAfter("?>", lower)
+ return when {
+ Regex("<gpx[\\s>]").containsMatchIn(afterDecl) -> ImportFormat.GPX
+ Regex("<kml[\\s>]").containsMatchIn(afterDecl) -> ImportFormat.KML
+ else -> null
+ }
+ }
+
+ // Inspect top-level JSON keys, not substrings - a foreign GeoJSON with a property
+ // named "locations" would otherwise misdetect as Google Timeline.
+ private fun detectJsonFormat(sniff: String): ImportFormat? {
+ val keys = HashSet<String>()
+ try {
+ JsonReader(sniff.reader()).use { reader ->
+ reader.isLenient = true
+ if (reader.peek() != JsonToken.BEGIN_OBJECT) return null
+ reader.beginObject()
+ while (reader.hasNext()) {
+ keys.add(reader.nextName())
+ try {
+ reader.skipValue()
+ } catch (_: Exception) {
+ // Sniff is truncated mid-document; the keys collected so far are enough.
+ break
+ }
+ }
+ }
+ } catch (_: Exception) {
+ }
+
+ return when {
+ "locations" in keys -> ImportFormat.GOOGLE_TIMELINE_LEGACY
+ "semanticSegments" in keys || "rawSignals" in keys -> ImportFormat.GOOGLE_TIMELINE_NEW
+ "features" in keys && "type" in keys -> ImportFormat.GEOJSON
+ else -> null
+ }
+ }
+
+ private fun detectCsvFormat(sniff: String): ImportFormat? {
+ val firstLine = sniff.lineSequence().firstOrNull { it.isNotBlank() } ?: return null
+ if (!firstLine.contains(',')) return null
+ val headerLower = firstLine.lowercase()
+ val hasLat = "latitude" in headerLower || Regex("(^|,)\\s*lat\\s*(,|$)").containsMatchIn(headerLower)
+ val hasLon = "longitude" in headerLower || Regex("(^|,)\\s*lon\\s*(,|$)").containsMatchIn(headerLower) ||
+ Regex("(^|,)\\s*lng\\s*(,|$)").containsMatchIn(headerLower)
+ val hasTime = "time" in headerLower || "timestamp" in headerLower
+ return if (hasLat && hasLon && hasTime) ImportFormat.CSV else null
+ }
+
+ private fun openStream(contentResolver: ContentResolver, uri: Uri): InputStream =
+ contentResolver.openInputStream(uri)
+ ?: throw IllegalStateException("Could not open input stream for $uri")
+
+ private fun readSniff(contentResolver: ContentResolver, uri: Uri): Pair<ByteArray, String> {
+ openStream(contentResolver, uri).use { stream ->
+ val buf = ByteArray(SNIFF_BYTES)
+ var total = 0
+ while (total < buf.size) {
+ val n = stream.read(buf, total, buf.size - total)
+ if (n <= 0) break
+ total += n
+ }
+ val trimmed = if (total == buf.size) buf else buf.copyOf(total)
+ return trimmed to String(trimmed, Charsets.UTF_8)
+ }
+ }
+
+ internal fun dedupKey(ts: Long, lat: Double, lon: Double): DedupKey =
+ DedupKey(ts, (lat * DEDUP_PRECISION).roundToLong(), (lon * DEDUP_PRECISION).roundToLong())
+}
+
+internal data class DedupKey(val ts: Long, val latE6: Long, val lonE6: Long)
+
+data class PreviewResult(
+ val format: ImportFormat,
+ val totalParsed: Int,
+ val invalid: Int,
+ val duplicates: Int,
+ val dateRangeStartSec: Long?,
+ val dateRangeEndSec: Long?,
+ val rowsToCommit: List<ImportRow>,
+)
+
+data class CommitOptions(
+ val asQueued: Boolean = false,
+ val endpoint: String = "",
+ val isOfflineMode: Boolean = false,
+ val fieldMap: Map<String, String> = emptyMap(),
+ val customFields: Map<String, String> = emptyMap(),
+ val apiFormat: ApiFormat = ApiFormat.FIELD_MAPPED,
+)
diff --git a/apps/mobile/android/app/src/main/java/com/colota/backup/BackupPickerCoordinator.kt b/apps/mobile/android/app/src/main/java/com/colota/util/SafPickerCoordinator.kt
similarity index 51%
rename from apps/mobile/android/app/src/main/java/com/colota/backup/BackupPickerCoordinator.kt
rename to apps/mobile/android/app/src/main/java/com/colota/util/SafPickerCoordinator.kt
index ed0fae87..ea6845b1 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/backup/BackupPickerCoordinator.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/util/SafPickerCoordinator.kt
@@ -3,39 +3,39 @@
* Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
*/
-package com.Colota.backup
+package com.Colota.util
import android.app.Activity
import android.content.Intent
-import android.net.Uri
-import android.provider.OpenableColumns
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.BaseActivityEventListener
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
-// SAF picker plumbing for BackupServiceModule. One in-flight promise; timeout releases the slot
-// so a never-returning picker activity can't permanently block.
-class BackupPickerCoordinator(
+/** Generic SAF picker plumbing. One in-flight pick per instance; timeout releases
+ * the slot if the picker activity never returns. */
+class SafPickerCoordinator(
private val reactContext: ReactApplicationContext,
private val scope: CoroutineScope,
) {
companion object {
- private const val REQUEST_PICK_DESTINATION = 9101
- private const val REQUEST_PICK_SOURCE = 9102
- private const val DEFAULT_FILENAME = "colota_backup.colota"
- private const val MIME_BACKUP = "application/octet-stream"
private const val PICKER_TIMEOUT_MS = 5L * 60_000L
}
private val lock = Any()
+ private var pending: PendingPick? = null
+ private var timeoutJob: Job? = null
- @Volatile private var pendingPromise: Promise? = null
- @Volatile private var timeoutJob: Job? = null
+ private data class PendingPick(
+ val promise: Promise,
+ val requestCode: Int,
+ val returnFilename: Boolean,
+ )
private val activityEventListener = object : BaseActivityEventListener() {
override fun onActivityResult(
@@ -44,46 +44,36 @@ class BackupPickerCoordinator(
resultCode: Int,
data: Intent?,
) {
- if (requestCode != REQUEST_PICK_DESTINATION && requestCode != REQUEST_PICK_SOURCE) return
- val promise = synchronized(lock) {
- val p = pendingPromise
- pendingPromise = null
+ val pick = synchronized(lock) {
+ val p = pending
+ if (p == null || p.requestCode != requestCode) return
+ pending = null
timeoutJob?.cancel()
timeoutJob = null
p
- } ?: return
+ }
if (resultCode != Activity.RESULT_OK || data?.data == null) {
- promise.resolve(null)
+ pick.promise.resolve(null)
return
}
val uri = data.data!!
- if (requestCode == REQUEST_PICK_SOURCE) {
- // Source picker returns the filename too so the password prompt has context.
+ if (!pick.returnFilename) {
+ pick.promise.resolve(uri.toString())
+ return
+ }
+ // queryDisplayName can hit the network on cloud providers; off the main thread.
+ scope.launch(Dispatchers.IO) {
+ val name = reactContext.contentResolver.queryDisplayName(uri)
val result = Arguments.createMap().apply {
putString("uri", uri.toString())
- putString("displayName", queryDisplayName(uri))
+ putString("displayName", name)
}
- promise.resolve(result)
- } else {
- promise.resolve(uri.toString())
+ pick.promise.resolve(result)
}
}
}
- // Some providers (Drive, MTP) hide DISPLAY_NAME; callers must tolerate null.
- private fun queryDisplayName(uri: Uri): String? {
- return try {
- reactContext.contentResolver
- .query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null)
- ?.use { cursor ->
- if (cursor.moveToFirst() && !cursor.isNull(0)) cursor.getString(0) else null
- }
- } catch (e: Exception) {
- null
- }
- }
-
init {
reactContext.addActivityEventListener(activityEventListener)
}
@@ -92,50 +82,56 @@ class BackupPickerCoordinator(
reactContext.removeActivityEventListener(activityEventListener)
}
- fun pickDestination(promise: Promise) {
+ fun pickCreateDocument(mime: String, defaultName: String, requestCode: Int, promise: Promise) {
val activity = reactContext.currentActivity
if (activity == null) {
promise.reject("E_NO_ACTIVITY", "No current activity")
return
}
- if (!claim(promise)) return
+ if (!claim(promise, requestCode, returnFilename = false)) return
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
- type = MIME_BACKUP
- putExtra(Intent.EXTRA_TITLE, DEFAULT_FILENAME)
+ type = mime
+ putExtra(Intent.EXTRA_TITLE, defaultName)
}
- activity.startActivityForResult(intent, REQUEST_PICK_DESTINATION)
+ activity.startActivityForResult(intent, requestCode)
}
- fun pickSource(promise: Promise) {
+ fun pickOpenDocument(
+ mime: String,
+ extraMimes: Array<String>?,
+ requestCode: Int,
+ promise: Promise,
+ ) {
val activity = reactContext.currentActivity
if (activity == null) {
promise.reject("E_NO_ACTIVITY", "No current activity")
return
}
- if (!claim(promise)) return
+ if (!claim(promise, requestCode, returnFilename = true)) return
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
- type = MIME_BACKUP
+ type = mime
+ if (extraMimes != null) putExtra(Intent.EXTRA_MIME_TYPES, extraMimes)
}
- activity.startActivityForResult(intent, REQUEST_PICK_SOURCE)
+ activity.startActivityForResult(intent, requestCode)
}
- // Timeout releases the slot if the activity result never arrives.
- private fun claim(promise: Promise): Boolean {
+ private fun claim(promise: Promise, requestCode: Int, returnFilename: Boolean): Boolean {
synchronized(lock) {
- if (pendingPromise != null) {
+ if (pending != null) {
promise.reject("E_BUSY", "Another picker is already open")
return false
}
- pendingPromise = promise
+ pending = PendingPick(promise, requestCode, returnFilename)
timeoutJob = scope.launch {
delay(PICKER_TIMEOUT_MS)
val stale = synchronized(lock) {
- if (pendingPromise === promise) {
- pendingPromise = null
+ val p = pending
+ if (p?.promise === promise) {
+ pending = null
timeoutJob = null
- promise
+ p.promise
} else null
}
stale?.reject("E_PICKER_TIMEOUT", "File picker timed out")
diff --git a/apps/mobile/android/app/src/main/java/com/colota/util/UriHelpers.kt b/apps/mobile/android/app/src/main/java/com/colota/util/UriHelpers.kt
new file mode 100644
index 00000000..732b39d4
--- /dev/null
+++ b/apps/mobile/android/app/src/main/java/com/colota/util/UriHelpers.kt
@@ -0,0 +1,20 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.util
+
+import android.content.ContentResolver
+import android.net.Uri
+import android.provider.OpenableColumns
+
+// Null when the provider doesn't expose DISPLAY_NAME (some cloud providers).
+internal fun ContentResolver.queryDisplayName(uri: Uri): String? {
+ return try {
+ query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null)
+ ?.use { c -> if (c.moveToFirst() && !c.isNull(0)) c.getString(0) else null }
+ } catch (e: Exception) {
+ null
+ }
+}
diff --git a/apps/mobile/android/app/src/test/java/com/Colota/importer/CsvParserTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/importer/CsvParserTest.kt
new file mode 100644
index 00000000..36f27b1e
--- /dev/null
+++ b/apps/mobile/android/app/src/test/java/com/Colota/importer/CsvParserTest.kt
@@ -0,0 +1,153 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.importer
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.RobolectricTestRunner
+import java.util.concurrent.atomic.AtomicBoolean
+
+@RunWith(RobolectricTestRunner::class)
+class CsvParserTest {
+
+ private val cancelled = AtomicBoolean(false)
+ private val nowSec = 1_750_000_000L
+
+ @Test
+ fun `parses Colota CSV export header order`() {
+ val csv = """
+ id,timestamp,iso_time,latitude,longitude,accuracy,altitude,speed,battery
+ 0,1704110400,2024-01-01T12:00:00.000Z,52.5,13.4,5,30,2,80
+ """.trimIndent()
+
+ val result = CsvParser.parse(csv.byteInputStream(), cancelled, nowSec)
+ assertEquals(0, result.invalid)
+ assertEquals(1, result.rows.size)
+ val row = result.rows[0]
+ assertEquals(52.5, row.latitude, 1e-9)
+ assertEquals(13.4, row.longitude, 1e-9)
+ assertEquals(1_704_110_400L, row.timestamp)
+ assertEquals(5, row.accuracy)
+ assertEquals(30, row.altitude)
+ assertEquals(2, row.speed)
+ assertEquals(80, row.battery)
+ }
+
+ @Test
+ fun `accepts foreign column order via header lookup`() {
+ val csv = """
+ time,lat,lon,acc
+ 2024-01-01T12:00:00Z,52.5,13.4,7
+ """.trimIndent()
+
+ val result = CsvParser.parse(csv.byteInputStream(), cancelled, nowSec)
+ assertEquals(1, result.rows.size)
+ assertEquals(7, result.rows[0].accuracy)
+ }
+
+ @Test
+ fun `falls back from numeric timestamp to ISO when numeric is blank`() {
+ val csv = """
+ timestamp,iso_time,latitude,longitude
+ ,2024-01-01T12:00:00Z,52.5,13.4
+ """.trimIndent()
+
+ val result = CsvParser.parse(csv.byteInputStream(), cancelled, nowSec)
+ assertEquals(1, result.rows.size)
+ assertEquals(1_704_110_400L, result.rows[0].timestamp)
+ }
+
+ @Test
+ fun `rejects header missing required columns`() {
+ val csv = """
+ id,name,description
+ 0,foo,bar
+ """.trimIndent()
+
+ try {
+ CsvParser.parse(csv.byteInputStream(), cancelled, nowSec)
+ } catch (e: UnsupportedFormatException) {
+ assertTrue(e.message?.contains("latitude") == true)
+ return
+ }
+ throw AssertionError("expected UnsupportedFormatException for missing required columns")
+ }
+
+ @Test
+ fun `drops malformed rows but continues parsing`() {
+ val csv = """
+ timestamp,latitude,longitude
+ 1704110400,52.5,13.4
+ ,,
+ 1704110500,52.6,13.5
+ """.trimIndent()
+
+ val result = CsvParser.parse(csv.byteInputStream(), cancelled, nowSec)
+ assertEquals(2, result.rows.size)
+ assertEquals(1, result.invalid)
+ }
+
+ @Test
+ fun `skips blank lines without counting them as invalid`() {
+ val csv = """
+ timestamp,latitude,longitude
+ 1704110400,52.5,13.4
+
+ 1704110500,52.6,13.5
+ """.trimIndent()
+
+ val result = CsvParser.parse(csv.byteInputStream(), cancelled, nowSec)
+ assertEquals(2, result.rows.size)
+ assertEquals(0, result.invalid)
+ }
+
+ @Test
+ fun `splitCsvLine strips surrounding double quotes`() {
+ val parts = CsvParser.splitCsvLine("\"a\",b,\"c\"")
+ assertEquals(listOf("a", "b", "c"), parts)
+ }
+
+ @Test
+ fun `rejects rows where a quoted field contains an unescaped comma`() {
+ // Without this guard the split would shift columns and could swap lat / lon.
+ val csv = """
+ name,timestamp,latitude,longitude
+ "Foo, Bar",1704110400,52.5,13.4
+ Clean,1704110500,52.6,13.5
+ """.trimIndent()
+
+ val result = CsvParser.parse(csv.byteInputStream(), cancelled, nowSec)
+ assertEquals(1, result.rows.size)
+ assertEquals(1, result.invalid)
+ assertEquals(1_704_110_500L, result.rows[0].timestamp)
+ }
+
+ @Test
+ fun `parseHeader maps aliases to canonical indices`() {
+ val cols = CsvParser.parseHeader("ts,lat,lng,velocity,ele")
+ assertEquals(0, cols.tsNumeric)
+ assertEquals(1, cols.lat)
+ assertEquals(2, cols.lon)
+ assertEquals(3, cols.speed)
+ assertEquals(4, cols.altitude)
+ }
+
+ @Test
+ fun `cancellation flag interrupts parse`() {
+ val csv = buildString {
+ append("timestamp,latitude,longitude\n")
+ for (i in 0 until 1000) append("1704110${400 + i},52.5,13.4\n")
+ }
+ try {
+ CsvParser.parse(csv.byteInputStream(), AtomicBoolean(true), nowSec)
+ } catch (_: InterruptedException) {
+ return
+ }
+ throw AssertionError("expected InterruptedException")
+ }
+}
diff --git a/apps/mobile/android/app/src/test/java/com/Colota/importer/GeoJsonParserTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/importer/GeoJsonParserTest.kt
new file mode 100644
index 00000000..e847fd20
--- /dev/null
+++ b/apps/mobile/android/app/src/test/java/com/Colota/importer/GeoJsonParserTest.kt
@@ -0,0 +1,194 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.importer
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNotNull
+import org.junit.Assert.assertNull
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.RobolectricTestRunner
+import java.io.ByteArrayInputStream
+import java.util.concurrent.atomic.AtomicBoolean
+
+@RunWith(RobolectricTestRunner::class)
+class GeoJsonParserTest {
+
+ private val cancelled = AtomicBoolean(false)
+ private val nowSec = 1_750_000_000L // fixed "now" so future-date rejection is deterministic
+
+ @Test
+ fun `parses Colota export round-trip shape`() {
+ val json = """
+ {
+ "type": "FeatureCollection",
+ "features": [
+ {
+ "type": "Feature",
+ "geometry": { "type": "Point", "coordinates": [13.4, 52.5] },
+ "properties": {
+ "accuracy": 5,
+ "altitude": 30,
+ "speed": 2,
+ "battery": 80,
+ "time": "2024-01-01T12:00:00.000Z"
+ }
+ }
+ ]
+ }
+ """.trimIndent()
+
+ val result = GeoJsonParser.parse(json.byteInputStream(), cancelled, nowSec)
+ assertEquals(0, result.invalid)
+ assertEquals(1, result.rows.size)
+ val row = result.rows[0]
+ assertEquals(52.5, row.latitude, 1e-9)
+ assertEquals(13.4, row.longitude, 1e-9)
+ assertEquals(5, row.accuracy)
+ assertEquals(30, row.altitude)
+ assertEquals(2, row.speed)
+ assertEquals(80, row.battery)
+ // 2024-01-01T12:00:00Z = 1704110400
+ assertEquals(1_704_110_400L, row.timestamp)
+ }
+
+ @Test
+ fun `rejects features without a recognised time`() {
+ val json = """
+ {
+ "type": "FeatureCollection",
+ "features": [
+ {
+ "type": "Feature",
+ "geometry": { "type": "Point", "coordinates": [1.0, 2.0] },
+ "properties": {}
+ }
+ ]
+ }
+ """.trimIndent()
+
+ val result = GeoJsonParser.parse(json.byteInputStream(), cancelled, nowSec)
+ assertEquals(1, result.invalid)
+ assertTrue(result.rows.isEmpty())
+ }
+
+ @Test
+ fun `rejects non-Point geometries`() {
+ val json = """
+ {
+ "type": "FeatureCollection",
+ "features": [
+ {
+ "type": "Feature",
+ "geometry": { "type": "LineString", "coordinates": [[1,2],[3,4]] },
+ "properties": { "time": "2024-01-01T00:00:00.000Z" }
+ }
+ ]
+ }
+ """.trimIndent()
+
+ val result = GeoJsonParser.parse(json.byteInputStream(), cancelled, nowSec)
+ assertEquals(1, result.invalid)
+ assertTrue(result.rows.isEmpty())
+ }
+
+ @Test
+ fun `rejects future-dated features beyond clock-skew window`() {
+ val futureTs = nowSec + 24 * 60 * 60
+ val json = """
+ {
+ "type": "FeatureCollection",
+ "features": [
+ {
+ "type": "Feature",
+ "geometry": { "type": "Point", "coordinates": [1.0, 2.0] },
+ "properties": { "time": $futureTs }
+ }
+ ]
+ }
+ """.trimIndent()
+
+ val result = GeoJsonParser.parse(json.byteInputStream(), cancelled, nowSec)
+ assertEquals("future-dated row must be invalid", 1, result.invalid)
+ assertTrue(result.rows.isEmpty())
+ }
+
+ @Test
+ fun `accepts timestamps within 5 minute clock-skew window`() {
+ val nearFuture = nowSec + 60
+ val json = """
+ {
+ "type": "FeatureCollection",
+ "features": [
+ {
+ "type": "Feature",
+ "geometry": { "type": "Point", "coordinates": [1.0, 2.0] },
+ "properties": { "time": $nearFuture }
+ }
+ ]
+ }
+ """.trimIndent()
+
+ val result = GeoJsonParser.parse(json.byteInputStream(), cancelled, nowSec)
+ assertEquals(0, result.invalid)
+ assertEquals(1, result.rows.size)
+ }
+
+ @Test
+ fun `tolerates extra top-level keys and unknown property fields`() {
+ val json = """
+ {
+ "type": "FeatureCollection",
+ "name": "exotic-export",
+ "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::4326" } },
+ "features": [
+ {
+ "type": "Feature",
+ "id": 42,
+ "geometry": { "type": "Point", "coordinates": [1.0, 2.0, 100.0] },
+ "properties": {
+ "time": "2024-01-01T00:00:00.000Z",
+ "unrecognised": "value",
+ "nested": { "ignored": true }
+ }
+ }
+ ]
+ }
+ """.trimIndent()
+
+ val result = GeoJsonParser.parse(json.byteInputStream(), cancelled, nowSec)
+ assertEquals(0, result.invalid)
+ assertEquals(1, result.rows.size)
+ }
+
+ @Test
+ fun `parseIso8601Seconds handles fractional and non-fractional forms`() {
+ assertEquals(1_704_110_400L, parseIso8601Seconds("2024-01-01T12:00:00.000Z"))
+ assertEquals(1_704_110_400L, parseIso8601Seconds("2024-01-01T12:00:00Z"))
+ assertNull(parseIso8601Seconds("not a date"))
+ }
+
+ @Test
+ fun `cancellation flag interrupts parse`() {
+ val json = buildString {
+ append("{\"type\":\"FeatureCollection\",\"features\":[")
+ for (i in 0 until 1000) {
+ if (i > 0) append(",")
+ append("""{"type":"Feature","geometry":{"type":"Point","coordinates":[1.0,2.0]},"properties":{"time":"2024-01-01T00:00:00.000Z"}}""")
+ }
+ append("]}")
+ }
+ val flag = AtomicBoolean(true) // pre-set to cancelled
+ try {
+ GeoJsonParser.parse(json.byteInputStream(), flag, nowSec)
+ } catch (e: InterruptedException) {
+ assertNotNull("cancellation must surface as InterruptedException", e.message)
+ return
+ }
+ throw AssertionError("expected InterruptedException")
+ }
+}
diff --git a/apps/mobile/android/app/src/test/java/com/Colota/importer/GoogleTimelineParserTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/importer/GoogleTimelineParserTest.kt
new file mode 100644
index 00000000..4ca5fd4b
--- /dev/null
+++ b/apps/mobile/android/app/src/test/java/com/Colota/importer/GoogleTimelineParserTest.kt
@@ -0,0 +1,193 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.importer
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNotNull
+import org.junit.Assert.assertNull
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.RobolectricTestRunner
+import java.util.concurrent.atomic.AtomicBoolean
+
+@RunWith(RobolectricTestRunner::class)
+class GoogleTimelineParserTest {
+
+ private val cancelled = AtomicBoolean(false)
+ private val nowSec = 1_750_000_000L
+
+ @Test
+ fun `parses legacy Takeout Records dot json`() {
+ val json = """
+ {
+ "locations": [
+ {
+ "latitudeE7": 525000000,
+ "longitudeE7": 134000000,
+ "timestampMs": "1704110400000",
+ "accuracy": 10,
+ "altitude": 30,
+ "velocity": 2
+ }
+ ]
+ }
+ """.trimIndent()
+
+ val result = GoogleTimelineParser.parse(json.byteInputStream(), cancelled, nowSec)
+ assertEquals(1, result.rows.size)
+ val row = result.rows[0]
+ assertEquals(52.5, row.latitude, 1e-7)
+ assertEquals(13.4, row.longitude, 1e-7)
+ assertEquals(1_704_110_400L, row.timestamp)
+ assertEquals(10, row.accuracy)
+ assertEquals(30, row.altitude)
+ assertEquals(2, row.speed)
+ }
+
+ @Test
+ fun `legacy parser tolerates timestamp as ISO string field`() {
+ val json = """
+ {
+ "locations": [
+ {
+ "latitudeE7": 525000000,
+ "longitudeE7": 134000000,
+ "timestamp": "2024-01-01T12:00:00Z"
+ }
+ ]
+ }
+ """.trimIndent()
+
+ val result = GoogleTimelineParser.parse(json.byteInputStream(), cancelled, nowSec)
+ assertEquals(1, result.rows.size)
+ assertEquals(1_704_110_400L, result.rows[0].timestamp)
+ }
+
+ @Test
+ fun `parses new on-device timeline rawSignals`() {
+ val json = """
+ {
+ "rawSignals": [
+ {
+ "position": {
+ "LatLng": "52.500000°,13.400000°",
+ "accuracyMeters": 8,
+ "altitudeMeters": 25,
+ "source": "WIFI",
+ "timestamp": "2024-01-01T12:00:00.000Z"
+ }
+ }
+ ]
+ }
+ """.trimIndent()
+
+ val result = GoogleTimelineParser.parse(json.byteInputStream(), cancelled, nowSec)
+ assertEquals(1, result.rows.size)
+ val row = result.rows[0]
+ assertEquals(52.5, row.latitude, 1e-7)
+ assertEquals(13.4, row.longitude, 1e-7)
+ assertEquals(8, row.accuracy)
+ assertEquals(25, row.altitude)
+ assertEquals(1_704_110_400L, row.timestamp)
+ }
+
+ @Test
+ fun `rawSignals position captures speedMetersPerSecond`() {
+ val json = """
+ {
+ "rawSignals": [
+ {
+ "position": {
+ "LatLng": "52.5°,13.4°",
+ "timestamp": "2024-01-01T12:00:00Z",
+ "speedMetersPerSecond": 7.2
+ }
+ }
+ ]
+ }
+ """.trimIndent()
+
+ val result = GoogleTimelineParser.parse(json.byteInputStream(), cancelled, nowSec)
+ assertEquals(1, result.rows.size)
+ assertEquals(7, result.rows[0].speed)
+ }
+
+ @Test
+ fun `parses new timeline semanticSegments timelinePath waypoints`() {
+ // semanticSegments also carries `visit` / `activity` blocks; walk past them.
+ val json = """
+ {
+ "semanticSegments": [
+ {
+ "startTime": "2024-01-01T00:00:00Z",
+ "endTime": "2024-01-01T01:00:00Z",
+ "timelinePath": [
+ { "point": "52.5°,13.4°", "time": "2024-01-01T00:30:00Z" },
+ { "point": "52.51°,13.41°", "time": "2024-01-01T00:45:00Z" }
+ ]
+ },
+ {
+ "startTime": "2024-01-01T02:00:00Z",
+ "endTime": "2024-01-01T03:00:00Z",
+ "visit": { "topCandidate": { "placeLocation": { "latLng": "52.5°,13.4°" } } }
+ }
+ ]
+ }
+ """.trimIndent()
+
+ val result = GoogleTimelineParser.parse(json.byteInputStream(), cancelled, nowSec)
+ assertEquals(2, result.rows.size)
+ assertEquals(52.5, result.rows[0].latitude, 1e-7)
+ assertEquals(52.51, result.rows[1].latitude, 1e-7)
+ }
+
+ @Test
+ fun `parseLatLngString handles degree suffixes and whitespace`() {
+ assertEquals(52.5 to 13.4, GoogleTimelineParser.parseLatLngString("52.5°,13.4°"))
+ assertEquals(52.5 to 13.4, GoogleTimelineParser.parseLatLngString("52.5 , 13.4"))
+ assertEquals(-33.86 to 151.21, GoogleTimelineParser.parseLatLngString("-33.86°, 151.21°"))
+ assertNull(GoogleTimelineParser.parseLatLngString("not coordinates"))
+ assertNull(GoogleTimelineParser.parseLatLngString("52.5"))
+ }
+
+ @Test
+ fun `legacy parser drops malformed entries but continues`() {
+ val json = """
+ {
+ "locations": [
+ { "latitudeE7": 525000000, "longitudeE7": 134000000, "timestampMs": "1704110400000" },
+ { "latitudeE7": 525000000 },
+ { "latitudeE7": 525000000, "longitudeE7": 134000000, "timestampMs": "1704110500000" }
+ ]
+ }
+ """.trimIndent()
+
+ val result = GoogleTimelineParser.parse(json.byteInputStream(), cancelled, nowSec)
+ assertEquals(2, result.rows.size)
+ assertEquals(1, result.invalid)
+ }
+
+ @Test
+ fun `cancellation interrupts mid-array`() {
+ val json = buildString {
+ append("{\"locations\":[")
+ for (i in 0 until 1000) {
+ if (i > 0) append(",")
+ append("""{"latitudeE7":525000000,"longitudeE7":134000000,"timestampMs":"1704110400000"}""")
+ }
+ append("]}")
+ }
+ val flag = AtomicBoolean(true)
+ try {
+ GoogleTimelineParser.parse(json.byteInputStream(), flag, nowSec)
+ } catch (e: InterruptedException) {
+ assertNotNull(e.message)
+ return
+ }
+ throw AssertionError("expected InterruptedException")
+ }
+}
diff --git a/apps/mobile/android/app/src/test/java/com/Colota/importer/GpxParserTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/importer/GpxParserTest.kt
new file mode 100644
index 00000000..64ee4f50
--- /dev/null
+++ b/apps/mobile/android/app/src/test/java/com/Colota/importer/GpxParserTest.kt
@@ -0,0 +1,133 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.importer
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.RobolectricTestRunner
+import java.util.concurrent.atomic.AtomicBoolean
+
+@RunWith(RobolectricTestRunner::class)
+class GpxParserTest {
+
+ private val cancelled = AtomicBoolean(false)
+ private val nowSec = 1_750_000_000L
+
+ @Test
+ fun `parses Colota GPX export with track-points and extensions`() {
+ val gpx = """
+ <?xml version="1.0" encoding="UTF-8"?>
+ <gpx version="1.1" creator="Colota">
+ <trk>
+ <name>Test</name>
+ <trkseg>
+ <trkpt lat="52.500000" lon="13.400000">
+ <ele>30</ele>
+ <time>2024-01-01T12:00:00.000Z</time>
+ <extensions>
+ <accuracy>5</accuracy>
+ <speed>2</speed>
+ <battery>80</battery>
+ </extensions>
+ </trkpt>
+ </trkseg>
+ </trk>
+ </gpx>
+ """.trimIndent()
+
+ val result = GpxParser.parse(gpx.byteInputStream(), cancelled, nowSec)
+ assertEquals(0, result.invalid)
+ assertEquals(1, result.rows.size)
+ val row = result.rows[0]
+ assertEquals(52.5, row.latitude, 1e-9)
+ assertEquals(13.4, row.longitude, 1e-9)
+ assertEquals(1_704_110_400L, row.timestamp)
+ assertEquals(30, row.altitude)
+ assertEquals(5, row.accuracy)
+ assertEquals(2, row.speed)
+ assertEquals(80, row.battery)
+ }
+
+ @Test
+ fun `parses GPX waypoints and track-points together`() {
+ val gpx = """
+ <?xml version="1.0" encoding="UTF-8"?>
+ <gpx version="1.1">
+ <wpt lat="52.5" lon="13.4">
+ <time>2024-01-01T12:00:00Z</time>
+ </wpt>
+ <trk>
+ <trkseg>
+ <trkpt lat="52.6" lon="13.5">
+ <time>2024-01-01T12:01:00Z</time>
+ </trkpt>
+ </trkseg>
+ </trk>
+ </gpx>
+ """.trimIndent()
+
+ val result = GpxParser.parse(gpx.byteInputStream(), cancelled, nowSec)
+ assertEquals(2, result.rows.size)
+ assertEquals(52.5, result.rows[0].latitude, 1e-9)
+ assertEquals(52.6, result.rows[1].latitude, 1e-9)
+ }
+
+ @Test
+ fun `parses Garmin TrackPointExtension nested namespaced extensions`() {
+ val gpx = """
+ <?xml version="1.0" encoding="UTF-8"?>
+ <gpx version="1.1" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1">
+ <trk><trkseg>
+ <trkpt lat="52.5" lon="13.4">
+ <time>2024-01-01T12:00:00Z</time>
+ <extensions>
+ <gpxtpx:TrackPointExtension>
+ <gpxtpx:speed>3.5</gpxtpx:speed>
+ <gpxtpx:course>180.0</gpxtpx:course>
+ </gpxtpx:TrackPointExtension>
+ </extensions>
+ </trkpt>
+ </trkseg></trk>
+ </gpx>
+ """.trimIndent()
+
+ val result = GpxParser.parse(gpx.byteInputStream(), cancelled, nowSec)
+ assertEquals(1, result.rows.size)
+ assertEquals(3, result.rows[0].speed)
+ assertEquals(180.0, result.rows[0].bearing!!, 1e-9)
+ }
+
+ @Test
+ fun `drops trkpt without time as invalid`() {
+ val gpx = """
+ <?xml version="1.0" encoding="UTF-8"?>
+ <gpx version="1.1">
+ <trk><trkseg>
+ <trkpt lat="52.5" lon="13.4"><ele>30</ele></trkpt>
+ </trkseg></trk>
+ </gpx>
+ """.trimIndent()
+
+ val result = GpxParser.parse(gpx.byteInputStream(), cancelled, nowSec)
+ assertEquals(1, result.invalid)
+ assertTrue(result.rows.isEmpty())
+ }
+
+ @Test
+ fun `cancellation flag interrupts parse`() {
+ val gpx = "<?xml version=\"1.0\"?><gpx><trk><trkseg>" +
+ "<trkpt lat=\"1\" lon=\"1\"><time>2024-01-01T00:00:00Z</time></trkpt>".repeat(1000) +
+ "</trkseg></trk></gpx>"
+ try {
+ GpxParser.parse(gpx.byteInputStream(), AtomicBoolean(true), nowSec)
+ } catch (_: InterruptedException) {
+ return
+ }
+ throw AssertionError("expected InterruptedException")
+ }
+}
diff --git a/apps/mobile/android/app/src/test/java/com/Colota/importer/KmlParserTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/importer/KmlParserTest.kt
new file mode 100644
index 00000000..d6915487
--- /dev/null
+++ b/apps/mobile/android/app/src/test/java/com/Colota/importer/KmlParserTest.kt
@@ -0,0 +1,121 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.importer
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNotNull
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.RobolectricTestRunner
+import java.util.concurrent.atomic.AtomicBoolean
+
+@RunWith(RobolectricTestRunner::class)
+class KmlParserTest {
+
+ private val cancelled = AtomicBoolean(false)
+ private val nowSec = 1_750_000_000L
+
+ @Test
+ fun `parses Colota KML export Point Placemark with TimeStamp`() {
+ val kml = """
+ <?xml version="1.0" encoding="UTF-8"?>
+ <kml xmlns="http://www.opengis.net/kml/2.2">
+ <Document>
+ <Placemark>
+ <TimeStamp><when>2024-01-01T12:00:00.000Z</when></TimeStamp>
+ <Point>
+ <coordinates>13.4,52.5,30</coordinates>
+ </Point>
+ </Placemark>
+ </Document>
+ </kml>
+ """.trimIndent()
+
+ val result = KmlParser.parse(kml.byteInputStream(), cancelled, nowSec)
+ assertEquals(0, result.invalid)
+ assertEquals(1, result.rows.size)
+ val row = result.rows[0]
+ // KML coordinates are lon,lat - the parser must flip back.
+ assertEquals(52.5, row.latitude, 1e-9)
+ assertEquals(13.4, row.longitude, 1e-9)
+ assertEquals(30, row.altitude)
+ assertEquals(1_704_110_400L, row.timestamp)
+ }
+
+ @Test
+ fun `skips style-only Placemarks without counting them as invalid`() {
+ val kml = """
+ <?xml version="1.0" encoding="UTF-8"?>
+ <kml xmlns="http://www.opengis.net/kml/2.2">
+ <Document>
+ <Style id="pathStyle"><LineStyle><color>ff0000ff</color></LineStyle></Style>
+ <Placemark>
+ <TimeStamp><when>2024-01-01T12:00:00Z</when></TimeStamp>
+ <Point><coordinates>13.4,52.5</coordinates></Point>
+ </Placemark>
+ <Placemark>
+ <name>Track Path</name>
+ <styleUrl>#pathStyle</styleUrl>
+ </Placemark>
+ </Document>
+ </kml>
+ """.trimIndent()
+
+ val result = KmlParser.parse(kml.byteInputStream(), cancelled, nowSec)
+ assertEquals(1, result.rows.size)
+ assertEquals("metadata-only Placemark shouldn't count as invalid", 0, result.invalid)
+ }
+
+ @Test
+ fun `drops LineString-only Placemark since vertices have no per-point time`() {
+ val kml = """
+ <?xml version="1.0" encoding="UTF-8"?>
+ <kml xmlns="http://www.opengis.net/kml/2.2">
+ <Document>
+ <Placemark>
+ <LineString>
+ <coordinates>13.4,52.5 13.5,52.6 13.6,52.7</coordinates>
+ </LineString>
+ </Placemark>
+ </Document>
+ </kml>
+ """.trimIndent()
+
+ val result = KmlParser.parse(kml.byteInputStream(), cancelled, nowSec)
+ assertTrue(result.rows.isEmpty())
+ assertEquals("three skipped vertices counted as invalid", 3, result.invalid)
+ }
+
+ @Test
+ fun `parseCoordinatesText handles whitespace-separated tuples`() {
+ val text = """
+ 13.4,52.5,30
+ 13.5,52.6
+ 13.6,52.7,40
+ """.trimIndent()
+ val parsed = KmlParser.parseCoordinatesText(text)
+ assertEquals(3, parsed.size)
+ assertEquals(52.5 to 13.4, parsed[0].first to parsed[0].second)
+ assertEquals(30, parsed[0].third)
+ assertEquals(52.6 to 13.5, parsed[1].first to parsed[1].second)
+ assertNotNull("alt 30 should round-trip", parsed[0].third)
+ }
+
+ @Test
+ fun `cancellation flag interrupts parse`() {
+ val kml = """<?xml version="1.0"?><kml xmlns="http://www.opengis.net/kml/2.2"><Document>""" +
+ ("<Placemark><TimeStamp><when>2024-01-01T12:00:00Z</when></TimeStamp>" +
+ "<Point><coordinates>13.4,52.5</coordinates></Point></Placemark>").repeat(1000) +
+ "</Document></kml>"
+ try {
+ KmlParser.parse(kml.byteInputStream(), AtomicBoolean(true), nowSec)
+ } catch (_: InterruptedException) {
+ return
+ }
+ throw AssertionError("expected InterruptedException")
+ }
+}
diff --git a/apps/mobile/android/app/src/test/java/com/Colota/importer/LocationImporterTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/importer/LocationImporterTest.kt
new file mode 100644
index 00000000..c3d10df6
--- /dev/null
+++ b/apps/mobile/android/app/src/test/java/com/Colota/importer/LocationImporterTest.kt
@@ -0,0 +1,478 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.importer
+
+import androidx.test.core.app.ApplicationProvider
+import com.Colota.data.DatabaseHelper
+import com.Colota.util.AppLogger
+import io.mockk.Runs
+import io.mockk.every
+import io.mockk.just
+import io.mockk.mockkObject
+import io.mockk.unmockkObject
+import org.junit.After
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNotNull
+import org.junit.Assert.assertNull
+import org.junit.Assert.assertTrue
+import org.junit.Before
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.RobolectricTestRunner
+import java.util.concurrent.atomic.AtomicBoolean
+
+@RunWith(RobolectricTestRunner::class)
+class LocationImporterTest {
+
+ private lateinit var db: DatabaseHelper
+
+ @Before
+ fun setUp() {
+ resetDbSingleton()
+ mockkObject(AppLogger)
+ every { AppLogger.d(any(), any()) } just Runs
+ every { AppLogger.i(any(), any()) } just Runs
+ every { AppLogger.w(any(), any()) } just Runs
+ every { AppLogger.e(any(), any(), any()) } just Runs
+
+ db = DatabaseHelper.getInstance(ApplicationProvider.getApplicationContext())
+ }
+
+ @After
+ fun tearDown() {
+ db.close()
+ resetDbSingleton()
+ unmockkObject(AppLogger)
+ }
+
+ private fun resetDbSingleton() {
+ val field = DatabaseHelper::class.java.getDeclaredField("INSTANCE")
+ field.isAccessible = true
+ field.set(null, null)
+ }
+
+ @Test
+ fun `detectFormat identifies Colota GeoJSON by FeatureCollection sniff`() {
+ val sniff = """{"type":"FeatureCollection","features":[]}"""
+ assertEquals(ImportFormat.GEOJSON, LocationImporter.detectFormat(sniff))
+ }
+
+ @Test
+ fun `detectFormat identifies legacy Takeout by latitudeE7 sniff`() {
+ // Records.json may be huge; sniffing the leading bytes must be enough.
+ val sniff = """{"locations":[{"latitudeE7":525000000,"longitudeE7":134000000,"timestampMs":"1700000000000"}"""
+ assertEquals(ImportFormat.GOOGLE_TIMELINE_LEGACY, LocationImporter.detectFormat(sniff))
+ }
+
+ @Test
+ fun `detectFormat identifies new on-device timeline by semanticSegments`() {
+ val sniff = """{"semanticSegments":[{"startTime":"2024-01-01T00:00:00Z"}]}"""
+ assertEquals(ImportFormat.GOOGLE_TIMELINE_NEW, LocationImporter.detectFormat(sniff))
+ }
+
+ @Test
+ fun `detectFormat identifies new on-device timeline by rawSignals`() {
+ val sniff = """{"rawSignals":[{"position":{"LatLng":"52.5°,13.4°"}}]}"""
+ assertEquals(ImportFormat.GOOGLE_TIMELINE_NEW, LocationImporter.detectFormat(sniff))
+ }
+
+ @Test
+ fun `detectFormat returns null for unrecognised content`() {
+ assertNull(LocationImporter.detectFormat("this is just text"))
+ }
+
+ @Test
+ fun `Google Timeline precedence over generic GeoJSON`() {
+ val sniff = """{"type":"something","features":[],"locations":[{"latitudeE7":1,"timestampMs":"1"}]}"""
+ assertEquals(ImportFormat.GOOGLE_TIMELINE_LEGACY, LocationImporter.detectFormat(sniff))
+ }
+
+ @Test
+ fun `detectFormat rejects XML files with DOCTYPE declarations`() {
+ val gpxWithDoctype = """
+ <?xml version="1.0"?>
+ <!DOCTYPE gpx [
+ <!ENTITY lol "lol">
+ <!ENTITY lol2 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
+ ]>
+ <gpx version="1.1"></gpx>
+ """.trimIndent()
+ try {
+ LocationImporter.detectFormat(gpxWithDoctype)
+ throw AssertionError("expected UnsupportedFormatException for DOCTYPE")
+ } catch (e: UnsupportedFormatException) {
+ assertTrue(e.message!!.contains("DOCTYPE"))
+ }
+ }
+
+ @Test
+ fun `detectFormat identifies GPX by root element`() {
+ val sniff = """<?xml version="1.0"?><gpx version="1.1" xmlns="http://www.topografix.com/GPX/1/1">"""
+ assertEquals(ImportFormat.GPX, LocationImporter.detectFormat(sniff))
+ }
+
+ @Test
+ fun `detectFormat identifies KML by root element`() {
+ val sniff = """<?xml version="1.0"?><kml xmlns="http://www.opengis.net/kml/2.2">"""
+ assertEquals(ImportFormat.KML, LocationImporter.detectFormat(sniff))
+ }
+
+ @Test
+ fun `detectFormat identifies CSV by header with lat lon and time columns`() {
+ val sniff = "id,timestamp,iso_time,latitude,longitude,accuracy\n0,1704110400,2024-01-01T12:00:00Z,52.5,13.4,5"
+ assertEquals(ImportFormat.CSV, LocationImporter.detectFormat(sniff))
+ }
+
+ @Test
+ fun `detectFormat rejects CSV without recognisable header columns`() {
+ val sniff = "id,name,description\n0,foo,bar"
+ assertNull(LocationImporter.detectFormat(sniff))
+ }
+
+ @Test
+ fun `dedupKey rounds to 6 decimals so identical samples collide`() {
+ val a = LocationImporter.dedupKey(1000L, 52.5000001, 13.4000001)
+ val b = LocationImporter.dedupKey(1000L, 52.500000, 13.400000)
+ assertEquals(a, b)
+ }
+
+ @Test
+ fun `dedupKey separates samples with different timestamps`() {
+ val a = LocationImporter.dedupKey(1000L, 52.5, 13.4)
+ val b = LocationImporter.dedupKey(1001L, 52.5, 13.4)
+ assertTrue(a != b)
+ }
+
+ @Test
+ fun `commit writes rows with sent equals one so they do not re-upload`() {
+ val rows = listOf(
+ ImportRow(1_700_000_000L, 52.5, 13.4, 5, 30, 2, null, 80),
+ )
+ val inserted = db.bulkInsertImportedLocations(rows)
+ assertEquals(1, inserted)
+
+ db.readableDatabase.rawQuery(
+ "SELECT sent, latitude, longitude, timestamp, accuracy FROM ${DatabaseHelper.TABLE_LOCATIONS}",
+ null,
+ ).use { c ->
+ assertTrue(c.moveToFirst())
+ assertEquals(1, c.getInt(0))
+ assertEquals(52.5, c.getDouble(1), 1e-9)
+ assertEquals(13.4, c.getDouble(2), 1e-9)
+ assertEquals(1_700_000_000L, c.getLong(3))
+ assertEquals(5, c.getInt(4))
+ }
+ }
+
+ @Test
+ fun `bulk insert commits the full batch in one transaction`() {
+ val rows = (0 until 100).map { i ->
+ ImportRow(
+ timestamp = 1_700_000_000L + i,
+ latitude = 52.5 + i * 0.001,
+ longitude = 13.4 + i * 0.001,
+ accuracy = 5,
+ altitude = null,
+ speed = null,
+ bearing = null,
+ battery = null,
+ )
+ }
+ val inserted = db.bulkInsertImportedLocations(rows)
+ assertEquals(100, inserted)
+ assertEquals(100, db.getStats().total)
+ }
+
+ @Test
+ fun `forEachLocationKeyInRange streams only rows in the timestamp window`() {
+ db.bulkInsertImportedLocations(listOf(
+ ImportRow(1_000L, 1.0, 1.0, null, null, null, null, null),
+ ImportRow(2_000L, 2.0, 2.0, null, null, null, null, null),
+ ImportRow(3_000L, 3.0, 3.0, null, null, null, null, null),
+ ))
+
+ val seen = mutableListOf<Long>()
+ db.forEachLocationKeyInRange(1_500L, 2_500L) { ts, _, _ -> seen.add(ts) }
+ assertEquals(listOf(2_000L), seen)
+ }
+
+ @Test
+ fun `dedup analysis skips rows already present in DB across same date range`() {
+ db.bulkInsertImportedLocations(listOf(
+ ImportRow(1_700_000_000L, 52.5, 13.4, null, null, null, null, null),
+ ))
+
+ val incomingKey = LocationImporter.dedupKey(1_700_000_000L, 52.5, 13.4)
+ val existing = HashSet<DedupKey>()
+ db.forEachLocationKeyInRange(1_700_000_000L, 1_700_000_000L) { ts, lat, lon ->
+ existing.add(LocationImporter.dedupKey(ts, lat, lon))
+ }
+ assertTrue("seeded row must be in existing set", existing.contains(incomingKey))
+ }
+
+ @Test
+ fun `commit returns inserted count and stats reflect new rows`() {
+ val rows = listOf(
+ ImportRow(1_700_000_001L, 52.5, 13.4, 5, null, null, null, null),
+ ImportRow(1_700_000_002L, 52.6, 13.5, 5, null, null, null, null),
+ )
+ val inserted = LocationImporter.commit(db, rows)
+ assertEquals(2, inserted)
+ val stats = db.getStats()
+ assertEquals(2, stats.total)
+ // Imported rows are sent=1 so they are not counted as queued.
+ assertEquals(2, stats.sent)
+ assertEquals(0, stats.queued)
+ }
+
+ @Test
+ fun `empty commit is a no-op`() {
+ assertEquals(0, LocationImporter.commit(db, emptyList()))
+ assertEquals(0, db.getStats().total)
+ }
+
+ @Test
+ fun `commit with asQueued=true writes sent=0 plus paired queue rows in one transaction`() {
+ val rows = listOf(
+ ImportRow(1_700_000_001L, 52.5, 13.4, 5, 30, 2, null, 80),
+ ImportRow(1_700_000_002L, 52.6, 13.5, 7, null, null, null, null),
+ )
+ val options = com.Colota.importer.CommitOptions(
+ asQueued = true,
+ endpoint = "https://my-backend.example.com/locations",
+ isOfflineMode = false,
+ fieldMap = mapOf("lat" to "lat", "lon" to "lon", "acc" to "acc", "tst" to "tst", "batt" to "batt", "bs" to "bs"),
+ customFields = emptyMap(),
+ apiFormat = com.Colota.sync.ApiFormat.FIELD_MAPPED,
+ )
+
+ val inserted = LocationImporter.commit(db, rows, options)
+
+ assertEquals(2, inserted)
+ val stats = db.getStats()
+ assertEquals(2, stats.total)
+ assertEquals(0, stats.sent)
+ assertEquals(2, stats.queued)
+
+ db.readableDatabase.rawQuery(
+ "SELECT payload FROM ${DatabaseHelper.TABLE_QUEUE} ORDER BY id ASC", null,
+ ).use { c ->
+ assertTrue(c.moveToFirst())
+ val payload = org.json.JSONObject(c.getString(0))
+ assertEquals(52.5, payload.getDouble("lat"), 1e-9)
+ assertEquals(13.4, payload.getDouble("lon"), 1e-9)
+ assertEquals(1_700_000_001L, payload.getLong("tst"))
+ }
+ }
+
+ @Test
+ fun `commit with asQueued=true rejects when endpoint is blank`() {
+ // Fail loud rather than silently downgrade to sent=1, which would mask the misconfig.
+ val rows = listOf(ImportRow(1_700_000_001L, 52.5, 13.4, 5, null, null, null, null))
+ try {
+ LocationImporter.commit(
+ db,
+ rows,
+ com.Colota.importer.CommitOptions(asQueued = true, endpoint = ""),
+ )
+ throw AssertionError("expected IllegalArgumentException for blank endpoint")
+ } catch (_: IllegalArgumentException) {
+ // expected
+ }
+ assertEquals("no rows should have been written", 0, db.getStats().total)
+ }
+
+ @Test
+ fun `commit with asQueued=true rejects when offline mode is on`() {
+ val rows = listOf(ImportRow(1_700_000_001L, 52.5, 13.4, 5, null, null, null, null))
+ try {
+ LocationImporter.commit(
+ db,
+ rows,
+ com.Colota.importer.CommitOptions(
+ asQueued = true,
+ endpoint = "https://my-backend.example.com",
+ isOfflineMode = true,
+ ),
+ )
+ throw AssertionError("expected IllegalArgumentException for offline mode")
+ } catch (_: IllegalArgumentException) {
+ // expected
+ }
+ assertEquals("no rows should have been written", 0, db.getStats().total)
+ }
+
+ @Test
+ fun `preview folds adjacent file-internal duplicates`() {
+ val geojson = """
+ {
+ "type": "FeatureCollection",
+ "features": [
+ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [13.4, 52.5] },
+ "properties": { "time": "2024-01-01T12:00:00.000Z" } },
+ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [13.4, 52.5] },
+ "properties": { "time": "2024-01-01T12:00:00.000Z" } }
+ ]
+ }
+ """.trimIndent()
+
+ val uri = writeFixture("dup.geojson", geojson)
+ val resolver = ApplicationProvider.getApplicationContext<android.content.Context>().contentResolver
+
+ val preview = LocationImporter.preview(
+ contentResolver = resolver,
+ uri = uri,
+ db = db,
+ cancelled = AtomicBoolean(false),
+ nowSec = 1_750_000_000L,
+ )
+
+ assertEquals(2, preview.totalParsed)
+ assertEquals(1, preview.duplicates)
+ assertEquals(1, preview.rowsToCommit.size)
+ }
+
+ @Test
+ fun `file-internal duplicate that also matches a DB row is fully deduplicated`() {
+ // Naive merge-walk would emit the second copy as new; pre-fold prevents that.
+ db.bulkInsertImportedLocations(listOf(
+ ImportRow(1_704_110_400L, 52.5, 13.4, null, null, null, null, null),
+ ))
+
+ val geojson = """
+ {
+ "type": "FeatureCollection",
+ "features": [
+ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [13.4, 52.5] },
+ "properties": { "time": "2024-01-01T12:00:00.000Z" } },
+ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [13.4, 52.5] },
+ "properties": { "time": "2024-01-01T12:00:00.000Z" } }
+ ]
+ }
+ """.trimIndent()
+
+ val uri = writeFixture("dup-in-file-and-db.geojson", geojson)
+ val resolver = ApplicationProvider.getApplicationContext<android.content.Context>().contentResolver
+
+ val preview = LocationImporter.preview(
+ contentResolver = resolver,
+ uri = uri,
+ db = db,
+ cancelled = AtomicBoolean(false),
+ nowSec = 1_750_000_000L,
+ )
+
+ assertEquals(2, preview.totalParsed)
+ assertEquals(2, preview.duplicates)
+ assertEquals(0, preview.rowsToCommit.size)
+ }
+
+ @Test
+ fun `preview merge-walk handles unsorted source by sorting first`() {
+ db.bulkInsertImportedLocations(listOf(
+ ImportRow(1_704_110_500L, 52.51, 13.41, null, null, null, null, null),
+ ))
+
+ val geojson = """
+ {
+ "type": "FeatureCollection",
+ "features": [
+ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [13.41, 52.51] },
+ "properties": { "time": "2024-01-01T12:01:40.000Z" } },
+ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [13.40, 52.50] },
+ "properties": { "time": "2024-01-01T12:00:00.000Z" } }
+ ]
+ }
+ """.trimIndent()
+
+ val uri = writeFixture("unsorted.geojson", geojson)
+ val resolver = ApplicationProvider.getApplicationContext<android.content.Context>().contentResolver
+
+ val preview = LocationImporter.preview(
+ contentResolver = resolver,
+ uri = uri,
+ db = db,
+ cancelled = AtomicBoolean(false),
+ nowSec = 1_750_000_000L,
+ )
+
+ // Second feature (12:01:40) collides with the seeded DB row; first feature is new.
+ assertEquals(2, preview.totalParsed)
+ assertEquals(1, preview.duplicates)
+ assertEquals(1, preview.rowsToCommit.size)
+ // The surviving row is the 12:00:00 one.
+ assertEquals(1_704_110_400L, preview.rowsToCommit[0].timestamp)
+ }
+
+ @Test
+ fun `bulkInsertImportedLocations rejects mismatched payloads length`() {
+ // Length mismatch is a caller bug; failing the require keeps the DB clean.
+ val rows = listOf(
+ ImportRow(1L, 1.0, 1.0, null, null, null, null, null),
+ ImportRow(2L, 2.0, 2.0, null, null, null, null, null),
+ )
+ try {
+ db.bulkInsertImportedLocations(rows, payloads = listOf("only one payload"))
+ throw AssertionError("expected IllegalArgumentException for length mismatch")
+ } catch (_: IllegalArgumentException) {
+ // expected
+ }
+ assertEquals(0, db.getStats().total)
+ }
+
+ @Test
+ fun `preview parses Colota GeoJSON, dedups against DB, returns staged rows`() {
+ // Seed one existing point that will collide with the second feature in the import.
+ db.bulkInsertImportedLocations(listOf(
+ ImportRow(1_704_110_500L, 52.51, 13.41, null, null, null, null, null),
+ ))
+
+ val geojson = """
+ {
+ "type": "FeatureCollection",
+ "features": [
+ {
+ "type": "Feature",
+ "geometry": { "type": "Point", "coordinates": [13.40, 52.50] },
+ "properties": { "time": "2024-01-01T12:00:00.000Z" }
+ },
+ {
+ "type": "Feature",
+ "geometry": { "type": "Point", "coordinates": [13.41, 52.51] },
+ "properties": { "time": "2024-01-01T12:01:40.000Z" }
+ }
+ ]
+ }
+ """.trimIndent()
+
+ val uri = writeFixture("colota-export.geojson", geojson)
+ val resolver = ApplicationProvider.getApplicationContext<android.content.Context>().contentResolver
+
+ val preview = LocationImporter.preview(
+ contentResolver = resolver,
+ uri = uri,
+ db = db,
+ cancelled = AtomicBoolean(false),
+ nowSec = 1_750_000_000L,
+ )
+
+ assertEquals(ImportFormat.GEOJSON, preview.format)
+ assertEquals(2, preview.totalParsed)
+ assertEquals(0, preview.invalid)
+ assertEquals(1, preview.duplicates)
+ assertEquals(1, preview.rowsToCommit.size)
+ assertNotNull(preview.dateRangeStartSec)
+ assertEquals(1_704_110_400L, preview.dateRangeStartSec)
+ assertEquals(1_704_110_500L, preview.dateRangeEndSec)
+ }
+
+ private fun writeFixture(name: String, content: String): android.net.Uri {
+ val ctx = ApplicationProvider.getApplicationContext<android.content.Context>()
+ val file = java.io.File(ctx.cacheDir, name)
+ file.writeText(content, Charsets.UTF_8)
+ return android.net.Uri.fromFile(file)
+ }
+}
diff --git a/apps/mobile/android/apps/mobile/android/app/src/test/java/com/colota/importer/CsvParserTest.kt b/apps/mobile/android/apps/mobile/android/app/src/test/java/com/colota/importer/CsvParserTest.kt
new file mode 100644
index 00000000..ceedc165
--- /dev/null
+++ b/apps/mobile/android/apps/mobile/android/app/src/test/java/com/colota/importer/CsvParserTest.kt
@@ -0,0 +1,150 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.importer
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.RobolectricTestRunner
+import java.util.concurrent.atomic.AtomicBoolean
+
+@RunWith(RobolectricTestRunner::class)
+class CsvParserTest {
+
+ private val cancelled = AtomicBoolean(false)
+ private val nowSec = 1_750_000_000L
+
+ @Test
+ fun `parses Colota CSV export header order`() {
+ // Mirrors apps/mobile/android/app/src/main/java/com/colota/export/ExportConverters.kt
+ // CsvFormat header: id,timestamp,iso_time,latitude,longitude,accuracy,altitude,speed,battery
+ val csv = """
+ id,timestamp,iso_time,latitude,longitude,accuracy,altitude,speed,battery
+ 0,1704110400,2024-01-01T12:00:00.000Z,52.5,13.4,5,30,2,80
+ """.trimIndent()
+
+ val result = CsvParser.parse(csv.byteInputStream(), cancelled, nowSec)
+ assertEquals(0, result.invalid)
+ assertEquals(1, result.rows.size)
+ val row = result.rows[0]
+ assertEquals(52.5, row.latitude, 1e-9)
+ assertEquals(13.4, row.longitude, 1e-9)
+ assertEquals(1_704_110_400L, row.timestamp)
+ assertEquals(5, row.accuracy)
+ assertEquals(30, row.altitude)
+ assertEquals(2, row.speed)
+ assertEquals(80, row.battery)
+ }
+
+ @Test
+ fun `accepts foreign column order via header lookup`() {
+ // Foreign CSVs (Strava, custom spreadsheets) have the same fields in different
+ // orders. The parser maps by header name, not by position, so this should work.
+ val csv = """
+ time,lat,lon,acc
+ 2024-01-01T12:00:00Z,52.5,13.4,7
+ """.trimIndent()
+
+ val result = CsvParser.parse(csv.byteInputStream(), cancelled, nowSec)
+ assertEquals(1, result.rows.size)
+ assertEquals(7, result.rows[0].accuracy)
+ }
+
+ @Test
+ fun `falls back from numeric timestamp to ISO when numeric is blank`() {
+ // Tolerates a partially populated row: numeric ts blank, ISO column present.
+ val csv = """
+ timestamp,iso_time,latitude,longitude
+ ,2024-01-01T12:00:00Z,52.5,13.4
+ """.trimIndent()
+
+ val result = CsvParser.parse(csv.byteInputStream(), cancelled, nowSec)
+ assertEquals(1, result.rows.size)
+ assertEquals(1_704_110_400L, result.rows[0].timestamp)
+ }
+
+ @Test
+ fun `rejects header missing required columns`() {
+ // No lat/lon means we can't import anything; surface a clear error rather
+ // than silently producing zero rows.
+ val csv = """
+ id,name,description
+ 0,foo,bar
+ """.trimIndent()
+
+ try {
+ CsvParser.parse(csv.byteInputStream(), cancelled, nowSec)
+ } catch (e: UnsupportedFormatException) {
+ assertTrue(e.message?.contains("latitude") == true)
+ return
+ }
+ throw AssertionError("expected UnsupportedFormatException for missing required columns")
+ }
+
+ @Test
+ fun `drops malformed rows but continues parsing`() {
+ // A single bad row shouldn't abort the whole import.
+ val csv = """
+ timestamp,latitude,longitude
+ 1704110400,52.5,13.4
+ ,,
+ 1704110500,52.6,13.5
+ """.trimIndent()
+
+ val result = CsvParser.parse(csv.byteInputStream(), cancelled, nowSec)
+ assertEquals(2, result.rows.size)
+ assertEquals(1, result.invalid)
+ }
+
+ @Test
+ fun `skips blank lines without counting them as invalid`() {
+ // Blank lines are common in hand-edited CSVs (trailing newline, separator
+ // between sections). They're not data, not malformed data.
+ val csv = """
+ timestamp,latitude,longitude
+ 1704110400,52.5,13.4
+
+ 1704110500,52.6,13.5
+ """.trimIndent()
+
+ val result = CsvParser.parse(csv.byteInputStream(), cancelled, nowSec)
+ assertEquals(2, result.rows.size)
+ assertEquals(0, result.invalid)
+ }
+
+ @Test
+ fun `splitCsvLine strips surrounding double quotes`() {
+ val parts = CsvParser.splitCsvLine("\"a\",b,\"c\"")
+ assertEquals(listOf("a", "b", "c"), parts)
+ }
+
+ @Test
+ fun `parseHeader maps aliases to canonical indices`() {
+ // Common aliases (lat/lng/velocity etc.) should map correctly so we accept
+ // exports from other apps without forcing the user to rename columns.
+ val cols = CsvParser.parseHeader("ts,lat,lng,velocity,ele")
+ assertEquals(0, cols.tsNumeric)
+ assertEquals(1, cols.lat)
+ assertEquals(2, cols.lon)
+ assertEquals(3, cols.speed)
+ assertEquals(4, cols.altitude)
+ }
+
+ @Test
+ fun `cancellation flag interrupts parse`() {
+ val csv = buildString {
+ append("timestamp,latitude,longitude\n")
+ for (i in 0 until 1000) append("1704110${400 + i},52.5,13.4\n")
+ }
+ try {
+ CsvParser.parse(csv.byteInputStream(), AtomicBoolean(true), nowSec)
+ } catch (_: InterruptedException) {
+ return
+ }
+ throw AssertionError("expected InterruptedException")
+ }
+}
diff --git a/apps/mobile/android/apps/mobile/android/app/src/test/java/com/colota/importer/GpxParserTest.kt b/apps/mobile/android/apps/mobile/android/app/src/test/java/com/colota/importer/GpxParserTest.kt
new file mode 100644
index 00000000..3d85acc8
--- /dev/null
+++ b/apps/mobile/android/apps/mobile/android/app/src/test/java/com/colota/importer/GpxParserTest.kt
@@ -0,0 +1,141 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.importer
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.RobolectricTestRunner
+import java.util.concurrent.atomic.AtomicBoolean
+
+@RunWith(RobolectricTestRunner::class)
+class GpxParserTest {
+
+ private val cancelled = AtomicBoolean(false)
+ private val nowSec = 1_750_000_000L
+
+ @Test
+ fun `parses Colota GPX export with track-points and extensions`() {
+ // Mirrors apps/mobile/android/app/src/main/java/com/colota/export/ExportConverters.kt
+ // GpxFormat output: trkpt with ele/time/extensions for accuracy/speed/battery.
+ val gpx = """
+ <?xml version="1.0" encoding="UTF-8"?>
+ <gpx version="1.1" creator="Colota">
+ <trk>
+ <name>Test</name>
+ <trkseg>
+ <trkpt lat="52.500000" lon="13.400000">
+ <ele>30</ele>
+ <time>2024-01-01T12:00:00.000Z</time>
+ <extensions>
+ <accuracy>5</accuracy>
+ <speed>2</speed>
+ <battery>80</battery>
+ </extensions>
+ </trkpt>
+ </trkseg>
+ </trk>
+ </gpx>
+ """.trimIndent()
+
+ val result = GpxParser.parse(gpx.byteInputStream(), cancelled, nowSec)
+ assertEquals(0, result.invalid)
+ assertEquals(1, result.rows.size)
+ val row = result.rows[0]
+ assertEquals(52.5, row.latitude, 1e-9)
+ assertEquals(13.4, row.longitude, 1e-9)
+ assertEquals(1_704_110_400L, row.timestamp)
+ assertEquals(30, row.altitude)
+ assertEquals(5, row.accuracy)
+ assertEquals(2, row.speed)
+ assertEquals(80, row.battery)
+ }
+
+ @Test
+ fun `parses GPX waypoints and track-points together`() {
+ // Real GPX files mix wpt (standalone), rte/rtept, trk/trkseg/trkpt. The semantic
+ // distinction (planned route vs recorded track) doesn't survive into the flat
+ // locations table - we collect them all.
+ val gpx = """
+ <?xml version="1.0" encoding="UTF-8"?>
+ <gpx version="1.1">
+ <wpt lat="52.5" lon="13.4">
+ <time>2024-01-01T12:00:00Z</time>
+ </wpt>
+ <trk>
+ <trkseg>
+ <trkpt lat="52.6" lon="13.5">
+ <time>2024-01-01T12:01:00Z</time>
+ </trkpt>
+ </trkseg>
+ </trk>
+ </gpx>
+ """.trimIndent()
+
+ val result = GpxParser.parse(gpx.byteInputStream(), cancelled, nowSec)
+ assertEquals(2, result.rows.size)
+ assertEquals(52.5, result.rows[0].latitude, 1e-9)
+ assertEquals(52.6, result.rows[1].latitude, 1e-9)
+ }
+
+ @Test
+ fun `parses Garmin TrackPointExtension nested namespaced extensions`() {
+ // Sport watches commonly use the GPX 1.1 TrackPointExtension wrapper - the
+ // parser must recurse into it to find leaf elements like <gpxtpx:speed>.
+ val gpx = """
+ <?xml version="1.0" encoding="UTF-8"?>
+ <gpx version="1.1" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1">
+ <trk><trkseg>
+ <trkpt lat="52.5" lon="13.4">
+ <time>2024-01-01T12:00:00Z</time>
+ <extensions>
+ <gpxtpx:TrackPointExtension>
+ <gpxtpx:speed>3.5</gpxtpx:speed>
+ <gpxtpx:course>180.0</gpxtpx:course>
+ </gpxtpx:TrackPointExtension>
+ </extensions>
+ </trkpt>
+ </trkseg></trk>
+ </gpx>
+ """.trimIndent()
+
+ val result = GpxParser.parse(gpx.byteInputStream(), cancelled, nowSec)
+ assertEquals(1, result.rows.size)
+ assertEquals(3, result.rows[0].speed)
+ assertEquals(180.0, result.rows[0].bearing!!, 1e-9)
+ }
+
+ @Test
+ fun `drops trkpt without time as invalid`() {
+ // Time is required for the locations table; a trkpt missing it is unusable.
+ val gpx = """
+ <?xml version="1.0" encoding="UTF-8"?>
+ <gpx version="1.1">
+ <trk><trkseg>
+ <trkpt lat="52.5" lon="13.4"><ele>30</ele></trkpt>
+ </trkseg></trk>
+ </gpx>
+ """.trimIndent()
+
+ val result = GpxParser.parse(gpx.byteInputStream(), cancelled, nowSec)
+ assertEquals(1, result.invalid)
+ assertTrue(result.rows.isEmpty())
+ }
+
+ @Test
+ fun `cancellation flag interrupts parse`() {
+ val gpx = "<?xml version=\"1.0\"?><gpx><trk><trkseg>" +
+ "<trkpt lat=\"1\" lon=\"1\"><time>2024-01-01T00:00:00Z</time></trkpt>".repeat(1000) +
+ "</trkseg></trk></gpx>"
+ try {
+ GpxParser.parse(gpx.byteInputStream(), AtomicBoolean(true), nowSec)
+ } catch (_: InterruptedException) {
+ return
+ }
+ throw AssertionError("expected InterruptedException")
+ }
+}
diff --git a/apps/mobile/android/apps/mobile/android/app/src/test/java/com/colota/importer/KmlParserTest.kt b/apps/mobile/android/apps/mobile/android/app/src/test/java/com/colota/importer/KmlParserTest.kt
new file mode 100644
index 00000000..ce428185
--- /dev/null
+++ b/apps/mobile/android/apps/mobile/android/app/src/test/java/com/colota/importer/KmlParserTest.kt
@@ -0,0 +1,133 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.importer
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNotNull
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.RobolectricTestRunner
+import java.util.concurrent.atomic.AtomicBoolean
+
+@RunWith(RobolectricTestRunner::class)
+class KmlParserTest {
+
+ private val cancelled = AtomicBoolean(false)
+ private val nowSec = 1_750_000_000L
+
+ @Test
+ fun `parses Colota KML export Point Placemark with TimeStamp`() {
+ // Mirrors apps/mobile/android/app/src/main/java/com/colota/export/ExportConverters.kt
+ // KmlFormat output: each track sample is a Placemark with Point + TimeStamp/when.
+ val kml = """
+ <?xml version="1.0" encoding="UTF-8"?>
+ <kml xmlns="http://www.opengis.net/kml/2.2">
+ <Document>
+ <Placemark>
+ <TimeStamp><when>2024-01-01T12:00:00.000Z</when></TimeStamp>
+ <Point>
+ <coordinates>13.4,52.5,30</coordinates>
+ </Point>
+ </Placemark>
+ </Document>
+ </kml>
+ """.trimIndent()
+
+ val result = KmlParser.parse(kml.byteInputStream(), cancelled, nowSec)
+ assertEquals(0, result.invalid)
+ assertEquals(1, result.rows.size)
+ val row = result.rows[0]
+ // KML coordinate order is lon,lat - the parser must flip it back.
+ assertEquals(52.5, row.latitude, 1e-9)
+ assertEquals(13.4, row.longitude, 1e-9)
+ assertEquals(30, row.altitude)
+ assertEquals(1_704_110_400L, row.timestamp)
+ }
+
+ @Test
+ fun `skips style-only Placemarks without counting them as invalid`() {
+ // Colota's KML export includes a final LineString-only Placemark for the
+ // rendered track path. It has no per-point timestamp and no Point geometry;
+ // dropping it shouldn't pollute the invalid counter (which the UI shows).
+ val kml = """
+ <?xml version="1.0" encoding="UTF-8"?>
+ <kml xmlns="http://www.opengis.net/kml/2.2">
+ <Document>
+ <Style id="pathStyle"><LineStyle><color>ff0000ff</color></LineStyle></Style>
+ <Placemark>
+ <TimeStamp><when>2024-01-01T12:00:00Z</when></TimeStamp>
+ <Point><coordinates>13.4,52.5</coordinates></Point>
+ </Placemark>
+ <Placemark>
+ <name>Track Path</name>
+ <styleUrl>#pathStyle</styleUrl>
+ </Placemark>
+ </Document>
+ </kml>
+ """.trimIndent()
+
+ val result = KmlParser.parse(kml.byteInputStream(), cancelled, nowSec)
+ assertEquals(1, result.rows.size)
+ assertEquals("metadata-only Placemark shouldn't count as invalid", 0, result.invalid)
+ }
+
+ @Test
+ fun `drops LineString-only Placemark since vertices have no per-point time`() {
+ // KML LineStrings carry a whole-line TimeSpan at best; vertex-level timestamps
+ // aren't a thing in the schema. Importing the vertices would invent timestamps,
+ // so we drop them. They count as invalid so the user knows data was skipped.
+ val kml = """
+ <?xml version="1.0" encoding="UTF-8"?>
+ <kml xmlns="http://www.opengis.net/kml/2.2">
+ <Document>
+ <Placemark>
+ <LineString>
+ <coordinates>13.4,52.5 13.5,52.6 13.6,52.7</coordinates>
+ </LineString>
+ </Placemark>
+ </Document>
+ </kml>
+ """.trimIndent()
+
+ val result = KmlParser.parse(kml.byteInputStream(), cancelled, nowSec)
+ assertTrue(result.rows.isEmpty())
+ assertEquals("three skipped vertices counted as invalid", 3, result.invalid)
+ }
+
+ @Test
+ fun `parseCoordinatesText handles whitespace-separated tuples`() {
+ // KML LineString coords are whitespace-separated tuples; the spec allows
+ // newlines and tab indentation between them.
+ val text = """
+ 13.4,52.5,30
+ 13.5,52.6
+ 13.6,52.7,40
+ """.trimIndent()
+ val parsed = KmlParser.parseCoordinatesText(text)
+ assertEquals(3, parsed.size)
+ // tuples are (lat, lon, alt?) after the flip
+ assertEquals(52.5 to 13.4, parsed[0].first to parsed[0].second)
+ assertEquals(30, parsed[0].third)
+ // missing altitude -> null
+ assertEquals(52.6 to 13.5, parsed[1].first to parsed[1].second)
+ assertNotNull("alt 30 should round-trip", parsed[0].third)
+ }
+
+ @Test
+ fun `cancellation flag interrupts parse`() {
+ val kml = """<?xml version="1.0"?><kml xmlns="http://www.opengis.net/kml/2.2"><Document>""" +
+ ("<Placemark><TimeStamp><when>2024-01-01T12:00:00Z</when></TimeStamp>" +
+ "<Point><coordinates>13.4,52.5</coordinates></Point></Placemark>").repeat(1000) +
+ "</Document></kml>"
+ try {
+ KmlParser.parse(kml.byteInputStream(), AtomicBoolean(true), nowSec)
+ } catch (_: InterruptedException) {
+ return
+ }
+ throw AssertionError("expected InterruptedException")
+ }
+}
diff --git a/apps/mobile/src/components/index.ts b/apps/mobile/src/components/index.ts
index df7c92b7..6c1b7aab 100644
--- a/apps/mobile/src/components/index.ts
+++ b/apps/mobile/src/components/index.ts
@@ -23,6 +23,7 @@ export { TimePicker } from "./ui/TimePicker"
export { FloatingSaveIndicator } from "./ui/FloatingSaveIndicator"
export { Footer } from "./ui/Footer"
export { SpinningLoader } from "./ui/SpinningLoader"
+export { LoadingOverlay } from "./ui/LoadingOverlay"
export { LocationDisclosureModal } from "./ui/LocationDisclosureModal"
export { LocalNetworkDisclosureModal } from "./ui/LocalNetworkDisclosureModal"
export { AppModal } from "./ui/AppModal"
diff --git a/apps/mobile/src/components/ui/LoadingOverlay.tsx b/apps/mobile/src/components/ui/LoadingOverlay.tsx
new file mode 100644
index 00000000..2f4181b3
--- /dev/null
+++ b/apps/mobile/src/components/ui/LoadingOverlay.tsx
@@ -0,0 +1,61 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+import { ActivityIndicator, StyleSheet, Text, View } from "react-native"
+import { useTheme } from "../../hooks/useTheme"
+import { fonts } from "../../styles/typography"
+
+type Props = {
+ visible: boolean
+ title: string
+ message?: string
+}
+
+export function LoadingOverlay({ visible, title, message }: Props) {
+ const { colors } = useTheme()
+ if (!visible) return null
+ return (
+ <View style={[styles.overlay, { backgroundColor: colors.overlay }]}>
+ <View style={[styles.card, { backgroundColor: colors.card }]}>
+ <ActivityIndicator size="large" color={colors.primary} />
+ <Text style={[styles.title, { color: colors.text }]}>{title}</Text>
+ {message ? <Text style={[styles.message, { color: colors.textSecondary }]}>{message}</Text> : null}
+ </View>
+ </View>
+ )
+}
+
+const styles = StyleSheet.create({
+ overlay: {
+ position: "absolute",
+ top: 0,
+ left: 0,
+ right: 0,
+ bottom: 0,
+ zIndex: 999,
+ justifyContent: "center",
+ alignItems: "center"
+ },
+ card: {
+ padding: 32,
+ borderRadius: 16,
+ alignItems: "center",
+ shadowOffset: { width: 0, height: 4 },
+ shadowOpacity: 0.3,
+ shadowRadius: 8,
+ elevation: 8,
+ minWidth: 240
+ },
+ title: {
+ fontSize: 16,
+ ...fonts.semiBold,
+ marginTop: 16,
+ marginBottom: 8
+ },
+ message: {
+ fontSize: 13,
+ textAlign: "center"
+ }
+})
diff --git a/apps/mobile/src/screens/AutoExportScreen.tsx b/apps/mobile/src/screens/AutoExportScreen.tsx
index 6ec091ef..ff430a5e 100644
--- a/apps/mobile/src/screens/AutoExportScreen.tsx
+++ b/apps/mobile/src/screens/AutoExportScreen.tsx
@@ -347,7 +347,6 @@ export function AutoExportScreen(_props: ScreenProps) {
<Container>
<ScrollView contentContainerStyle={styles.scrollContent} showsVerticalScrollIndicator={false}>
<View style={styles.header}>
- <Text style={[styles.title, { color: colors.text }]}>Auto-Export</Text>
<Text style={[styles.subtitle, { color: colors.textSecondary }]}>
Automatically export your location data on a schedule
</Text>
@@ -595,14 +594,8 @@ const styles = StyleSheet.create({
marginTop: 20,
marginBottom: 20
},
- title: {
- fontSize: 28,
- ...fonts.bold,
- letterSpacing: -0.5
- },
subtitle: {
fontSize: 14,
- marginTop: 4,
lineHeight: 20
},
section: {
diff --git a/apps/mobile/src/screens/DataManagementScreen.tsx b/apps/mobile/src/screens/DataManagementScreen.tsx
index 37ddcd22..5a8a5caa 100644
--- a/apps/mobile/src/screens/DataManagementScreen.tsx
+++ b/apps/mobile/src/screens/DataManagementScreen.tsx
@@ -29,6 +29,8 @@ import { useTimeout } from "../hooks/useTimeout"
import { showConfirm } from "../services/modalService"
import { logger } from "../utils/logger"
+const BACKUP_TIP = "Tip: back up your data first (Settings -> Backup & Restore)."
+
export function DataManagementScreen({}: ScreenProps) {
const { colors } = useTheme()
const { settings } = useTracking()
@@ -161,7 +163,7 @@ export function DataManagementScreen({}: ScreenProps) {
const handleClearSentHistory = useCallback(async () => {
const confirmed = await showConfirm({
title: "Clear Sent History",
- message: `Delete ${stats.sent} sent location${stats.sent !== 1 ? "s" : ""}? This cannot be undone.`,
+ message: `Delete ${stats.sent} sent location${stats.sent !== 1 ? "s" : ""}? This cannot be undone.\n\n${BACKUP_TIP}`,
confirmText: "Clear",
destructive: true
})
@@ -176,7 +178,7 @@ export function DataManagementScreen({}: ScreenProps) {
const handleClearQueue = useCallback(async () => {
const confirmed = await showConfirm({
title: "Clear Queue",
- message: `Delete ${stats.queued} pending location${stats.queued !== 1 ? "s" : ""}? These will not be synced.`,
+ message: `Delete ${stats.queued} pending location${stats.queued !== 1 ? "s" : ""}? These will not be synced.\n\n${BACKUP_TIP}`,
confirmText: "Clear",
destructive: true
})
@@ -191,7 +193,7 @@ export function DataManagementScreen({}: ScreenProps) {
const handleDeleteAllLocations = useCallback(async () => {
const confirmed = await showConfirm({
title: "Delete All Locations",
- message: `Delete all ${stats.total} stored location${stats.total !== 1 ? "s" : ""}? This cannot be undone.`,
+ message: `Delete all ${stats.total} stored location${stats.total !== 1 ? "s" : ""}? This cannot be undone.\n\n${BACKUP_TIP}`,
confirmText: "Delete All",
destructive: true
})
@@ -212,7 +214,7 @@ export function DataManagementScreen({}: ScreenProps) {
const confirmed = await showConfirm({
title: "Delete Old Locations",
- message: `Delete all locations older than ${days} day${days !== 1 ? "s" : ""}? This cannot be undone.`,
+ message: `Delete all locations older than ${days} day${days !== 1 ? "s" : ""}? This cannot be undone.\n\n${BACKUP_TIP}`,
confirmText: "Delete",
destructive: true
})
diff --git a/apps/mobile/src/screens/ExportDataScreen.tsx b/apps/mobile/src/screens/ExportLocationsScreen.tsx
similarity index 73%
rename from apps/mobile/src/screens/ExportDataScreen.tsx
rename to apps/mobile/src/screens/ExportLocationsScreen.tsx
index eb7ea279..070c978d 100644
--- a/apps/mobile/src/screens/ExportDataScreen.tsx
+++ b/apps/mobile/src/screens/ExportLocationsScreen.tsx
@@ -4,10 +4,10 @@
*/
import { useState, useEffect, useCallback } from "react"
-import { Text, StyleSheet, View, ActivityIndicator, ScrollView } from "react-native"
+import { Text, StyleSheet, View, ScrollView } from "react-native"
import { fonts } from "../styles/typography"
-import { Download, MapPinOff } from "lucide-react-native"
-import { Container, Card, SectionTitle, Button, FormatSelector } from "../components"
+import { MapPinOff, Upload } from "lucide-react-native"
+import { Container, Card, SectionTitle, Button, FormatSelector, LoadingOverlay } from "../components"
import { useTheme } from "../hooks/useTheme"
import NativeLocationService from "../services/NativeLocationService"
import { EXPORT_FORMATS, ExportFormat } from "../utils/exportConverters"
@@ -15,7 +15,7 @@ import { logger } from "../utils/logger"
import { showAlert } from "../services/modalService"
import { ScreenProps } from "../types/global"
-export function ExportDataScreen({}: ScreenProps) {
+export function ExportLocationsScreen({}: ScreenProps) {
const { colors } = useTheme()
const [exporting, setExporting] = useState(false)
const [exportProgress, setExportProgress] = useState<string>("")
@@ -27,7 +27,7 @@ export function ExportDataScreen({}: ScreenProps) {
const stats = await NativeLocationService.getStats()
setTotalLocations(stats.total ?? 0)
} catch (error) {
- logger.error("[ExportDataScreen] Failed to load stats:", error)
+ logger.error("[ExportLocationsScreen] Failed to load stats:", error)
}
}, [])
@@ -64,10 +64,10 @@ export function ExportDataScreen({}: ScreenProps) {
`Colota Export - ${result.rowCount} locations`
)
} catch (shareError: any) {
- logger.warn("[ExportDataScreen] Share error:", shareError)
+ logger.warn("[ExportLocationsScreen] Share error:", shareError)
}
} catch (error) {
- logger.error("[ExportDataScreen] Export failed:", error)
+ logger.error("[ExportLocationsScreen] Export failed:", error)
showAlert("Export Failed", "Unable to export your data. Please try again.", "error")
} finally {
setExporting(false)
@@ -79,11 +79,6 @@ export function ExportDataScreen({}: ScreenProps) {
return (
<Container>
<ScrollView contentContainerStyle={styles.scrollContent} showsVerticalScrollIndicator={false}>
- {/* Header */}
- <View style={styles.header}>
- <Text style={[styles.title, { color: colors.text }]}>Export Data</Text>
- </View>
-
{totalLocations === 0 ? (
<Card style={styles.emptyCard}>
<View style={styles.emptyState}>
@@ -123,7 +118,7 @@ export function ExportDataScreen({}: ScreenProps) {
onPress={() => handleExport(selectedFormat)}
disabled={exporting}
title={`Export ${EXPORT_FORMATS[selectedFormat].label}`}
- icon={Download}
+ icon={Upload}
/>
</View>
)}
@@ -131,16 +126,7 @@ export function ExportDataScreen({}: ScreenProps) {
)}
</ScrollView>
- {/* Loading Overlay */}
- {exporting && (
- <View style={[styles.loader, { backgroundColor: colors.overlay }]}>
- <View style={[styles.loaderCard, { backgroundColor: colors.card }]}>
- <ActivityIndicator size="large" color={colors.primary} />
- <Text style={[styles.loaderTitle, { color: colors.text }]}>Exporting Data</Text>
- <Text style={[styles.loaderText, { color: colors.textSecondary }]}>{exportProgress}</Text>
- </View>
- </View>
- )}
+ <LoadingOverlay visible={exporting} title="Exporting Data" message={exportProgress} />
</Container>
)
}
@@ -148,17 +134,9 @@ export function ExportDataScreen({}: ScreenProps) {
const styles = StyleSheet.create({
scrollContent: {
paddingHorizontal: 16,
+ paddingTop: 20,
paddingBottom: 40
},
- header: {
- marginTop: 20,
- marginBottom: 20
- },
- title: {
- fontSize: 28,
- ...fonts.bold,
- letterSpacing: -0.5
- },
emptyCard: {
marginBottom: 24
},
@@ -207,36 +185,6 @@ const styles = StyleSheet.create({
section: {
marginBottom: 24
},
- loader: {
- position: "absolute",
- top: 0,
- left: 0,
- right: 0,
- bottom: 0,
- zIndex: 999,
- justifyContent: "center",
- alignItems: "center"
- },
- loaderCard: {
- padding: 32,
- borderRadius: 16,
- alignItems: "center",
- shadowOffset: { width: 0, height: 4 },
- shadowOpacity: 0.3,
- shadowRadius: 8,
- elevation: 8,
- minWidth: 240
- },
- loaderTitle: {
- fontSize: 16,
- ...fonts.semiBold,
- marginTop: 16,
- marginBottom: 8
- },
- loaderText: {
- fontSize: 13,
- textAlign: "center"
- },
exportButtonWrapper: {
marginBottom: 16
}
diff --git a/apps/mobile/src/screens/ImportLocationsScreen.tsx b/apps/mobile/src/screens/ImportLocationsScreen.tsx
new file mode 100644
index 00000000..f4568dfb
--- /dev/null
+++ b/apps/mobile/src/screens/ImportLocationsScreen.tsx
@@ -0,0 +1,383 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+import React, { useCallback, useEffect, useState } from "react"
+import { ScrollView, StyleSheet, Text, View } from "react-native"
+import { Activity, Database, Download, Globe, Map, MapPin, Table2 } from "lucide-react-native"
+import type { LucideIcon } from "lucide-react-native"
+import { fonts } from "../styles/typography"
+import { Button, Card, Container, Divider, LoadingOverlay, SectionTitle } from "../components"
+import { useTheme } from "../hooks/useTheme"
+import NativeLocationService from "../services/NativeLocationService"
+import ImportService, { type ImportFormat, type ImportPreview } from "../services/ImportService"
+import { showAlert, showChoice } from "../services/modalService"
+import { logger } from "../utils/logger"
+import { ScreenProps } from "../types/global"
+import type { ThemeColors } from "../types/global"
+
+const IMPORT_FORMAT_LABELS: Record<ImportFormat, string> = {
+ geojson: "GeoJSON",
+ google_timeline_legacy: "Google Timeline (Records.json)",
+ google_timeline_new: "Google Timeline",
+ gpx: "GPX",
+ kml: "KML",
+ csv: "CSV"
+}
+
+// Warn before queueing this many points - the backend has to absorb one upload per row.
+const SYNC_WARN_THRESHOLD = 10_000
+
+type FormatEntry = {
+ icon: LucideIcon
+ title: string
+ extension: string
+ description: string
+}
+
+const SUPPORTED_FORMATS: FormatEntry[] = [
+ {
+ icon: Globe,
+ title: "GeoJSON",
+ extension: ".geojson",
+ description: "FeatureCollection of Point features with a time property. Colota's own export round-trips cleanly."
+ },
+ {
+ icon: Database,
+ title: "Google Timeline (legacy)",
+ extension: "Records.json",
+ description:
+ "Older bulk Location History export from Google Takeout. Google removed this from Takeout in late 2024; use this for archived files."
+ },
+ {
+ icon: MapPin,
+ title: "Google Timeline",
+ extension: ".json",
+ description: "On-device export from Android Settings -> Location -> Location services -> Timeline."
+ },
+ {
+ icon: Activity,
+ title: "GPX",
+ extension: ".gpx",
+ description: "GPS Exchange Format. Sport watches, Strava exports, tracking apps."
+ },
+ {
+ icon: Map,
+ title: "KML",
+ extension: ".kml",
+ description:
+ "Keyhole Markup Language. Google Earth, My Maps. Placemarks with TimeStamps only - LineString-only tracks are skipped."
+ },
+ {
+ icon: Table2,
+ title: "CSV",
+ extension: ".csv",
+ description: "Comma-separated table. Header must include latitude, longitude, and a time column."
+ }
+]
+
+function formatDate(unixSeconds: number | null): string {
+ if (unixSeconds == null) return "-"
+ return new Date(unixSeconds * 1000).toLocaleDateString()
+}
+
+function buildPreviewMessage(preview: ImportPreview): string {
+ const lines: string[] = []
+ lines.push(`Format: ${IMPORT_FORMAT_LABELS[preview.format]}`)
+ lines.push(`Points found: ${preview.totalParsed.toLocaleString()}`)
+ if (preview.duplicates > 0) {
+ lines.push(`Duplicates skipped: ${preview.duplicates.toLocaleString()}`)
+ }
+ if (preview.invalid > 0) {
+ lines.push(`Invalid: ${preview.invalid.toLocaleString()}`)
+ }
+ lines.push(`Date range: ${formatDate(preview.dateRangeStartSec)} - ${formatDate(preview.dateRangeEndSec)}`)
+ lines.push("")
+ lines.push(`Import ${preview.newRows.toLocaleString()} new location${preview.newRows !== 1 ? "s" : ""}?`)
+ if (preview.canQueueForSync) {
+ lines.push("")
+ lines.push('Choose "Import + Queue for Sync" to also upload these to your configured backend.')
+ if (preview.newRows >= SYNC_WARN_THRESHOLD) {
+ lines.push(
+ `Heads up: queueing ${preview.newRows.toLocaleString()} points means ${preview.newRows.toLocaleString()} upload requests against your backend.`
+ )
+ }
+ }
+ lines.push("")
+ lines.push("Tip: back up your data first (Settings -> Backup & Restore). Imports can't be selectively undone.")
+ return lines.join("\n")
+}
+
+function importErrorMessage(e: unknown): string {
+ const code = (e as { code?: string }).code
+ switch (code) {
+ case "E_IMPORT_UNSUPPORTED":
+ return "This file format isn't recognised. Supported: GeoJSON, Google Timeline, GPX, KML, and CSV (with latitude/longitude/time columns)."
+ case "E_IMPORT_CANCELLED":
+ return "Import cancelled."
+ case "E_BUSY":
+ return "Another backup, restore or import is already in progress."
+ case "E_IMPORT_NO_PENDING":
+ return "No staged import to commit. Choose a file again."
+ case "E_IMPORT_SYNC_UNAVAILABLE":
+ return "Sync isn't configured (offline mode or empty endpoint). Import without sync instead, or set up sync in Settings first."
+ default:
+ return e instanceof Error ? e.message : "Import failed."
+ }
+}
+
+const FormatRow = ({ entry, colors }: { entry: FormatEntry; colors: ThemeColors }) => {
+ const Icon = entry.icon
+ return (
+ <View style={styles.formatRow}>
+ <Icon size={22} color={colors.textLight} />
+ <View style={styles.formatTextContent}>
+ <View style={styles.formatTitleRow}>
+ <Text style={[styles.formatTitle, { color: colors.text }]}>{entry.title}</Text>
+ <View
+ style={[
+ styles.extensionBadge,
+ { backgroundColor: colors.primary + "15", borderColor: colors.primary + "30" }
+ ]}
+ >
+ <Text style={[styles.extensionText, { color: colors.primaryDark }]}>{entry.extension}</Text>
+ </View>
+ </View>
+ <Text style={[styles.formatDescription, { color: colors.textLight }]}>{entry.description}</Text>
+ </View>
+ </View>
+ )
+}
+
+export function ImportLocationsScreen({}: ScreenProps) {
+ const { colors } = useTheme()
+ const [busy, setBusy] = useState(false)
+ const [progressLabel, setProgressLabel] = useState<string>("")
+ const [totalLocations, setTotalLocations] = useState(0)
+
+ const loadStats = useCallback(async () => {
+ try {
+ const stats = await NativeLocationService.getStats()
+ setTotalLocations(stats.total ?? 0)
+ } catch (error) {
+ logger.error("[ImportLocationsScreen] Failed to load stats:", error)
+ }
+ }, [])
+
+ useEffect(() => {
+ loadStats()
+ }, [loadStats])
+
+ const handleChooseFile = useCallback(async () => {
+ let source: { uri: string; displayName: string | null } | null = null
+ try {
+ source = await ImportService.pickImportSource()
+ } catch (err) {
+ logger.error("[ImportLocationsScreen] pickImportSource failed:", err)
+ showAlert("Picker error", "Could not open the file picker.", "error")
+ return
+ }
+ if (!source) return
+
+ setBusy(true)
+ setProgressLabel("Parsing file...")
+ let preview: ImportPreview
+ try {
+ preview = await ImportService.importLocationsFromFile(source.uri)
+ } catch (err) {
+ logger.error("[ImportLocationsScreen] importLocationsFromFile failed:", err)
+ setBusy(false)
+ setProgressLabel("")
+ showAlert("Import failed", importErrorMessage(err), "error")
+ return
+ }
+
+ if (preview.newRows === 0) {
+ await ImportService.cancelImport().catch(() => {})
+ setBusy(false)
+ setProgressLabel("")
+ const title = preview.totalParsed === 0 ? "Nothing to import" : "Already imported"
+ const message =
+ preview.totalParsed === 0
+ ? "No locations were found in this file."
+ : `All ${preview.duplicates.toLocaleString()} points in this file are already in your history.`
+ showAlert(title, message, "info")
+ return
+ }
+
+ setProgressLabel("")
+
+ const buttons: Array<{ text: string; style?: "primary" | "secondary" | "destructive" }> = [
+ { text: "Cancel", style: "secondary" },
+ { text: "Import", style: "primary" }
+ ]
+ if (preview.canQueueForSync) {
+ // Destructive: queueing fires uploads to the backend, can't be undone server-side.
+ buttons.push({ text: "Import + Queue for Sync", style: "destructive" })
+ }
+
+ const choice = await showChoice({
+ title: "Import locations?",
+ message: buildPreviewMessage(preview),
+ variant: "info",
+ buttons
+ })
+
+ if (choice === 0) {
+ await ImportService.cancelImport().catch(() => {})
+ setBusy(false)
+ return
+ }
+ const asQueued = choice === 2
+
+ setProgressLabel(
+ asQueued
+ ? `Importing ${preview.newRows.toLocaleString()} locations and queueing for sync...`
+ : `Importing ${preview.newRows.toLocaleString()} locations...`
+ )
+ try {
+ const inserted = await ImportService.commitImport(asQueued)
+ await loadStats()
+ showAlert(
+ "Import complete",
+ asQueued
+ ? `Imported ${inserted.toLocaleString()} location${inserted !== 1 ? "s" : ""} and queued for sync to your backend.`
+ : `Imported ${inserted.toLocaleString()} location${inserted !== 1 ? "s" : ""}.`,
+ "success"
+ )
+ } catch (err) {
+ logger.error("[ImportLocationsScreen] commitImport failed:", err)
+ showAlert("Import failed", importErrorMessage(err), "error")
+ } finally {
+ setBusy(false)
+ setProgressLabel("")
+ }
+ }, [loadStats])
+
+ return (
+ <Container>
+ <ScrollView contentContainerStyle={styles.scrollContent} showsVerticalScrollIndicator={false}>
+ {/* Stats Card */}
+ <View style={[styles.statsContainer, { backgroundColor: colors.card, borderColor: colors.border }]}>
+ <View style={styles.statsGrid}>
+ <View style={styles.statItem}>
+ <Text style={[styles.statLabel, { color: colors.textSecondary }]}>Total Locations</Text>
+ <Text style={[styles.statValue, { color: colors.primaryDark }]}>{totalLocations.toLocaleString()}</Text>
+ </View>
+ </View>
+ </View>
+
+ {/* Supported formats */}
+ <View style={styles.section}>
+ <SectionTitle>Supported Formats</SectionTitle>
+ <Card>
+ {SUPPORTED_FORMATS.map((entry, i) => (
+ <React.Fragment key={entry.title}>
+ {i > 0 && <Divider />}
+ <FormatRow entry={entry} colors={colors} />
+ </React.Fragment>
+ ))}
+ </Card>
+ </View>
+
+ {/* Import action */}
+ <View style={styles.section}>
+ <SectionTitle>Import from File</SectionTitle>
+ <Card>
+ <Text style={[styles.intro, { color: colors.textSecondary }]}>
+ Merge location history from external files into your Colota database. Duplicates are skipped
+ automatically.
+ </Text>
+ <Text style={[styles.intro, { color: colors.textSecondary }]}>
+ Imported rows are flagged as already replicated by default, so they stay local. If you've configured an
+ optional sync backend, the confirm dialog also offers to queue them for upload.
+ </Text>
+ <Button onPress={handleChooseFile} disabled={busy} title="Choose File" icon={Download} />
+ </Card>
+ </View>
+ </ScrollView>
+
+ <LoadingOverlay visible={busy} title="Importing" message={progressLabel} />
+ </Container>
+ )
+}
+
+const styles = StyleSheet.create({
+ scrollContent: {
+ paddingHorizontal: 16,
+ paddingTop: 20,
+ paddingBottom: 40
+ },
+ statsContainer: {
+ borderRadius: 16,
+ borderWidth: 2,
+ marginBottom: 24,
+ overflow: "hidden"
+ },
+ statsGrid: {
+ flexDirection: "row",
+ padding: 20
+ },
+ statItem: {
+ flex: 1,
+ alignItems: "center"
+ },
+ statLabel: {
+ fontSize: 12,
+ ...fonts.semiBold,
+ textTransform: "uppercase",
+ letterSpacing: 0.8,
+ marginBottom: 8
+ },
+ statValue: {
+ fontSize: 20,
+ ...fonts.bold,
+ letterSpacing: -0.5,
+ textAlign: "center"
+ },
+ section: {
+ marginBottom: 24
+ },
+ intro: {
+ marginBottom: 12,
+ fontSize: 14,
+ lineHeight: 20
+ },
+ formatRow: {
+ flexDirection: "row",
+ alignItems: "center",
+ paddingVertical: 10,
+ gap: 16
+ },
+ formatTextContent: {
+ flex: 1
+ },
+ formatTitleRow: {
+ flexDirection: "row",
+ alignItems: "center",
+ gap: 8,
+ marginBottom: 4,
+ flexWrap: "wrap"
+ },
+ formatTitle: {
+ fontSize: 15,
+ ...fonts.semiBold,
+ letterSpacing: -0.2
+ },
+ extensionBadge: {
+ paddingHorizontal: 8,
+ paddingVertical: 3,
+ borderRadius: 6,
+ borderWidth: 1
+ },
+ extensionText: {
+ fontSize: 10,
+ ...fonts.bold,
+ letterSpacing: 0.3
+ },
+ formatDescription: {
+ fontSize: 12,
+ lineHeight: 16
+ }
+})
diff --git a/apps/mobile/src/screens/SettingsScreen.tsx b/apps/mobile/src/screens/SettingsScreen.tsx
index 030a9c50..8f6ec870 100644
--- a/apps/mobile/src/screens/SettingsScreen.tsx
+++ b/apps/mobile/src/screens/SettingsScreen.tsx
@@ -20,6 +20,7 @@ import {
Palette,
Database,
Download,
+ Upload,
Map,
ScrollText,
ShieldCheck,
@@ -176,11 +177,19 @@ export function SettingsScreen({ navigation }: Props) {
/>
<Divider />
<ListItem
- testID="nav-export-data"
+ testID="nav-import-locations"
icon={Download}
- label="Export Data"
+ label="Import Locations"
+ sub="Merge locations from a GeoJSON or Google Timeline file"
+ onPress={() => navigation.navigate("Import Locations")}
+ />
+ <Divider />
+ <ListItem
+ testID="nav-export-locations"
+ icon={Upload}
+ label="Export Locations"
sub="Export locations as CSV, GeoJSON, GPX or KML"
- onPress={() => navigation.navigate("Export Data")}
+ onPress={() => navigation.navigate("Export Locations")}
/>
<Divider />
<ListItem
@@ -191,14 +200,6 @@ export function SettingsScreen({ navigation }: Props) {
onPress={() => navigation.navigate("Auto-Export")}
/>
<Divider />
- <ListItem
- testID="nav-offline-maps"
- icon={Map}
- label="Offline Maps"
- sub="Download map tiles for use without internet"
- onPress={() => navigation.navigate("Offline Maps")}
- />
- <Divider />
<ListItem
testID="nav-backup-restore"
icon={ShieldCheck}
@@ -207,6 +208,14 @@ export function SettingsScreen({ navigation }: Props) {
onPress={() => navigation.navigate("Backup & Restore")}
/>
<Divider />
+ <ListItem
+ testID="nav-offline-maps"
+ icon={Map}
+ label="Offline Maps"
+ sub="Download map tiles for use without internet"
+ onPress={() => navigation.navigate("Offline Maps")}
+ />
+ <Divider />
<ListItem
testID="nav-activity-log"
icon={ScrollText}
diff --git a/apps/mobile/src/screens/__tests__/AutoExportScreen.test.tsx b/apps/mobile/src/screens/__tests__/AutoExportScreen.test.tsx
index 784bb9b6..7f74d20d 100644
--- a/apps/mobile/src/screens/__tests__/AutoExportScreen.test.tsx
+++ b/apps/mobile/src/screens/__tests__/AutoExportScreen.test.tsx
@@ -235,11 +235,12 @@ describe("AutoExportScreen", () => {
mockGetExportFiles.mockResolvedValue([])
})
- it("renders title and subtitle", async () => {
+ it("renders subtitle", async () => {
+ // The "Auto-Export" page title lives in the navigation header, not the screen body
+ // (removed to avoid duplicating the nav-bar title), so we only assert on the subtitle.
const { getByText } = render(<AutoExportScreen {...mockProps} />)
await waitFor(() => {
- expect(getByText("Auto-Export")).toBeTruthy()
expect(getByText("Automatically export your location data on a schedule")).toBeTruthy()
})
})
@@ -570,7 +571,7 @@ describe("AutoExportScreen", () => {
const { getByText } = render(<AutoExportScreen {...mockProps} />)
await waitFor(() => {
- expect(getByText("Auto-Export")).toBeTruthy()
+ expect(getByText("Automatically export your location data on a schedule")).toBeTruthy()
})
await waitFor(() => {
@@ -661,7 +662,7 @@ describe("AutoExportScreen", () => {
const { getByText } = render(<AutoExportScreen {...mockProps} />)
await waitFor(() => {
- expect(getByText("Auto-Export")).toBeTruthy()
+ expect(getByText("Automatically export your location data on a schedule")).toBeTruthy()
})
await act(async () => {
@@ -855,7 +856,7 @@ describe("AutoExportScreen", () => {
const { getByText } = render(<AutoExportScreen {...mockProps} />)
await waitFor(() => {
- expect(getByText("Auto-Export")).toBeTruthy()
+ expect(getByText("Automatically export your location data on a schedule")).toBeTruthy()
})
await act(async () => {
diff --git a/apps/mobile/src/screens/__tests__/ExportDataScreen.test.tsx b/apps/mobile/src/screens/__tests__/ExportLocationsScreen.test.tsx
similarity index 90%
rename from apps/mobile/src/screens/__tests__/ExportDataScreen.test.tsx
rename to apps/mobile/src/screens/__tests__/ExportLocationsScreen.test.tsx
index 6086fe76..dc4a913c 100644
--- a/apps/mobile/src/screens/__tests__/ExportDataScreen.test.tsx
+++ b/apps/mobile/src/screens/__tests__/ExportLocationsScreen.test.tsx
@@ -162,6 +162,15 @@ jest.mock("../../components", () => {
)
})
)
+ },
+ LoadingOverlay: function (props: any) {
+ if (!props.visible) return null
+ return R.createElement(
+ RN.View,
+ { testID: "loading-overlay" },
+ R.createElement(RN.Text, null, props.title),
+ props.message ? R.createElement(RN.Text, null, props.message) : null
+ )
}
}
})
@@ -186,9 +195,9 @@ jest.mock("lucide-react-native", () => {
}
})
-import { ExportDataScreen } from "../ExportDataScreen"
+import { ExportLocationsScreen } from "../ExportLocationsScreen"
-describe("ExportDataScreen", () => {
+describe("ExportLocationsScreen", () => {
beforeEach(() => {
jest.clearAllMocks()
mockGetStats.mockResolvedValue({ total: 100 })
@@ -202,14 +211,16 @@ describe("ExportDataScreen", () => {
const mockNavigation = { navigate: jest.fn() } as any
function renderScreen() {
- return render(<ExportDataScreen navigation={mockNavigation} />)
+ return render(<ExportLocationsScreen navigation={mockNavigation} />)
}
- it("shows Export Data title", async () => {
+ it("renders the format selector", async () => {
+ // The "Export Locations" page title lives in the navigation header, not the screen
+ // body, so this smoke test asserts on the SectionTitle that anchors the page content.
const { getByText } = renderScreen()
await waitFor(() => {
- expect(getByText("Export Data")).toBeTruthy()
+ expect(getByText("Select Format")).toBeTruthy()
})
})
diff --git a/apps/mobile/src/screens/index.ts b/apps/mobile/src/screens/index.ts
index 614d6180..eec195eb 100644
--- a/apps/mobile/src/screens/index.ts
+++ b/apps/mobile/src/screens/index.ts
@@ -10,7 +10,8 @@ export { AuthSettingsScreen } from "./AuthSettingsScreen"
export { MtlsSettingsScreen } from "./MtlsSettingsScreen"
export { DashboardScreen } from "./DashboardScreen"
export { DataManagementScreen } from "./DataManagementScreen"
-export { ExportDataScreen } from "./ExportDataScreen"
+export { ExportLocationsScreen } from "./ExportLocationsScreen"
+export { ImportLocationsScreen } from "./ImportLocationsScreen"
export { GeofenceScreen } from "./GeofenceScreen"
export { GeofenceEditorScreen } from "./GeofenceEditorScreen"
export { LocationHistoryScreen } from "./LocationInspectorScreen"
diff --git a/apps/mobile/src/services/ImportService.ts b/apps/mobile/src/services/ImportService.ts
new file mode 100644
index 00000000..1c4cd6e6
--- /dev/null
+++ b/apps/mobile/src/services/ImportService.ts
@@ -0,0 +1,56 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+import { NativeModules } from "react-native"
+
+const { ImportServiceModule } = NativeModules
+
+export type ImportFormat = "geojson" | "google_timeline_legacy" | "google_timeline_new" | "gpx" | "kml" | "csv"
+
+export type ImportSource = {
+ uri: string
+ displayName: string | null
+}
+
+export type ImportPreview = {
+ format: ImportFormat
+ totalParsed: number
+ invalid: number
+ duplicates: number
+ newRows: number
+ dateRangeStartSec: number | null
+ dateRangeEndSec: number | null
+ canQueueForSync: boolean
+}
+
+class ImportService {
+ private static ensureModule(): void {
+ if (!ImportServiceModule) {
+ throw new Error("[ImportService] ImportServiceModule not available. Check native linking.")
+ }
+ }
+
+ static async pickImportSource(): Promise<ImportSource | null> {
+ ImportService.ensureModule()
+ return ImportServiceModule.pickImportSource()
+ }
+
+ static async importLocationsFromFile(uri: string): Promise<ImportPreview> {
+ ImportService.ensureModule()
+ return ImportServiceModule.importLocationsFromFile(uri)
+ }
+
+ static async commitImport(asQueued: boolean = false): Promise<number> {
+ ImportService.ensureModule()
+ return ImportServiceModule.commitImport(asQueued)
+ }
+
+ static async cancelImport(): Promise<void> {
+ ImportService.ensureModule()
+ await ImportServiceModule.cancelImport()
+ }
+}
+
+export default ImportService
diff --git a/apps/mobile/src/types/navigation.ts b/apps/mobile/src/types/navigation.ts
index de51006e..4fa4820d 100644
--- a/apps/mobile/src/types/navigation.ts
+++ b/apps/mobile/src/types/navigation.ts
@@ -21,7 +21,8 @@ export type RootStackParamList = {
"Geofence Editor": { geofenceId?: number; name?: string; radius?: number; lat?: number; lon?: number } | undefined
"Location History": { initialTab?: InspectorTab; initialDate?: string } | undefined
"Location Summary": undefined
- "Export Data": undefined
+ "Export Locations": undefined
+ "Import Locations": undefined
"Auto-Export": undefined
"Data Management": undefined
"Tracking Profiles": undefined