photos: ExifWorker moves out of SyncWorker for the file limit
- Commit
3b7c3669520b4d1d66e22e1265e7fa75592edce2- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-23 16:14 UTC (12 days ago)
- Parents
d5dc0a9
SyncWorker regrew to 856 with the triggered-import work; ExifWorker + setExifData move verbatim to ExifWorker.kt (SyncWorker 764). Verified: lintVitalRelease green, 24 sync tests pass.
Diff
photos/src/main/java/com/vayunmathur/photos/platform/ExifWorker.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/platform/ExifWorker.kt b/photos/src/main/java/com/vayunmathur/photos/platform/ExifWorker.ktnew file mode 100644index 0000000000..8ee822074b--- /dev/null+++ b/photos/src/main/java/com/vayunmathur/photos/platform/ExifWorker.kt@@ -0,0 +1,107 @@+package com.vayunmathur.photos.platform++import android.content.Context+import android.provider.MediaStore+import androidx.core.net.toUri+import androidx.exifinterface.media.ExifInterface+import androidx.work.CoroutineWorker+import androidx.work.ExistingWorkPolicy+import androidx.work.ForegroundInfo+import androidx.work.OneTimeWorkRequestBuilder+import androidx.work.WorkManager+import androidx.work.WorkerParameters+import androidx.work.ListenableWorker.Result as WorkResult+import com.vayunmathur.photos.data.ExifResult+import com.vayunmathur.photos.data.PhotoScanRepository+import com.vayunmathur.photos.domain.PanoXmpParser+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.async+import kotlinx.coroutines.awaitAll+import kotlinx.coroutines.coroutineScope+import kotlinx.coroutines.withContext++/**+ * Trailing EXIF pass for triggered / foreground imports.+ *+ * A triggered sync upserts its rows first (visible in the gallery flow) and+ * enqueues this worker instead of running [setExifData] inline, so geo /+ * panorama enrichment lands on the already-visible rows shortly after —+ * without a large unscanned backlog delaying the new photo's worker.+ * Generation-complete syncs do NOT use this worker for their own pass:+ * they run EXIF inline ahead of the watermark write (see [exifRunsInline]).+ */+class ExifWorker(context: Context, params: WorkerParameters) : CoroutineWorker(context, params) {++ override suspend fun doWork(): WorkResult = withContext(Dispatchers.IO) {+ setForeground(createForegroundInfo())+ val scanRepository = PhotoScanRepository.get(applicationContext)+ setExifData(scanRepository, applicationContext)+ WorkResult.success()+ }++ private fun createForegroundInfo(): ForegroundInfo = applicationContext.syncForegroundInfo(+ notificationId = 105,+ channelId = "exif_worker",+ channelName = "Photo Sync",+ title = "Reading Photo Details",+ text = "Extracting locations and photo details...",+ )++ /** Shared constants and helpers for ExifWorker. */+ companion object {+ const val WORK_NAME = "ExifWorker"++ fun enqueue(context: Context) {+ val request = OneTimeWorkRequestBuilder<ExifWorker>().build()+ WorkManager.getInstance(context).enqueueUniqueWork(+ WORK_NAME,+ ExistingWorkPolicy.KEEP,+ request,+ )+ }+ }+}++/**+ * Parse EXIF/XMP for every photo that hasn't been through it yet and write the+ * geo/panorama columns back.+ *+ * Driven off a projection of the un-parsed rows rather than the whole library:+ * this used to be handed `getAll()` (a `SELECT *` over every photo, embeddings+ * included) and wrote those same blobs straight back via `upsertAll`. The write+ * is column-targeted for the same reason — reconstructing a [Photo] from the+ * projection and upserting it would write NULL over every clipEmbedding.+ */+suspend fun setExifData(scanRepository: PhotoScanRepository, context: Context) = coroutineScope {+ val targets = scanRepository.getUnscannedForExif()+ targets.chunked(EXIF_CHUNK).forEach { chunk ->+ val results = chunk.map { target ->+ async(Dispatchers.IO) {+ try {+ val (latLong, panoData) = context.contentResolver.openInputStream(+ MediaStore.setRequireOriginal(+ target.uri.toUri()+ )+ )?.use { inputStream ->+ val exif = ExifInterface(inputStream)+ val ll = exif.latLong+ val pano = PanoXmpParser.parse(exif.getAttribute(ExifInterface.TAG_XMP))+ Pair(ll, pano)+ } ?: Pair(null, null)+ ExifResult(+ id = target.id,+ lat = latLong?.getOrNull(0),+ long = latLong?.getOrNull(1),+ pano = panoData,+ )+ } catch (_: Exception) {+ // Mark as set even on error to avoid retry every time.+ ExifResult(id = target.id, lat = null, long = null, pano = null)+ }+ }+ }.awaitAll()+ scanRepository.setExifResults(results)+ }+}++private const val EXIF_CHUNK = 50
photos/src/main/java/com/vayunmathur/photos/platform/SyncWorker.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/platform/SyncWorker.kt b/photos/src/main/java/com/vayunmathur/photos/platform/SyncWorker.ktindex c710fcee34..c26b9a7cbe 100644--- a/photos/src/main/java/com/vayunmathur/photos/platform/SyncWorker.kt+++ b/photos/src/main/java/com/vayunmathur/photos/platform/SyncWorker.kt@@ -1,5 +1,4 @@ package com.vayunmathur.photos.platform-import com.vayunmathur.photos.domain.PanoXmpParser import android.app.NotificationChannel import android.app.NotificationManager@@ -15,7 +14,6 @@ import com.vayunmathur.library.log.Log import androidx.core.app.NotificationCompat import androidx.core.database.getLongOrNull import androidx.core.net.toUri-import androidx.exifinterface.media.ExifInterface import androidx.work.Constraints import androidx.work.CoroutineWorker import androidx.work.ExistingWorkPolicy@@ -27,7 +25,6 @@ import androidx.work.ListenableWorker.Result as WorkResult import com.vayunmathur.library.ocr.OcrEngine import com.vayunmathur.library.util.DataStoreUtils import com.vayunmathur.photos.data.ClipResult-import com.vayunmathur.photos.data.ExifResult import com.vayunmathur.photos.data.FaceRepository import com.vayunmathur.photos.data.OcrResult import com.vayunmathur.photos.data.Person@@ -46,8 +43,6 @@ import android.graphics.ImageDecoder import kotlinx.coroutines.CancellationException import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.NonCancellable-import kotlinx.coroutines.async-import kotlinx.coroutines.awaitAll import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.currentCoroutineContext import kotlinx.coroutines.delay@@ -205,48 +200,6 @@ class OCRWorker(context: Context, params: WorkerParameters) : CoroutineWorker(co } } -/**- * Trailing EXIF pass for triggered / foreground imports.- *- * A triggered sync upserts its rows first (visible in the gallery flow) and- * enqueues this worker instead of running [setExifData] inline, so geo /- * panorama enrichment lands on the already-visible rows shortly after —- * without a large unscanned backlog delaying the new photo's worker.- * Generation-complete syncs do NOT use this worker for their own pass:- * they run EXIF inline ahead of the watermark write (see [exifRunsInline]).- */-class ExifWorker(context: Context, params: WorkerParameters) : CoroutineWorker(context, params) {-- override suspend fun doWork(): WorkResult = withContext(Dispatchers.IO) {- setForeground(createForegroundInfo())- val scanRepository = PhotoScanRepository.get(applicationContext)- setExifData(scanRepository, applicationContext)- WorkResult.success()- }-- private fun createForegroundInfo(): ForegroundInfo = applicationContext.syncForegroundInfo(- notificationId = 105,- channelId = "exif_worker",- channelName = "Photo Sync",- title = "Reading Photo Details",- text = "Extracting locations and photo details...",- )-- /** Shared constants and helpers for ExifWorker. */- companion object {- const val WORK_NAME = "ExifWorker"-- fun enqueue(context: Context) {- val request = OneTimeWorkRequestBuilder<ExifWorker>().build()- WorkManager.getInstance(context).enqueueUniqueWork(- WORK_NAME,- ExistingWorkPolicy.KEEP,- request,- )- }- }-}- /** * A persisted "has this one-off job run yet" bit. An interface so the * once-only behaviour of [syncLibrary] is testable without DataStore.@@ -693,50 +646,6 @@ internal fun Context.syncForegroundInfo( return ForegroundInfo(notificationId, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC) } -/**- * Parse EXIF/XMP for every photo that hasn't been through it yet and write the- * geo/panorama columns back.- *- * Driven off a projection of the un-parsed rows rather than the whole library:- * this used to be handed `getAll()` (a `SELECT *` over every photo, embeddings- * included) and wrote those same blobs straight back via `upsertAll`. The write- * is column-targeted for the same reason — reconstructing a [Photo] from the- * projection and upserting it would write NULL over every clipEmbedding.- */-suspend fun setExifData(scanRepository: PhotoScanRepository, context: Context) = coroutineScope {- val targets = scanRepository.getUnscannedForExif()- targets.chunked(EXIF_CHUNK).forEach { chunk ->- val results = chunk.map { target ->- async(Dispatchers.IO) {- try {- val (latLong, panoData) = context.contentResolver.openInputStream(- MediaStore.setRequireOriginal(- target.uri.toUri()- )- )?.use { inputStream ->- val exif = ExifInterface(inputStream)- val ll = exif.latLong- val pano = PanoXmpParser.parse(exif.getAttribute(ExifInterface.TAG_XMP))- Pair(ll, pano)- } ?: Pair(null, null)- ExifResult(- id = target.id,- lat = latLong?.getOrNull(0),- long = latLong?.getOrNull(1),- pano = panoData,- )- } catch (_: Exception) {- // Mark as set even on error to avoid retry every time.- ExifResult(id = target.id, lat = null, long = null, pano = null)- }- }- }.awaitAll()- scanRepository.setExifResults(results)- }-}--private const val EXIF_CHUNK = 50- /** * Sustained on-device AI indexing (OCR, CLIP, face models) heats the phone. * On top of the short per-item pauses, each indexing loop takes a longer