@poolse/native 0.0.0-stage → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/LICENSE +206 -0
  3. package/README.md +148 -2
  4. package/android/build.gradle +58 -0
  5. package/android/consumer-rules.pro +11 -0
  6. package/android/src/main/AndroidManifest.xml +46 -0
  7. package/android/src/main/java/dev/poolse/callcore/CallCoreRuntime.kt +540 -0
  8. package/android/src/main/java/dev/poolse/callcore/CallMachine.kt +492 -0
  9. package/android/src/main/java/dev/poolse/callcore/Ports.kt +132 -0
  10. package/android/src/main/java/dev/poolse/callcore/android/AndroidRingSurface.kt +383 -0
  11. package/android/src/main/java/dev/poolse/callcore/android/CallActionReceiver.kt +45 -0
  12. package/android/src/main/java/dev/poolse/callcore/android/CallCore.kt +578 -0
  13. package/android/src/main/java/dev/poolse/callcore/android/CallForegroundService.kt +294 -0
  14. package/android/src/main/java/dev/poolse/callcore/android/CallHttpClient.kt +207 -0
  15. package/android/src/main/java/dev/poolse/callcore/android/CallMessagingService.kt +161 -0
  16. package/android/src/main/java/dev/poolse/callcore/android/CallVideo.kt +36 -0
  17. package/android/src/main/java/dev/poolse/callcore/android/HandlerTimerPort.kt +42 -0
  18. package/android/src/main/java/dev/poolse/callcore/android/IncomingCallActivity.kt +314 -0
  19. package/android/src/main/java/dev/poolse/callcore/android/MessageNotifications.kt +353 -0
  20. package/android/src/main/java/dev/poolse/callcore/android/MissedCallNotification.kt +103 -0
  21. package/android/src/main/java/dev/poolse/callcore/android/RingBundle.kt +42 -0
  22. package/android/src/main/java/dev/poolse/callcore/android/SnapshotJson.kt +99 -0
  23. package/android/src/main/java/expo/modules/poolsecalls/CallLockscreen.kt +42 -0
  24. package/android/src/main/java/expo/modules/poolsecalls/CallPictureInPicture.kt +92 -0
  25. package/android/src/main/java/expo/modules/poolsecalls/PoolseCallVideoView.kt +64 -0
  26. package/android/src/main/java/expo/modules/poolsecalls/PoolseCallsModule.kt +205 -0
  27. package/android/src/main/livekit/java/dev/poolse/callcore/livekit/LiveKitMediaPort.kt +594 -0
  28. package/android/src/main/livekit/java/dev/poolse/callcore/livekit/LiveKitVideoSurface.kt +107 -0
  29. package/android/src/test/java/dev/poolse/callcore/android/CallHttpClientTest.kt +61 -0
  30. package/app.plugin.js +5 -0
  31. package/dist/index.cjs +197 -0
  32. package/dist/index.cjs.map +1 -0
  33. package/dist/index.d.cts +313 -0
  34. package/dist/index.d.ts +313 -0
  35. package/dist/index.js +161 -0
  36. package/dist/index.js.map +1 -0
  37. package/expo-module.config.json +11 -0
  38. package/ios/Core/CallCoreRuntime.swift +543 -0
  39. package/ios/Core/CallHttpClient.swift +213 -0
  40. package/ios/Core/CallMachine.swift +394 -0
  41. package/ios/Core/PORTS_READY.md +93 -0
  42. package/ios/Core/Ports.swift +238 -0
  43. package/ios/Core/SnapshotEncoding.swift +122 -0
  44. package/ios/Module/PoolseCallVideoView.swift +70 -0
  45. package/ios/Module/PoolseCallsAppDelegateSubscriber.swift +16 -0
  46. package/ios/Module/PoolseCallsModule.swift +150 -0
  47. package/ios/Platform/CallCore.swift +623 -0
  48. package/ios/Platform/CallKitController.swift +326 -0
  49. package/ios/Platform/CallPictureInPicture.swift +145 -0
  50. package/ios/Platform/LiveKitMediaPort.swift +798 -0
  51. package/ios/Platform/LiveKitVideoSurface.swift +68 -0
  52. package/ios/Platform/MessageNotifications.swift +163 -0
  53. package/ios/Platform/Ringback.swift +61 -0
  54. package/ios/Platform/VoipPush.swift +174 -0
  55. package/ios/PoolseCalls.podspec +68 -0
  56. package/package.json +53 -4
  57. package/plugin/index.js +42 -0
  58. package/plugin/package.json +1 -0
  59. package/plugin/withCallDelegate.js +35 -0
  60. package/plugin/withLockscreenCalls.js +84 -0
  61. package/plugin/withNativeCallMedia.js +19 -0
  62. package/plugin/withPictureInPicture.js +28 -0
  63. package/plugin/withRingback.js +86 -0
  64. package/src/expo-modules-core.d.ts +16 -0
  65. package/src/index.ts +564 -0
  66. package/src/react-shims.d.ts +22 -0
