# AGENTS.md — email/ (':email') > Scoped supplement. Global rules in root `AGENTS.md` apply. Stay inside `email/` + allowed shared modules. Do not root-scan. _Install: ./install email (dev by default)._ - Gradle: :email / dir: email/ - Package roots present: ui, data, platform, network, intents, widget - Entry files: MainActivity.kt (sole launcher; routes watches to WearMainActivity), Route.kt, Navigation.kt, ui/WearMainActivity.kt - Deps: :library:room, :library:widgets, :library:network, :library:wearlink, :library:log, Wear Compose (material + foundation) - Metadata: metadata_data/email.md (present) - Rust: no / screenshotTest: yes ## Overview **Purpose:** A modern open-source email client with a unified inbox across multiple accounts. Requires internet. **Key features:** - Unified inbox over any IMAP/SMTP provider, with presets + OAuth for Gmail/Outlook/Yahoo/iCloud/Fastmail - Push mail over IMAP IDLE plus hourly poll for the rest; outbox retry, drafts, and snooze via WorkManager - Composer with inline images (HTML editor + CID spans) and an EML file viewer - Home-screen widget, rich notifications with actions, and assistant read/search intents **Architecture / data-flow:** `ui/` Compose screens → `platform/EmailViewModel` (+ `EmailManager`, send/draft action helpers) → `data/` Room stores (`*Dao`/`*Store` pairs + `EmailRepository`) fed by workers (`EmailSyncWorker`, `ImapIdleService`, `OutboxSendWorker`, `SnoozeWorker`) → `network/` raw IMAP/SMTP clients. `MainActivity.onCreateBeforeContent`: `NetworkClient.init(SYSTEM)` first, then widget previews, intent handling, hourly-sync schedule, outbox flush, IDLE start, and two one-time backfills. **Entry points + reading order:** `Route` (`MessageList` / `MessageThread` / `Composer` / `Outbox` / `Drafts` / `AddAccount` / `AddAccountForm` / `Settings` / `EmlViewer` / `UnsubscribeDialog` / `MessageDetailsDialog`) → `MainActivity.kt` → `Navigation.kt` → `ui/MainContent.kt` → `platform/EmailViewModel.kt` → `data/EmailRepository.kt`. **Gotchas:** - `NetworkClient.init(this, TrustBundle.SYSTEM)` must stay first in `onCreateBeforeContent` — mail servers need system CAs, not the first-party bundle - `DateMillisBackfill`/`PeekContentBackfill` are one-time migrations gated on `runIfNeeded`; skipping them breaks sort order and snippet previews - IDLE push is foreground-gated via `AppLifecycleTracker` — background delivery goes through the hourly worker + notifications, not the socket - Startup never crashes the first screen: constructing `EmailRepository` and `EmailViewModel` never builds the database (stores and `draftsActions` are lazy, DAO flows are acquired via a guard that falls back to an empty flow), `MainActivity.onCreateBeforeContent` steps run in isolation via `platform/StartupGuard` (catches `Throwable`, rethrows only cancellation), the backfills and IDLE start guard their own database access, and `EmailViewModel.accounts` degrades to an empty list (the Add Account gate) when the database will not build or open on the device - `network/imap/TrustAll.kt` exists for debug only; never widen its use - Upgrades from app versions before v2.6.0 start with a fresh local database (supported upgrade floor: Email DB v17; DB versions 1–16 open destructively, v17 onward migrates normally) - Platform backup never copies the raw SQLCipher DB or any credential: account passwords are sealed by the device-bound Keystore (`CredentialCrypto`) and cannot transfer, so `AppBackupAgent` backs up only the sanitized `EmailBackup` payload; restored accounts have empty credential fields, are skipped by sync/IDLE, and prompt for re-auth in Settings ## Key files ### Root — entry + navigation - com/vayunmathur/email/MainActivity.kt - com/vayunmathur/email/Navigation.kt - com/vayunmathur/email/Route.kt ### data/ — Room stores + sync workers - com/vayunmathur/email/data/EmailModels.kt - com/vayunmathur/email/data/EmailDatabase.kt - com/vayunmathur/email/data/EmailAccountDao.kt - com/vayunmathur/email/data/EmailAccountStore.kt - com/vayunmathur/email/data/EmailMessageDao.kt - com/vayunmathur/email/data/EmailMessageStore.kt - com/vayunmathur/email/data/EmailOutboxDao.kt - com/vayunmathur/email/data/EmailOutboxStore.kt - com/vayunmathur/email/data/EmailQueryDao.kt - com/vayunmathur/email/data/EmailQueryStore.kt - com/vayunmathur/email/data/EmailRepository.kt - com/vayunmathur/email/data/EmailSettings.kt - com/vayunmathur/email/data/EmailSyncState.kt - com/vayunmathur/email/data/EmailSyncWorker.kt - com/vayunmathur/email/data/ImapIdleService.kt - com/vayunmathur/email/data/ImapIdleRetryWorker.kt - com/vayunmathur/email/data/OutboxSendWorker.kt - com/vayunmathur/email/data/SnoozeWorker.kt - com/vayunmathur/email/data/OutlookOAuth.kt - com/vayunmathur/email/data/ProviderPresets.kt - com/vayunmathur/email/data/CredentialCrypto.kt - com/vayunmathur/email/data/EmailBackup.kt — sanitized platform-backup payload (accounts/folders/blocked senders, no secrets) + `needsReauth()` - com/vayunmathur/email/data/DateMillisBackfill.kt - com/vayunmathur/email/data/PeekContentBackfill.kt ### network/ — raw IMAP + SMTP clients - com/vayunmathur/email/network/imap/ImapClient.kt - com/vayunmathur/email/network/imap/RawImapConnection.kt - com/vayunmathur/email/network/imap/ImapWireIo.kt - com/vayunmathur/email/network/imap/ImapParser.kt - com/vayunmathur/email/network/imap/ImapFetchPipeline.kt - com/vayunmathur/email/network/imap/MimeParser.kt - com/vayunmathur/email/network/imap/MimeDecoding.kt — quoted-printable/charset/RFC2047/date/filename/multipart decoding extracted from MimeParser (MimeParser delegates) - com/vayunmathur/email/network/imap/MimeParserParts.kt - com/vayunmathur/email/network/imap/ImapAuthException.kt - com/vayunmathur/email/network/imap/TrustAll.kt — debug only - com/vayunmathur/email/network/smtp/SmtpClient.kt - com/vayunmathur/email/network/smtp/RawSmtpConnection.kt - com/vayunmathur/email/network/smtp/MimeBuilder.kt ### platform/ — ViewModel, actions, notifications, lifecycle - com/vayunmathur/email/platform/EmailViewModel.kt - com/vayunmathur/email/platform/StartupGuard.kt — isolated best-effort startup steps (#939) - com/vayunmathur/email/platform/AppBackupAgent.kt — key/value backup agent: sanitized account payload + `email_settings` prefs; restored accounts prompt for the app password in Settings - com/vayunmathur/email/platform/EmailManager.kt - com/vayunmathur/email/platform/EmailUiContract.kt - com/vayunmathur/email/platform/EmailClientFlag.kt - com/vayunmathur/email/platform/EmailSendActions.kt - com/vayunmathur/email/platform/EmailDraftActions.kt - com/vayunmathur/email/platform/AiSummaryHelper.kt - com/vayunmathur/email/platform/EmailNotifications.kt - com/vayunmathur/email/platform/EmailNotificationActionReceiver.kt - com/vayunmathur/email/platform/AppLifecycleTracker.kt - com/vayunmathur/email/platform/BootReceiver.kt - com/vayunmathur/email/platform/EmlUtils.kt - com/vayunmathur/email/platform/IntentState.kt ### ui/ — list, thread, composer, account flows - com/vayunmathur/email/ui/MainContent.kt - com/vayunmathur/email/ui/MessageListScreen.kt - com/vayunmathur/email/ui/MessageThreadScreen.kt — thread pane: strip + one open message, no outer scroller - com/vayunmathur/email/ui/ThreadMessageStrip.kt - com/vayunmathur/email/ui/MessageItem.kt — header / self-scrolling body / footer - com/vayunmathur/email/ui/MessageFooter.kt - com/vayunmathur/email/ui/FolderList.kt - com/vayunmathur/email/ui/DetailItem.kt - com/vayunmathur/email/ui/ComposerScreen.kt - com/vayunmathur/email/ui/ComposerHeader.kt - com/vayunmathur/email/ui/ComposerAttachments.kt - com/vayunmathur/email/ui/ComposerHelpers.kt - com/vayunmathur/email/ui/AttachmentItem.kt - com/vayunmathur/email/ui/composer/EmailHtmlEditor.kt - com/vayunmathur/email/ui/composer/EmailHtmlEditorController.kt - com/vayunmathur/email/ui/composer/EmailHtmlSerializer.kt - com/vayunmathur/email/ui/composer/EmailHtmlTagHandler.kt - com/vayunmathur/email/ui/composer/EmailHtmlInline.kt - com/vayunmathur/email/ui/composer/InlineImage.kt - com/vayunmathur/email/ui/composer/CidImageSpan.kt - com/vayunmathur/email/ui/DraftsContent.kt, OutboxContent.kt, SettingsContent.kt, EmlViewerContent.kt — stateless slices for previews - com/vayunmathur/email/ui/EmailDialogEntries.kt — `DialogPage()` entries for `UnsubscribeDialog` + `MessageDetailsDialog` - com/vayunmathur/email/ui/dialogs/UnsubscribeDialogContent.kt — stateless unsubscribe confirmation - com/vayunmathur/email/ui/dialogs/MessageDetailsDialogContent.kt — stateless message-details sheet - com/vayunmathur/email/ui/NavigationEntries.kt - com/vayunmathur/email/ui/OutboxScreen.kt - com/vayunmathur/email/ui/EmlViewerScreen.kt - com/vayunmathur/email/ui/AddAccountScreen.kt - com/vayunmathur/email/ui/AddAccountSection.kt - com/vayunmathur/email/ui/AddAccountInstructionsCard.kt - com/vayunmathur/email/ui/OAuthActivity.kt - com/vayunmathur/email/ui/SettingsScreen.kt - com/vayunmathur/email/ui/EmailUiHelpers.kt ### intents/ — assistant IPC - com/vayunmathur/email/intents/GetRecentIntent.kt - com/vayunmathur/email/intents/SearchIntent.kt - com/vayunmathur/email/intents/EmailDataMapper.kt ### Watch experience (ui/ + data/ + sync/ + platform/) - com/vayunmathur/email/ui/WearMainActivity.kt — watch entry (WearShellActivity, exported=false) - com/vayunmathur/email/ui/WearRoute.kt, WearInboxListScreen.kt, WearMessageScreen.kt - com/vayunmathur/email/data/WearEmailStore.kt — watch snapshot + pending mark-read ops - com/vayunmathur/email/sync/WearEmailSync.kt — phone conduit publish + op apply - com/vayunmathur/email/sync/WearLinkBridge.kt, WearWatchStatePublisher.kt, ConduitSyncTriggerReceiver.kt - com/vayunmathur/email/platform/FormFactor.kt — watch detection (FEATURE_WATCH) ### widget/ — unified-inbox home widget - com/vayunmathur/email/widget/EmailWidget.kt — includes `EmailWidgetReceiver` (one widget, one file) ## One APK, two form factors (phone + watch) Single merged APK (same applicationId/signing on phone and watch; no watch uses-feature, touchscreen not-required, standalone=false). `platform/FormFactor` reads `PackageManager.FEATURE_WATCH`: `MainActivity` forwards watches to `ui/WearMainActivity` before any phone state loads; the watch activity has no launcher entry. ## Wear sync (Watch conduit client) Protocol in `:library:wearlink` at `com.vayunmathur.library.wearlink.email.WearEmailProtocol` (golden/cap tests in `src/test/.../sync/WearEmailProtocolTest`). Phone publishes a bounded recent unified-inbox snapshot (`/email/snapshot`: 30 messages, sender/subject capped, plain-text body ≤4,000 chars, ≤90,000 JSON bytes) via `sync/WearEmailSync`; watch persists it in `data/WearEmailStore` and renders offline. Watch mark read/unread ops (`/email/op`) apply on the phone (Room + IMAP \\Seen, mirroring `EmailViewModel.markAsRead`) and stay pending until a snapshot reflects them. No archive op: the phone has no IMAP archive/move primitive (only seen, expunge-delete, snooze), so the watch offers no fake archive. Triggers: conduit Connected, watch request (`/email/request-sync`), and `sync/ConduitSyncTriggerReceiver` (CONDUIT signature permission). Transport is `WearLinkClientNode` (appId `email`); only the Watch conduit app holds Bluetooth permission. ## Watch experience `ui/WearMainActivity` (WearShellActivity) routes `ui/WearRoute` (Inbox / Message(id)): inbox list (sender/subject/time, unread bold) and message detail (readable plain-text body, Mark read/unread filled button, Open on phone last via `/email/open-on-phone`). Watch screenshots are `group="wear"` previews in `src/screenshotTest/.../ui/WearMetadataPreviews.kt` via `:email:metadata`. ## Verify (this module only) ``` ./gradlew :email:compileDevKotlin ./gradlew :email:lint :email:lintVitalRelease :email:testDevUnitTest ./gradlew :email:checkMetadata ./gradlew :email:metadata ```