Introducing a new way to review your PRs.

Read the announcement

capcom6 / android-sms-gateway

The SMS Gateway for Android™ app enables sending and receiving SMS messages through an API that can be accessed directly on the device or via a cloud server when direct device access is not possible.

View on GitHub

20 documents · Updated Nov 20, 2026

docs / application-lifecycle

Application lifecycle and module composition

The Android application turns a phone into an SMS gateway. It starts the user interface and a coordinated background runtime through Android entry points. That runtime connects phone message intake with cloud or local network work while shared settings, durable data, and in-process events join the feature areas.

Application startup

The Android process starts with App, before the launcher activity or a service runs. App establishes the shared application context and dependency graph, prepares process-wide support such as locale and crash handling, and asks the runtime coordinator to start with automatic startup allowed. (app/…/App.kt• L32-77)

Automatic startup controls background work rather than access to the user interface. When the setting is off, the coordinator leaves the gateway runtime stopped even though Android can still open MainActivity through the launcher entry point. See app configuration for the setting contract. (app/…/OrchestratorService.kt• L29-38, app/…/MainActivity.kt• L15-63)

User interface entry point

MainActivity is the entry point for user-driven work. It presents the main product areas and can open a selected area from an intent. It does not own the gateway runtime, so background recovery can start without opening the interface. (app/…/AndroidManifest.xml• L113-121, app/…/MainActivity.kt• L23-91)

References: app/…/AndroidManifest.xml• L113-121 app/…/MainActivity.kt• L15-91

Startup coordinator

OrchestratorService is the lifecycle boundary for background capabilities. It starts enabled feature services, starts the local server and health work, registers phone message intake, and reports that the application has started. Its stop path owns the matching release of those capabilities. (app/…/OrchestratorService.kt• L18-79)

The coordinator owns order but not feature choice. Each feature checks its own settings during startup. This keeps one system-wide start and stop path while allowing configuration to decide which capabilities run. (app/…/OrchestratorService.kt• L29-67, app/…/GatewayService.kt• L44-65, app/…/LocalServerService.kt• L31-47)

References: app/…/OrchestratorService.kt• L18-79 app/…/GatewayService.kt• L44-65 app/…/LocalServerService.kt• L31-49

Manifest-declared components

The manifest defines the Android-facing entry points and platform access needed by the gateway. It connects system events to the app and declares the activity, receivers, services, and controlled file-sharing provider that Android may create. Some phone-message receivers are declared in a disabled state and become active only when the coordinated runtime starts. (app/…/AndroidManifest.xml• L5-131)

Component groupAndroid roleProduct purpose
MainActivityLauncher activityOpens the user interface.
MessagesReceiverSMS broadcast receiverAccepts SMS and data-SMS input while the receiver feature is active.
MmsReceiverMMS broadcast receiverAccepts incoming MMS notification data while the receiver feature is active.
BootReceiverBoot and shutdown receiverRequests background recovery after device state changes.
EventsReceiverSMS result receiverAccepts sent and delivered message results.
PushServiceFirebase message serviceAccepts cloud notifications.
SSEForegroundServiceForeground data serviceKeeps the selected cloud event stream active.
PingForegroundServiceForeground data serviceSends periodic health information.
WebServiceForeground connected-device serviceKeeps the local HTTP interface available.
SystemForegroundServiceWorkManager support serviceGives scheduled foreground work an Android service host.
FileProviderPrivate content providerShares files through controlled content URIs.

The manifest also requires telephony hardware and declares access for SMS and MMS, network use, boot recovery, wake locks, and foreground work. These declarations are platform prerequisites for the gateway. The file provider is not an external app interface; it limits file sharing to granted content URIs. (app/…/AndroidManifest.xml• L5-22, app/…/AndroidManifest.xml• L104-131)

Runtime broadcast registration

Some message paths exist only while the coordinated runtime is active. App registers the process-wide sent and delivered result receiver, while ReceiverService registers SMS and MMS intake and starts related observation when its feature is enabled. The manifest keeps these phone receivers disabled until ReceiverService enables them. Stopping the runtime removes the active intake paths and disables the manifest receivers. (app/…/App.kt• L74-76, app/…/ReceiverService.kt• L29-57, app/…/AndroidManifest.xml• L44-66)

References: app/…/AndroidManifest.xml• L33-86 app/…/AndroidManifest.xml• L88-131 app/…/App.kt• L74-76 app/…/ReceiverService.kt• L29-57

Dependency and module composition

The project builds one Android application. Within it, feature areas act as runtime modules. Each module contributes the services and shared resources needed for one product area to a Koin dependency graph. (settings.gradle• L1-16, app/build.gradle• L1-10, app/…/App.kt• L40-62)

