package io.appwrite.flutter.services import android.Manifest import android.app.ActivityManager import android.app.AlarmManager import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent import android.app.job.JobInfo import android.app.job.JobScheduler import android.content.ComponentName import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.graphics.Bitmap import android.graphics.BitmapFactory import android.net.ConnectivityManager import android.net.Network import android.net.Uri import android.os.Build import android.os.PowerManager import android.os.SystemClock import android.provider.Settings import android.util.AtomicFile import android.util.Log import android.webkit.CookieManager import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import androidx.core.content.ContextCompat import com.hivemq.client.mqtt.MqttGlobalPublishFilter import com.hivemq.client.mqtt.datatypes.MqttQos import com.hivemq.client.mqtt.mqtt5.Mqtt5AsyncClient import com.hivemq.client.mqtt.mqtt5.exceptions.Mqtt5ConnAckException import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5Publish import io.appwrite.flutter.exceptions.AppwriteException import org.json.JSONArray import org.json.JSONObject import java.io.ByteArrayOutputStream import java.io.File import java.io.IOException import java.io.InputStream import java.net.HttpURLConnection import java.net.URL import java.util.UUID import java.util.concurrent.CopyOnWriteArrayList import java.util.concurrent.ExecutionException import java.util.concurrent.Executors import java.util.concurrent.TimeUnit import java.util.concurrent.TimeoutException import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicInteger private const val LOG_TAG = "AppwritePush" /** * What a message asks a background notification to show. A [raw] payload, one that is not a JSON * object, is shown as the body under the subscription's title; an envelope is [visible] only with a * title or body in its `notification`. */ internal data class PushNotificationContent(val raw: Boolean, val title: String?, val body: String?, val image: String?) { val visible: Boolean get() = raw || title != null || body != null } /** A background subscription saved across restarts: its filter, QoS choice and notification title. */ internal data class PushEntry( val filter: String, val retry: Boolean, val title: String?, val notifyInForeground: Boolean = false, ) /** * The adaptive heartbeat: its interval, the current streak of failed heartbeats, and when the * interval last shrank (wall-clock ms, as it must survive reboots). */ internal data class PushHeartbeat( val intervalMs: Long, val failures: Int = 0, val shrunkAt: Long = 0L, ) /** * The process-wide host of the background push connection, modelled on how socket push * services survive on Android without keeping a service alive. * * The background subscriptions and their connection config are saved to private storage (see * [PushStore]), so delivery survives the process being killed, a reboot and an app update. A * job ([PushJobService]) that reschedules itself on every run, backed by an alarm * ([PushAlarmReceiver]), wakes the process every 15 seconds (60 when the app may use exact * alarms or is exempt from battery optimisation). Each run holds a short wakelock and * reconnects when the connection is gone or the default network changed, sends a heartbeat * when one is due, and re-arms the job. While the process is alive between runs the socket * stays open and messages arrive immediately; while it is dead the broker keeps the backlog * for this client id and replays it when the next run subscribes again. * * With foreground mode enabled ([Push.setForeground]) a foreground service ([PushService]) also keeps * the process at foreground priority, so delivery is immediate after a kill and during Doze. * * Messages go to the live in-app callbacks that match them. When none match (the process was * started in the background), a declared [PushReceiver] gets the message, and a notification * is posted unless it returns true. */ internal object PushBackground { const val CHANNEL_ID = "appwrite-push" const val NOTIFICATION_ID = 1911 const val ACTION_TICK = "io.appwrite.flutter.push.TICK" const val ACTION_MESSAGE = "io.appwrite.flutter.push.MESSAGE" const val NOTIFICATION_ICON = "io.appwrite.flutter.push.notification_icon" const val EXTRA_TOPIC = "io.appwrite.flutter.push.TOPIC" const val EXTRA_PAYLOAD = "io.appwrite.flutter.push.PAYLOAD" private const val SERVICE_CHANNEL_ID = "appwrite-push-service" const val JOB_ID = 0x50555348 const val NEXT_JOB_ID = 0x5055534A const val EXPEDITED_JOB_ID = 0x5055534B const val WATCHDOG_JOB_ID = 0x50555349 private const val WATCHDOG_INTERVAL_MS = 15 * 60 * 1_000L private const val INTERVAL_MS = 15_000L private const val PRIVILEGED_INTERVAL_MS = 60_000L private const val RESTART_DELAY_MS = 3_000L // An upper bound covering a run's connect (20 s) and its requests (10 s each); the wakelock is // released as soon as the run finishes. private const val WAKE_LOCK_MS = 60_000L private const val KEEP_ALIVE_SECONDS = 300 // The heartbeat interval: below the broker's 1.5 x keep-alive expiry, measured on a clock that // counts deep sleep. Networks that drop idle connections sooner (carrier NATs) make heartbeats // fail; after HEARTBEAT_FAILURES in a row the interval shrinks by a quarter, down to // MIN_HEARTBEAT_MS, and returns to the default HEARTBEAT_RESET_MS after the last shrink, in // case the device moved to a better network. const val HEARTBEAT_MS = 180_000L private const val MIN_HEARTBEAT_MS = 60_000L private const val HEARTBEAT_FAILURES = 3 private const val HEARTBEAT_RESET_MS = 3 * 24 * 60 * 60 * 1_000L private const val REQUEST_TIMEOUT_SECONDS = 10L private const val IMAGE_TIMEOUT_MS = 5_000 // How long a scheduled run stays up after connecting so the broker can replay what was missed: // until nothing arrived for DRAIN_QUIET_MS and nothing is being delivered, at most the run's cap. const val JOB_DRAIN_MS = 15_000L const val RECEIVER_DRAIN_MS = 5_000L private const val DRAIN_QUIET_MS = 2_000L private const val DRAIN_POLL_MS = 200L private const val IMAGE_MAX_BYTES = 5 * 1024 * 1024 private const val IMAGE_MAX_PX = 1_024 private const val CONNECT_TIMEOUT_SECONDS = 20L // How long a message waits for a listener that acknowledges it itself (the React Native and // Flutter bridges, once their callback has run) before it is acknowledged anyway, so one stuck // callback cannot hold back the acknowledgements after it. private const val ACK_TIMEOUT_SECONDS = 10L // Unsubscribing a filter nothing subscribes to is a harmless round trip the broker always // acknowledges, so it proves the connection is alive. The broker ignores client PUBLISHes // without a PUBACK, so a publish cannot serve as the heartbeat. private const val HEARTBEAT_FILTER = "\$heartbeat" @Volatile var channelName: String = "Notifications" @Volatile var ongoingTitle: String = "Appwrite" // Reports errors to the Push instance that hosts its subscriptions here. Returns whether the // app's onError received the error (false when it registered none). @Volatile var errorHandler: ((Throwable) -> Boolean)? = null // Tells the Push instance that hosts its subscriptions here when the connection opens (true) // or closes (false). @Volatile var connectionHandler: ((Boolean) -> Unit)? = null // Whether the live connection (not a retired one) is open. @Volatile var isConnected = false private set @Volatile var serviceRunning = false // Whether a refused service start was reported, so a job that retries it every run does not // report the same failure every 15 seconds. @Volatile private var serviceStartReported = false // The live in-app subscriptions hosted here, each with its callback. Empty after the process // was restarted in the background, until the app subscribes again. val listeners = CopyOnWriteArrayList() // Guards the saved state below and its copy on disk. private val lock = Any() private var loaded = false private var config: PushConfig? = null // Background subscriptions saved by an earlier run that no live subscription has taken over. private var saved: List = emptyList() // Every MQTT call runs on this thread, in order, so the connection state below needs no lock. private val worker = Executors.newSingleThreadScheduledExecutor { runnable -> Thread(runnable, "AppwritePushBackground").apply { isDaemon = true } } private var mqtt: Mqtt5AsyncClient? = null // Set when [mqtt] is replaced or closed, so that client stops reconnecting and delivering. private var mqttRetired: AtomicBoolean? = null // Messages being delivered, and when one last arrived or finished, for a run's drain window. private val delivering = AtomicInteger(0) @Volatile private var lastDelivery = 0L private var connectedConfig: PushConfig? = null private var connectedNetwork: Network? = null private var lastHeartbeat = 0L private var heartbeatState: PushHeartbeat? = null private val subscribed = mutableMapOf() private var networkCallback: ConnectivityManager.NetworkCallback? = null // Worker thread only. While a caller waits for a converge (host with a completion), its // failures go to that caller, which reports them, instead of onError. private var waiting = false private var failure: Throwable? = null /** * Host [newListeners] (the app's live subscriptions) on the background connection with * [newConfig], save the background ones, and arm the scheduled wake-ups. Saved subscriptions * from an earlier run stay unless a live subscription on the same filter takes them over * (with background on or off), or [newConfig] is for another client id (another user), whose * saved subscriptions are dropped. * * With [done], it is called once the connection is up and every filter is subscribed (SUBACK), * or with the failure, which is then not reported to onError: the caller reports it. */ fun host( context: Context, newConfig: PushConfig, newListeners: List, done: ((Throwable?) -> Unit)? = null, ) { val app = context.applicationContext synchronized(lock) { load(app) if (config != null && config?.clientId != newConfig.clientId) { saved = emptyList() } // A live subscription on a saved filter takes it over, whether or not it wants background: // one that turned background off, or is later unsubscribed, must not leave the saved // entry delivering in the background. saved = saved.filter { entry -> newListeners.none { entry.filter in it.topics } } config = newConfig listeners.clear() listeners.addAll(newListeners) persist(app) } activate(app, done) } /** * Stop hosting the app's live subscriptions, keeping the saved ones from an earlier run. * Shuts down when none remain. */ fun release(context: Context) { val app = context.applicationContext val active = synchronized(lock) { listeners.clear() persist(app) isActive() } if (active) { worker.execute { converge(app, heartbeat = false) } } else { shutdown(app) } } /** Stop background delivery completely and forget every saved subscription. */ fun stop(context: Context) { val app = context.applicationContext synchronized(lock) { listeners.clear() config = null saved = emptyList() loaded = true PushStore.clearState(app) } shutdown(app) } /** Whether subscriptions saved by an earlier run are still delivered in the background. */ fun hasSaved(context: Context): Boolean = synchronized(lock) { load(context.applicationContext) saved.isNotEmpty() } /** Persist the foreground-mode choice and start or stop the foreground service to match. */ fun setForeground(context: Context, enabled: Boolean) { val app = context.applicationContext PushStore.setForeground(app, enabled) serviceStartReported = false ensureService(app) } /** Whether the foreground service should run: foreground mode is enabled and there is work to do. */ fun wantsService(context: Context): Boolean { val app = context.applicationContext return PushStore.foreground(app) && synchronized(lock) { load(app) isActive() } } /** * Resume saved background delivery when the app starts, without waiting for the next run, with * the app's current credential. A rotated session of the same user replaces the saved one; a * different user drops the saved subscriptions, as does no credential when * [signedOutWhenMissing] (the caller read the session store, so none means signed out). * While a live Push hosts subscriptions here, it does nothing: that Push owns the connection. */ fun resume(context: Context, authMethod: String? = null, credential: String? = null, signedOutWhenMissing: Boolean = false) { val app = context.applicationContext val resume = synchronized(lock) { load(app) val current = config when { !isActive() || current == null -> null // A live Push hosts here and owns the connection and its credential; resuming only // reconciles saved delivery, so it leaves a live host alone. listeners.isNotEmpty() -> null // No credential: signed out when the caller can tell, else possibly not set yet. authMethod == null || credential == null -> if (signedOutWhenMissing) false else null current.authMethod == authMethod && current.credential == credential -> true // A new credential for the same user (a rotated session) replaces the saved one, // so the stale one is never sent and refused. sameUser(current.authMethod, current.credential, authMethod, credential) -> { config = current.copy(authMethod = authMethod, credential = credential) persist(app) true } // Another user signed in: the saved subscriptions were not theirs. else -> false } } when (resume) { true -> tick(app) {} false -> stop(app) null -> Unit } } private enum class CredentialRefresh { UNCHANGED, CHANGED, GONE } // Bring the saved credential up to date with the session cookie the app // signs in with, when it comes from one (see [PushConfig.sessionCookieUrl]): a rotated session of // the same user replaces it, and no session or another user's is GONE. UNCHANGED when there is // no cookie source or it cannot be read. private fun refreshCredential(context: Context): CredentialRefresh { val current = synchronized(lock) { config } ?: return CredentialRefresh.UNCHANGED val url = current.sessionCookieUrl ?: return CredentialRefresh.UNCHANGED val session = readSessionCookie(url, current.project) ?: return CredentialRefresh.UNCHANGED return when { session.isEmpty() -> CredentialRefresh.GONE current.authMethod == "appwrite-session" && session == current.credential -> CredentialRefresh.UNCHANGED sameUser(current.authMethod, current.credential, "appwrite-session", session) -> { synchronized(lock) { config = current.copy(authMethod = "appwrite-session", credential = session) persist(context.applicationContext) } CredentialRefresh.CHANGED } else -> CredentialRefresh.GONE } } // The credential to send for a connection made with [connected]: the session re-read from the // cookie store when one backs it (so an automatic reconnect uses a rotated session), else the // one it connected with. Only a newer credential of the same user, for the same connection // (broker, client id, project), is borrowed: a connection being replaced by [host] keeps its // own. Null when the app signed out or another user signed in: authentication is aborted and // delivery stops. private fun currentCredential(context: Context, connected: PushConfig): String? { if (refreshCredential(context) == CredentialRefresh.GONE) { worker.execute { stop(context) } return null } val current = synchronized(lock) { config } ?: return connected.credential val sameConnection = current.host == connected.host && current.port == connected.port && current.clientId == connected.clientId && current.project == connected.project && current.authMethod == connected.authMethod return if (sameConnection && sameUser(connected.authMethod, connected.credential, current.authMethod, current.credential)) { current.credential } else { connected.credential } } // The `a_session_` cookie for [url] in the WebView cookie store, decoded; "" when there // is none, and null when the store cannot be read. private fun readSessionCookie(url: String, project: String): String? = runCatching { val name = "a_session_$project" CookieManager.getInstance().getCookie(url) ?.split(";") ?.map { it.trim() } ?.firstOrNull { it.substringBefore("=") == name } ?.substringAfter("=") ?.let { Uri.decode(it) } ?: "" }.getOrNull() private fun sameUser(savedMethod: String, saved: String, method: String, credential: String): Boolean { fun userId(method: String, credential: String) = if (method == "appwrite-session") userIdFromSession(credential) else userIdFromJwt(credential) val user = userId(method, credential) return user.isNotEmpty() && user == userId(savedMethod, saved) } /** * One scheduled run: under a wakelock, reconnect if needed, send a heartbeat when one is due, * re-arm the next run, and call [done] once finished. Shuts down when nothing is saved. * * A run of a chained job ([jobId]) arms the next run under the other chained id: scheduling the * id that is running would stop the run in progress. With [drainMs], the run stays up after * connecting while the broker replays what was missed (at most that long), so a process woken * from the background is not cached and frozen before the messages are delivered. */ fun tick(context: Context, jobId: Int? = null, drainMs: Long = 0, done: () -> Unit) { val app = context.applicationContext val wakeLock = acquireWakeLock(app) worker.execute { try { val active = synchronized(lock) { load(app) isActive() } if (active) { // Arm the next run first, so a run cut short (killed, or a slow broker outlasting // the wakelock) still leaves the chain armed; the watchdog job restarts a chain // that was dropped anyway. A refused credential in converge() shuts down, // cancelling them again. schedule(app, INTERVAL_MS, runningJobId = jobId) ensureWatchdog(app) ensureService(app) registerNetworkCallback(app) converge(app, heartbeat = true) if (drainMs > 0 && mqtt != null) { awaitDeliveries(drainMs) } } else { shutdown(app) } } catch (e: Exception) { report(e) } finally { releaseWakeLock(wakeLock) done() } } } // Worker thread only. Wait until no message arrived for DRAIN_QUIET_MS and none is being // delivered, at most [maxMs]. Timed with the real monotonic clock ([nowMs]), which keeps moving // while this thread sleeps. private fun awaitDeliveries(maxMs: Long) { val start = nowMs() if (lastDelivery < start) { lastDelivery = start } while (nowMs() - start < maxMs) { if (delivering.get() == 0 && nowMs() - lastDelivery >= DRAIN_QUIET_MS) { return } Thread.sleep(DRAIN_POLL_MS) } } private fun nowMs(): Long = System.nanoTime() / 1_000_000 /** * Android 12+: hand a run to an expedited job, which starts at once with network access and * keeps the process runnable until it finishes. A broadcast receiver woken in a cached process * runs at background priority and can be frozen before it connects. Returns false when the * job cannot be scheduled (older Android, or the expedited quota is used up), so the caller * runs the tick itself. */ fun runExpedited(context: Context): Boolean { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { return false } return try { val job = JobInfo.Builder(EXPEDITED_JOB_ID, ComponentName(context, PushJobService::class.java)) .setExpedited(true) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) .build() context.getSystemService(JobScheduler::class.java)?.schedule(job) == JobScheduler.RESULT_SUCCESS } catch (e: Exception) { false } } /** Run once more shortly after the task was removed, so an OEM kill that follows is undone. */ fun scheduleRestart(context: Context) { schedule(context.applicationContext, RESTART_DELAY_MS, alarmOnly = true) } /** * Forget everything held in memory, as the process dying does, keeping only what is saved. * For tests of what happens after the app's process was killed. */ internal fun dropProcessState(context: Context) { synchronized(lock) { listeners.clear() config = null saved = emptyList() loaded = false } errorHandler = null connectionHandler = null unregisterNetworkCallback(context.applicationContext) worker.submit { disconnect() }.get() } /** Report an error to the hosting Push instance's onError; logged when there is none. */ fun report(error: Throwable): Boolean { val delivered = try { errorHandler?.invoke(error) ?: false } catch (callbackError: Exception) { Log.e(LOG_TAG, "Push onError threw", callbackError) true } if (!delivered) { Log.e(LOG_TAG, "Push background error", error) } return delivered } // Must hold [lock]. private fun load(context: Context) { if (loaded) { return } loaded = true val state = PushStore.loadState(context) ?: return config = state.first saved = state.second } // Must hold [lock]. private fun isActive(): Boolean = config != null && (saved.isNotEmpty() || listeners.isNotEmpty()) // Must hold [lock]. Every background subscription, live or saved, one per filter and title. // Subscriptions sharing a filter and title are saved once, with QoS 1 when any of them wants it. private fun entries(): List = (saved + liveEntries()) .groupBy { it.filter to it.title } .map { (key, group) -> PushEntry(key.first, group.any { it.retry }, key.second, group.any { it.notifyInForeground }) } // Must hold [lock]. private fun liveEntries(): List = listeners.filter { it.background }.flatMap { listener -> listener.topics.map { PushEntry(it, listener.retry, listener.title, listener.notifyInForeground) } } // Must hold [lock]. private fun persist(context: Context) { val current = config val entries = entries() if (current == null || entries.isEmpty()) { PushStore.clearState(context) } else { PushStore.saveState(context, current, entries) } } // Must hold [lock]. The broker subscriptions wanted, at the highest QoS any subscription on a // filter asks for. private fun wanted(): Map { val qos = linkedMapOf() fun want(filter: String, retry: Boolean) { if (retry) { qos[filter] = MqttQos.AT_LEAST_ONCE } else if (filter !in qos) { qos[filter] = MqttQos.AT_MOST_ONCE } } for (listener in listeners) { listener.topics.forEach { want(it, listener.retry) } } saved.forEach { want(it.filter, it.retry) } return qos } private fun activate(context: Context, done: ((Throwable?) -> Unit)? = null) { registerNetworkCallback(context) schedule(context, INTERVAL_MS) ensureWatchdog(context) ensureService(context) worker.execute { converge(context, heartbeat = false, done) } } private fun shutdown(context: Context) { cancelSchedule(context) unregisterNetworkCallback(context) if (serviceRunning) { context.stopService(Intent(context, PushService::class.java)) } worker.execute { disconnect() } } // Worker thread only. Bring the connection to the wanted state: reconnect when the config or // the default network changed or a due heartbeat fails, then subscribe and unsubscribe the // difference. With [done], failures go to it (see [waiting]) instead of onError. private fun converge(context: Context, heartbeat: Boolean, done: ((Throwable?) -> Unit)? = null) { waiting = done != null failure = null try { convergeOnce(context, heartbeat) } catch (e: Exception) { fail(e) } finally { waiting = false done?.invoke(failure) } } // Worker thread only. Record a failure for the waiting caller, or report it. private fun fail(error: Throwable) { if (failure == null) { failure = error } if (!waiting) { report(error) } } private fun convergeOnce(context: Context, heartbeat: Boolean) { // Signed out, or signed in as someone else, since the subscriptions were saved. if (refreshCredential(context) == CredentialRefresh.GONE) { stop(context) return } val (current, wanted) = synchronized(lock) { config to wanted() } if (current == null || wanted.isEmpty()) { disconnect() return } var client = mqtt if (client != null && (current != connectedConfig || networkChanged(context))) { disconnect() client = null } if (client != null && heartbeat && SystemClock.elapsedRealtime() - lastHeartbeat >= heartbeatInterval(context)) { val alive = sendHeartbeat(client) recordHeartbeat(context, alive) if (alive) { lastHeartbeat = SystemClock.elapsedRealtime() } else { disconnect() client = null } } if (client == null) { client = connect(context, current) ?: return } for (filter in subscribed.keys - wanted.keys) { request { client.unsubscribeWith().topicFilter(filter).send().get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS) } subscribed.remove(filter) } for ((filter, qos) in wanted) { if (subscribed[filter] != qos && subscribe(client, filter, qos)) { subscribed[filter] = qos } } } // Worker thread only. Connect with [config], or report why not and return null. A refused // credential stops background delivery: retrying it would only be refused again. private fun connect(context: Context, config: PushConfig): Mqtt5AsyncClient? { // A failed first connect of a waited-for converge goes to the waiting caller only. val quiet = waiting val connected = AtomicBoolean(false) val retired = AtomicBoolean(false) val client = buildPushClient( config.copy(keepAlive = KEEP_ALIVE_SECONDS), onConnected = { if (!retired.get()) { isConnected = true connectionHandler?.invoke(true) } }, onDisconnected = { if (!retired.get()) { isConnected = false connectionHandler?.invoke(false) } }, onError = { error -> if (!quiet || connected.get()) { report(error) } }, onAuthRefused = { error -> worker.execute { refused(context, error) } }, credential = { currentCredential(context, config) }, retired = retired, ) // Registered before connecting, so the backlog the broker replays right after each // SUBSCRIBE reaches the app however the subscription was made (including the ones the // client restores itself after an automatic reconnect). Acknowledged by hand, once the // message was delivered (see [deliver]). client.publishes( MqttGlobalPublishFilter.ALL, { publish -> if (!retired.get()) { deliver(context, publish) } }, true, ) try { client.sendPushConnect(config.copy(keepAlive = KEEP_ALIVE_SECONDS), CONNECT_TIMEOUT_SECONDS) { currentCredential(context, config) } } catch (e: Exception) { val cause = (e as? ExecutionException)?.cause ?: e retired.set(true) client.disconnect() val error = connectError(cause) when { isAuthRefusal(cause) -> refused(context, error) waiting -> fail(error) // A network failure already reached onError through the client's disconnect // listener; a refused CONNECT and a timeout did not. cause is Mqtt5ConnAckException || cause is TimeoutException -> report(error) } return null } connected.set(true) mqtt = client mqttRetired = retired connectedConfig = config connectedNetwork = activeNetwork(context) lastHeartbeat = SystemClock.elapsedRealtime() subscribed.clear() return client } // Worker thread only. private fun subscribe(client: Mqtt5AsyncClient, filter: String, qos: MqttQos): Boolean = request { val subAck = client.subscribeWith().topicFilter(filter).qos(qos).send().get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS) val code = subAck.reasonCodes.firstOrNull { it.isError } if (code != null) { throw AppwriteException( subAck.reasonString.map { it.toString() }.orElse("Subscribing to $filter was refused (reason $code)"), ) } } // Worker thread only. The current heartbeat interval, back at the default once a shrink is // old enough. private fun heartbeatInterval(context: Context): Long { val state = heartbeatState ?: PushStore.heartbeat(context).also { heartbeatState = it } if (state.intervalMs < HEARTBEAT_MS && System.currentTimeMillis() - state.shrunkAt >= HEARTBEAT_RESET_MS) { saveHeartbeat(context, PushHeartbeat(HEARTBEAT_MS)) return HEARTBEAT_MS } return state.intervalMs } // Worker thread only. Count a failed heartbeat (a connection the network dropped silently), // shrinking the interval after HEARTBEAT_FAILURES in a row; a delivered one ends the streak. private fun recordHeartbeat(context: Context, alive: Boolean) { val state = heartbeatState ?: PushStore.heartbeat(context) val next = when { alive -> state.copy(failures = 0) state.failures + 1 < HEARTBEAT_FAILURES -> state.copy(failures = state.failures + 1) else -> PushHeartbeat(maxOf(MIN_HEARTBEAT_MS, state.intervalMs * 3 / 4), 0, System.currentTimeMillis()) } if (next != state) { saveHeartbeat(context, next) } } private fun saveHeartbeat(context: Context, state: PushHeartbeat) { heartbeatState = state PushStore.setHeartbeat(context, state) } // Worker thread only. private fun sendHeartbeat(client: Mqtt5AsyncClient): Boolean = try { client.unsubscribeWith().topicFilter(HEARTBEAT_FILTER).send().get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS) true } catch (e: Exception) { false } // Run a broker request, recording or reporting its failure. Returns whether it succeeded. private fun request(block: () -> Unit): Boolean = try { block() true } catch (e: Exception) { fail((e as? ExecutionException)?.cause ?: e) false } // Worker thread only. private fun disconnect() { mqttRetired?.set(true) mqttRetired = null // A retired client's own disconnect is not reported, so report the live one closing here. if (isConnected) { isConnected = false connectionHandler?.invoke(false) } mqtt?.disconnect() mqtt = null connectedConfig = null connectedNetwork = null subscribed.clear() } // Worker thread only. The broker refused the credential: report it and stop. With no onError // registered the message is also saved, and the next onError registration receives it. A // waiting caller receives it instead. private fun refused(context: Context, error: Throwable) { // The saved credential may be a session the app has since rotated: retry once with the // current one. Refused again, it is unchanged and delivery stops below. if (!waiting) { when (refreshCredential(context)) { CredentialRefresh.CHANGED -> { converge(context, heartbeat = false) return } CredentialRefresh.GONE -> { stop(context) return } CredentialRefresh.UNCHANGED -> Unit } } if (waiting) { fail(error) } else if (!report(error)) { PushStore.setStoppedError(context, error.message ?: "Push connection refused") } stop(context) } // Deliver a message, then acknowledge it: right away, or once every listener that acknowledges // messages itself has done so (at most ACK_TIMEOUT_SECONDS later). // A message counts as being delivered until it is acknowledged: for the React Native and Flutter // bridges, once their callback has run (or the acknowledgement times out), not when the event is // handed over, so a run's drain window waits for it. private fun deliver(context: Context, publish: Mqtt5Publish) { delivering.incrementAndGet() lastDelivery = nowMs() deliverAcknowledged(context, publish) { lastDelivery = nowMs() delivering.decrementAndGet() } } private fun deliverAcknowledged(context: Context, publish: Mqtt5Publish, settled: () -> Unit) { val acknowledged = AtomicBoolean(false) val acknowledge = { if (acknowledged.compareAndSet(false, true)) { runCatching { publish.acknowledge() } settled() } } val pending = AtomicInteger(1) val settle = { if (pending.decrementAndGet() == 0) { acknowledge() } } try { deliverMessage(context, publish.toPushMessage()) { pending.incrementAndGet() val once = AtomicBoolean(false) val ack: () -> Unit = { if (once.compareAndSet(false, true)) { settle() } } ack } } finally { settle() } if (!acknowledged.get()) { ackTimer.schedule({ acknowledge() }, ACK_TIMEOUT_SECONDS, TimeUnit.SECONDS) } } // Deliver a message to the matching live callbacks; with none, to the declared PushReceiver // and/or a notification per saved subscription that matches it. [deferAck] hands a listener // that acknowledges messages itself its acknowledgement. internal fun deliverMessage(context: Context, message: PushMessage, deferAck: () -> () -> Unit) { val matching = listeners.filter { listener -> listener.topics.any { matchesTopic(it, message.topic) } } // Saved background subscriptions no live one has taken over; they deliver even when a live // foreground-only subscription on an overlapping filter received the message too. val entries = synchronized(lock) { saved }.filter { matchesTopic(it.filter, message.topic) } if (matching.isEmpty() && entries.isEmpty()) { return } for (listener in matching) { try { val acknowledging = listener.acknowledgingCallback if (acknowledging != null) { acknowledging(message, deferAck()) } else { listener.callback(message) } } catch (e: Exception) { report(e) } } val wakeLock = acquireWakeLock(context) try { // The app's PushReceiver gets only messages no live callback received. val handled = matching.isEmpty() && deliverToReceivers(context, message) val content = notificationContent(message) // Background subscriptions notify while the app is not visible; on screen, only those // that opted in with notifyInForeground do, since the app shows the message itself. A // message no live callback received still notifies, as the app has not shown it. val foreground = appInForeground() val shown = foreground && matching.isNotEmpty() val titles = matching.filter { it.background && (!foreground || it.notifyInForeground) }.map { it.title ?: message.topic } + if (handled) emptyList() else entries.filter { !shown || it.notifyInForeground }.map { it.title ?: message.topic } // A title the server sent replaces every subscription's, so one notification is posted. titles.map { content.title ?: it }.distinct().forEach { notify(context, message, it, content) } } finally { releaseWakeLock(wakeLock) } } // Call every PushReceiver the app declares. Returns whether one handled the message. private fun deliverToReceivers(context: Context, message: PushMessage): Boolean { val intent = Intent(ACTION_MESSAGE).setPackage(context.packageName) val receivers = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { context.packageManager.queryBroadcastReceivers(intent, PackageManager.ResolveInfoFlags.of(0)) } else { @Suppress("DEPRECATION") context.packageManager.queryBroadcastReceivers(intent, 0) } var handled = false for (info in receivers) { val name = info.activityInfo?.name ?: continue try { val receiver = Class.forName(name, true, context.classLoader).getDeclaredConstructor().newInstance() if (receiver is PushReceiver && receiver.onMessage(context, message)) { handled = true } } catch (e: Exception) { report(e) } } return handled } /** * Post a notification for [message] that opens the app, with the message in its extras. It shows * the server's title, body and image from [content], falling back to [title], and to the payload * as the body when it is not a JSON object. An envelope with neither a title nor a body, such as * a data-only push, posts nothing. */ fun notify(context: Context, message: PushMessage, title: String, content: PushNotificationContent = notificationContent(message)) { if (!content.visible) { return } val manager = NotificationManagerCompat.from(context) if (!manager.areNotificationsEnabled()) { return } createChannels(context) val id = 31 * message.hashCode() + title.hashCode() val body = content.body ?: message.data.takeIf { content.raw } val builder = NotificationCompat.Builder(context, CHANNEL_ID) .setContentTitle(content.title ?: title) .setSmallIcon(notificationIcon(context)) .setAutoCancel(true) if (body != null) { builder.setContentText(body).setStyle(NotificationCompat.BigTextStyle().bigText(body)) } content.image?.let { loadImage(it) }?.let { image -> builder.setLargeIcon(image) .setStyle(NotificationCompat.BigPictureStyle().bigPicture(image).bigLargeIcon(null as Bitmap?).setSummaryText(body)) } val open = Intent(context, PushOpenActivity::class.java) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_ANIMATION) .putExtra(EXTRA_TOPIC, message.topic) .putExtra(EXTRA_PAYLOAD, message.data) builder.setContentIntent( PendingIntent.getActivity(context, id, open, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE), ) try { manager.notify(id, builder.build()) } catch (e: SecurityException) { // POST_NOTIFICATIONS not granted; nothing to show. } } // Whether one of the app's activities is visible: a foreground service alone does not count. private fun appInForeground(): Boolean = ActivityManager.RunningAppProcessInfo() .also { ActivityManager.getMyMemoryState(it) } .importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND /** The server's `notification` block in [message], [PushNotificationContent.raw] when the payload is not a JSON object. */ fun notificationContent(message: PushMessage): PushNotificationContent { val envelope = runCatching { JSONObject(message.data) }.getOrNull() ?: return PushNotificationContent(true, null, null, null) val notification = envelope.optJSONObject("notification") fun field(name: String) = (notification?.opt(name) as? String)?.takeIf { it.isNotEmpty() } return PushNotificationContent(false, field("title"), field("body"), field("image")) } // Downloads notification images, so a slow one is abandoned without holding up delivery. // Acknowledgement timeouts, apart from [worker] so they fire while a run's drain window holds it. private val ackTimer = Executors.newSingleThreadScheduledExecutor { runnable -> Thread(runnable, "AppwritePushAck").apply { isDaemon = true } } private val imageLoader = Executors.newCachedThreadPool { runnable -> Thread(runnable, "AppwritePushImage").apply { isDaemon = true } } // Download a notification image, or null when it cannot be fetched and decoded within // IMAGE_TIMEOUT_MS overall. On timeout the connection is closed, which ends a read in progress. private fun loadImage(url: String): Bitmap? { val connection = runCatching { URL(url).openConnection() as HttpURLConnection }.getOrNull() ?: return null connection.connectTimeout = IMAGE_TIMEOUT_MS connection.readTimeout = IMAGE_TIMEOUT_MS val download = imageLoader.submit { connection.inputStream.use { readLimited(it, IMAGE_MAX_BYTES) }?.let { decodeImage(it) } } return try { download.get(IMAGE_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS) } catch (e: Exception) { download.cancel(true) null } finally { connection.disconnect() } } // At most [limit] bytes of [input], or null when it holds more. private fun readLimited(input: InputStream, limit: Int): ByteArray? { val out = ByteArrayOutputStream() val buffer = ByteArray(8_192) while (true) { val read = input.read(buffer) if (read < 0) { return out.toByteArray() } if (out.size() + read > limit) { return null } out.write(buffer, 0, read) } } // Decode [bytes] downsampled so neither side exceeds IMAGE_MAX_PX, so a large photo cannot // exhaust memory while a notification is posted. private fun decodeImage(bytes: ByteArray): Bitmap? { val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true } BitmapFactory.decodeByteArray(bytes, 0, bytes.size, bounds) if (bounds.outWidth <= 0 || bounds.outHeight <= 0) { return null } var sample = 1 while (bounds.outWidth / sample > IMAGE_MAX_PX || bounds.outHeight / sample > IMAGE_MAX_PX) { sample *= 2 } return BitmapFactory.decodeByteArray(bytes, 0, bytes.size, BitmapFactory.Options().apply { inSampleSize = sample }) } /** The ongoing notification the foreground service shows, on its own quiet channel. */ fun ongoingNotification(context: Context) = NotificationCompat.Builder(context, SERVICE_CHANNEL_ID) .also { createChannels(context) } .setContentTitle(ongoingTitle) .setContentText("Listening for messages") .setSmallIcon(notificationIcon(context)) .setPriority(NotificationCompat.PRIORITY_LOW) .setOngoing(true) .build() private fun createChannels(context: Context) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { return } val manager = context.getSystemService(NotificationManager::class.java) ?: return manager.createNotificationChannel(NotificationChannel(CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH)) manager.createNotificationChannel( NotificationChannel(SERVICE_CHANNEL_ID, "Background connection", NotificationManager.IMPORTANCE_LOW), ) } // The icon named by the app's ``, else a // generic one. private fun notificationIcon(context: Context): Int { val metaData = runCatching { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { context.packageManager.getApplicationInfo(context.packageName, PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA.toLong())) } else { @Suppress("DEPRECATION") context.packageManager.getApplicationInfo(context.packageName, PackageManager.GET_META_DATA) }.metaData }.getOrNull() return metaData?.getInt(NOTIFICATION_ICON, 0)?.takeIf { it != 0 } ?: android.R.drawable.stat_notify_chat } private fun ensureService(context: Context) { if (!wantsService(context)) { if (serviceRunning) { context.stopService(Intent(context, PushService::class.java)) } return } if (serviceRunning) { return } try { ContextCompat.startForegroundService(context, Intent(context, PushService::class.java)) } catch (e: Exception) { // Android 12+ refuses a foreground-service start from the background; the scheduled // runs keep delivering, and the next start from the foreground succeeds. if (!serviceStartReported) { serviceStartReported = true report(e) } } } // Arm the next run in [delayMs]: the job, and an alarm just after it in case the job is late. // The chained job alternates between two ids, so a run ([runningJobId]) never schedules its // own id, which would stop it; the other id is cancelled only when it is not the one running. private fun schedule(context: Context, delayMs: Long, alarmOnly: Boolean = false, runningJobId: Int? = null) { val interval = interval(context, delayMs) if (!alarmOnly) { val next = if (runningJobId == JOB_ID) NEXT_JOB_ID else JOB_ID if (runningJobId == null) { context.getSystemService(JobScheduler::class.java)?.cancel(if (next == JOB_ID) NEXT_JOB_ID else JOB_ID) } scheduleJob(context, delayMs, next) } val alarms = context.getSystemService(AlarmManager::class.java) ?: return val at = SystemClock.elapsedRealtime() + interval + 1_000L val intent = tickIntent(context) try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !alarms.canScheduleExactAlarms()) { alarms.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, at, intent) } else { alarms.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, at, intent) } } catch (e: SecurityException) { alarms.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, at, intent) } } private fun interval(context: Context, delayMs: Long): Long = if (delayMs == INTERVAL_MS && privileged(context)) PRIVILEGED_INTERVAL_MS else delayMs private fun scheduleJob(context: Context, delayMs: Long, id: Int) { try { val job = JobInfo.Builder(id, ComponentName(context, PushJobService::class.java)) .setMinimumLatency(interval(context, delayMs)) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) .setPersisted(true) .build() context.getSystemService(JobScheduler::class.java)?.schedule(job) } catch (e: Exception) { report(e) } } // A periodic job, independent of the chain of runs, that restarts the chain when a run was // dropped (the process killed between runs, a deferred alarm, a throttled job). Scheduled once, // so its period is not reset by every run. private fun ensureWatchdog(context: Context) { val jobs = context.getSystemService(JobScheduler::class.java) ?: return try { if (jobs.allPendingJobs.any { it.id == WATCHDOG_JOB_ID }) { return } jobs.schedule( JobInfo.Builder(WATCHDOG_JOB_ID, ComponentName(context, PushJobService::class.java)) .setPeriodic(WATCHDOG_INTERVAL_MS) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) .setPersisted(true) .build(), ) } catch (e: Exception) { report(e) } } private fun cancelSchedule(context: Context) { context.getSystemService(JobScheduler::class.java)?.cancel(JOB_ID) context.getSystemService(JobScheduler::class.java)?.cancel(NEXT_JOB_ID) context.getSystemService(JobScheduler::class.java)?.cancel(EXPEDITED_JOB_ID) context.getSystemService(JobScheduler::class.java)?.cancel(WATCHDOG_JOB_ID) context.getSystemService(AlarmManager::class.java)?.cancel(tickIntent(context)) } private fun tickIntent(context: Context): PendingIntent = PendingIntent.getBroadcast( context, 0, Intent(context, PushAlarmReceiver::class.java).setAction(ACTION_TICK), PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE, ) /** What background delivery can rely on now; see [PushBackgroundStatus]. */ fun backgroundStatus(context: Context): PushBackgroundStatus { val app = context.applicationContext return PushBackgroundStatus( exactAlarms = Build.VERSION.SDK_INT < Build.VERSION_CODES.S || app.getSystemService(AlarmManager::class.java)?.canScheduleExactAlarms() == true, ignoringBatteryOptimizations = app.getSystemService(PowerManager::class.java)?.isIgnoringBatteryOptimizations(app.packageName) == true, foregroundService = PushStore.foreground(app), backgroundRestricted = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && app.getSystemService(ActivityManager::class.java)?.isBackgroundRestricted == true, ) } /** * Open the system screen where the user allows exact alarms (Android 12+). Returns false when * there is nothing to ask: already allowed, older Android, or the app does not declare * `SCHEDULE_EXACT_ALARM`. */ fun requestExactAlarms(context: Context): Boolean { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || backgroundStatus(context).exactAlarms) { return false } return openSettings(context, Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM, Manifest.permission.SCHEDULE_EXACT_ALARM) } /** * Ask the user to exempt the app from battery optimisation. Returns false when there is * nothing to ask: already exempt, or the app does not declare * `REQUEST_IGNORE_BATTERY_OPTIMIZATIONS`. */ fun requestIgnoreBatteryOptimizations(context: Context): Boolean { if (backgroundStatus(context).ignoringBatteryOptimizations) { return false } return openSettings( context, Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, ) } // Open a settings screen for this package when the app declares [permission]. private fun openSettings(context: Context, action: String, permission: String): Boolean { val app = context.applicationContext val declared = runCatching { @Suppress("DEPRECATION") app.packageManager.getPackageInfo(app.packageName, PackageManager.GET_PERMISSIONS).requestedPermissions }.getOrNull()?.contains(permission) == true if (!declared) { return false } return try { app.startActivity(Intent(action, Uri.parse("package:${app.packageName}")).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) true } catch (e: Exception) { false } } // Allowed exact alarms or exempt from battery optimisation: the alarm is reliable, so the // runs can be spaced further apart. private fun privileged(context: Context): Boolean = Build.VERSION.SDK_INT < Build.VERSION_CODES.S || context.getSystemService(AlarmManager::class.java)?.canScheduleExactAlarms() == true || context.getSystemService(PowerManager::class.java)?.isIgnoringBatteryOptimizations(context.packageName) == true private fun activeNetwork(context: Context): Network? = runCatching { context.getSystemService(ConnectivityManager::class.java)?.activeNetwork }.getOrNull() private fun networkChanged(context: Context): Boolean { val now = activeNetwork(context) return now != null && connectedNetwork != null && now != connectedNetwork } // Reconnect as soon as the default network changes (e.g. from cellular to Wi-Fi) while the // process is alive, instead of waiting for the next run to notice. private fun registerNetworkCallback(context: Context) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { return } synchronized(lock) { if (networkCallback != null) { return } val callback = object : ConnectivityManager.NetworkCallback() { override fun onAvailable(network: Network) { worker.execute { if (mqtt != null && network != connectedNetwork) { converge(context, heartbeat = false) } } } } try { context.getSystemService(ConnectivityManager::class.java)?.registerDefaultNetworkCallback(callback) networkCallback = callback } catch (e: Exception) { report(e) } } } private fun unregisterNetworkCallback(context: Context) { val callback = synchronized(lock) { networkCallback.also { networkCallback = null } } ?: return runCatching { context.getSystemService(ConnectivityManager::class.java)?.unregisterNetworkCallback(callback) } } private fun acquireWakeLock(context: Context): PowerManager.WakeLock? = runCatching { context.getSystemService(PowerManager::class.java) ?.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Appwrite:push") ?.apply { setReferenceCounted(false) acquire(WAKE_LOCK_MS) } }.getOrNull() private fun releaseWakeLock(wakeLock: PowerManager.WakeLock?) { runCatching { if (wakeLock?.isHeld == true) { wakeLock.release() } } } } /** * Background push state in the app's no-backup directory, so a restored backup on another * device never carries this install's id or credential: the saved subscriptions with their * connection config (cleared when background delivery stops), and the settings that outlive * it (the install id, the foreground-mode choice, a refusal waiting for onError). */ internal object PushStore { private const val STATE_FILE = "appwrite_push_state.json" private const val SETTINGS_FILE = "appwrite_push_settings.json" fun loadState(context: Context): Pair>? = synchronized(this) { val json = read(context, STATE_FILE) ?: return null runCatching { val config = json.getJSONObject("config").let { PushConfig( host = it.getString("host"), port = it.getInt("port"), tls = it.getBoolean("tls"), tlsInsecure = it.getBoolean("tlsInsecure"), clientId = it.getString("clientId"), keepAlive = it.getInt("keepAlive"), authMethod = it.getString("authMethod"), credential = it.getString("credential"), project = it.getString("project"), sessionCookieUrl = if (it.isNull("sessionCookieUrl")) null else it.optString("sessionCookieUrl").ifEmpty { null }, ) } val list = json.getJSONArray("entries") val entries = (0 until list.length()).map { index -> val entry = list.getJSONObject(index) PushEntry( filter = entry.getString("filter"), retry = entry.getBoolean("retry"), title = if (entry.isNull("title")) null else entry.getString("title"), notifyInForeground = entry.optBoolean("notifyInForeground", false), ) } config to entries }.getOrNull() } fun saveState(context: Context, config: PushConfig, entries: List) = synchronized(this) { val json = JSONObject() .put( "config", JSONObject() .put("host", config.host) .put("port", config.port) .put("tls", config.tls) .put("tlsInsecure", config.tlsInsecure) .put("clientId", config.clientId) .put("keepAlive", config.keepAlive) .put("authMethod", config.authMethod) .put("credential", config.credential) .put("project", config.project) .put("sessionCookieUrl", config.sessionCookieUrl ?: JSONObject.NULL), ) .put( "entries", JSONArray(entries.map { JSONObject().put("filter", it.filter).put("retry", it.retry).put("title", it.title ?: JSONObject.NULL).put("notifyInForeground", it.notifyInForeground) }), ) write(context, STATE_FILE, json) } fun clearState(context: Context) = synchronized(this) { file(context, STATE_FILE).delete() } /** A random id for this install, created once, which keeps the broker's replay position. */ fun installId(context: Context): String = synchronized(this) { val settings = read(context, SETTINGS_FILE) ?: JSONObject() settings.optString("installId").ifEmpty { val id = UUID.randomUUID().toString() write(context, SETTINGS_FILE, settings.put("installId", id)) id } } fun foreground(context: Context): Boolean = synchronized(this) { read(context, SETTINGS_FILE)?.optBoolean("foreground", false) ?: false } fun setForeground(context: Context, enabled: Boolean) = synchronized(this) { write(context, SETTINGS_FILE, (read(context, SETTINGS_FILE) ?: JSONObject()).put("foreground", enabled)) } /** The saved adaptive heartbeat; the default interval until one was saved. */ fun heartbeat(context: Context): PushHeartbeat = synchronized(this) { val json = read(context, SETTINGS_FILE)?.optJSONObject("heartbeat") ?: return PushHeartbeat(PushBackground.HEARTBEAT_MS) PushHeartbeat( intervalMs = json.optLong("intervalMs", PushBackground.HEARTBEAT_MS), failures = json.optInt("failures", 0), shrunkAt = json.optLong("shrunkAt", 0L), ) } fun setHeartbeat(context: Context, state: PushHeartbeat) = synchronized(this) { val heartbeat = JSONObject() .put("intervalMs", state.intervalMs) .put("failures", state.failures) .put("shrunkAt", state.shrunkAt) write(context, SETTINGS_FILE, (read(context, SETTINGS_FILE) ?: JSONObject()).put("heartbeat", heartbeat)) } fun setStoppedError(context: Context, message: String) = synchronized(this) { write(context, SETTINGS_FILE, (read(context, SETTINGS_FILE) ?: JSONObject()).put("stoppedError", message)) } /** The refusal that stopped background delivery while no onError was registered, once. */ fun takeStoppedError(context: Context): String? = synchronized(this) { val settings = read(context, SETTINGS_FILE) ?: return null val message = settings.optString("stoppedError").ifEmpty { return null } settings.remove("stoppedError") write(context, SETTINGS_FILE, settings) message } private fun file(context: Context, name: String) = AtomicFile(File(context.noBackupFilesDir, name)) private fun read(context: Context, name: String): JSONObject? = runCatching { JSONObject(String(file(context, name).readFully(), Charsets.UTF_8)) }.getOrNull() private fun write(context: Context, name: String, json: JSONObject) { val file = file(context, name) val out = try { file.startWrite() } catch (e: IOException) { Log.e(LOG_TAG, "Could not save push state", e) return } try { out.write(json.toString().toByteArray(Charsets.UTF_8)) file.finishWrite(out) } catch (e: IOException) { file.failWrite(out) Log.e(LOG_TAG, "Could not save push state", e) } } }