Syncing an SMS app with the Android telephony provider
androidsmsmmssynccontent-providerperformance
A third-party SMS app usually keeps its own database (Room in the Message app) and mirrors the system providers content://sms and content://mms. The provider is the source of truth for messages; the app database adds local-only state (blocked, pinned, starred, quarantined, private, drafts). Most bugs live at the seam between the two.
Incremental sync
- A full scan per pull-to-refresh took 46.8 s on a mailbox of about 4,100 MMS, because it ran one
content://mms/{id}/addrquery per MMS and kept the SMS watermark only in memory (so every process start re-synced everything). - Fix: persisted watermarks (SharedPreferences), part/addr queries scoped with
mid IN (...)chunked at 500. Result: 12 to 20 ms. - The MMS watermark must be the provider
_ID, not the date. Delayed-download MMS keep their originalDATEheader and would fall behind a time cursor forever. - After applying a delta, recompute counts from the messages table. Writing
messageCount/unreadCountas absolute values computed from the delta reset a 1,000-message thread to 1, permanently. The old accidental full re-sync on every cold start had been silently healing this, so removing it turned a latent bug into data corruption. Review caught it; the test suite did not.
Never blind-upsert rows that carry local state
Room's @Upsert replaces every column, including ones with defaults. Blocked conversations reappeared after every sync because SMS sync upserted a fresh entity with isBlocked=false. Use a targeted updateSyncFields() UPDATE for existing rows and a full insert only for new ones. Same rule for group membership: heal recipient lists grow-only, because the provider's canonical-address tables can transiently return fewer rows.
Read state
markRead that only zeroed the denormalized counter was undone by the next recount. Mark the message rows read and recount in one transaction, and mirror read=1 to the system provider.
Status transitions
Use MAX(status, :new) in the UPDATE so a late sync cannot move SENT back to PENDING; claim scheduled work with an atomic UPDATE ... WHERE status = 0 rather than check-then-act.
Imports bypass receive-time rules
Anything decided only in the receive path (spam quarantine, kid-mode blocking) is skipped by a provider import. A fresh install imported every existing thread as trusted. Apply the same gate to threads new to the app database during sync. See Unknown-sender quarantine and OTP exemptions for SMS.
Phone number normalization
Normalize only things that look like phone numbers (normalizeSafe). Email gateways and alphanumeric sender IDs must be kept as typed. Navigating to a thread id computed from the raw address while the send wrote under the normalized one created empty duplicate conversations.
Gotchas
getOrCreateThreadIdthrowsAbstractMethodErrorunder Robolectric; import tests need a device.ContentObservercallbacks: do not rely on them arriving on the main looper for debouncing; use an atomic swap of a lazy job.- Any query that feeds a destructive action must be scoped to exactly what the user sees. A 30-day auto-delete of quarantined rows almost deleted threads that were also flagged kid-blocked or private, because the flags co-occur.
See also
- Room database pitfalls on Android
- Receiving MMS as the default SMS app
- Android notifications and PendingIntent pitfalls
Linked from
- Android notifications and PendingIntent pitfalls
- Message, Justin's Android SMS app (architecture and decisions)
- Receiving MMS as the default SMS app
- Room database pitfalls on Android
- Sending MMS from an Android SMS app
- SMS reactions (tapbacks) across iPhone, Google Messages and Android apps
- Unknown-sender quarantine and OTP exemptions for SMS