App is the composition root, which means it assembles the objects used by the running application. It loads settings, durable data access, the event bus, device services, support services, feature services, and the orchestrator. Feature areas receive these shared objects through the graph instead of building separate application runtimes. See persistent storage for data lifetime and app configuration for settings behavior. (app/…/App.kt• L40-62, app/…/Module.kt• L7-10)

Shared concernSystem relationship
SettingsSelect which capabilities start and how they behave.
Durable data accessPreserve message, webhook, log, and account state across process restarts.
Device servicesProvide device-scoped capabilities and key storage to feature areas.
Event busCarry typed work signals between feature areas in one process.
OrchestratorDefine the common start and stop boundary for long-running features.

The composition also keeps two data lifetimes distinct. Database objects support durable records, while feature settings use preference-backed storage. Device settings join the same settings boundary, while device key storage remains a separate device service. This split lets runtime choices change without treating them as message or delivery records. (app/…/Module.kt• L5-12, app/…/Module.kt• L21-86, app/…/Module.kt• L7-10)

Feature service boundaries

Each feature service owns one product capability and offers lifecycle operations to the coordinator. The gateway manages cloud connectivity, the local server exposes the on-device HTTP path, the receiver accepts phone messages, webhooks deliver queued callbacks, and messages manage outgoing SMS work. (app/…/GatewayService.kt• L28-65, app/…/LocalServerService.kt• L14-49, app/…/ReceiverService.kt• L29-57, app/…/WebHooksService.kt• L26-46, app/…/MessagesService.kt• L47-92)

This boundary limits the effect of a partial startup failure. A failed optional service or phone receiver registration is isolated and logged. Other coordinator steps can continue, which separates runtime setup problems from failures in cloud access, the local server, or message delivery. (app/…/OrchestratorService.kt• L40-59)

References: app/…/App.kt• L40-62 app/…/Module.kt• L5-12 app/…/Module.kt• L7-10 app/…/Module.kt• L21-86 app/…/OrchestratorService.kt• L40-59

Background-service startup

Foreground services keep long-running network, local server, and health work active under Android's background rules. The owning feature requests the service through the common lifecycle, and the service presents a user-visible notification while it holds resources for its work. (app/…/SSEForegroundService.kt• L83-105, app/…/PingForegroundService.kt• L68-85, app/…/WebService.kt• L220-242)

The manifest declares the service type for each foreground role. This tells Android whether the service performs data sync or connected-device work. The services release their network and power resources when Android stops them. (app/…/AndroidManifest.xml• L33-42, app/…/AndroidManifest.xml• L104-111, app/…/SSEForegroundService.kt• L128-160)

The cloud gateway supports two transport paths. Firebase supplies push messages, while the server-event stream keeps a foreground connection when that mode is selected. Both paths hand the same kind of external event to the event boundary, so downstream feature work does not depend on the transport. See event routing for the routing model. (app/…/GatewayService.kt• L44-54, app/…/PushService.kt• L19-41, app/…/SSEForegroundService.kt• L43-125)

References: app/…/AndroidManifest.xml• L33-42 app/…/AndroidManifest.xml• L68-75 app/…/AndroidManifest.xml• L104-111 app/…/GatewayService.kt• L44-65 app/…/LocalServerService.kt• L31-47 app/…/PingService.kt• L6-16

Boot recovery

BootReceiver is the Android entry point for device restart and related power events. It accepts the supported boot and shutdown action forms, ignores unrelated broadcasts, and sends an accepted event to the same coordinator used by normal application startup. (app/…/AndroidManifest.xml• L87-102, app/…/BootReceiver.kt• L10-26)

The automatic-start preference controls both sides of recovery. The coordinator checks it before starting background work, and the settings helper enables or disables the boot receiver with the same choice. A disabled preference therefore prevents both normal automatic startup and future boot-triggered startup. (app/…/SettingsHelper.kt• L11-29, app/…/OrchestratorService.kt• L29-38)

Boot recovery rebuilds the normal runtime instead of duplicating service-start logic in the receiver. This keeps the start order and feature gates consistent across process launch and device recovery.

The recovery path moves from a device event to a guarded coordinator request and then to the enabled runtime capabilities.

flowchart TD
    A[Device boot or power event] --> B[BootReceiver]
    B --> C{Automatic start enabled?}
    C -- No --> D[Leave background runtime stopped]
    C -- Yes --> E[OrchestratorService start]
    E --> F[Start enabled feature services]
    F --> G[Register SMS and MMS intake]
    G --> H[Emit application-started event]

