@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.
- package/CHANGELOG.md +25 -0
- package/LICENSE +206 -0
- package/README.md +148 -2
- package/android/build.gradle +58 -0
- package/android/consumer-rules.pro +11 -0
- package/android/src/main/AndroidManifest.xml +46 -0
- package/android/src/main/java/dev/poolse/callcore/CallCoreRuntime.kt +540 -0
- package/android/src/main/java/dev/poolse/callcore/CallMachine.kt +492 -0
- package/android/src/main/java/dev/poolse/callcore/Ports.kt +132 -0
- package/android/src/main/java/dev/poolse/callcore/android/AndroidRingSurface.kt +383 -0
- package/android/src/main/java/dev/poolse/callcore/android/CallActionReceiver.kt +45 -0
- package/android/src/main/java/dev/poolse/callcore/android/CallCore.kt +578 -0
- package/android/src/main/java/dev/poolse/callcore/android/CallForegroundService.kt +294 -0
- package/android/src/main/java/dev/poolse/callcore/android/CallHttpClient.kt +207 -0
- package/android/src/main/java/dev/poolse/callcore/android/CallMessagingService.kt +161 -0
- package/android/src/main/java/dev/poolse/callcore/android/CallVideo.kt +36 -0
- package/android/src/main/java/dev/poolse/callcore/android/HandlerTimerPort.kt +42 -0
- package/android/src/main/java/dev/poolse/callcore/android/IncomingCallActivity.kt +314 -0
- package/android/src/main/java/dev/poolse/callcore/android/MessageNotifications.kt +353 -0
- package/android/src/main/java/dev/poolse/callcore/android/MissedCallNotification.kt +103 -0
- package/android/src/main/java/dev/poolse/callcore/android/RingBundle.kt +42 -0
- package/android/src/main/java/dev/poolse/callcore/android/SnapshotJson.kt +99 -0
- package/android/src/main/java/expo/modules/poolsecalls/CallLockscreen.kt +42 -0
- package/android/src/main/java/expo/modules/poolsecalls/CallPictureInPicture.kt +92 -0
- package/android/src/main/java/expo/modules/poolsecalls/PoolseCallVideoView.kt +64 -0
- package/android/src/main/java/expo/modules/poolsecalls/PoolseCallsModule.kt +205 -0
- package/android/src/main/livekit/java/dev/poolse/callcore/livekit/LiveKitMediaPort.kt +594 -0
- package/android/src/main/livekit/java/dev/poolse/callcore/livekit/LiveKitVideoSurface.kt +107 -0
- package/android/src/test/java/dev/poolse/callcore/android/CallHttpClientTest.kt +61 -0
- package/app.plugin.js +5 -0
- package/dist/index.cjs +197 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +313 -0
- package/dist/index.d.ts +313 -0
- package/dist/index.js +161 -0
- package/dist/index.js.map +1 -0
- package/expo-module.config.json +11 -0
- package/ios/Core/CallCoreRuntime.swift +543 -0
- package/ios/Core/CallHttpClient.swift +213 -0
- package/ios/Core/CallMachine.swift +394 -0
- package/ios/Core/PORTS_READY.md +93 -0
- package/ios/Core/Ports.swift +238 -0
- package/ios/Core/SnapshotEncoding.swift +122 -0
- package/ios/Module/PoolseCallVideoView.swift +70 -0
- package/ios/Module/PoolseCallsAppDelegateSubscriber.swift +16 -0
- package/ios/Module/PoolseCallsModule.swift +150 -0
- package/ios/Platform/CallCore.swift +623 -0
- package/ios/Platform/CallKitController.swift +326 -0
- package/ios/Platform/CallPictureInPicture.swift +145 -0
- package/ios/Platform/LiveKitMediaPort.swift +798 -0
- package/ios/Platform/LiveKitVideoSurface.swift +68 -0
- package/ios/Platform/MessageNotifications.swift +163 -0
- package/ios/Platform/Ringback.swift +61 -0
- package/ios/Platform/VoipPush.swift +174 -0
- package/ios/PoolseCalls.podspec +68 -0
- package/package.json +53 -4
- package/plugin/index.js +42 -0
- package/plugin/package.json +1 -0
- package/plugin/withCallDelegate.js +35 -0
- package/plugin/withLockscreenCalls.js +84 -0
- package/plugin/withNativeCallMedia.js +19 -0
- package/plugin/withPictureInPicture.js +28 -0
- package/plugin/withRingback.js +86 -0
- package/src/expo-modules-core.d.ts +16 -0
- package/src/index.ts +564 -0
- package/src/react-shims.d.ts +22 -0
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
package dev.poolse.callcore.android
|
|
2
|
+
|
|
3
|
+
import android.app.NotificationChannel
|
|
4
|
+
import android.app.NotificationManager
|
|
5
|
+
import android.app.PendingIntent
|
|
6
|
+
import android.content.Context
|
|
7
|
+
import android.content.Intent
|
|
8
|
+
import android.media.AudioAttributes
|
|
9
|
+
import android.media.AudioManager
|
|
10
|
+
import android.media.MediaPlayer
|
|
11
|
+
import android.media.ToneGenerator
|
|
12
|
+
import android.os.Handler
|
|
13
|
+
import android.os.Looper
|
|
14
|
+
import android.media.Ringtone
|
|
15
|
+
import android.media.RingtoneManager
|
|
16
|
+
import android.os.Build
|
|
17
|
+
import android.util.Log
|
|
18
|
+
import androidx.core.app.NotificationCompat
|
|
19
|
+
import androidx.core.app.NotificationManagerCompat
|
|
20
|
+
import androidx.core.app.Person
|
|
21
|
+
import dev.poolse.callcore.RingContext
|
|
22
|
+
import dev.poolse.callcore.RingSurfacePort
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The incoming-call UX: a CallStyle notification with a full-screen
|
|
26
|
+
* intent, so a locked phone lights up with a call screen the way the
|
|
27
|
+
* dialer does, plus the ringtone and the caller's ringback.
|
|
28
|
+
*
|
|
29
|
+
* Tones are owned here, not delegated to react-native-incall-manager,
|
|
30
|
+
* whose stream handling without its full lifecycle played the ringback
|
|
31
|
+
* on the loudspeaker and the ringtone in the earpiece. Both players use
|
|
32
|
+
* explicit [AudioAttributes]: ringtone on the ringtone stream (loud,
|
|
33
|
+
* speaker), ringback on voice-communication (quiet, follows the call
|
|
34
|
+
* route). The ringback waits for [setCommunicationReady]: before the
|
|
35
|
+
* SFU's audio handler has switched the device into communication mode,
|
|
36
|
+
* a voice-communication stream still routes to the loudspeaker — the
|
|
37
|
+
* "ringback blares from the speaker" bug.
|
|
38
|
+
*
|
|
39
|
+
* Tap actions resolve to broadcasts handled by [CallActionReceiver],
|
|
40
|
+
* which forwards into the single native entry point so that notification
|
|
41
|
+
* button, full-screen button and app-closed cold-launch all funnel to
|
|
42
|
+
* one place.
|
|
43
|
+
*/
|
|
44
|
+
internal class AndroidRingSurface(
|
|
45
|
+
private val context: Context,
|
|
46
|
+
/** Read per use, so a re-configure (icon, ringback) applies at once. */
|
|
47
|
+
private val configOf: () -> RingSurfaceConfig,
|
|
48
|
+
) : RingSurfacePort {
|
|
49
|
+
|
|
50
|
+
private val config get() = configOf()
|
|
51
|
+
|
|
52
|
+
private var ringtone: Ringtone? = null
|
|
53
|
+
private var ringback: MediaPlayer? = null
|
|
54
|
+
private var ringbackWanted = false
|
|
55
|
+
private var communicationReady = false
|
|
56
|
+
|
|
57
|
+
/** The call waiting beside the current one, while its surface is up. */
|
|
58
|
+
private var waitingCallId: String? = null
|
|
59
|
+
private var waitingTone: ToneGenerator? = null
|
|
60
|
+
private val main = Handler(Looper.getMainLooper())
|
|
61
|
+
private val waitingBeep = object : Runnable {
|
|
62
|
+
override fun run() {
|
|
63
|
+
if (waitingCallId == null) return
|
|
64
|
+
runCatching { waitingTone?.startTone(ToneGenerator.TONE_SUP_CALL_WAITING, WAITING_BEEP_MS) }
|
|
65
|
+
main.postDelayed(this, WAITING_BEEP_EVERY_MS)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The call's audio path is (or is no longer) in communication mode.
|
|
71
|
+
* A ringback requested earlier starts now, through the call route.
|
|
72
|
+
*/
|
|
73
|
+
fun setCommunicationReady(ready: Boolean) {
|
|
74
|
+
communicationReady = ready
|
|
75
|
+
if (ready && ringbackWanted && ringback == null) playRingback()
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
override fun show(ring: RingContext) {
|
|
79
|
+
ensureChannel()
|
|
80
|
+
val extras = ring.toBundle()
|
|
81
|
+
|
|
82
|
+
fun actionPending(action: String, requestOffset: Int): PendingIntent {
|
|
83
|
+
val intent = Intent(context, CallActionReceiver::class.java)
|
|
84
|
+
.setAction("$ACTION_PREFIX.$action")
|
|
85
|
+
.putExtra(EXTRA_RING, extras)
|
|
86
|
+
return PendingIntent.getBroadcast(
|
|
87
|
+
context,
|
|
88
|
+
ring.callId.hashCode() + requestOffset,
|
|
89
|
+
intent,
|
|
90
|
+
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
fun incomingActivity(autoAnswer: Boolean, requestOffset: Int): PendingIntent {
|
|
95
|
+
val intent = Intent(context, IncomingCallActivity::class.java)
|
|
96
|
+
.putExtra(EXTRA_RING, extras)
|
|
97
|
+
.putExtra(EXTRA_AUTO_ANSWER, autoAnswer)
|
|
98
|
+
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
|
|
99
|
+
return PendingIntent.getActivity(
|
|
100
|
+
context,
|
|
101
|
+
ring.callId.hashCode() + requestOffset,
|
|
102
|
+
intent,
|
|
103
|
+
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
val fullScreen = incomingActivity(autoAnswer = false, requestOffset = 0)
|
|
108
|
+
|
|
109
|
+
// Answer launches an ACTIVITY, not a broadcast: a background receiver
|
|
110
|
+
// cannot start an activity (Android 10+), so answering from the
|
|
111
|
+
// notification used to connect audio while the app stayed hidden. An
|
|
112
|
+
// activity launched from a notification tap is allowed to come
|
|
113
|
+
// forward — it answers and brings the app up. Decline stays a
|
|
114
|
+
// broadcast; it needs no UI.
|
|
115
|
+
val answer = incomingActivity(autoAnswer = true, requestOffset = 2)
|
|
116
|
+
|
|
117
|
+
val caller = Person.Builder()
|
|
118
|
+
.setName(ring.conversationName ?: ring.callerName ?: "Incoming call")
|
|
119
|
+
.setImportant(true)
|
|
120
|
+
.build()
|
|
121
|
+
|
|
122
|
+
val notification = NotificationCompat.Builder(context, CHANNEL_ID)
|
|
123
|
+
.setSmallIcon(config.smallIcon)
|
|
124
|
+
.setCategory(NotificationCompat.CATEGORY_CALL)
|
|
125
|
+
.setPriority(NotificationCompat.PRIORITY_MAX)
|
|
126
|
+
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
|
127
|
+
.setOngoing(true)
|
|
128
|
+
.setAutoCancel(false)
|
|
129
|
+
.setFullScreenIntent(fullScreen, true)
|
|
130
|
+
// Tapping the heads-up (not a button) opens the ring screen too;
|
|
131
|
+
// without this the tap did nothing.
|
|
132
|
+
.setContentIntent(fullScreen)
|
|
133
|
+
.setStyle(
|
|
134
|
+
NotificationCompat.CallStyle.forIncomingCall(
|
|
135
|
+
caller,
|
|
136
|
+
actionPending(ACTION_DECLINE, 1),
|
|
137
|
+
answer,
|
|
138
|
+
),
|
|
139
|
+
)
|
|
140
|
+
.build()
|
|
141
|
+
|
|
142
|
+
// The ring-mode foreground service adopts this exact notification.
|
|
143
|
+
shownRing = ring.callId to notification
|
|
144
|
+
try {
|
|
145
|
+
NotificationManagerCompat.from(context).notify(ring.callId.notificationId(), notification)
|
|
146
|
+
} catch (e: SecurityException) {
|
|
147
|
+
// POST_NOTIFICATIONS denied: the ringtone still plays, but there is
|
|
148
|
+
// no surface. Loud in the log — this is a support question.
|
|
149
|
+
Log.e(TAG, "ring notification refused (POST_NOTIFICATIONS?)", e)
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* A call waiting beside the one in progress: a quiet heads-up — not
|
|
155
|
+
* the full-screen ring, which would cover the call — with Decline and
|
|
156
|
+
* End & answer, and the call-waiting beep through the call's own
|
|
157
|
+
* audio route (the ringtone would blare over the conversation).
|
|
158
|
+
*/
|
|
159
|
+
override fun showWaiting(ring: RingContext) {
|
|
160
|
+
ensureWaitingChannel()
|
|
161
|
+
val extras = ring.toBundle()
|
|
162
|
+
|
|
163
|
+
fun actionPending(action: String, requestOffset: Int): PendingIntent {
|
|
164
|
+
val intent = Intent(context, CallActionReceiver::class.java)
|
|
165
|
+
.setAction("$ACTION_PREFIX.$action")
|
|
166
|
+
.putExtra(EXTRA_RING, extras)
|
|
167
|
+
return PendingIntent.getBroadcast(
|
|
168
|
+
context,
|
|
169
|
+
ring.callId.hashCode() + requestOffset,
|
|
170
|
+
intent,
|
|
171
|
+
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
|
|
172
|
+
)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
val open = context.packageManager.getLaunchIntentForPackage(context.packageName)?.let {
|
|
176
|
+
PendingIntent.getActivity(
|
|
177
|
+
context,
|
|
178
|
+
ring.callId.hashCode() + 5,
|
|
179
|
+
it.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT),
|
|
180
|
+
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
|
|
181
|
+
)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// End & answer launches an ACTIVITY, like the ring's Answer: only an
|
|
185
|
+
// activity started from the tap may bring the app forward (Android
|
|
186
|
+
// 10+), and the call that connects should be seen.
|
|
187
|
+
val answerWaiting = PendingIntent.getActivity(
|
|
188
|
+
context,
|
|
189
|
+
ring.callId.hashCode() + 4,
|
|
190
|
+
Intent(context, IncomingCallActivity::class.java)
|
|
191
|
+
.putExtra(EXTRA_RING, extras)
|
|
192
|
+
.putExtra(EXTRA_WAITING_ANSWER, true)
|
|
193
|
+
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP),
|
|
194
|
+
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
val who = ring.conversationName ?: ring.callerName ?: "Someone"
|
|
198
|
+
val what = if (ring.media == "video") "video call" else "call"
|
|
199
|
+
val notification = NotificationCompat.Builder(context, WAITING_CHANNEL_ID)
|
|
200
|
+
.setSmallIcon(config.smallIcon)
|
|
201
|
+
.setCategory(NotificationCompat.CATEGORY_CALL)
|
|
202
|
+
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
|
203
|
+
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
|
204
|
+
.setContentTitle("$who — incoming $what")
|
|
205
|
+
.setContentText("You're on a call. Answering ends it.")
|
|
206
|
+
.setOngoing(true)
|
|
207
|
+
.setAutoCancel(false)
|
|
208
|
+
.setOnlyAlertOnce(true)
|
|
209
|
+
.apply { if (open != null) setContentIntent(open) }
|
|
210
|
+
.addAction(0, "Decline", actionPending(ACTION_WAITING_DECLINE, 3))
|
|
211
|
+
.addAction(0, "End & answer", answerWaiting)
|
|
212
|
+
// Never outlive the ring: if its ring_ended/finished push is lost,
|
|
213
|
+
// the system takes the heads-up down at the deadline anyway.
|
|
214
|
+
.setTimeoutAfter((ring.ringDeadlineEpochMs - System.currentTimeMillis()).coerceAtLeast(10_000L))
|
|
215
|
+
.build()
|
|
216
|
+
|
|
217
|
+
waitingCallId = ring.callId
|
|
218
|
+
try {
|
|
219
|
+
NotificationManagerCompat.from(context).notify(ring.callId.notificationId(), notification)
|
|
220
|
+
} catch (e: SecurityException) {
|
|
221
|
+
Log.e(TAG, "call-waiting notification refused (POST_NOTIFICATIONS?)", e)
|
|
222
|
+
}
|
|
223
|
+
startWaitingBeep()
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
override fun dismissWaiting(callId: String) {
|
|
227
|
+
if (waitingCallId != callId) return
|
|
228
|
+
stopWaitingBeep()
|
|
229
|
+
runCatching { NotificationManagerCompat.from(context).cancel(callId.notificationId()) }
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** SHOW_RING follows, reposting under the same id as the full ring. */
|
|
233
|
+
override fun promoteWaiting(callId: String) = dismissWaiting(callId)
|
|
234
|
+
|
|
235
|
+
private fun startWaitingBeep() {
|
|
236
|
+
main.removeCallbacks(waitingBeep)
|
|
237
|
+
if (waitingTone == null) {
|
|
238
|
+
waitingTone = runCatching { ToneGenerator(AudioManager.STREAM_VOICE_CALL, WAITING_BEEP_VOLUME) }.getOrNull()
|
|
239
|
+
}
|
|
240
|
+
main.post(waitingBeep)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private fun stopWaitingBeep() {
|
|
244
|
+
waitingCallId = null
|
|
245
|
+
main.removeCallbacks(waitingBeep)
|
|
246
|
+
runCatching { waitingTone?.stopTone(); waitingTone?.release() }
|
|
247
|
+
waitingTone = null
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
private fun ensureWaitingChannel() {
|
|
251
|
+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
|
|
252
|
+
val manager = context.getSystemService(NotificationManager::class.java)
|
|
253
|
+
if (manager.getNotificationChannel(WAITING_CHANNEL_ID) != null) return
|
|
254
|
+
// Silent: the beep is ours, through the call route. A channel sound
|
|
255
|
+
// would play on the notification stream over the conversation.
|
|
256
|
+
manager.createNotificationChannel(
|
|
257
|
+
NotificationChannel(WAITING_CHANNEL_ID, "Calls while on a call", NotificationManager.IMPORTANCE_HIGH).apply {
|
|
258
|
+
setSound(null, null)
|
|
259
|
+
enableVibration(false)
|
|
260
|
+
lockscreenVisibility = NotificationCompat.VISIBILITY_PUBLIC
|
|
261
|
+
},
|
|
262
|
+
)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
override fun dismiss(callId: String) {
|
|
266
|
+
// Every teardown for a call (ring_ended, finished) also ends its
|
|
267
|
+
// waiting surface, even after a restart lost the in-memory state.
|
|
268
|
+
if (waitingCallId == callId) stopWaitingBeep()
|
|
269
|
+
if (shownRing?.first == callId) shownRing = null
|
|
270
|
+
runCatching { NotificationManagerCompat.from(context).cancel(callId.notificationId()) }
|
|
271
|
+
// The full-screen activity is its own window: tell it to close.
|
|
272
|
+
runCatching {
|
|
273
|
+
context.sendBroadcast(
|
|
274
|
+
Intent(ACTION_RING_CLOSED).setPackage(context.packageName).putExtra(EXTRA_CALL_ID, callId),
|
|
275
|
+
)
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
override fun ringtoneStart() {
|
|
280
|
+
ringtoneStop()
|
|
281
|
+
runCatching {
|
|
282
|
+
val uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE) ?: return
|
|
283
|
+
ringtone = RingtoneManager.getRingtone(context, uri)?.apply {
|
|
284
|
+
audioAttributes = AudioAttributes.Builder()
|
|
285
|
+
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
|
|
286
|
+
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
|
287
|
+
.build()
|
|
288
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) isLooping = true
|
|
289
|
+
play()
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
override fun ringtoneStop() {
|
|
295
|
+
runCatching { ringtone?.stop() }
|
|
296
|
+
ringtone = null
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
override fun ringbackStart() {
|
|
300
|
+
ringbackStop()
|
|
301
|
+
ringbackWanted = true
|
|
302
|
+
if (communicationReady) playRingback()
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
private fun playRingback() {
|
|
306
|
+
val resId = config.ringbackRawRes
|
|
307
|
+
if (resId == 0) return
|
|
308
|
+
runCatching {
|
|
309
|
+
ringback = MediaPlayer.create(
|
|
310
|
+
context,
|
|
311
|
+
resId,
|
|
312
|
+
AudioAttributes.Builder()
|
|
313
|
+
.setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
|
|
314
|
+
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
|
315
|
+
.build(),
|
|
316
|
+
/* audioSessionId = */ 0,
|
|
317
|
+
)?.apply {
|
|
318
|
+
isLooping = true
|
|
319
|
+
start()
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
override fun ringbackStop() {
|
|
325
|
+
ringbackWanted = false
|
|
326
|
+
runCatching {
|
|
327
|
+
ringback?.stop()
|
|
328
|
+
ringback?.release()
|
|
329
|
+
}
|
|
330
|
+
ringback = null
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private fun ensureChannel() {
|
|
334
|
+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
|
|
335
|
+
val manager = context.getSystemService(NotificationManager::class.java)
|
|
336
|
+
if (manager.getNotificationChannel(CHANNEL_ID) != null) return
|
|
337
|
+
// Silent channel: the ringtone is ours (looping, controllable). A
|
|
338
|
+
// channel sound on top would double-ring and could not be stopped
|
|
339
|
+
// the instant the ring resolves.
|
|
340
|
+
manager.createNotificationChannel(
|
|
341
|
+
NotificationChannel(CHANNEL_ID, "Incoming calls", NotificationManager.IMPORTANCE_HIGH).apply {
|
|
342
|
+
setSound(null, null)
|
|
343
|
+
enableVibration(true)
|
|
344
|
+
vibrationPattern = longArrayOf(0, 400, 300, 400)
|
|
345
|
+
lockscreenVisibility = NotificationCompat.VISIBILITY_PUBLIC
|
|
346
|
+
},
|
|
347
|
+
)
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
companion object {
|
|
351
|
+
const val CHANNEL_ID = "poolse_incoming_calls"
|
|
352
|
+
const val ACTION_PREFIX = "dev.poolse.callcore.action"
|
|
353
|
+
const val ACTION_ANSWER = "ANSWER"
|
|
354
|
+
const val ACTION_DECLINE = "DECLINE"
|
|
355
|
+
const val ACTION_OPEN = "OPEN"
|
|
356
|
+
const val ACTION_WAITING_ANSWER = "WAITING_ANSWER"
|
|
357
|
+
const val ACTION_WAITING_DECLINE = "WAITING_DECLINE"
|
|
358
|
+
const val WAITING_CHANNEL_ID = "poolse_call_waiting"
|
|
359
|
+
private const val WAITING_BEEP_MS = 400
|
|
360
|
+
private const val WAITING_BEEP_EVERY_MS = 3_000L
|
|
361
|
+
private const val WAITING_BEEP_VOLUME = 70
|
|
362
|
+
const val EXTRA_RING = "poolse_ring"
|
|
363
|
+
const val EXTRA_AUTO_ANSWER = "poolse_auto_answer"
|
|
364
|
+
const val EXTRA_WAITING_ANSWER = "poolse_waiting_answer"
|
|
365
|
+
/** Package-local broadcast: the ring for [EXTRA_CALL_ID] is over. */
|
|
366
|
+
const val ACTION_RING_CLOSED = "dev.poolse.callcore.RING_CLOSED"
|
|
367
|
+
const val EXTRA_CALL_ID = "poolse_call_id"
|
|
368
|
+
private const val TAG = "PoolseRingSurface"
|
|
369
|
+
|
|
370
|
+
/** The CallStyle notification on screen, for the ring-mode service to wear. */
|
|
371
|
+
@Volatile internal var shownRing: Pair<String, android.app.Notification>? = null
|
|
372
|
+
|
|
373
|
+
/** Stable per-call notification id, so teardown targets the right one. */
|
|
374
|
+
fun String.notificationId(): Int = hashCode()
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** Host-supplied, platform-specific bits the core cannot know. */
|
|
379
|
+
data class RingSurfaceConfig(
|
|
380
|
+
val smallIcon: Int,
|
|
381
|
+
/** `R.raw.incallmanager_ringback`, or 0 to fall back to silence. */
|
|
382
|
+
val ringbackRawRes: Int,
|
|
383
|
+
)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
package dev.poolse.callcore.android
|
|
2
|
+
|
|
3
|
+
import android.content.BroadcastReceiver
|
|
4
|
+
import android.content.Context
|
|
5
|
+
import android.content.Intent
|
|
6
|
+
import androidx.core.app.NotificationManagerCompat
|
|
7
|
+
import dev.poolse.callcore.android.AndroidRingSurface.Companion.ACTION_ANSWER
|
|
8
|
+
import dev.poolse.callcore.android.AndroidRingSurface.Companion.ACTION_DECLINE
|
|
9
|
+
import dev.poolse.callcore.android.AndroidRingSurface.Companion.ACTION_OPEN
|
|
10
|
+
import dev.poolse.callcore.android.AndroidRingSurface.Companion.ACTION_PREFIX
|
|
11
|
+
import dev.poolse.callcore.android.AndroidRingSurface.Companion.ACTION_WAITING_ANSWER
|
|
12
|
+
import dev.poolse.callcore.android.AndroidRingSurface.Companion.ACTION_WAITING_DECLINE
|
|
13
|
+
import dev.poolse.callcore.android.AndroidRingSurface.Companion.EXTRA_RING
|
|
14
|
+
import dev.poolse.callcore.android.AndroidRingSurface.Companion.notificationId
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Answer / Decline taps on the CallStyle notification land here, as do
|
|
18
|
+
* programmatic opens. Everything funnels into [CallCore], the one native
|
|
19
|
+
* entry point — the same place the full-screen activity and a cold
|
|
20
|
+
* launch resolve to, so there is a single answer/decline path regardless
|
|
21
|
+
* of how the tap arrived.
|
|
22
|
+
*/
|
|
23
|
+
class CallActionReceiver : BroadcastReceiver() {
|
|
24
|
+
override fun onReceive(context: Context, intent: Intent) {
|
|
25
|
+
val ring = intent.getBundleExtra(EXTRA_RING)?.toRingContext() ?: return
|
|
26
|
+
val core = CallCore.getOrNull(context) ?: return
|
|
27
|
+
when (intent.action) {
|
|
28
|
+
"$ACTION_PREFIX.$ACTION_ANSWER" -> core.onUserAnswer(ring)
|
|
29
|
+
"$ACTION_PREFIX.$ACTION_DECLINE" -> core.onUserDecline(ring)
|
|
30
|
+
"$ACTION_PREFIX.$ACTION_OPEN" -> core.onUserOpen(ring)
|
|
31
|
+
// Call waiting: keyed to THIS call, so a stale notification can
|
|
32
|
+
// never act on a different one — and one outliving its call (a
|
|
33
|
+
// process restart) is simply taken down.
|
|
34
|
+
"$ACTION_PREFIX.$ACTION_WAITING_ANSWER", "$ACTION_PREFIX.$ACTION_WAITING_DECLINE" -> {
|
|
35
|
+
if (core.state().waiting?.callId != ring.callId) {
|
|
36
|
+
NotificationManagerCompat.from(context).cancel(ring.callId.notificationId())
|
|
37
|
+
} else if (intent.action == "$ACTION_PREFIX.$ACTION_WAITING_ANSWER") {
|
|
38
|
+
core.answerWaiting(ring.callId)
|
|
39
|
+
} else {
|
|
40
|
+
core.declineWaiting(ring.callId)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|