@@ -0,0 +1,92 @@
1
+ package expo.modules.poolsecalls
2
+
3
+ import android.app.Activity
4
+ import android.app.PictureInPictureParams
5
+ import android.content.pm.PackageManager
6
+ import android.os.Build
7
+ import android.util.Log
8
+ import android.util.Rational
9
+ import androidx.core.app.OnPictureInPictureModeChangedProvider
10
+ import androidx.core.app.PictureInPictureModeChangedInfo
11
+ import androidx.core.util.Consumer
12
+ import java.lang.ref.WeakReference
13
+
14
+ /**
15
+ * Video-call picture-in-picture on Android (8+): leave the app during a
16
+ * video call and the whole activity shrinks into a floating window. The
17
+ * app keeps rendering — the host draws a compact layout while
18
+ * [active] — and grows back when the window is tapped.
19
+ *
20
+ * Android 12+ enters by itself on Home / a swipe up (auto-enter); older
21
+ * versions are entered from the module's user-leave hook. Armed by the
22
+ * host only while there is a picture worth floating.
23
+ *
24
+ * Main thread only. The activity needs `android:supportsPictureInPicture`.
25
+ */
26
+ internal object CallPictureInPicture {
27
+ private const val TAG = "PoolsePip"
28
+
29
+ /** Fires with true when the window opens, false when it closes. */
30
+ var listener: ((Boolean) -> Unit)? = null
31
+
32
+ var enabled = false
33
+ private set
34
+ var active = false
35
+ private set
36
+
37
+ private var watched: WeakReference<Activity>? = null
38
+ private val onModeChanged = Consumer<PictureInPictureModeChangedInfo> { info ->
39
+ setActive(info.isInPictureInPictureMode)
40
+ }
41
+
42
+ fun supported(activity: Activity?): Boolean =
43
+ Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
44
+ activity?.packageManager?.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE) == true
45
+
46
+ fun setEnabled(activity: Activity?, on: Boolean) {
47
+ enabled = on
48
+ if (activity == null || !supported(activity)) return
49
+ watch(activity)
50
+ runCatching { activity.setPictureInPictureParams(params(on)) }
51
+ .onFailure { Log.w(TAG, "setPictureInPictureParams failed", it) }
52
+ }
53
+
54
+ /** Before Android 12 nothing enters by itself: the user leaving is the cue. */
55
+ fun onUserLeave(activity: Activity?) {
56
+ if (!enabled || activity == null || !supported(activity)) return
57
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) return
58
+ runCatching { activity.enterPictureInPictureMode(params(true)) }
59
+ .onFailure { Log.w(TAG, "enterPictureInPictureMode failed", it) }
60
+ }
61
+
62
+ fun refresh(activity: Activity?) {
63
+ if (activity == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
64
+ setActive(activity.isInPictureInPictureMode)
65
+ }
66
+
67
+ private fun params(on: Boolean): PictureInPictureParams {
68
+ val b = PictureInPictureParams.Builder()
69
+ // Portrait, the shape of a phone camera.
70
+ .setAspectRatio(Rational(9, 16))
71
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
72
+ b.setAutoEnterEnabled(on)
73
+ b.setSeamlessResizeEnabled(true)
74
+ }
75
+ return b.build()
76
+ }
77
+
78
+ private fun watch(activity: Activity) {
79
+ if (watched?.get() === activity) return
80
+ (watched?.get() as? OnPictureInPictureModeChangedProvider)
81
+ ?.removeOnPictureInPictureModeChangedListener(onModeChanged)
82
+ (activity as? OnPictureInPictureModeChangedProvider)
83
+ ?.addOnPictureInPictureModeChangedListener(onModeChanged)
84
+ watched = WeakReference(activity)
85
+ }
86
+
87
+ private fun setActive(on: Boolean) {
88
+ if (active == on) return
89
+ active = on
90
+ listener?.invoke(on)
91
+ }
92
+ }
@@ -0,0 +1,64 @@
1
+ package expo.modules.poolsecalls
2
+
3
+ import android.content.Context
4
+ import android.view.ViewGroup
5
+ import dev.poolse.callcore.android.CallCore
6
+ import dev.poolse.callcore.android.CallVideoSurface
7
+ import expo.modules.kotlin.AppContext
8
+ import expo.modules.kotlin.views.ExpoView
9
+
10
+ /**
11
+ * `<CallVideoView>` on Android: a thin host for the core's
12
+ * [CallVideoSurface]. All binding logic (which track, rebinds on
13
+ * subscribe/mute/rejoin) lives in the media adapter; this view only
14
+ * forwards props and ties the surface's lifetime to the window.
15
+ */
16
+ class PoolseCallVideoView(context: Context, appContext: AppContext) : ExpoView(context, appContext) {
17
+
18
+ // The renderer is a native child React does not lay out: let Android
19
+ // measure it (and re-measure when the video's size changes).
20
+ override val shouldUseAndroidLayout = true
21
+
22
+ private var surface: CallVideoSurface? = null
23
+
24
+ var participantId: String? = null
25
+ set(v) { field = v; apply() }
26
+ var local: Boolean = false
27
+ set(v) { field = v; apply() }
28
+ var mirror: Boolean? = null
29
+ set(v) { field = v; apply() }
30
+ var fit: String = "cover"
31
+ set(v) { field = v; apply() }
32
+
33
+ override fun onAttachedToWindow() {
34
+ super.onAttachedToWindow()
35
+ attachSurface()
36
+ }
37
+
38
+ override fun onDetachedFromWindow() {
39
+ detachSurface()
40
+ super.onDetachedFromWindow()
41
+ }
42
+
43
+ private fun attachSurface() {
44
+ if (surface != null) return
45
+ // No core yet (never configured) or no video-capable SFU: stay empty.
46
+ val s = CallCore.peek()?.createVideoSurface(context) ?: return
47
+ surface = s
48
+ addView(s.view, LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
49
+ apply()
50
+ }
51
+
52
+ internal fun detachSurface() {
53
+ val s = surface ?: return
54
+ surface = null
55
+ s.release()
56
+ removeView(s.view)
57
+ }
58
+
59
+ private fun apply() {
60
+ // Mounted before the core existed (configure still pending): try again.
61
+ val s = surface ?: run { if (isAttachedToWindow) attachSurface(); return }
62
+ s.configure(participantId, local, mirror, if (fit == "contain") "contain" else "cover")
63
+ }
64
+ }
@@ -0,0 +1,205 @@
1
+ package expo.modules.poolsecalls
2
+
3
+ import android.app.NotificationManager
4
+ import android.content.Context
5
+ import android.content.Intent
6
+ import android.net.Uri
7
+ import android.os.Build
8
+ import android.provider.Settings
9
+ import android.util.Log
10
+ import dev.poolse.callcore.android.CallCore
11
+ import dev.poolse.callcore.android.MessageNotifications
12
+ import dev.poolse.callcore.android.MissedCallNotification
13
+ import expo.modules.kotlin.modules.Module
14
+ import expo.modules.kotlin.modules.ModuleDefinition
15
+
16
+ /**
17
+ * The React Native face of the native call core. Deliberately thin: it
18
+ * forwards commands to [CallCore] and streams snapshots out. All call
19
+ * logic lives in the core; this class only crosses the JS boundary.
20
+ *
21
+ * `configure` is the single required setup call — the host passes the
22
+ * API base URL; the icon and ringback resources are resolved by name so
23
+ * the JS side needs no R references. The SFU factory defaults to the
24
+ * shipped LiveKit adapter when present, else a no-op (audio-less), so a
25
+ * build without the LiveKit source set still rings and signals.
26
+ *
27
+ * Nothing here throws before `configure`: reads return idle, commands
28
+ * no-op (a fresh install renders its UI before JS has configured).
29
+ */
30
+ class PoolseCallsModule : Module() {
31
+
32
+ override fun definition() = ModuleDefinition {
33
+ Name("PoolseCalls")
34
+
35
+ Events("onCallState", "onCallAction", "onPictureInPicture")
36
+
37
+ // <CallVideoView>: renders a participant's (or our own) camera.
38
+ View(PoolseCallVideoView::class) {
39
+ Name("PoolseCallVideo")
40
+ Prop("participantId") { view: PoolseCallVideoView, id: String? -> view.participantId = id }
41
+ Prop("local") { view: PoolseCallVideoView, local: Boolean? -> view.local = local ?: false }
42
+ Prop("mirror") { view: PoolseCallVideoView, mirror: Boolean? -> view.mirror = mirror }
43
+ Prop("fit") { view: PoolseCallVideoView, fit: String? -> view.fit = fit ?: "cover" }
44
+ // iOS's picture-in-picture source; Android floats the whole activity.
45
+ Prop("pictureInPicture") { _: PoolseCallVideoView, _: Boolean? -> }
46
+ OnViewDestroys { view: PoolseCallVideoView -> view.detachSurface() }
47
+ }
48
+
49
+ // Snapshots → the single JS event. Null (idle) becomes an explicit
50
+ // cleared payload so the JS side always has a definite state. The
51
+ // listener is process-level, so subscribing before configure works.
52
+ OnStartObserving {
53
+ CallCore.listener = { snapshot -> sendEvent("onCallState", mapOf("snapshot" to snapshot)) }
54
+ CallPictureInPicture.listener = { active -> sendEvent("onPictureInPicture", mapOf("active" to active)) }
55
+ }
56
+ OnStopObserving {
57
+ CallCore.listener = null
58
+ CallPictureInPicture.listener = null
59
+ }
60
+
61
+ // Picture-in-picture: armed while a video call shows someone. Android
62
+ // 12+ enters by itself on Home; older versions enter here.
63
+ Function("setPictureInPictureEnabled") { on: Boolean ->
64
+ val activity = appContext.currentActivity
65
+ activity?.runOnUiThread { CallPictureInPicture.setEnabled(activity, on) }
66
+ }
67
+ Function("isInPictureInPicture") { CallPictureInPicture.active }
68
+ Function("pictureInPictureSupported") { CallPictureInPicture.supported(appContext.currentActivity) }
69
+ OnUserLeavesActivity { CallPictureInPicture.onUserLeave(appContext.currentActivity) }
70
+ OnActivityEntersForeground { CallPictureInPicture.refresh(appContext.currentActivity) }
71
+
72
+ AsyncFunction("configure") { apiBaseUrl: String, options: Map<String, Any?>? ->
73
+ val ctx = context ?: return@AsyncFunction null
74
+ // Resource NAMES go to the core, which resolves + persists them —
75
+ // so a killed app woken by a ring can rebuild this without JS.
76
+ CallCore.configure(
77
+ ctx,
78
+ apiBaseUrl,
79
+ options?.get("smallIcon") as? String,
80
+ options?.get("ringback") as? String,
81
+ )
82
+ // Materialise the core now, so a call already in flight (cold
83
+ // answer) is visible to the first getCallState.
84
+ CallCore.getOrNull(ctx)
85
+ null
86
+ }
87
+
88
+ Function("getCallState") {
89
+ mapOf("snapshot" to (CallCore.peek() ?: context?.let(CallCore::getOrNull))?.snapshot())
90
+ }
91
+
92
+ AsyncFunction("place") { conversationId: String, media: String, sessionToken: String ->
93
+ core()?.place(conversationId, media, sessionToken)
94
+ }
95
+ AsyncFunction("join") { callId: String, conversationId: String?, sessionToken: String ->
96
+ core()?.join(callId, conversationId, sessionToken)
97
+ }
98
+ Function("answer") { core()?.answer() }
99
+ Function("decline") { core()?.decline() }
100
+ Function("hangUp") { core()?.hangUp() }
101
+ Function("answerWaiting") { callId: String? -> core()?.answerWaiting(callId) }
102
+ Function("declineWaiting") { callId: String? -> core()?.declineWaiting(callId) }
103
+ Function("setMuted") { muted: Boolean -> core()?.setMuted(muted) }
104
+ Function("setCamera") { on: Boolean -> core()?.setCamera(on) }
105
+ Function("switchCamera") { core()?.switchCamera() }
106
+ Function("setSpeaker") { on: Boolean -> core()?.setSpeaker(on) }
107
+ Function("setAudioOutput") { id: String -> core()?.setAudioOutput(id) }
108
+
109
+ // What authorizes acting on the call itself (ringing people in).
110
+ AsyncFunction("callControl") {
111
+ core()?.control?.let { (callId, token) -> mapOf("callId" to callId, "controlToken" to token) }
112
+ }
113
+
114
+ // Socket forwarding (open app): idempotent with the FCM path.
115
+ Function("onRingEnded") { callId: String, reason: String -> core()?.onRingEnded(callId, reason) }
116
+ Function("onCallFinished") { callId: String, reason: String? ->
117
+ core()?.onCallFinished(callId, reason?.ifEmpty { null })
118
+ }
119
+
120
+ // A missed-call notification's tap / Call back. Cold start: the
121
+ // launch intent still carries it — the host reads it once on mount.
122
+ // Running app: the tap arrives as a new intent.
123
+ Function("takeCallAction") { appContext.currentActivity?.intent?.let(::consumeCallAction) }
124
+ OnNewIntent { intent ->
125
+ consumeCallAction(intent)?.let { sendEvent("onCallAction", it) }
126
+ }
127
+
128
+ // The chat on screen: its messages are not notified, and opening it
129
+ // clears what was already shown for it.
130
+ Function("setActiveConversation") { conversationId: String? ->
131
+ MessageNotifications.activeConversationId = conversationId
132
+ if (conversationId != null) context?.let { MessageNotifications.clear(it, conversationId) }
133
+ }
134
+
135
+ // While a call rings or runs the app may sit over the lock screen.
136
+ Function("setShowOnLockscreen") { enabled: Boolean ->
137
+ CallLockscreen.set(appContext.currentActivity, enabled)
138
+ }
139
+
140
+ // Android 14+: full-screen intents need a user grant for non-dialer apps.
141
+ Function("canUseFullScreenIntent") {
142
+ val ctx = context ?: return@Function true
143
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
144
+ ctx.getSystemService(NotificationManager::class.java).canUseFullScreenIntent()
145
+ } else {
146
+ true
147
+ }
148
+ }
149
+ Function("openFullScreenIntentSettings") {
150
+ val ctx = context ?: return@Function null
151
+ val pkg = Uri.parse("package:${ctx.packageName}")
152
+ val intents = buildList {
153
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
154
+ add(Intent(Settings.ACTION_MANAGE_APP_USE_FULL_SCREEN_INTENT, pkg))
155
+ }
156
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
157
+ add(Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).putExtra(Settings.EXTRA_APP_PACKAGE, ctx.packageName))
158
+ }
159
+ add(Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, pkg))
160
+ }
161
+ val opener = appContext.currentActivity
162
+ for (intent in intents) {
163
+ val ok = runCatching {
164
+ if (opener != null) opener.startActivity(intent)
165
+ else ctx.startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
166
+ }.onFailure { Log.w(TAG, "settings intent ${intent.action} unavailable", it) }.isSuccess
167
+ if (ok) break
168
+ }
169
+ null
170
+ }
171
+ }
172
+
173
+ private val context: Context? get() = appContext.reactContext
174
+
175
+ private fun core(): CallCore? = context?.let(CallCore::getOrNull)
176
+
177
+ /**
178
+ * The call action an intent carries, removed from it so a later read
179
+ * (rotation, re-mount) cannot replay it. Acting on it from the app
180
+ * clears its notification too: action buttons do not auto-cancel.
181
+ */
182
+ private fun consumeCallAction(intent: Intent): Map<String, Any?>? {
183
+ val action = intent.getStringExtra(MissedCallNotification.EXTRA_ACTION) ?: return null
184
+ val conversationId = intent.getStringExtra(MissedCallNotification.EXTRA_CONVERSATION_ID) ?: return null
185
+ val callId = intent.getStringExtra(MissedCallNotification.EXTRA_CALL_ID)
186
+ val media = intent.getStringExtra(MissedCallNotification.EXTRA_MEDIA) ?: "audio"
187
+ for (key in listOf(
188
+ MissedCallNotification.EXTRA_ACTION,
189
+ MissedCallNotification.EXTRA_CONVERSATION_ID,
190
+ MissedCallNotification.EXTRA_CALL_ID,
191
+ MissedCallNotification.EXTRA_MEDIA,
192
+ )) intent.removeExtra(key)
193
+ if (callId != null) context?.let { MissedCallNotification.cancel(it, callId) }
194
+ return mapOf(
195
+ "type" to action,
196
+ "conversationId" to conversationId,
197
+ "callId" to callId,
198
+ "media" to media,
199
+ )
200
+ }
201
+
202
+ private companion object {
203
+ const val TAG = "PoolseCalls"
204
+ }
205
+ }