package com.cottondb.calling import android.content.Context import android.os.Handler import android.os.Looper import com.google.firebase.FirebaseApp import com.google.firebase.messaging.FirebaseMessaging /** * THE ONE SEAM a ring comes through (iOS: VoIPPushes.swift). The Worker rings an Android phone with an FCM * high-priority data message (apps/business/src/calling/fcm.ts): `{ kind: ring, room, video: "1"|"0", box? }` rings, * `{ kind: stop, room, stop, box?, at?, ended? }` ends a ring (`box`, `at`, `ended` on a missed call to a phone that * speaks the new way: who called, sealed, and when it rang, for its Missed row), and `{ kind: missed, … }` over the chat * socket is a missed call kept for this phone, told once as it opened: it writes the row and rings nothing (CALLS FIX 2); * every value a string, and none of them a number or a name in the clear: * `box` is who is calling, sealed for this phone's chat key, which the core opens at once (`calls.incoming {ring}`). Whatever hands a push in (Firebase's service, a debug build's walk * probe) hands it here. */ sealed class CallPush { abstract val room: String /** [box]: who is calling, sealed by the person's own object for this phone's chat key (opened by the core, never here). */ /** [at]: when the ring began, as the object kept it (to a phone that speaks the new way), so one rung late starts there. */ data class Ring(override val room: String, val video: Boolean, val box: String? = null, val at: Long? = null) : CallPush() data class Stop(override val room: String, val reason: String, val box: String? = null, val at: Long? = null, val ended: Long? = null) : CallPush() companion object { /** A push's data, or null when it is no call's (a ring with no room, another kind). */ fun of(data: Map): CallPush? { val room = data["room"]?.takeIf { it.isNotEmpty() } ?: return null return when (data["kind"]) { "ring" -> Ring(room, data["video"] == "1" || data["video"] == "true", data["box"]?.takeIf { it.startsWith("c1:") }, data["at"]?.toLongOrNull()?.takeIf { it > 0 }) "stop", "missed" -> { val at = data["at"]?.toLongOrNull() val ended = data["ended"]?.toLongOrNull() val times = at != null && ended != null && at > 0 && ended >= at Stop(room, data["stop"] ?: if (data["kind"] == "missed") "missed" else "cancelled", data["box"]?.takeIf { it.startsWith("c1:") }, at.takeIf { times }, ended.takeIf { times }) } else -> null } } /** * Hand [push] to the engine, on the main thread. [pushed]: it came by FCM, not over the chat socket (whose * frames Chat acks at once): the object is told this phone has it (`calls.ack`), so it pushes no more and * keeps no missed call for it (CALLS FIX 2). */ fun handle(context: Context, push: CallPush, pushed: Boolean = false) { Handler(Looper.getMainLooper()).post { val center = CallingEngine.of(context).center when (push) { is Ring -> center.incoming(push.room, push.video, push.box, push.at) is Stop -> center.stop(push.room, push.reason, push.box, push.at, push.ended) } if (pushed) center.ack(push.room) } } /** * Whether this build carries a Firebase config (`src//google-services.json`, whose client strings * build.gradle.kts writes): without one there is no token to file, and a ring can reach this phone only while * someone hands it in by hand (the walk's probe). That file is the coordinator's to add (report.md). */ fun configured(context: Context): Boolean = context.resources.getIdentifier("google_app_id", "string", context.packageName) != 0 && FirebaseApp.getApps(context).isNotEmpty() /** The FCM token, filed with the object through the core (`calls.pushToken`), where Firebase is configured. */ fun register(context: Context) { val engine = CallingEngine.of(context) if (!configured(context)) return engine.log.i("calls: no Firebase config in this build, no push token") runCatching { FirebaseMessaging.getInstance().token.addOnSuccessListener { engine.center.pushToken(it) } } .onFailure { engine.log.i("calls: no push token: $it") } } } }