References: app/…/AndroidManifest.xml• L16-17 app/…/AndroidManifest.xml• L87-102 app/…/BootReceiver.kt• L10-26 app/…/SettingsHelper.kt• L18-29 app/…/OrchestratorService.kt• L29-67

Event orchestration boundary

The event boundary converts input from outside the process into typed work inside the process. Cloud stream data and Firebase data become an ExternalEvent, which carries an event type and optional data. EventsRouter maps that external contract to the internal event bus. See event routing for event producers and consumers. (app/…/ExternalEvent.kt• L3-6, app/…/EventsRouter.kt• L16-55)

The router exposes a narrow set of actions for message enqueue, webhook changes, message export, settings changes, and message cancellation. The event bus then lets feature areas react without a direct link from cloud transport to message, webhook, or settings work. (app/…/ExternalEventType.kt• L5-20, app/…/EventBus.kt• L8-23)

When an external event arrives, the system follows this sequence:

  1. The cloud stream or Firebase supplies an event name and data.
  2. The transport maps the name to an external event type.
  3. EventsRouter checks required data and emits the matching internal event.
  4. Feature receivers collect the internal event and start the related work.

The diagram shows how different cloud transports share one boundary before feature work begins.

flowchart LR
    A[SSE stream] --> C[ExternalEvent]
    B[Firebase push] --> C
    C --> D[EventsRouter]
    D --> E[EventBus]
    E --> F[Feature event receivers]
    F --> G[Message, webhook, settings, or export work]

Unknown event names fail at the transport boundary and are logged by the source service. This prevents an unknown remote action from becoming an unrecognized internal event. (app/…/SSEForegroundService.kt• L64-79, app/…/PushService.kt• L27-51)

References: app/…/SSEForegroundService.kt• L64-125 app/…/PushService.kt• L19-51 app/…/EventsRouter.kt• L16-55 app/…/EventBus.kt• L8-23

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the App dependency list aligned with every service that the coordinator or a manifest component resolves.
  • Keep manifest component names, enabled states, foreground service types, and platform permissions aligned with each service start path.
  • Keep dynamic SMS and MMS receiver registration aligned with the manifest declarations and the runtime stop path.
  • Keep the automatic-start preference aligned with both boot receiver enablement and the coordinator gate.
  • Keep external event names aligned across the cloud contract, ExternalEventType, and each transport parser.
  • Keep event payload requirements aligned between EventsRouter and internal event types that require data.
  • Keep start and stop changes paired so each new receiver, worker, or foreground service is also released.

Important failure modes

TriggerConsequenceGuard
Automatic startup is disabled when the app or boot receiver requests a startThe background runtime stays stopped without a startup errorThe coordinator checks the setting, and the settings helper applies it to boot receiver enablement.
Android rejects a foreground-service launch while the app is in the backgroundLocal server or health work may not start while other startup work continuesThe coordinator isolates this startup group and logs a warning.
SMS or MMS receiver registration failsNew phone messages do not enter the gatewayReceiver startup is isolated and logged; check Android permissions and the receiver warning.
A manifest-declared SMS or MMS receiver stays disabled after runtime startAndroid does not deliver matching phone broadcasts through that componentReceiverService enables the components as it registers them and disables them on stop.
An SSE or Firebase event has an unknown nameThe event does not reach internal feature workEach transport rejects and logs the event before routing.
The event stream starts without a stored registration tokenThe authenticated stream connection cannot be builtGateway registration and configuration must provide the token before stream startup.
The device lacks telephony support or required SMS and MMS accessCore phone gateway functions cannot runThe manifest requires telephony hardware and declares the required platform access.

Key files

FileWhy to read it
app/…/App.ktShows process startup and dependency composition.
app/…/OrchestratorService.ktDefines the shared start and stop boundary.
app/…/AndroidManifest.xmlDefines Android entry points, platform access, receiver activation, and service roles.
app/…/BootReceiver.ktShows boot recovery and its link to automatic startup.
app/…/EventsRouter.ktDefines the boundary from external events to internal work.
app/…/EventBus.ktShows the in-process event contract between feature areas.
app/…/SSEForegroundService.ktIllustrates the cloud stream path and foreground lifecycle.
app/…/PushService.ktIllustrates the Firebase event path.
app/…/ReceiverService.ktShows how SMS and MMS intake joins the coordinated runtime.
app/…/Module.ktShows how feature and device settings enter the dependency graph.
app/…/Module.ktShows how device services and key storage enter the dependency graph.
app/…/Module.ktShows how durable data access enters the dependency graph.